Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Validate fulfilled quantity

Anna Siwikb32a30f0cae9
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 72e54f5dce..990e2b0944 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -324,6 +324,7 @@ class AddressForm(StorefrontAddressForm):
class FulfillmentForm(forms.ModelForm):
+ """Create fulfillment group for a given order."""
class Meta:
model = Fulfillment
fields = ['tracking_number']
@@ -342,16 +343,22 @@ class BaseFulfillmentLineFormSet(forms.BaseModelFormSet):
class FulfillmentLineForm(forms.ModelForm):
+ """Fulfill order line with given quantity by decreasing stock."""
class Meta:
model = FulfillmentLine
fields = ['order_line', 'quantity']
def clean_quantity(self):
quantity = self.cleaned_data.get('quantity')
- if quantity == 0:
- raise forms.ValidationError(pgettext_lazy(
+ order_line = self.cleaned_data.get('order_line')
+ if quantity > order_line.quantity_unfulfilled:
+ raise forms.ValidationError(npgettext_lazy(
'Fulfill order line form error',
- 'Order line could not be fulfilled with zero quantity.'))
+ '%(quantity)d item remaining to fulfill.',
+ '%(quantity)d items remaining to fulfill.',
+ 'quantity') % {
+ 'quantity': order_line.quantity_unfulfilled,
+ 'order_line': order_line})
return quantity
def save(self, commit=True):
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 9abddd4330..6caaa5877e 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -387,17 +387,21 @@ def fulfill_order_lines(request, order_pk):
formset = FulfillmentLineFormSet(
request.POST or None, queryset=FulfillmentLine.objects.none(),
initial=initial)
- valid_forms = [line_form for line_form in formset if line_form.is_valid()]
- if valid_forms and form.is_valid():
- fulfillment = form.save()
- for line_form in valid_forms:
- line = line_form.save(commit=False)
- line.fulfillment = fulfillment
- line.save()
- order.status = (
- OrderStatus.FULFILLED if order.is_fulfilled()
- else OrderStatus.PARTIALLY_FULFILLED)
- order.save(update_fields=['status'])
+ all_forms_valid = all([line_form.is_valid() for line_form in formset])
+ if all_forms_valid and form.is_valid():
+ forms_to_save = [
+ line_form for line_form in formset
+ if line_form.cleaned_data.get('quantity') > 0]
+ if forms_to_save:
+ fulfillment = form.save()
+ for line_form in forms_to_save:
+ line = line_form.save(commit=False)
+ line.fulfillment = fulfillment
+ line.save()
+ order.status = (
+ OrderStatus.FULFILLED if order.is_fulfilled()
+ else OrderStatus.PARTIALLY_FULFILLED)
+ order.save(update_fields=['status'])
return redirect('dashboard:order-details', order_pk=order.pk)
elif form.errors:
status = 400
diff --git a/templates/dashboard/order/fulfillment.html b/templates/dashboard/order/fulfillment.html
index 4cad618da2..c7fc155912 100644
--- a/templates/dashboard/order/fulfillment.html
+++ b/templates/dashboard/order/fulfillment.html
@@ -96,6 +96,16 @@
required="{{ line_form.quantity.field.required }}"
type="number"
value="{{ line_form.quantity.value }}">
+ <label for="{{ line_form.quantity.auto_id }}">
+ {% for error in line_form.quantity.errors %}
+ <p class="help-block materialize-red-text">{{ error }}</p>
+ {% endfor %}
+ {% if line_form.quantity.help_text %}
+ <p class="help-block">
+ {{ line_form.quantity.field.help_text|safe }}
+ </p>
+ {% endif %}
+ </label>
</td>
</tr>
{% endfor %}
@@ -112,7 +122,7 @@
{% trans "Cancel" context "Dashboard cancel action" %}
</a>
<button type="submit" class="btn waves-effect">
- {% trans "Save" context "Dashboard update action" %}
+ {% trans "Fulfill" context "Dashboard update action" %}
</button>
</div>
</form>