From 6591f328d79d4bf6a8506bc52ad2446417136f2d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 25 Jul 2022 12:19:13 +1000 Subject: [PATCH 1/2] Pre-fill quantity on part ordering form - Adds new API endpoint for requirement data - Load data on the fly when launching ordering form --- InvenTree/part/api.py | 36 ++++++++++++++++++++++ InvenTree/templates/js/translated/order.js | 25 ++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 90579a021c9c..abab3a4e9e90 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -567,6 +567,40 @@ def add_schedule_entry(date, quantity, title, label, url): return Response(schedule) +class PartRequirements(RetrieveAPI): + """API endpoint detailing 'requirements' information for aa particular part. + + This endpoint returns information on upcoming requirements for: + + - Sales Orders + - Build Orders + - Total requirements + + As this data is somewhat complex to calculate, is it not included in the default API + """ + + queryset = Part.objects.all() + + def retrieve(self, request, *args, **kwargs): + """Construct a response detailing Part requirements""" + + part = self.get_object() + + data = { + "available_stock": part.available_stock, + "on_order": part.on_order, + "required_build_order_quantity": part.required_build_order_quantity(), + "allocated_build_order_quantity": part.build_order_allocation_count(), + "required_sales_order_quantity": part.required_sales_order_quantity(), + "allocated_sales_order_quantity": part.sales_order_allocation_count(pending=True), + } + + data["allocated"] = data["allocated_build_order_quantity"] + data["allocated_sales_order_quantity"] + data["required"] = data["required_build_order_quantity"] + data["required_sales_order_quantity"] + + return Response(data) + + class PartMetadata(RetrieveUpdateAPI): """API endpoint for viewing / updating Part metadata.""" @@ -1997,6 +2031,8 @@ class BomItemSubstituteDetail(RetrieveUpdateDestroyAPI): # Endpoint for future scheduling information re_path(r'^scheduling/', PartScheduling.as_view(), name='api-part-scheduling'), + re_path(r'^requirements/', PartRequirements.as_view(), name='api-part-requirements'), + # Endpoint for duplicating a BOM for the specific Part re_path(r'^bom-copy/', PartCopyBOM.as_view(), name='api-part-bom-copy'), diff --git a/InvenTree/templates/js/translated/order.js b/InvenTree/templates/js/translated/order.js index e6300b7326d5..221c0c1d19f3 100644 --- a/InvenTree/templates/js/translated/order.js +++ b/InvenTree/templates/js/translated/order.js @@ -817,7 +817,7 @@ function orderParts(parts_list, options={}) { var thumb = thumbnailImage(part.thumbnail || part.image); - // The "quantity" field should have been provided for each part + // Default quantity value var quantity = part.quantity || 1; if (quantity < 0) { @@ -1017,6 +1017,29 @@ function orderParts(parts_list, options={}) { return '{% trans "No matching purchase orders" %}'; } }, null, opts); + + // Request 'requirements' information for each part + inventreeGet(`/api/part/${part.pk}/requirements/`, {}, { + success: function(response) { + var required = response.required || 0; + var allocated = response.allocated || 0; + var available = response.available_stock || 0; + + // Based on what we currently 'have' on hand, what do we need to order? + var deficit = Math.max(required - allocated, 0); + + if (available < deficit) { + var q = deficit - available; + + updateFieldValue( + `quantity_${part.pk}`, + q, + {}, + opts + ); + } + } + }); }); // Add callback for "add to purchase order" button From f1c75eb696dda264e6cf459b40eedc2ccdb416c9 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 25 Jul 2022 12:21:06 +1000 Subject: [PATCH 2/2] Bump API version --- InvenTree/InvenTree/api_version.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/api_version.py b/InvenTree/InvenTree/api_version.py index 7f2168d4e241..4a6cb3dba339 100644 --- a/InvenTree/InvenTree/api_version.py +++ b/InvenTree/InvenTree/api_version.py @@ -2,11 +2,15 @@ # InvenTree API version -INVENTREE_API_VERSION = 66 +INVENTREE_API_VERSION = 67 """ Increment this API version number whenever there is a significant change to the API that any clients need to know about +v67 -> 2022-07-25 : https://github.com/inventree/InvenTree/pull/3395 + - Adds a 'requirements' endpoint for Part instance + - Provides information on outstanding order requirements for a given part + v66 -> 2022-07-24 : https://github.com/inventree/InvenTree/pull/3393 - Part images can now be downloaded from a remote URL via the API - Company images can now be downloaded from a remote URL via the API