From 72cfaccac574814e6e201706d117bb2295f9f29d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 15 May 2020 21:16:00 +1000 Subject: [PATCH] Pass StockItem object through to the SerializeStock form --- InvenTree/stock/forms.py | 15 ++++++++++++++- InvenTree/stock/views.py | 13 ++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index a4578440cb..0286054766 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -13,6 +13,8 @@ from mptt.fields import TreeNodeChoiceField from InvenTree.helpers import GetExportFormats from InvenTree.forms import HelperForm +from InvenTree.fields import RoundingDecimalFormField + from .models import StockLocation, StockItem, StockItemTracking, StockItemAttachment @@ -79,7 +81,7 @@ class CreateStockItemForm(HelperForm): self._clean_form() -class SerializeStockForm(forms.ModelForm): +class SerializeStockForm(HelperForm): """ Form for serializing a StockItem. """ destination = TreeNodeChoiceField(queryset=StockLocation.objects.all(), label='Destination', required=True, help_text='Destination for serialized stock (by default, will remain in current location)') @@ -88,6 +90,17 @@ class SerializeStockForm(forms.ModelForm): note = forms.CharField(label='Notes', required=False, help_text='Add transaction note (optional)') + quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5) + + def __init__(self, *args, **kwargs): + + # Extract the stock item + stock_item = kwargs.pop('item') + + super().__init__(*args, **kwargs) + + # TODO - Pre-fill the serial numbers! + class Meta: model = StockItem diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index e616be1f35..b94036f66e 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -755,7 +755,18 @@ class StockItemSerialize(AjaxUpdateView): model = StockItem ajax_template_name = 'stock/item_serialize.html' ajax_form_title = _('Serialize Stock') - form_class = SerializeStockForm + #form_class = SerializeStockForm + + def get_form(self): + + context = self.get_form_kwargs() + + # Pass the StockItem object through to the form + context['item'] = self.get_object() + + form = SerializeStockForm(**context) + + return form def get_initial(self):