From 0ec067da40cfbaf1155d7dbbf1bc60f440462f93 Mon Sep 17 00:00:00 2001 From: Maksim Stojkovic <18454392+maksimstojkovic@users.noreply.github.com> Date: Sun, 22 May 2022 09:34:38 +1000 Subject: [PATCH] [Feature] Shipment Creation in Stock Allocation Modal (#3024) * Added order reference to line item options * Basic working version implemented * Re-execute fields function in secondary modals * Added missing argument and parameter * Added missing parentheses * Fixed hidden field name for depth > 0 --- .../templates/order/sales_order_detail.html | 1 + InvenTree/templates/js/translated/forms.js | 18 +++++-- InvenTree/templates/js/translated/order.js | 51 +++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/InvenTree/order/templates/order/sales_order_detail.html b/InvenTree/order/templates/order/sales_order_detail.html index f55480d288..48a62114dc 100644 --- a/InvenTree/order/templates/order/sales_order_detail.html +++ b/InvenTree/order/templates/order/sales_order_detail.html @@ -257,6 +257,7 @@ '#so-lines-table', { order: {{ order.pk }}, + reference: '{{ order.reference }}', status: {{ order.status }}, } ); diff --git a/InvenTree/templates/js/translated/forms.js b/InvenTree/templates/js/translated/forms.js index ec23fd93e1..b4d5fe6cdc 100644 --- a/InvenTree/templates/js/translated/forms.js +++ b/InvenTree/templates/js/translated/forms.js @@ -913,7 +913,7 @@ function getFormFieldElement(name, options) { el = $(`#id_${field_name}`); } - if (!el.exists) { + if (!el.exists()) { console.error(`Could not find form element for field '${name}'`); } @@ -1568,11 +1568,18 @@ function addSecondaryModal(field, fields, options) { var url = secondary.api_url || field.api_url; // If the "fields" attribute is a function, call it with data - if (secondary.fields instanceof Function) { + if (secondary.fields instanceof Function || secondary.fieldsFunction instanceof Function) { // Extract form values at time of button press var data = extractFormData(fields, options); + // Backup and execute fields function in sequential executions of modal + if (secondary.fields instanceof Function) { + secondary.fieldsFunction = secondary.fields; + } else if (secondary.fieldsFunction instanceof Function) { + secondary.fields = secondary.fieldsFunction; + } + secondary.fields = secondary.fields(data); } @@ -2012,7 +2019,7 @@ function constructField(name, parameters, options={}) { // Hidden inputs are rendered without label / help text / etc if (parameters.hidden) { - return constructHiddenInput(name, parameters, options); + return constructHiddenInput(field_name, parameters, options); } // Are we ending a group? @@ -2361,13 +2368,14 @@ function constructInputOptions(name, classes, type, parameters, options={}) { // Construct a "hidden" input -function constructHiddenInput(name, parameters) { +function constructHiddenInput(name, parameters, options={}) { return constructInputOptions( name, 'hiddeninput', 'hidden', - parameters + parameters, + options ); } diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index 4c6e218ea2..ad9849edb8 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -2593,6 +2593,55 @@ function allocateStockToSalesOrder(order_id, line_items, options={}) { }, value: options.shipment || null, auto_fill: true, + secondary: { + method: 'POST', + title: '{% trans "Add Shipment" %}', + fields: function() { + var ref = null; + + // TODO: Refactor code for getting next shipment number + inventreeGet( + '{% url "api-so-shipment-list" %}', + { + order: options.order, + }, + { + async: false, + success: function(results) { + // "predict" the next reference number + ref = results.length + 1; + + var found = false; + + while (!found) { + + var no_match = true; + + for (var ii = 0; ii < results.length; ii++) { + if (ref.toString() == results[ii].reference.toString()) { + no_match = false; + break; + } + } + + if (no_match) { + break; + } else { + ref++; + } + } + } + } + ); + + var fields = salesOrderShipmentFields(options); + + fields.reference.value = ref; + fields.reference.prefix = global_settings.SALESORDER_REFERENCE_PREFIX + options.reference; + + return fields; + } + } } }, preFormContent: html, @@ -3475,6 +3524,8 @@ function loadSalesOrderLineItemTable(table, options={}) { line_item ], { + order: options.order, + reference: options.reference, success: function() { // Reload this table $(table).bootstrapTable('refresh');