From ba542dcbdb6a4fdc4c5599d515003090ed47fc91 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 17 Feb 2021 13:06:18 +1100 Subject: [PATCH] Auto-fill build quantity --- InvenTree/build/forms.py | 5 +++++ InvenTree/build/views.py | 22 +++++++++++++++------- InvenTree/part/models.py | 39 +++++++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/InvenTree/build/forms.py b/InvenTree/build/forms.py index f5764b9d93..061ae0d16d 100644 --- a/InvenTree/build/forms.py +++ b/InvenTree/build/forms.py @@ -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 = [ diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py index 76cc9bcfde..18971d74a6 100644 --- a/InvenTree/build/views.py +++ b/InvenTree/build/views.py @@ -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 diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index c5e562ec80..82a7073612 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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