From e43439ef5b6bd0b89c394fbdf1a08cb40d7e4963 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 16 Apr 2018 21:49:38 +1000 Subject: [PATCH] Function to calculate how many parts can be made with current stock --- InvenTree/part/models.py | 38 +++++++++++++++++--- InvenTree/part/templates/part/bom.html | 2 +- InvenTree/part/templates/part/delete.html | 4 +-- InvenTree/part/templates/part/part_base.html | 10 ++++-- InvenTree/part/templates/part/tabs.html | 8 ++--- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index d9bdc50311..bf421cef3b 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -132,8 +132,34 @@ class Part(models.Model): verbose_name_plural = "Parts" @property - def tracked_parts(self): - return self.serials.order_by('serial') + def available_stock(self): + """ + Return the total available stock. + This subtracts stock which is already allocated + """ + + # TODO - For now, just return total stock count + return self.stock + + @property + def can_build(self): + """ Return the number of units that can be build with available stock + """ + + # If this part does NOT have a BOM, result is simply the currently available stock + if not self.has_bom: + return self.available_stock + + total = None + + for item in self.bom_items.all(): + stock = item.sub_part.available_stock + n = int(1.0 * stock / item.quantity) + + if total is None or n < total: + total = n + + return total @property def stock(self): @@ -149,11 +175,15 @@ class Part(models.Model): return result['total'] @property - def bomItemCount(self): + def has_bom(self): + return self.bom_item_count > 0 + + @property + def bom_item_count(self): return self.bom_items.all().count() @property - def usedInCount(self): + def used_in_count(self): return self.used_in.all().count() """ diff --git a/InvenTree/part/templates/part/bom.html b/InvenTree/part/templates/part/bom.html index 24046b5ca4..31ae8919bd 100644 --- a/InvenTree/part/templates/part/bom.html +++ b/InvenTree/part/templates/part/bom.html @@ -15,7 +15,7 @@ {{ sub_part.name }} {{ sub_part.description }} - {{ bom_item.quantity }} + {{ bom_item.quantity }}{{ bom_item.sub_part.available_stock }} {% endwith %} {% endfor %} diff --git a/InvenTree/part/templates/part/delete.html b/InvenTree/part/templates/part/delete.html index df28dc82d8..5e536508d9 100644 --- a/InvenTree/part/templates/part/delete.html +++ b/InvenTree/part/templates/part/delete.html @@ -7,8 +7,8 @@ {% block del_body %} - {% if part.usedInCount > 0 %} -

This part is used in BOMs for {{ part.usedInCount }} other parts. If you delete this part, the BOMs for the following parts will be updated: + {% if part.used_in_count %} +

This part is used in BOMs for {{ part.used_in_count }} other parts. If you delete this part, the BOMs for the following parts will be updated: