Skip to content

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

prevent tax rate calculation from raising zero division error (#8961)

Jakubf4b6aa470c59
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.py
index 82b066560a..924d6a8e06 100644
--- a/saleor/checkout/base_calculations.py
+++ b/saleor/checkout/base_calculations.py
@@ -223,8 +223,8 @@ def base_order_line_total(order_line: "OrderLine") -> OrderTaxedPricesData:
def base_tax_rate(price: TaxedMoney):
tax_rate = Decimal("0.0")
- # The condition will return False when unit_price.gross is 0.0
- if not isinstance(price, Decimal) and price.gross:
+ # The condition will return False when unit_price.gross or unit_price.net is 0.0
+ if not isinstance(price, Decimal) and all((price.gross, price.net)):
tax_rate = price.tax / price.net
return tax_rate
diff --git a/saleor/checkout/tests/test_base_calculations.py b/saleor/checkout/tests/test_base_calculations.py
index e34ca3a696..8eda1fa4c8 100644
--- a/saleor/checkout/tests/test_base_calculations.py
+++ b/saleor/checkout/tests/test_base_calculations.py
@@ -1,10 +1,11 @@
from decimal import Decimal
-from prices import Money
+from prices import Money, TaxedMoney
from ...discount import DiscountValueType, VoucherType
from ...discount.utils import get_product_discount_on_sale
from ..base_calculations import (
+ base_tax_rate,
calculate_base_line_total_price,
calculate_base_line_unit_price,
)
@@ -588,3 +589,13 @@ def test_calculate_base_line_total_price_with_variant_on_sale_and_voucher_applie
prices_data.price_with_discounts
== (expected_unit_price * quantity) - voucher_amount
)
+
+
+def test_base_tax_rate_net_price_zero():
+ price = TaxedMoney(net=Money(0, "USD"), gross=Money(3, "USD"))
+ assert base_tax_rate(price) == Decimal("0.0")
+
+
+def test_base_tax_rate_gross_price_zero():
+ price = TaxedMoney(net=Money(3, "USD"), gross=Money(0, "USD"))
+ assert base_tax_rate(price) == Decimal("0.0")