Further improvements

- Add callback for when a select2 form field is updated
- Adjust selected quantity based on returned data
- auto_fill the stock_item field
This commit is contained in:
Oliver 2021-10-20 23:03:03 +11:00
parent f7b6c68237
commit 92568748cf
2 changed files with 23 additions and 2 deletions

View File

@ -1426,6 +1426,11 @@ function initializeRelatedField(field, fields, options) {
data = item.element.instance;
}
// Run optional callback function
if (field.onSelect && data) {
field.onSelect(data, field, options);
}
if (!data.pk) {
return field.placeholder || '';
}

View File

@ -1661,7 +1661,23 @@ function loadSalesOrderLineItemTable(table, options={}) {
in_stock: true,
part: line_item.part,
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,
@ -1760,7 +1776,7 @@ function loadSalesOrderLineItemTable(table, options={}) {
showFooter: true,
uniqueId: 'pk',
detailView: show_detail,
detailViewByClick: show_detail,
detailViewByClick: false,
detailFilter: function(index, row) {
if (pending) {
// Order is pending