From 7dd7b68ed6df0909bbcf050964f8df20ed51a785 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 7 May 2019 23:03:05 +1000 Subject: [PATCH] Improve functions --- InvenTree/build/models.py | 19 +++++++++++-------- InvenTree/build/views.py | 7 ++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 2a998dc26e..64cf8105c4 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -221,6 +221,16 @@ class Build(models.Model): self.status = self.COMPLETE self.save() + def getRequiredQuantity(self, part): + """ Calculate the quantity of required to make this build. + """ + + try: + item = BomItem.objects.get(part=self.part.id, sub_part=part.id) + return item.quantity * self.quantity + except BomItem.DoesNotExist: + return 0 + def getAllocatedQuantity(self, part): """ Calculate the total number of currently allocated to this build """ @@ -244,14 +254,7 @@ class Build(models.Model): The remaining allocated quantity """ - try: - bom_item = BomItem.objects.get(part=self.part.id, sub_part=part.id) - except BomItem.DoesNotExist: - return 0 - - quantity = bom_item.quantity * self.quantity - - return quantity - self.getAllocatedQuantity(part) + return max(self.getRequiredQuantity(part) - self.getAllocatedQuantity(part), 0) @property def required_parts(self): diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py index cccd705e43..19af4b6458 100644 --- a/InvenTree/build/views.py +++ b/InvenTree/build/views.py @@ -294,6 +294,10 @@ class BuildItemCreate(AjaxCreateView): # If there is only one item selected, select it if len(stocks) == 1: form.fields['stock_item'].initial = stocks[0].id + # There is no stock available + elif len(stocks) == 0: + # TODO - Add a message to the form describing the problem + pass except Part.DoesNotExist: pass @@ -324,7 +328,8 @@ class BuildItemCreate(AjaxCreateView): # Try to work out how many parts to allocate if part: - initials['quantity'] = build.getUnallocatedQuantity(part) + unallocated = build.getUnallocatedQuantity(part) + initials['quantity'] = unallocated except Build.DoesNotExist: pass