diff --git a/myproject/inventory/forms.py b/myproject/inventory/forms.py index 03b03db..19fe023 100644 --- a/myproject/inventory/forms.py +++ b/myproject/inventory/forms.py @@ -45,16 +45,33 @@ class WarehouseForm(forms.ModelForm): class SaleForm(forms.ModelForm): class Meta: model = Sale - fields = ['product', 'warehouse', 'quantity', 'sale_price', 'order', 'document_number'] + fields = ['product', 'warehouse', 'sales_unit', 'quantity', 'sale_price', 'order', 'document_number'] widgets = { 'product': forms.Select(attrs={'class': 'form-control'}), 'warehouse': forms.Select(attrs={'class': 'form-control'}), + 'sales_unit': forms.Select(attrs={'class': 'form-control'}), 'quantity': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.001'}), 'sale_price': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}), 'order': forms.Select(attrs={'class': 'form-control'}), 'document_number': forms.TextInput(attrs={'class': 'form-control'}), } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + # Динамический queryset для sales_unit + if self.instance.pk and self.instance.product: + from products.models import ProductSalesUnit + self.fields['sales_unit'].queryset = ProductSalesUnit.objects.filter( + product=self.instance.product, + is_active=True + ).order_by('position', 'id') + else: + from products.models import ProductSalesUnit + self.fields['sales_unit'].queryset = ProductSalesUnit.objects.none() + + self.fields['sales_unit'].required = False + def clean_quantity(self): quantity = self.cleaned_data.get('quantity') if quantity and quantity <= 0: diff --git a/myproject/inventory/templates/inventory/sale/sale_form.html b/myproject/inventory/templates/inventory/sale/sale_form.html index d30f50b..50b38a8 100644 --- a/myproject/inventory/templates/inventory/sale/sale_form.html +++ b/myproject/inventory/templates/inventory/sale/sale_form.html @@ -52,6 +52,22 @@ + + +
@@ -295,6 +299,7 @@ +
@@ -308,6 +313,9 @@
+ + +
@@ -1809,7 +1817,8 @@ document.addEventListener('DOMContentLoaded', function() {
- + +