Skip to content

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

Update discount amount in an order action

Anna Siwika5fde9ebb5b1
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 371f5a6aae..c38e69dacd 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -194,6 +194,22 @@ class OrderRemoveShippingForm(forms.ModelForm):
return super().save(commit)
+class OrderEditDiscountForm(forms.ModelForm):
+ """Edit discount amount in an order."""
+
+ class Meta:
+ model = Order
+ fields = ['discount_amount']
+ labels = {
+ 'discount_amount': pgettext_lazy(
+ 'Order discount amount fixed value',
+ 'Discount amount')}
+
+ def save(self, commit=True):
+ recalculate_order(self.instance)
+ return super().save(commit)
+
+
class OrderNoteForm(forms.ModelForm):
class Meta:
model = OrderNote
diff --git a/saleor/dashboard/order/urls.py b/saleor/dashboard/order/urls.py
index 57ad010da3..0517e266a0 100644
--- a/saleor/dashboard/order/urls.py
+++ b/saleor/dashboard/order/urls.py
@@ -15,12 +15,14 @@ urlpatterns = [
views.cancel_order, name='order-cancel'),
url(r'^(?P<order_pk>\d+)/address/(?P<address_type>billing|shipping)/$',
views.address_view, name='address-edit'),
- url(r'^(?P<order_pk>\d+)/customer/edit/$',
+ url(r'^(?P<order_pk>\d+)/edit-customer/$',
views.order_customer_edit, name='order-customer-edit'),
- url(r'^(?P<order_pk>\d+)/shipping/edit/$',
+ url(r'^(?P<order_pk>\d+)/edit-shipping/$',
views.order_shipping_edit, name='order-shipping-edit'),
- url(r'^(?P<order_pk>\d+)/shipping/remove/$',
+ url(r'^(?P<order_pk>\d+)/remove-shipping/$',
views.order_shipping_remove, name='order-shipping-remove'),
+ url(r'^(?P<order_pk>\d+)/edit-discount/$',
+ views.order_discount_edit, name='order-discount-edit'),
url(r'^(?P<order_pk>\d+)/delete/$',
views.remove_draft_order, name='draft-order-delete'),
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
index 18b11192a5..fec6fb0ef4 100644
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -30,9 +30,9 @@ from .forms import (
CancelFulfillmentForm, CancelOrderForm, CancelOrderLineForm,
CapturePaymentForm, ChangeQuantityForm, ChangeStockForm,
ConfirmDraftOrderForm, FulfillmentForm, FulfillmentLineForm,
- FulfillmentTrackingNumberForm, OrderCustomerForm, OrderNoteForm,
- OrderRemoveShippingForm, OrderShippingForm, RefundPaymentForm,
- ReleasePaymentForm, RemoveVoucherForm)
+ FulfillmentTrackingNumberForm, OrderCustomerForm, OrderEditDiscountForm,
+ OrderNoteForm, OrderRemoveShippingForm, OrderShippingForm,
+ RefundPaymentForm, ReleasePaymentForm, RemoveVoucherForm)
from .utils import (
create_invoice_pdf, create_packing_slip_pdf, get_statics_absolute_url)
@@ -407,6 +407,25 @@ def order_shipping_remove(request, order_pk):
status=status)
+@staff_member_required
+@permission_required('order.edit_order')
+def order_discount_edit(request, order_pk):
+ order = get_object_or_404(Order, pk=order_pk, status=OrderStatus.DRAFT)
+ form = OrderEditDiscountForm(request.POST or None, instance=order)
+ status = 200
+ if form.is_valid():
+ form.save()
+ msg = pgettext_lazy('Dashboard message', 'Discount updated')
+ messages.success(request, msg)
+ return redirect('dashboard:order-details', order_pk=order_pk)
+ elif form.errors:
+ status = 400
+ ctx = {'order': order, 'form': form}
+ return TemplateResponse(
+ request, 'dashboard/order/modal/edit_discount.html', ctx,
+ status=status)
+
+
@staff_member_required
@permission_required('order.edit_order')
def cancel_order(request, order_pk):
diff --git a/templates/dashboard/order/detail.html b/templates/dashboard/order/detail.html
index 2bddca0a3f..3bd11ecc28 100644
--- a/templates/dashboard/order/detail.html
+++ b/templates/dashboard/order/detail.html
@@ -386,11 +386,19 @@
</a>
<ul id="voucher-actions" class="dropdown-content">
<li>
- <a href="#base-modal" data-href="{% url 'dashboard:order-remove-voucher' order_pk=order.pk %}"
- class="modal-trigger-custom" title="{% trans "Remove voucher" context "Order totals action" %}">
- {% trans "Remove voucher" context "Order totals action" %}
+ <a href="#base-modal" data-href="{% url 'dashboard:order-discount-edit' order_pk=order.pk %}"
+ class="modal-trigger-custom" title="{% trans "Edit discount" context "Order totals action" %}">
+ {% trans "Edit discount" context "Order totals action" %}
</a>
</li>
+ {% if order.voucher %}
+ <li>
+ <a href="#base-modal" data-href="{% url 'dashboard:order-remove-voucher' order_pk=order.pk %}"
+ class="modal-trigger-custom" title="{% trans "Remove voucher" context "Order totals action" %}">
+ {% trans "Remove voucher" context "Order totals action" %}
+ </a>
+ </li>
+ {% endif %}
</ul>
{% elif order.discount_amount %}
{% if order.discount_name %}
diff --git a/templates/dashboard/order/modal/edit_discount.html b/templates/dashboard/order/modal/edit_discount.html
new file mode 100644
index 0000000000..b0339e929e
--- /dev/null
+++ b/templates/dashboard/order/modal/edit_discount.html
@@ -0,0 +1,22 @@
+{% extends "dashboard/base_modal.html" %}
+
+{% load i18n %}
+{% load materializecss %}
+
+{% block action_url %}
+{% url 'dashboard:order-discount-edit' order_pk=order.pk %}
+{% endblock %}
+
+{% block title %}
+{% trans "Edit discount" context "Modal edit discount title" %}
+{% endblock %}
+
+{% block content %}
+ <div class="col s12">
+ {{ form.discount_amount|materializecss }}
+ </div>
+{% endblock %}
+
+{% block primary_action %}
+{% trans "Update" context "Modal edit discount primary action" %}
+{% endblock %}
diff --git a/tests/dashboard/test_order.py b/tests/dashboard/test_order.py
index e3f3a3a94d..5945dfc3fc 100644
--- a/tests/dashboard/test_order.py
+++ b/tests/dashboard/test_order.py
@@ -2,7 +2,6 @@ from decimal import Decimal
from unittest.mock import patch
import pytest
-from django.conf import settings
from django.urls import reverse
from payments import PaymentStatus
from prices import Money, TaxedMoney
@@ -1065,7 +1064,8 @@ def test_view_order_customer_edit_not_valid(admin_client, customer_user):
@pytest.mark.django_db
-def test_view_order_shipping_edit(admin_client, draft_order, shipping_method):
+def test_view_order_shipping_edit(
+ admin_client, draft_order, shipping_method, settings):
method = shipping_method.price_per_country.create(
price=Money(5, settings.DEFAULT_CURRENCY), country_code='PL')
url = reverse(
@@ -1139,3 +1139,25 @@ def test_view_remove_draft_order_invalid(admin_client, order_with_lines):
assert response.status_code == 404
assert Order.objects.count() == 1
+
+
+def test_view_edit_discount(admin_client, draft_order, settings):
+ discount_value = 5
+ total_without_discount = draft_order.total
+ url = reverse(
+ 'dashboard:order-discount-edit',
+ kwargs={'order_pk': draft_order.pk})
+ data = {'discount_amount': discount_value}
+
+ response = admin_client.post(url, data)
+
+ assert response.status_code == 302
+ redirect_url = reverse(
+ 'dashboard:order-details', kwargs={'order_pk': draft_order.pk})
+ assert get_redirect_location(response) == redirect_url
+
+ draft_order.refresh_from_db()
+ discount_amount = Money(discount_value, settings.DEFAULT_CURRENCY)
+ assert draft_order.discount_amount == discount_amount
+ assert draft_order.total == total_without_discount - discount_amount