Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix avatax calculations
Tomasz Szymanskie243eb76d9ee
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex 829edf3563..dbb347a553 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -745,7 +745,7 @@ class OrderLineUpdate(ModelMutation):@classmethoddef save(cls, info, instance, cleaned_input):change_order_line_quantity(- info.context.user, instance, instance.old_quantity, instance.quantity+ info.context, instance, instance.old_quantity, instance.quantity)recalculate_order(instance.order)diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex 4b833bef3e..4727f22768 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -558,10 +558,12 @@ def test_order_weight_add_new_variant(order_with_lines, product):)-def test_order_weight_change_line_quantity(order_with_lines):+def test_order_weight_change_line_quantity(staff_user, order_with_lines):+ context_mock = Mock(user=staff_user)+ context_mock.plugins.order_line_updated.return_value = Noneline = order_with_lines.lines.first()new_quantity = line.quantity + 2- change_order_line_quantity(None, line, new_quantity, line.quantity)+ change_order_line_quantity(context_mock, line, new_quantity, line.quantity)order_with_lines.refresh_from_db()assert order_with_lines.weight == _calculate_order_weight_from_lines(order_with_lines@@ -849,21 +851,25 @@ def test_category_voucher_checkout_discount_raises_not_applicable(get_voucher_discount_for_order(order_with_lines)-def test_ordered_item_change_quantity(transactional_db, order_with_lines):+def test_ordered_item_change_quantity(staff_user, transactional_db, order_with_lines):+ context_mock = Mock(user=staff_user)+ context_mock.plugins.order_line_updated.return_value = Noneassert not order_with_lines.events.count()lines = order_with_lines.lines.all()- change_order_line_quantity(None, lines[1], lines[1].quantity, 0)- change_order_line_quantity(None, lines[0], lines[0].quantity, 0)+ change_order_line_quantity(context_mock, lines[1], lines[1].quantity, 0)+ change_order_line_quantity(context_mock, lines[0], lines[0].quantity, 0)assert order_with_lines.get_total_quantity() == 0def test_change_order_line_quantity_changes_total_prices(- transactional_db, order_with_lines+ staff_user, transactional_db, order_with_lines):+ context_mock = Mock(user=staff_user)+ context_mock.plugins.order_line_updated.return_value = Noneassert not order_with_lines.events.count()line = order_with_lines.lines.all()[0]new_quantity = line.quantity + 1- change_order_line_quantity(None, line, line.quantity, new_quantity)+ change_order_line_quantity(context_mock, line, line.quantity, new_quantity)line.refresh_from_db()assert line.total_price == line.unit_price * new_quantitydiff --git a/saleor/order/tests/test_order_utils.py b/saleor/order/tests/test_order_utils.pyindex f1642e1b3b..028ae20265 100644--- a/saleor/order/tests/test_order_utils.py+++ b/saleor/order/tests/test_order_utils.py@@ -1,3 +1,5 @@+from unittest.mock import Mock+import pytestfrom .. import OrderStatus@@ -24,6 +26,9 @@ def test_change_quantity_generates_proper_event(order_with_lines,staff_user,):+ context_mock = Mock(user=staff_user)+ context_mock.plugins.order_line_updated.return_value = None+assert not OrderEvent.objects.exists()order_with_lines.status = statusorder_with_lines.save(update_fields=["status"])@@ -31,7 +36,7 @@ def test_change_quantity_generates_proper_event(line = order_with_lines.lines.last()line.quantity = previous_quantity- change_order_line_quantity(staff_user, line, previous_quantity, new_quantity)+ change_order_line_quantity(context_mock, line, previous_quantity, new_quantity)if removed_count:expected_type = OrderEvents.REMOVED_PRODUCTSdiff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex ac70660045..cc554d0d93 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -293,18 +293,17 @@ def add_gift_card_to_order(order, gift_card, total_price_left):return total_price_left-def change_order_line_quantity(user, line, old_quantity, new_quantity):+def change_order_line_quantity(context, line, old_quantity, new_quantity):"""Change the quantity of ordered items in a order line."""if new_quantity:line.quantity = new_quantity- manager = get_plugins_manager()- unit_price = manager.order_line_updated(line.order, line)- if unit_price:- manager.get_order_line_tax_rate(- line.order, line.variant.product, None, unit_price- )- total_price_net_amount = line.quantity * line.unit_price_net_amount- total_price_gross_amount = line.quantity * line.unit_price_gross_amount+ line.save(update_fields=["quantity"])+ net = line.unit_price.net.amount+ unit_price = context.plugins.order_line_updated(line.order, line)+ if unit_price is not None:+ net = unit_price.net.amount+ total_price_net_amount = net * line.quantity+ total_price_gross_amount = line.unit_price.gross.amount * line.quantityline.total_price_net_amount = total_price_net_amount.quantize(Decimal("0.001"))line.total_price_gross_amount = total_price_gross_amount.quantize(Decimal("0.001")@@ -325,11 +324,13 @@ def change_order_line_quantity(user, line, old_quantity, new_quantity):# Create the added / removed products eventif quantity_diff > 0:events.order_removed_products_event(- order=line.order, user=user, order_lines=[(quantity_diff, line)]+ order=line.order, user=context.user, order_lines=[(quantity_diff, line)])elif quantity_diff < 0:events.order_added_products_event(- order=line.order, user=user, order_lines=[(quantity_diff * -1, line)]+ order=line.order,+ user=context.user,+ order_lines=[(quantity_diff * -1, line)],)