diff --git a/InvenTree/order/templates/order/sales_order_base.html b/InvenTree/order/templates/order/sales_order_base.html index 83ba6d9614..af923ab097 100644 --- a/InvenTree/order/templates/order/sales_order_base.html +++ b/InvenTree/order/templates/order/sales_order_base.html @@ -226,7 +226,7 @@ $("#edit-order").click(function() { $("#complete-order-shipments").click(function() { - completeShipments( + completePendingShipments( {{ order.pk }}, { reload: true, diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index a6d8cbdc0d..b9b6da3b36 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -133,6 +133,7 @@ function completeShipment(shipment_id, options={}) { preFormContent: html, confirm: true, confirmMessage: '{% trans "Confirm Shipment" %}', + buttons: options.buttons, onSuccess: function(data) { // Reload tables $('#so-lines-table').bootstrapTable('refresh'); @@ -143,12 +144,107 @@ function completeShipment(shipment_id, options={}) { options.onSuccess(data); } }, - reload: !!options.reload + reload: options.reload }); } }); } +/* + * Launches a modal to mark all allocated pending shipments as complete + */ +function completePendingShipments(order_id, options={}) { + var pending_shipments = null; + + // Request the list of stock items which will be shipped + inventreeGet(`/api/order/so/shipment/.*`, + { + order: order_id, + shipped: false + }, + { + async: false, + success: function(shipments) { + pending_shipments = shipments; + } + } + ); + + var allocated_shipments = []; + + for (var idx = 0; idx < pending_shipments.length; idx++) { + if (pending_shipments[idx].allocations.length > 0) { + allocated_shipments.push(pending_shipments[idx]); + } + } + + if (allocated_shipments.length > 0) { + completePendingShipmentsHelper(allocated_shipments, 0, options); + + } else { + html = ` +
+ `; + + if (!pending_shipments.length) { + html += ` + {% trans "No pending shipments found" %} + `; + } else { + html += ` + {% trans "No stock items have been allocated to pending shipments" %} + `; + } + + html += ` +
+ `; + + constructForm(`/api/order/so/shipment/0/ship/`, { + method: 'POST', + title: '{% trans "Complete Shipments" %}', + preFormContent: html, + onSubmit: function(fields, options) { + handleFormSuccess(fields, options); + }, + closeText: 'Close', + hideSubmitButton: true, + }); + } +} + + +/* + * Recursive helper for opening shipment completion modals + */ +function completePendingShipmentsHelper(shipments, shipment_idx, options={}) { + if (shipment_idx < shipments.length) { + completeShipment(shipments[shipment_idx].pk, + { + buttons: [ + { + name: 'skip', + title: `{% trans "Skip" %}`, + onClick: function(form_options) { + if (form_options.modal) { + $(form_options.modal).modal('hide'); + } + + completePendingShipmentsHelper(shipments, shipment_idx + 1, options); + } + } + ], + onSuccess: function(data) { + completePendingShipmentsHelper(shipments, shipment_idx + 1, options); + }, + } + ); + + } else if (options.reload) { + location.reload(); + } +} + /* * Launches a modal form to mark a PurchaseOrder as "complete" */