mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Better caching and rendering of sub tables for particular build outputs
This commit is contained in:
parent
b8f274c680
commit
72bcea2f6d
@ -931,8 +931,6 @@ function loadBuildOutputTable(build_info, options={}) {
|
|||||||
rows.forEach(function(row) {
|
rows.forEach(function(row) {
|
||||||
row.allocations = allocations[row.pk] || [];
|
row.allocations = allocations[row.pk] || [];
|
||||||
$(table).bootstrapTable('updateByUniqueId', row.pk, row, true);
|
$(table).bootstrapTable('updateByUniqueId', row.pk, row, true);
|
||||||
|
|
||||||
console.log("Updating row for stock item", row.pk);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1280,24 +1278,37 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function sumAllocations(row) {
|
function getAllocationsForRow(row) {
|
||||||
// Calculat total allocations for a given row
|
var part_id = row.sub_part;
|
||||||
if (!row.allocations) {
|
|
||||||
row.allocated = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
var quantity = 0;
|
var allocations = [];
|
||||||
|
|
||||||
row.allocations.forEach(function(item) {
|
allocated_items.forEach(function(allocation) {
|
||||||
quantity += item.quantity;
|
if (allocation.bom_part == part_id) {
|
||||||
|
allocations.push(allocation);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
row.allocated = parseFloat(quantity.toFixed(15));
|
return allocations;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sumAllocations(row) {
|
||||||
|
|
||||||
|
var allocated_quantity = 0;
|
||||||
|
|
||||||
|
getAllocationsForRow(row).forEach(function(allocation) {
|
||||||
|
allocated_quantity += allocation.quantity;
|
||||||
|
});
|
||||||
|
|
||||||
|
row.allocated = parseFloat(allocated_quantity.toFixed(15));
|
||||||
|
|
||||||
return row.allocated;
|
return row.allocated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isRowFullyAllocated(row) {
|
||||||
|
return sumAllocations(row) >= requiredQuantity(row);
|
||||||
|
}
|
||||||
|
|
||||||
function setupCallbacks() {
|
function setupCallbacks() {
|
||||||
// Register button callbacks once table data are loaded
|
// Register button callbacks once table data are loaded
|
||||||
|
|
||||||
@ -1408,128 +1419,12 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) {
|
|||||||
onPostBody: function(data) {
|
onPostBody: function(data) {
|
||||||
// Setup button callbacks
|
// Setup button callbacks
|
||||||
setupCallbacks();
|
setupCallbacks();
|
||||||
// },
|
|
||||||
// onLoadSuccess: function(tableData) {
|
|
||||||
// Once the BOM data are loaded, request allocation data for this build output
|
|
||||||
|
|
||||||
console.log("old onLoadSuccess");
|
|
||||||
return;
|
|
||||||
|
|
||||||
var params = {
|
|
||||||
build: buildId,
|
|
||||||
part_detail: true,
|
|
||||||
location_detail: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (output) {
|
|
||||||
params.sub_part_trackable = true;
|
|
||||||
params.output = outputId;
|
|
||||||
} else {
|
|
||||||
params.sub_part_trackable = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
inventreeGet('/api/build/item/',
|
|
||||||
params,
|
|
||||||
{
|
|
||||||
success: function(data) {
|
|
||||||
// Iterate through the returned data, and group by the part they point to
|
|
||||||
var allocations = {};
|
|
||||||
|
|
||||||
// Total number of line items
|
|
||||||
var totalLines = tableData.length;
|
|
||||||
|
|
||||||
// Total number of "completely allocated" lines
|
|
||||||
var allocatedLines = 0;
|
|
||||||
|
|
||||||
data.forEach(function(item) {
|
|
||||||
|
|
||||||
// Group BuildItem objects by part
|
|
||||||
var part = item.bom_part || item.part;
|
|
||||||
var key = parseInt(part);
|
|
||||||
|
|
||||||
if (!(key in allocations)) {
|
|
||||||
allocations[key] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
allocations[key].push(item);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Now update the allocations for each row in the table
|
|
||||||
for (var key in allocations) {
|
|
||||||
|
|
||||||
// Select the associated row in the table
|
|
||||||
var tableRow = $(table).bootstrapTable('getRowByUniqueId', key);
|
|
||||||
|
|
||||||
if (!tableRow) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the allocation list for that row
|
|
||||||
tableRow.allocations = allocations[key];
|
|
||||||
|
|
||||||
// Calculate the total allocated quantity
|
|
||||||
var allocatedQuantity = sumAllocations(tableRow);
|
|
||||||
|
|
||||||
var requiredQuantity = 0;
|
|
||||||
|
|
||||||
if (output) {
|
|
||||||
requiredQuantity = tableRow.quantity * output.quantity;
|
|
||||||
} else {
|
|
||||||
requiredQuantity = tableRow.quantity * buildInfo.quantity;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is this line item fully allocated?
|
|
||||||
if (allocatedQuantity >= requiredQuantity) {
|
|
||||||
allocatedLines += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Push the updated row back into the main table
|
|
||||||
$(table).bootstrapTable('updateByUniqueId', key, tableRow, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update any rows which we did not receive allocation information for
|
|
||||||
var td = $(table).bootstrapTable('getData');
|
|
||||||
|
|
||||||
td.forEach(function(tableRow) {
|
|
||||||
if (tableRow.allocations == null) {
|
|
||||||
|
|
||||||
tableRow.allocations = [];
|
|
||||||
|
|
||||||
$(table).bootstrapTable('updateByUniqueId', tableRow.pk, tableRow, true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update the progress bar for this build output
|
|
||||||
var build_progress = $(`#output-progress-${outputId}`);
|
|
||||||
|
|
||||||
if (build_progress.exists()) {
|
|
||||||
if (totalLines > 0) {
|
|
||||||
|
|
||||||
var progress = makeProgressBar(
|
|
||||||
allocatedLines,
|
|
||||||
totalLines,
|
|
||||||
{
|
|
||||||
max_width: '150px',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
build_progress.html(progress);
|
|
||||||
} else {
|
|
||||||
build_progress.html('');
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
console.warn(`Could not find progress bar for output ${outputId}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
sortable: true,
|
sortable: true,
|
||||||
showColumns: false,
|
showColumns: false,
|
||||||
detailView: true,
|
detailView: true,
|
||||||
detailFilter: function(index, row) {
|
detailFilter: function(index, row) {
|
||||||
return row.allocations != null;
|
return sumAllocations(row) > 0;
|
||||||
},
|
},
|
||||||
detailFormatter: function(index, row, element) {
|
detailFormatter: function(index, row, element) {
|
||||||
// Contruct an 'inner table' which shows which stock items have been allocated
|
// Contruct an 'inner table' which shows which stock items have been allocated
|
||||||
@ -1543,7 +1438,7 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) {
|
|||||||
var subTable = $(`#${subTableId}`);
|
var subTable = $(`#${subTableId}`);
|
||||||
|
|
||||||
subTable.bootstrapTable({
|
subTable.bootstrapTable({
|
||||||
data: row.allocations,
|
data: getAllocationsForRow(row),
|
||||||
showHeader: true,
|
showHeader: true,
|
||||||
columns: [
|
columns: [
|
||||||
{
|
{
|
||||||
@ -1565,7 +1460,6 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) {
|
|||||||
|
|
||||||
var url = '';
|
var url = '';
|
||||||
|
|
||||||
|
|
||||||
var serial = row.serial;
|
var serial = row.serial;
|
||||||
|
|
||||||
if (row.stock_item_detail) {
|
if (row.stock_item_detail) {
|
||||||
@ -1744,19 +1638,9 @@ function loadBuildOutputAllocationTable(buildInfo, output, options={}) {
|
|||||||
title: '{% trans "Allocated" %}',
|
title: '{% trans "Allocated" %}',
|
||||||
sortable: true,
|
sortable: true,
|
||||||
formatter: function(value, row) {
|
formatter: function(value, row) {
|
||||||
var allocated = 0;
|
var allocated = sumAllocations(row);
|
||||||
|
var required = requiredQuantity(row)
|
||||||
if (row.allocations != null) {
|
return makeProgressBar(allocated, required);
|
||||||
row.allocations.forEach(function(item) {
|
|
||||||
allocated += item.quantity;
|
|
||||||
});
|
|
||||||
|
|
||||||
var required = requiredQuantity(row);
|
|
||||||
|
|
||||||
return makeProgressBar(allocated, required);
|
|
||||||
} else {
|
|
||||||
return `<em>{% trans "loading" %}...</em><span class='fas fa-spinner fa-spin float-right'></span>`;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
sorter: function(valA, valB, rowA, rowB) {
|
sorter: function(valA, valB, rowA, rowB) {
|
||||||
// Custom sorting function for progress bars
|
// Custom sorting function for progress bars
|
||||||
|
Loading…
Reference in New Issue
Block a user