Factor out function to calculate total allocations

This commit is contained in:
Oliver Walters 2020-04-25 21:31:58 +10:00
parent 912a3c4b99
commit 181d1d6b91

View File

@ -30,6 +30,21 @@ InvenTree | Allocate Parts
var buildTable = $("#build-item-list"); var buildTable = $("#build-item-list");
// Calculate sum of allocations for a particular table row
function sumAllocations(row) {
if (row.allocations == null) {
return 0;
}
var quantity = 0;
row.allocations.forEach(function(item) {
quantity += item.quantity;
});
return quantity;
}
buildTable.inventreeTable({ buildTable.inventreeTable({
uniqueId: 'sub_part', uniqueId: 'sub_part',
url: "{% url 'api-bom-list' %}", url: "{% url 'api-bom-list' %}",
@ -123,20 +138,14 @@ InvenTree | Allocate Parts
title: '{% trans "Allocated" %}', title: '{% trans "Allocated" %}',
formatter: function(value, row) { formatter: function(value, row) {
var allocated = 0; var allocated = sumAllocations(row);
if (row.allocations != null) {
row.allocations.forEach(function(allocation) {
allocated += allocation.quantity;
});
}
return makeProgressBar(allocated, row.quantity * {{ build.quantity }}); return makeProgressBar(allocated, row.quantity * {{ build.quantity }});
}, },
sorter: function(valA, valB, rowA, rowB) { sorter: function(valA, valB, rowA, rowB) {
var aA = rowA.allocated || 0; var aA = sumAllocations(rowA);
var aB = rowB.allocated || 0; var aB = sumAllocations(rowB);
var qA = rowA.quantity * {{ build.quantity }}; var qA = rowA.quantity * {{ build.quantity }};
var qB = rowB.quantity * {{ build.quantity }}; var qB = rowB.quantity * {{ build.quantity }};