Skip to content

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

Fix draft order tax mismatch (#7226)

Iga Karbowiakbad0abad393b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8ed20b2264..19163faba9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -67,6 +67,8 @@ All notable, unreleased changes to this project will be documented in this file.
- Change exported file name - #7218 by @IKarbowiak
- Performance upgrade on `OrderLine` type with `thumbnail` field - #7224 by @tomaszszymanski129
- Use GraphQL IDs instead of database IDs in export - #7240 by @IKarbowiak
+- Fix draft order tax mismatch - #7226 by @IKarbowiak
+ - Introduce `calculate_order_line_total` plugin method
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.py
index f59f677e82..596f96e4e8 100644
--- a/saleor/checkout/base_calculations.py
+++ b/saleor/checkout/base_calculations.py
@@ -17,6 +17,7 @@ from .fetch import CheckoutLineInfo
if TYPE_CHECKING:
from ..channel.models import Channel
from ..checkout.fetch import CheckoutInfo
+ from ..order.models import OrderLine
def base_checkout_shipping_price(
@@ -74,6 +75,11 @@ def base_checkout_line_total(
return TaxedMoney(net=price, gross=price)
+def base_order_line_total(order_line: "OrderLine"):
+ unit_price = order_line.unit_price * order_line.quantity
+ return quantize_price(unit_price, unit_price.currency)
+
+
def base_tax_rate(price: TaxedMoney):
tax_rate = Decimal("0.0")
# The condition will return False when unit_price.gross is 0.0
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index c66fa9e31b..1a89ede8ed 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -101,9 +101,11 @@ def test_add_variant_to_draft_order_adds_line_for_new_variant_with_tax(
variant = product.variants.get()
lines_before = order.lines.count()
settings.LANGUAGE_CODE = "fr"
- price = TaxedMoney(net=Money(8, "USD"), gross=Money(10, "USD"))
+ unit_price = TaxedMoney(net=Money(8, "USD"), gross=Money(10, "USD"))
+ total_price = TaxedMoney(net=Money("30.34", "USD"), gross=Money("36.49", "USD"))
manager = Mock(
- calculate_order_line_unit=Mock(return_value=price),
+ calculate_order_line_unit=Mock(return_value=unit_price),
+ calculate_order_line_total=Mock(return_value=total_price),
get_order_line_tax_rate=Mock(return_value=0.25),
)
@@ -113,8 +115,8 @@ def test_add_variant_to_draft_order_adds_line_for_new_variant_with_tax(
assert order.lines.count() == lines_before + 1
assert line.product_sku == variant.sku
assert line.quantity == 1
- assert line.unit_price == price
- assert line.total_price == price
+ assert line.unit_price == unit_price
+ assert line.total_price == total_price
assert line.translated_product_name == str(variant.product.translated)
assert line.variant_name == str(variant)
assert line.product_name == str(variant.product)
diff --git a/saleor/order/tests/test_order_utils.py b/saleor/order/tests/test_order_utils.py
index bd36624144..78848402e3 100644
--- a/saleor/order/tests/test_order_utils.py
+++ b/saleor/order/tests/test_order_utils.py
@@ -1,12 +1,18 @@
+from decimal import Decimal
+from unittest.mock import Mock
+
import pytest
+from prices import Money, TaxedMoney
from .. import OrderLineData, OrderStatus
from ..events import OrderEvents
from ..models import Order, OrderEvent
from ..utils import (
+ add_variant_to_order,
change_order_line_quantity,
get_valid_shipping_methods_for_order,
match_orders_with_new_user,
+ update_taxes_for_order_lines,
)
@@ -173,3 +179,46 @@ def test_get_valid_shipping_methods_for_order_shipping_not_required(
# then
assert valid_shipping_methods is None
+
+
+def test_update_taxes_for_order_lines(order_with_lines):
+ # given
+ unit_price = TaxedMoney(net=Money("10.23", "USD"), gross=Money("15.80", "USD"))
+ total_price = TaxedMoney(net=Money("30.34", "USD"), gross=Money("36.49", "USD"))
+ tax_rate = Decimal("0.23")
+ manager = Mock(
+ calculate_order_line_unit=Mock(return_value=unit_price),
+ calculate_order_line_total=Mock(return_value=total_price),
+ get_order_line_tax_rate=Mock(return_value=tax_rate),
+ )
+
+ # when
+ update_taxes_for_order_lines(
+ order_with_lines.lines.all(), order_with_lines, manager, True
+ )
+
+ # then
+ for line in order_with_lines.lines.all():
+ assert line.unit_price == unit_price
+ assert line.total_price == total_price
+ assert line.tax_rate == tax_rate
+
+
+def test_add_variant_to_order(order, customer_user, variant):
+ # given
+ unit_price = TaxedMoney(net=Money("10.23", "USD"), gross=Money("15.80", "USD"))
+ total_price = TaxedMoney(net=Money("30.34", "USD"), gross=Money("36.49", "USD"))
+ tax_rate = Decimal("0.23")
+ manager = Mock(
+ calculate_order_line_unit=Mock(return_value=unit_price),
+ calculate_order_line_total=Mock(return_value=total_price),
+ get_order_line_tax_rate=Mock(return_value=tax_rate),
+ )
+
+ # when
+ line = add_variant_to_order(order, variant, 4, customer_user, manager)
+
+ # then
+ assert line.unit_price == unit_price
+ assert line.total_price == total_price
+ assert line.tax_rate == tax_rate
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 5a16e97852..d7d6284a78 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -193,12 +193,13 @@ def update_taxes_for_order_line(
line_price = line.unit_price.gross if tax_included else line.unit_price.net
line.unit_price = TaxedMoney(line_price, line_price)
- price = manager.calculate_order_line_unit(order, line, variant, product)
- line.unit_price = price
- line.total_price = line.unit_price * line.quantity
- if price.tax and price.net:
+ unit_price = manager.calculate_order_line_unit(order, line, variant, product)
+ total_price = manager.calculate_order_line_total(order, line, variant, product)
+ line.unit_price = unit_price
+ line.total_price = total_price
+ if unit_price.tax and unit_price.net:
line.tax_rate = manager.get_order_line_tax_rate(
- order, product, variant, None, price
+ order, product, variant, None, unit_price
)
@@ -342,12 +343,12 @@ def add_variant_to_order(
variant=variant,
)
unit_price = manager.calculate_order_line_unit(order, line, variant, product)
+ total_price = manager.calculate_order_line_total(order, line, variant, product)
line.unit_price = unit_price
- line.total_price = unit_price * quantity
+ line.total_price = total_price
line.tax_rate = manager.get_order_line_tax_rate(
order, product, variant, None, unit_price
)
- line.total_price = unit_price * quantity
line.save(
update_fields=[
"currency",
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index 7c5b5374bd..c216836e17 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -305,19 +305,50 @@ class AvataxPlugin(BasePlugin):
return base_total
taxes_data = get_checkout_tax_data(checkout_info, lines, discounts, self.config)
+ return self._calculate_line_total_price(
+ taxes_data, checkout_line_info.variant.sku, previous_value
+ )
+
+ def calculate_order_line_total(
+ self,
+ order: "Order",
+ order_line: "OrderLine",
+ variant: "ProductVariant",
+ product: "Product",
+ previous_value: TaxedMoney,
+ ) -> TaxedMoney:
+ if self._skip_plugin(previous_value):
+ return previous_value
+
+ if not product.charge_taxes:
+ return previous_value
+
+ if not _validate_order(order):
+ return zero_taxed_money(order.total.currency)
+
+ taxes_data = self._get_order_tax_data(order, previous_value)
+ return self._calculate_line_total_price(taxes_data, variant.sku, previous_value)
+
+ @staticmethod
+ def _calculate_line_total_price(
+ taxes_data: Dict[str, Any],
+ item_code: str,
+ base_value: TaxedMoney,
+ ):
if not taxes_data or "error" in taxes_data:
- return base_total
+ return base_value
currency = taxes_data.get("currencyCode")
for line in taxes_data.get("lines", []):
- if line.get("itemCode") == checkout_line_info.variant.sku:
+ if line.get("itemCode") == item_code:
tax = Decimal(line.get("tax", 0.0))
- line_net = Decimal(line["lineAmount"])
- line_gross = Money(amount=line_net + tax, currency=currency)
- line_net = Money(amount=line_net, currency=currency)
+ net = Decimal(line["lineAmount"])
+
+ line_gross = Money(amount=net + tax, currency=currency)
+ line_net = Money(amount=net, currency=currency)
return TaxedMoney(net=line_net, gross=line_gross)
- return base_total
+ return base_value
def calculate_checkout_line_unit_price(
self,
diff --git a/saleor/plugins/avatax/tests/cassettes/test_calculate_order_line_total.yaml b/saleor/plugins/avatax/tests/cassettes/test_calculate_order_line_total.yaml
new file mode 100644
index 0000000000..212788b20a
--- /dev/null
+++ b/saleor/plugins/avatax/tests/cassettes/test_calculate_order_line_total.yaml
@@ -0,0 +1,86 @@
+interactions:
+- request:
+ body: '{"createTransactionModel": {"companyCode": "DEFAULT", "type": "SalesInvoice",
+ "lines": [{"quantity": 3, "amount": "36.900", "taxCode": "O9999999", "taxIncluded":
+ true, "itemCode": "SKU_A", "description": "Test product"}, {"quantity": 1, "amount":
+ "10.000", "taxCode": "FR020100", "taxIncluded": true, "itemCode": "Shipping",
+ "description": null}], "code": "f52d00ae-f27a-46f8-86ab-2593ad4dba0d", "date":
+ "2021-04-23", "customerCode": 0, "addresses": {"shipFrom": {"line1": "T\u0119czowa
+ 7", "line2": "", "city": "WROC\u0141AW", "region": "", "country": "PL", "postalCode":
+ "53-601"}, "shipTo": {"line1": "O\u0142awska 10", "line2": "", "city": "WROC\u0141AW",
+ "region": "", "country": "PL", "postalCode": "53-105"}}, "commit": false, "currencyCode":
+ "USD", "email": "[email protected]"}}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate
+ Authorization:
+ - Basic Og==
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '786'
+ User-Agent:
+ - python-requests/2.25.1
+ method: POST
+ uri: https://rest.avatax.com/api/v2/transactions/createoradjust
+ response:
+ body:
+ string: !!binary |
+ H4sIAAAAAAAEA+1ZW2/bNhT+K4WeY0M327Kf5uUyBAvsIHZaoEMxMBJtq5Ulj6LSeEX++75DkTKt
+ OJeiWdEVC/ogHx4eHp7Lx4/sFydNnFHfdT3XD71+GEbRkRMXCXdGzqLnJ67LeGfhD1gn7C+iTtRn
+ Nx2/NwxYEiY3zE0c0l5vWL49hx0/9IeD3pGTMEkGfNf3Om7Y8QOolZLJqoR0xm45zZPbDSnNWMbL
+ 8/y2SGMO6Q2T8eq4doCMV0LwPN5qyfXsBEJ+F69YvuRXWOb4kEJclbJYc3FdsiWf1+vQvFymcntd
+ ctt+rfmW50khtNxVC9cDlojf8fVGTgo4DQXB4yKP0wx7GS1YVvIjJytiJtMi35kXfFMImebLiwdD
+ m0pgFyWfioSLndEFp/1aHpYUnw0XpW1XsrvpLRciTcz2JkVO4bMGxuuiyqUzcrvunvyKM9iqNyEL
+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-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Fri, 23 Apr 2021 06:24:52 GMT
+ Location:
+ - /api/v2/companies/242975/transactions/6001024164488
+ Server:
+ - Kestrel
+ ServerDuration:
+ - '00:00:00.0565630'
+ Transfer-Encoding:
+ - chunked
+ Vary:
+ - Accept-Encoding
+ status:
+ code: 201
+ message: Created
+version: 1
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 6a784de63f..8f5ef1f2fe 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -97,6 +97,97 @@ def test_calculate_checkout_line_total(
)
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test_calculate_order_line_total(
+ order_line,
+ address,
+ ship_to_pl_address,
+ shipping_zone,
+ site_settings,
+ monkeypatch,
+ plugin_configuration,
+):
+ plugin_configuration()
+ manager = get_plugins_manager()
+
+ site_settings.company_address = address
+ site_settings.save(update_fields=["company_address"])
+
+ order = order_line.order
+ order.shipping_address = ship_to_pl_address
+ order.shipping_method = shipping_zone.shipping_methods.get()
+ order.save(update_fields=["shipping_address", "shipping_method"])
+
+ variant = order_line.variant
+ product = variant.product
+ product.metadata = {}
+ product.charge_taxes = True
+ product.save()
+ product.product_type.save()
+
+ channel = order_line.order.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+
+ net = variant.get_price(product, [], channel, channel_listing)
+ unit_price = TaxedMoney(net=net, gross=net)
+ order_line.unit_price = unit_price
+ total_price = unit_price * order_line.quantity
+ order_line.total_price = total_price
+ order_line.save(update_fields=["unit_price", "total_price"])
+
+ total = manager.calculate_order_line_total(
+ order_line.order,
+ order_line,
+ variant,
+ product,
+ )
+ total = quantize_price(total, total.currency)
+ assert total == TaxedMoney(net=Money("30.00", "USD"), gross=Money("36.90", "USD"))
+
+
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test_calculate_order_line_total_order_not_valid(
+ order_line,
+ address,
+ site_settings,
+ monkeypatch,
+ plugin_configuration,
+):
+ plugin_configuration()
+ manager = get_plugins_manager()
+
+ site_settings.company_address = address
+ site_settings.save(update_fields=["company_address"])
+
+ variant = order_line.variant
+ product = variant.product
+ product.metadata = {}
+ product.charge_taxes = True
+ product.save()
+ product.product_type.save()
+
+ order = order_line.order
+ channel = order.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+
+ net = variant.get_price(product, [], channel, channel_listing)
+ unit_price = TaxedMoney(net=net, gross=net)
+ order_line.unit_price = unit_price
+ total_price = unit_price * order_line.quantity
+ order_line.total_price = total_price
+ order_line.save(update_fields=["unit_price", "total_price"])
+
+ total = manager.calculate_order_line_total(
+ order_line.order,
+ order_line,
+ variant,
+ product,
+ )
+ total = quantize_price(total, total.currency)
+ assert total == TaxedMoney(net=Money("0.00", "USD"), gross=Money("0.00", "USD"))
+
+
@pytest.mark.vcr
@pytest.mark.parametrize(
"with_discount, expected_net, expected_gross, voucher_amount, taxes_in_prices",
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index f85bea38c4..5b39dc6f32 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -212,6 +212,21 @@ class BasePlugin:
"""
return NotImplemented
+ def calculate_order_line_total(
+ self,
+ order: "Order",
+ order_line: "OrderLine",
+ variant: "ProductVariant",
+ product: "Product",
+ previous_value: TaxedMoney,
+ ) -> TaxedMoney:
+ """Calculate order line total.
+
+ Overwrite this method if you need to apply specific logic for the calculation
+ of a order line total. Return TaxedMoney.
+ """
+ return NotImplemented
+
def calculate_checkout_line_unit_price(
self,
checkout_info: "CheckoutInfo",
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 877e0a0eaf..76c8eac738 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -256,6 +256,26 @@ class PluginsManager(PaymentInterface):
checkout_info.checkout.currency,
)
+ def calculate_order_line_total(
+ self,
+ order: "Order",
+ order_line: "OrderLine",
+ variant: "ProductVariant",
+ product: "Product",
+ ):
+ default_value = base_calculations.base_order_line_total(order_line)
+ return quantize_price(
+ self.__run_method_on_plugins(
+ "calculate_order_line_total",
+ default_value,
+ order,
+ order_line,
+ variant,
+ product,
+ ),
+ order.currency,
+ )
+
def calculate_checkout_line_unit_price(
self,
total_line_price: TaxedMoney,
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index 8cb3b457b5..aa84c3ae08 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -96,6 +96,17 @@ class PluginSample(BasePlugin):
price = Money("1.0", currency=checkout_info.checkout.currency)
return TaxedMoney(price, price)
+ def calculate_order_line_total(
+ self,
+ order: "Order",
+ order_line: "OrderLine",
+ variant: "ProductVariant",
+ product: "Product",
+ previous_value: TaxedMoney,
+ ) -> TaxedMoney:
+ price = Money("1.0", currency=order.currency)
+ return TaxedMoney(price, price)
+
def calculate_checkout_line_unit_price(
self,
checkout_info: "CheckoutInfo",
diff --git a/saleor/plugins/tests/test_manager.py b/saleor/plugins/tests/test_manager.py
index eeb2e29e0a..6ec94da551 100644
--- a/saleor/plugins/tests/test_manager.py
+++ b/saleor/plugins/tests/test_manager.py
@@ -7,6 +7,7 @@ from django_countries.fields import Country
from prices import Money, TaxedMoney
from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines
+from ...core.prices import quantize_price
from ...core.taxes import TaxType
from ...payment.interface import PaymentGateway
from ...product.models import Product
@@ -135,6 +136,23 @@ def test_manager_calculates_checkout_line_total(
assert TaxedMoney(expected_total, expected_total) == taxed_total
+ "plugins",
+ [["saleor.plugins.tests.sample_plugins.PluginSample"], []],
+)
+def test_manager_calculates_order_line_total(order_line, plugins):
+ currency = order_line.order.currency
+ expected_total = (
+ TaxedMoney(Money("1.0", currency), Money("1.0", currency))
+ if plugins
+ else quantize_price(order_line.unit_price * order_line.quantity, currency)
+ )
+ taxed_total = PluginsManager(plugins=plugins).calculate_order_line_total(
+ order_line.order, order_line, order_line.variant, order_line.variant.product
+ )
+ assert expected_total == taxed_total
+
+
def test_manager_get_checkout_line_tax_rate_sample_plugin(
checkout_with_item, discount_info
):
diff --git a/saleor/plugins/vatlayer/plugin.py b/saleor/plugins/vatlayer/plugin.py
index c6eebbf467..0fd2e610e3 100644
--- a/saleor/plugins/vatlayer/plugin.py
+++ b/saleor/plugins/vatlayer/plugin.py
@@ -267,6 +267,27 @@ class VatlayerPlugin(BasePlugin):
else previous_value
)
+ def calculate_order_line_total(
+ self,
+ order: "Order",
+ order_line: "OrderLine",
+ variant: "ProductVariant",
+ product: "Product",
+ previous_value: TaxedMoney,
+ ) -> TaxedMoney:
+ unit_price = self.__calculate_order_line_unit(
+ order,
+ order_line,
+ variant,
+ product,
+ previous_value,
+ )
+ return (
+ unit_price * order_line.quantity
+ if unit_price is not None
+ else previous_value
+ )
+
def calculate_checkout_line_unit_price(
self,
checkout_info: "CheckoutInfo",
@@ -316,13 +337,26 @@ class VatlayerPlugin(BasePlugin):
product: "Product",
previous_value: TaxedMoney,
) -> TaxedMoney:
+ unit_price = self.__calculate_order_line_unit(
+ order, order_line, variant, product, previous_value
+ )
+ return unit_price if unit_price is not None else previous_value
+
+ def __calculate_order_line_unit(
+ self,
+ order: "Order",
+ order_line: "OrderLine",
+ variant: "ProductVariant",
+ product: "Product",
+ previous_value: TaxedMoney,
+ ):
if self._skip_plugin(previous_value):
- return previous_value
+ return
address = order.shipping_address or order.billing_address
country = address.country if address else None
if not variant:
- return previous_value
+ return
return self.__apply_taxes_to_product(product, order_line.unit_price, country)
def get_checkout_line_tax_rate(
diff --git a/saleor/plugins/vatlayer/tests/test_vatlayer.py b/saleor/plugins/vatlayer/tests/test_vatlayer.py
index 388198d3d8..f154a1519f 100644
--- a/saleor/plugins/vatlayer/tests/test_vatlayer.py
+++ b/saleor/plugins/vatlayer/tests/test_vatlayer.py
@@ -726,6 +726,45 @@ def test_calculate_checkout_line_total_with_excluded_country(
)
+@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
+def test_calculate_order_line_total(vatlayer, order_line, site_settings):
+ manager = get_plugins_manager()
+
+ assert order_line.quantity > 1
+
+ variant = order_line.variant
+ product = variant.product
+ manager.assign_tax_code_to_object_meta(variant.product, "standard")
+ product.save()
+
+ channel = order_line.order.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+ net = variant.get_price(product, [], channel, channel_listing)
+ unit_price = TaxedMoney(net=net, gross=net)
+ order_line.unit_price = unit_price
+ total_price = unit_price * order_line.quantity
+ order_line.total_price = total_price
+ order_line.save(update_fields=["unit_price", "total_price"])
+
+ line_price = manager.calculate_order_line_total(
+ order_line.order,
+ order_line,
+ variant,
+ product,
+ )
+
+ total_price_amount = total_price.net.amount
+ currency = total_price.currency
+ expected_price = quantize_price(
+ TaxedMoney(
+ net=Money(total_price_amount / Decimal("1.23"), currency),
+ gross=Money(total_price_amount, currency),
+ ),
+ currency,
+ )
+ assert line_price == expected_price
+
+
@override_settings(PLUGINS=["saleor.plugins.vatlayer.plugin.VatlayerPlugin"])
def test_calculate_checkout_line_unit_price(
vatlayer, checkout_with_item, shipping_zone, address, site_settings