Improve functions

This commit is contained in:
Oliver Walters 2019-05-07 23:03:05 +10:00
parent 4f84febbd1
commit 7dd7b68ed6
2 changed files with 17 additions and 9 deletions

View File

@ -221,6 +221,16 @@ class Build(models.Model):
self.status = self.COMPLETE self.status = self.COMPLETE
self.save() self.save()
def getRequiredQuantity(self, part):
""" Calculate the quantity of <part> 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): def getAllocatedQuantity(self, part):
""" Calculate the total number of <part> currently allocated to this build """ Calculate the total number of <part> currently allocated to this build
""" """
@ -244,14 +254,7 @@ class Build(models.Model):
The remaining allocated quantity The remaining allocated quantity
""" """
try: return max(self.getRequiredQuantity(part) - self.getAllocatedQuantity(part), 0)
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)
@property @property
def required_parts(self): def required_parts(self):

View File

@ -294,6 +294,10 @@ class BuildItemCreate(AjaxCreateView):
# If there is only one item selected, select it # If there is only one item selected, select it
if len(stocks) == 1: if len(stocks) == 1:
form.fields['stock_item'].initial = stocks[0].id 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: except Part.DoesNotExist:
pass pass
@ -324,7 +328,8 @@ class BuildItemCreate(AjaxCreateView):
# Try to work out how many parts to allocate # Try to work out how many parts to allocate
if part: if part:
initials['quantity'] = build.getUnallocatedQuantity(part) unallocated = build.getUnallocatedQuantity(part)
initials['quantity'] = unallocated
except Build.DoesNotExist: except Build.DoesNotExist:
pass pass