Skip to content

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

Fix logic for tax calculation on draft orders. Fix tests in api/test_orders

Maciej Korycinski212107b9fe67
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.py
index fd299734b0..30bdb42ccc 100644
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@ -80,11 +80,12 @@ def get_voucher_discount_for_order(order):
"""
if not order.voucher:
return ZERO_MONEY
+ subtotal = order.get_subtotal()
if order.voucher.type == VoucherType.VALUE:
- return get_value_voucher_discount(order.voucher, order.get_subtotal())
+ return get_value_voucher_discount(order.voucher, subtotal.gross)
if order.voucher.type == VoucherType.SHIPPING:
return get_shipping_voucher_discount(
- order.voucher, order.get_subtotal(), order.shipping_price
+ order.voucher, subtotal.gross, order.shipping_price
)
if order.voucher.type in (
VoucherType.PRODUCT,
diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.py
index f6dd38a2fa..3fbd88a43b 100644
--- a/saleor/graphql/order/mutations/draft_orders.py
+++ b/saleor/graphql/order/mutations/draft_orders.py
@@ -114,14 +114,10 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
if shipping_address:
shipping_address.save()
instance.shipping_address = shipping_address.get_copy()
- if instance.is_shipping_required():
- update_order_prices(instance, info.context.discounts)
billing_address = cleaned_input.get("billing_address")
if billing_address:
billing_address.save()
instance.billing_address = billing_address.get_copy()
- if not instance.is_shipping_required():
- update_order_prices(instance, info.context.discounts)
@staticmethod
def _save_lines(info, instance, quantities, variants):
@@ -153,8 +149,22 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
instance.save(update_fields=["billing_address", "shipping_address"])
+ @classmethod
+ def _refresh_lines_unit_price(cls, info, instance, cleaned_input, new_instance):
+ if new_instance:
+ # It is a new instance, all new lines have already updated prices.
+ return
+ shipping_address = cleaned_input.get("shipping_address")
+ if shipping_address and instance.is_shipping_required():
+ update_order_prices(instance, info.context.discounts)
+ billing_address = cleaned_input.get("billing_address")
+ if billing_address and not instance.is_shipping_required():
+ update_order_prices(instance, info.context.discounts)
+
@classmethod
def save(cls, info, instance, cleaned_input):
+ new_instance = bool(instance.pk)
+
# Process addresses
cls._save_addresses(info, instance, cleaned_input)
@@ -169,6 +179,8 @@ class DraftOrderCreate(ModelMutation, I18nMixin):
cleaned_input.get("variants"),
)
+ cls._refresh_lines_unit_price(info, instance, cleaned_input, new_instance)
+
# Post-process the results
recalculate_order(instance)
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 5d41c97c11..471765a7ed 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -6,6 +6,7 @@ import graphene
import pytest
from django.core.exceptions import ValidationError
from freezegun import freeze_time
+from prices import Money, TaxedMoney
from saleor.account.models import CustomerEvent
from saleor.core.taxes import ZERO_TAXED_MONEY
@@ -184,8 +185,10 @@ def test_order_query(
assert order_data["paymentStatusDisplay"] == payment_status_display
assert order_data["isPaid"] == order.is_fully_paid()
assert order_data["userEmail"] == order.user_email
- expected_price = order_data["shippingPrice"]["gross"]["amount"]
- assert expected_price == order.shipping_price.gross.amount
+ expected_price = Money(
+ amount=str(order_data["shippingPrice"]["gross"]["amount"]), currency="USD"
+ )
+ assert expected_price == order.shipping_price.gross
assert len(order_data["lines"]) == order.lines.count()
fulfillment = order.fulfillments.first().fulfillment_order
fulfillment_order = order_data["fulfillments"][0]["fulfillmentOrder"]
@@ -385,7 +388,7 @@ def test_payment_information_order_events_query(
data = content["data"]["orders"]["edges"][0]["node"]["events"][0]
assert data["message"] is None
- assert data["amount"] == amount
+ assert Money(str(data["amount"]), "USD") == order.total.gross
assert data["emailType"] is None
assert data["quantity"] is None
assert data["composedId"] is None
@@ -1650,7 +1653,8 @@ def test_order_update_shipping(
assert data["order"]["id"] == order_id
order.refresh_from_db()
- shipping_price = shipping_method.get_total()
+ shipping_total = shipping_method.get_total()
+ shipping_price = TaxedMoney(shipping_total, shipping_total)
assert order.shipping_method == shipping_method
assert order.shipping_price_net == shipping_price.net
assert order.shipping_price_gross == shipping_price.gross
@@ -1661,7 +1665,9 @@ def test_order_update_shipping_clear_shipping_method(
staff_api_client, permission_manage_orders, order, staff_user, shipping_method
):
order.shipping_method = shipping_method
- order.shipping_price = shipping_method.get_total()
+ shipping_total = shipping_method.get_total()
+ shipping_price = TaxedMoney(shipping_total, shipping_total)
+ order.shipping_price = shipping_price
order.shipping_method_name = "Example shipping"
order.save()
@@ -1790,10 +1796,8 @@ def test_orders_total(staff_api_client, permission_manage_orders, order_with_lin
query, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response)
- assert (
- content["data"]["ordersTotal"]["gross"]["amount"]
- == order_with_lines.total.gross.amount
- )
+ amount = str(content["data"]["ordersTotal"]["gross"]["amount"])
+ assert Money(amount, "USD") == order_with_lines.total.gross
def test_order_by_token_query(api_client, order):