Merge pull request #2184 from SchrodingersGat/allocation-fixes

Sales order allocation fix
This commit is contained in:
Oliver 2021-10-20 23:53:59 +11:00 committed by GitHub
commit 5571cfdbdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 2 deletions

View File

@ -309,6 +309,8 @@ class BuildAllocationItemSerializer(serializers.Serializer):
def validate_bom_item(self, bom_item):
# TODO: Fix this validation - allow for variants and substitutes!
build = self.context['build']
# BomItem must point to the same 'part' as the parent build

View File

@ -523,6 +523,10 @@ $('#allocate-selected-items').click(function() {
var bom_items = $("#allocation-table-untracked").bootstrapTable("getSelections");
if (bom_items.length == 0) {
bom_items = $("#allocation-table-untracked").bootstrapTable('getData');
}
allocateStockToBuild(
{{ build.pk }},
{{ build.part.pk }},

View File

@ -1404,6 +1404,24 @@ function allocateStockToBuild(build_id, part_id, bom_items, options={}) {
render_part_detail: true,
render_location_detail: true,
auto_fill: true,
onSelect: function(data, field, opts) {
// Adjust the 'quantity' field based on availability
if (!('quantity' in data)) {
return;
}
// Quantity remaining to be allocated
var remaining = Math.max((bom_item.required || 0) - (bom_item.allocated || 0), 0);
// 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(`items_quantity_${bom_item.pk}`, desired, {}, opts);
},
adjustFilters: function(filters) {
// Restrict query to the selected location
var location = getFormFieldValue(

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

@ -1641,6 +1641,13 @@ function loadSalesOrderLineItemTable(table, options={}) {
var line_item = $(table).bootstrapTable('getRowByUniqueId', pk);
// 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: {
@ -1654,9 +1661,26 @@ 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,
},
};
@ -1752,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