Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Prevent from cancelling line when there is only one in order
Artur Smętfd04bec9c18a
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 1d8fb457ea..ba5773baae 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -127,11 +127,34 @@ class CancelItemsForm(forms.Form):self.item = kwargs.pop('item')super(CancelItemsForm, self).__init__(*args, **kwargs)+ def clean(self):+ data = super(CancelItemsForm, self).clean()+ # Check if this line is not the only line in this order+ order = self.item.delivery_group.order+ other_group_items = self.item.delivery_group.items.exclude(+ pk=self.item.pk)+ other_groups = order.groups.exclude(pk=self.item.delivery_group.pk)+ if not other_group_items and not other_groups:+ # There is only one group in this order and user wants+ # to cancel this one line+ # He should cancel whole order instead.+ raise forms.ValidationError(+ _('This is the only line in Order. '+ 'You should cancel whole order instead.'))+ return data+def cancel_item(self):delivery_group = self.item.delivery_groupStock.objects.deallocate_stock(self.item.stock, self.item.quantity)self.item.delete()- delivery_group.order.recalculate()+ delivery_group = DeliveryGroup.objects.get(pk=delivery_group.pk)+ order = delivery_group.order+ if not delivery_group.items.exists():+ # Should we cancel order after dropping this group?+ if order.groups.exclude(pk=delivery_group.pk).exists():+ # we have another delivery groups as well+ delivery_group.delete()+ order.recalculate()class ChangeQuantityForm(forms.ModelForm):diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.pyindex 1dc09963cb..389cb62069 100644--- a/saleor/dashboard/order/views.py+++ b/saleor/dashboard/order/views.py@@ -211,7 +211,7 @@ def orderline_cancel(request, order_pk, line_pk):return redirect('dashboard:order-details', order_pk=order.pk)elif form.errors:status = 400- ctx = {'order': order, 'item': item}+ ctx = {'order': order, 'item': item, 'form': form}return TemplateResponse(request, 'dashboard/order/modal_cancel_line.html',ctx, status=status)@@ -298,7 +298,6 @@ def cancel_order(request, order_pk):ctx, status=status)-def remove_order_voucher(request, order_pk):status = 200order = get_object_or_404(Order, pk=order_pk)diff --git a/saleor/dashboard/templates/dashboard/order/modal_cancel_line.html b/saleor/dashboard/templates/dashboard/order/modal_cancel_line.htmlindex 8fb7baaf01..6773dc18d2 100644--- a/saleor/dashboard/templates/dashboard/order/modal_cancel_line.html+++ b/saleor/dashboard/templates/dashboard/order/modal_cancel_line.html@@ -2,42 +2,42 @@{% load materializecss %}{% load prices_i18n %}-<div class="modal-content">- <h5>{% trans "Cancer order line?" %}</h5>- <p>- {% blocktrans with order=order%}- Are you sure, you want cancel this order line?- {% endblocktrans %}- </p>- <table>- <thead>- <tr>- <th class="wide">{% trans "Item" %}</th>- <th>{% trans "SKU" %}</th>- <th class="right-align">{% trans "Price" %}</th>- <th class="right-align">{% trans "Quantity" %}</th>- <th class="right-align">{% trans "Total" %}</th>- </tr>- </thead>- <tbody>- <tr>- <td>- {{ item.product_name }}<br>- </td>- <td>{{ item.product_sku }}</td>- <td class="right-align">{% gross item.get_price_per_item %}</td>- <td class="right-align">{{ item.quantity }}</td>- <td class="right-align">{% gross item.get_total %}</td>- </tr>- </tbody>- </table>-</div>+<form class="form-async" role="form" method="post" action="{% url 'dashboard:orderline-cancel' order_pk=order.pk line_pk=item.pk %}">+ {% csrf_token %}+ <div class="modal-content">+ <h5>{% trans "Cancer order line?" %}</h5>+ <p>+ {% blocktrans with order=order%}+ Are you sure, you want cancel this order line?+ {% endblocktrans %}+ </p>+ {{ form|materializecss }}+ <table>+ <thead>+ <tr>+ <th class="wide">{% trans "Item" %}</th>+ <th>{% trans "SKU" %}</th>+ <th class="right-align">{% trans "Price" %}</th>+ <th class="right-align">{% trans "Quantity" %}</th>+ <th class="right-align">{% trans "Total" %}</th>+ </tr>+ </thead>+ <tbody>+ <tr>+ <td>+ {{ item.product_name }}<br>+ </td>+ <td>{{ item.product_sku }}</td>+ <td class="right-align">{% gross item.get_price_per_item %}</td>+ <td class="right-align">{{ item.quantity }}</td>+ <td class="right-align">{% gross item.get_total %}</td>+ </tr>+ </tbody>+ </table>+ </div>-<div class="modal-footer">- <form id="release-form" method="post" role="form" class="form-async"- action="{% url 'dashboard:orderline-cancel' order_pk=order.pk line_pk=item.pk %}">- {% csrf_token %}- <button type="submit" class="modal-action btn-flat red-text">{% trans "Cancel Line" %}</button>- <a href="#!" aria-hidden="true" class="modal-action modal-close btn-flat blue-text">{% trans "Abort" %}</a>- </form>-</div>+ <div class="modal-footer">+ <button type="submit" class="modal-action btn-flat red-text">{% trans "Cancel Line" %}</button>+ <a href="#!" aria-hidden="true" class="modal-action modal-close btn-flat blue-text">{% trans "Abort" %}</a>+ </div>+</form>