From 0c41cc7c775164ce4ef57f4dcd3ec4b1577b8659 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 12 Jul 2021 18:13:06 +1000 Subject: [PATCH] Handle form submissions --- InvenTree/templates/js/forms.js | 4 +++ InvenTree/templates/js/stock.js | 58 ++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/InvenTree/templates/js/forms.js b/InvenTree/templates/js/forms.js index 490b67944f..65c1a11b44 100644 --- a/InvenTree/templates/js/forms.js +++ b/InvenTree/templates/js/forms.js @@ -715,6 +715,10 @@ function getFormFieldValue(name, field, options) { // Find the HTML element var el = $(options.modal).find(`#id_${name}`); + if (!el) { + return null; + } + var value = null; switch (field.type) { diff --git a/InvenTree/templates/js/stock.js b/InvenTree/templates/js/stock.js index 50bc49947f..8612e2758a 100644 --- a/InvenTree/templates/js/stock.js +++ b/InvenTree/templates/js/stock.js @@ -28,6 +28,9 @@ function adjustStock(items, options={}) { var formTitle = 'Form Title Here'; var actionTitle = null; + // API url + var url = null; + var specifyLocation = false; var allowSerializedStock = false; @@ -37,18 +40,22 @@ function adjustStock(items, options={}) { actionTitle = '{% trans "Move" %}'; specifyLocation = true; allowSerializedStock = true; + url = '{% url "api-stock-transfer" %}'; break; case 'count': formTitle = '{% trans "Count Stock" %}'; actionTitle = '{% trans "Count" %}'; + url = '{% url "api-stock-count" %}'; break; case 'take': formTitle = '{% trans "Remove Stock" %}'; actionTitle = '{% trans "Take" %}'; + url = '{% url "api-stock-remove" %}'; break; case 'add': formTitle = '{% trans "Add Stock" %}'; actionTitle = '{% trans "Add" %}'; + url = '{% url "api-stock-add" %}'; break; case 'delete': formTitle = '{% trans "Delete Stock" %}'; @@ -156,7 +163,7 @@ function adjustStock(items, options={}) { buttons += ``; html += ` - + ${item.part_detail.full_name} ${quantity}${status} ${location} @@ -192,7 +199,7 @@ function adjustStock(items, options={}) { api_url: `/api/stock/location/`, model: 'stocklocation', }, - note: { + notes: { label: '{% trans "Notes" %}', help_text: '{% trans "Stock transaction notes" %}', type: 'string', @@ -209,8 +216,48 @@ function adjustStock(items, options={}) { confirm: true, confirmMessage: '{% trans "Confirm stock adjustment" %}', modal: modal, - onSubmit: function(fields, options) { - console.log("submit!"); + onSubmit: function(fields, opts) { + + // Data to transmit + var data = { + items: [], + }; + + // Add values for each selected stock item + items.forEach(function(item) { + + var q = getFormFieldValue(item.pk, {}, {modal: modal}); + + data.items.push({pk: item.pk, quantity: q}) + }); + + // Add in extra field data + for (field_name in extraFields) { + data[field_name] = getFormFieldValue( + field_name, + fields[field_name], + { + modal: modal, + } + ); + } + + inventreePut( + url, + data, + { + method: 'POST', + success: function(response, status) { + + // Destroy the modal window + $(modal).modal('hide'); + + if (options.onSuccess) { + options.onSuccess(); + } + } + } + ); } }); @@ -957,6 +1004,9 @@ function loadStockTable(table, options) { adjustStock(items, { action: action, + onSuccess: function() { + $('#stock-table').bootstrapTable('refresh'); + } }); return;