mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Better table sorting for allocation quantity
This commit is contained in:
		@@ -107,6 +107,21 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) {
 | 
				
			|||||||
        $(table).bootstrapTable('refresh');
 | 
					        $(table).bootstrapTable('refresh');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    function sumAllocations(row) {
 | 
				
			||||||
 | 
					        // Calculat total allocations for a given row
 | 
				
			||||||
 | 
					        if (!row.allocations) {
 | 
				
			||||||
 | 
					            return 0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        var quantity = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        row.allocations.forEach(function(item) {
 | 
				
			||||||
 | 
					            quantity += item.quantity;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return quantity;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function setupCallbacks() {
 | 
					    function setupCallbacks() {
 | 
				
			||||||
        // Register button callbacks once table data are loaded
 | 
					        // Register button callbacks once table data are loaded
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -196,11 +211,7 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) {
 | 
				
			|||||||
                            tableRow.allocations = allocations[key];
 | 
					                            tableRow.allocations = allocations[key];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            // Calculate the total allocated quantity
 | 
					                            // Calculate the total allocated quantity
 | 
				
			||||||
                            var allocatedQuantity = 0;
 | 
					                            var allocatedQuantity = sumAllocations(tableRow);
 | 
				
			||||||
 | 
					 | 
				
			||||||
                            tableRow.allocations.forEach(function (allocation) {
 | 
					 | 
				
			||||||
                                allocatedQuantity += allocation.quantity;
 | 
					 | 
				
			||||||
                            });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            // Is this line item fully allocated?
 | 
					                            // Is this line item fully allocated?
 | 
				
			||||||
                            if (allocatedQuantity >= (tableRow.quantity * output.quantity)) {
 | 
					                            if (allocatedQuantity >= (tableRow.quantity * output.quantity)) {
 | 
				
			||||||
@@ -354,6 +365,11 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) {
 | 
				
			|||||||
                title: '{% trans "Reference" %}',
 | 
					                title: '{% trans "Reference" %}',
 | 
				
			||||||
                sortable: true,
 | 
					                sortable: true,
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                field: 'quantity',
 | 
				
			||||||
 | 
					                title: '{% trans "Quantity Per" %}',
 | 
				
			||||||
 | 
					                sortable: true,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                field: 'allocated',
 | 
					                field: 'allocated',
 | 
				
			||||||
                title: '{% trans "Allocated" %}',
 | 
					                title: '{% trans "Allocated" %}',
 | 
				
			||||||
@@ -370,6 +386,22 @@ function loadBuildOutputAllocationTable(buildId, partId, output, options={}) {
 | 
				
			|||||||
                    var required = row.quantity * output.quantity;
 | 
					                    var required = row.quantity * output.quantity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    return makeProgressBar(allocated, required);
 | 
					                    return makeProgressBar(allocated, required);
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                sorter: function(valA, valB, rowA, rowB) {
 | 
				
			||||||
 | 
					                    var aA = sumAllocations(rowA);
 | 
				
			||||||
 | 
					                    var aB = sumAllocations(rowB);
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                    var qA = rowA.quantity * output.quantity;
 | 
				
			||||||
 | 
					                    var qB = rowB.quantity * output.quantity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    if (aA == 0 && aB == 0) {
 | 
				
			||||||
 | 
					                        return (qA > qB) ? 1 : -1;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					                    var progressA = parseFloat(aA) / qA;
 | 
				
			||||||
 | 
					                    var progressB = parseFloat(aB) / qB;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					                    return (progressA < progressB) ? 1 : -1;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user