Auto-fill build quantity

This commit is contained in:
Oliver Walters 2021-02-17 13:06:18 +11:00
parent c8650ce34c
commit ba542dcbdb
3 changed files with 51 additions and 15 deletions

View File

@ -39,6 +39,11 @@ class EditBuildForm(HelperForm):
help_text=_('Target date for build completion. Build will be overdue after this date.')
)
quantity = RoundingDecimalFormField(
max_digits=10, decimal_places=5,
help_text=_('Number of items to build')
)
class Meta:
model = Build
fields = [

View File

@ -675,6 +675,14 @@ class BuildCreate(AjaxCreateView):
initials = super(BuildCreate, self).get_initial().copy()
initials['parent'] = self.request.GET.get('parent', None)
# User has provided a SalesOrder ID
initials['sales_order'] = self.request.GET.get('sales_order', None)
initials['quantity'] = self.request.GET.get('quantity', 1)
part = self.request.GET.get('part', None)
if part:
@ -684,18 +692,18 @@ class BuildCreate(AjaxCreateView):
# User has provided a Part ID
initials['part'] = part
initials['destination'] = part.get_default_location()
to_order = part.quantity_to_order
if to_order < 1:
to_order = 1
initials['quantity'] = to_order
except (ValueError, Part.DoesNotExist):
pass
initials['reference'] = Build.getNextBuildNumber()
initials['parent'] = self.request.GET.get('parent', None)
# User has provided a SalesOrder ID
initials['sales_order'] = self.request.GET.get('sales_order', None)
initials['quantity'] = self.request.GET.get('quantity', 1)
# Pre-fill the issued_by user
initials['issued_by'] = self.request.user

View File

@ -976,20 +976,43 @@ class Part(MPTTModel):
return quantity
def required_order_quantity(self):
"""
Return total required to fulfil orders
"""
return self.required_build_order_quantity() + self.required_sales_order_quantity()
@property
def quantity_to_order(self):
""" Return the quantity needing to be ordered for this part. """
"""
Return the quantity needing to be ordered for this part.
Here, an "order" could be one of:
- Build Order
- Sales Order
# How many do we need to have "on hand" at any point?
required = self.net_stock - self.minimum_stock
To work out how many we need to order:
if required < 0:
return abs(required)
Stock on hand = self.total_stock
Required for orders = self.required_order_quantity()
Currently on order = self.on_order
Currently building = self.quantity_being_built
"""
# Do not need to order any
return 0
# Total requirement
required = self.required_order_quantity()
# Subtract stock levels
required -= max(self.total_stock, self.minimum_stock)
# Subtract quantity on order
required -= self.on_order
# Subtract quantity being built
required -= self.quantity_being_built
required = self.net_stock
return max(required, 0)
@property