Auto-fill the "quantity" field for the salesorder allocation table

This commit is contained in:
Oliver 2021-10-27 00:47:15 +11:00
parent 7252b299f7
commit 4d0f905afc

View File

@ -1296,6 +1296,8 @@ function allocateStockToSalesOrder(order_id, line_items, options={}) {
var todo = "auto-calculate remaining quantity"; var todo = "auto-calculate remaining quantity";
var todo = "see how it is done for the build order allocation system!";
var remaining = 0; var remaining = 0;
table_entries += renderLineItemRow(line_item, remaining); table_entries += renderLineItemRow(line_item, remaining);
@ -1405,7 +1407,22 @@ function allocateStockToSalesOrder(order_id, line_items, options={}) {
onSelect: function(data, field, opts) { onSelect: function(data, field, opts) {
// Adjust the 'quantity' field based on availability // Adjust the 'quantity' field based on availability
var todo = "actually do this"; if (!('quantity' in data)) {
return;
}
// Calculate the available quantity
var available = Math.max((data.quantity || 0) - (data.allocated || 0), 0);
// Remaining quantity to be allocated?
var todo = "fix this calculation!";
var remaining = opts.quantity || available;
// Maximum amount that we need
var desired = Math.min(available, remaining);
updateFieldValue(`items_quantity_${pk}`, desired, {}, opts);
}, },
adjustFilters: function(filters) { adjustFilters: function(filters) {
// Restrict query to the selected location // Restrict query to the selected location
@ -1420,6 +1437,11 @@ function allocateStockToSalesOrder(order_id, line_items, options={}) {
filters.location = location; filters.location = location;
filters.cascade = true; filters.cascade = true;
// Exclude expired stock?
if (global_settings.STOCK_ENABLE_EXPIRY && !global_settings.STOCK_ALLOW_EXPIRED_SALE) {
fields.item.filters.expired = false;
}
return filters; return filters;
}, },
noResults: function(query) { noResults: function(query) {
@ -2084,67 +2106,6 @@ function loadSalesOrderLineItemTable(table, options={}) {
} }
} }
); );
return;
// Quantity remaining to be allocated
var remaining = (line_item.quantity || 0) - (line_item.allocated || 0);
if (remaining < 0) {
remaining = 0;
}
var fields = {
// SalesOrderLineItem reference
line: {
hidden: true,
value: pk,
},
item: {
filters: {
part_detail: true,
location_detail: true,
in_stock: true,
part: line_item.part,
include_variants: false,
exclude_so_allocation: options.order,
},
auto_fill: true,
onSelect: function(data, field, opts) {
// Quantity available from this stock item
if (!('quantity' in data)) {
return;
}
// Calculate the available quantity
var available = Math.max((data.quantity || 0) - (data.allocated || 0), 0);
// Maximum amount that we need
var desired = Math.min(available, remaining);
updateFieldValue('quantity', desired, {}, opts);
}
},
quantity: {
value: remaining,
},
};
// Exclude expired stock?
if (global_settings.STOCK_ENABLE_EXPIRY && !global_settings.STOCK_ALLOW_EXPIRED_SALE) {
fields.item.filters.expired = false;
}
constructForm(
`/api/order/so-allocation/`,
{
method: 'POST',
fields: fields,
title: '{% trans "Allocate Stock Item" %}',
onSuccess: reloadTable,
}
);
}); });
// Callback for creating a new build // Callback for creating a new build