mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Fix quantity aggregation for stock table (#5188)
* Fix quantity aggregation for stock table - Stock quantity can only be added together if units are the same * Add stock total footer to part table
This commit is contained in:
		@@ -2372,6 +2372,38 @@ function loadPartTable(table, url, options={}) {
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            return text;
 | 
			
		||||
        },
 | 
			
		||||
        footerFormatter: function(data) {
 | 
			
		||||
            // Display "total" stock quantity of all rendered rows
 | 
			
		||||
            // Requires that all parts have the same base units!
 | 
			
		||||
 | 
			
		||||
            let total = 0;
 | 
			
		||||
            let units = new Set();
 | 
			
		||||
 | 
			
		||||
            data.forEach(function(row) {
 | 
			
		||||
                units.add(row.units || null);
 | 
			
		||||
                if (row.total_in_stock != null) {
 | 
			
		||||
                    total += row.total_in_stock;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            if (data.length == 0) {
 | 
			
		||||
                return '-';
 | 
			
		||||
            } else if (units.size > 1) {
 | 
			
		||||
                return '-';
 | 
			
		||||
            } else {
 | 
			
		||||
                let output = `${total}`;
 | 
			
		||||
 | 
			
		||||
                if (units.size == 1) {
 | 
			
		||||
                    let unit = units.values().next().value;
 | 
			
		||||
 | 
			
		||||
                    if (unit) {
 | 
			
		||||
                        output += ` [${unit}]`;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return output;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
@@ -2447,6 +2479,7 @@ function loadPartTable(table, url, options={}) {
 | 
			
		||||
        showColumns: true,
 | 
			
		||||
        showCustomView: grid_view,
 | 
			
		||||
        showCustomViewButton: false,
 | 
			
		||||
        showFooter: true,
 | 
			
		||||
        onPostBody: function() {
 | 
			
		||||
            grid_view = inventreeLoad('part-grid-view') == 1;
 | 
			
		||||
            if (grid_view) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2068,13 +2068,36 @@ function loadStockTable(table, options) {
 | 
			
		||||
            // Display "total" stock quantity of all rendered rows
 | 
			
		||||
            let total = 0;
 | 
			
		||||
 | 
			
		||||
            // Keep track of the whether all units are the same
 | 
			
		||||
            // If different units are found, we cannot aggregate the quantities
 | 
			
		||||
            let units = new Set();
 | 
			
		||||
 | 
			
		||||
            data.forEach(function(row) {
 | 
			
		||||
 | 
			
		||||
                units.add(row.part_detail.units || null);
 | 
			
		||||
 | 
			
		||||
                if (row.quantity != null) {
 | 
			
		||||
                    total += row.quantity;
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            return total;
 | 
			
		||||
            if (data.length == 0) {
 | 
			
		||||
                return '-';
 | 
			
		||||
            } else if (units.size > 1) {
 | 
			
		||||
                return '-';
 | 
			
		||||
            } else {
 | 
			
		||||
                let output = `${total}`;
 | 
			
		||||
 | 
			
		||||
                if (units.size == 1) {
 | 
			
		||||
                    let unit = units.values().next().value;
 | 
			
		||||
 | 
			
		||||
                    if (unit) {
 | 
			
		||||
                        output += ` [${unit}]`;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return output;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user