From cda97829ab7280b36b27df08823f7061f8bab054 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 17 Feb 2021 10:27:36 +1100 Subject: [PATCH] Add function for required build order quantity --- InvenTree/part/models.py | 56 ++++++++++++++++++++ InvenTree/part/templates/part/part_base.html | 7 +++ 2 files changed, 63 insertions(+) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 8ba7ba799d..85e7c051bf 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -884,6 +884,62 @@ class Part(MPTTModel): return max(total, 0) + def requiring_build_orders(self): + """ + Return list of outstanding build orders which require this part + """ + + # List of BOM that this part is required for + boms = BomItem.objects.filter(sub_part=self) + + part_ids = [bom.part.pk for bom in boms] + + # Now, get a list of outstanding build orders which require this part + builds = BuildModels.Build.objects.filter( + part__in=part_ids, + status__in=BuildStatus.ACTIVE_CODES + ) + + return builds + + def required_build_order_quantity(self): + """ + Return the quantity of this part required for active build orders + """ + + # List of BOM that this part is required for + boms = BomItem.objects.filter(sub_part=self) + + part_ids = [bom.part.pk for bom in boms] + + # Now, get a list of outstanding build orders which require this part + builds = BuildModels.Build.objects.filter( + part__in=part_ids, + status__in=BuildStatus.ACTIVE_CODES + ) + + quantity = 0 + + for build in builds: + + bom_item = None + + # Match BOM item to build + for bom in boms: + if bom.part == build.part: + bom_item = bom + break + + if bom_item is None: + logger.warning("Found null BomItem when calculating required quantity") + continue + + build_quantity = build.quantity * bom_item.quantity + + quantity += build_quantity + + return quantity + @property def quantity_to_order(self): """ Return the quantity needing to be ordered for this part. """ diff --git a/InvenTree/part/templates/part/part_base.html b/InvenTree/part/templates/part/part_base.html index eb347fc29e..0ddb5d2180 100644 --- a/InvenTree/part/templates/part/part_base.html +++ b/InvenTree/part/templates/part/part_base.html @@ -127,6 +127,13 @@ {% include "part/stock_count.html" %} {% if not part.is_template %} + {% if part.required_build_order_quantity > 0 %} + + + {% trans "Required for Build Orders" %} + {% decimal part.required_build_order_quantity %} + + {% endif %} {% if part.build_order_allocation_count > 0 %}