Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor: unify how undiscounted price for checkout and order are handled (#14780)
Jakubd7f372e00776
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8c1347c743..04304d35cd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -18,6 +18,8 @@ All notable, unreleased changes to this project will be documented in this file.### Other changes- Add missing descriptions to order module - #14845 by @DevilsAutumn+- Unify how undiscounted prices are handled in orders and checkouts - #14780 by @jakubkuc+# 3.18.0### Highlightsdiff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex f7530a25dc..94e7b92f4b 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -56,6 +56,7 @@ from ..payment import PaymentError, TransactionKind, gatewayfrom ..payment.models import Payment, Transactionfrom ..payment.utils import fetch_customer_id, store_customer_idfrom ..product.models import ProductTranslation, ProductVariantTranslation+from ..tax.calculations import get_taxed_undiscounted_pricefrom ..tax.utils import (get_shipping_tax_class_kwargs_for_order,get_tax_class_kwargs_for_order_line,@@ -88,7 +89,6 @@ from .models import Checkoutfrom .utils import (get_checkout_metadata,get_or_create_checkout_metadata,- get_taxed_undiscounted_price,get_voucher_for_checkout_info,)diff --git a/saleor/checkout/tests/test_utils.py b/saleor/checkout/tests/test_utils.pyindex 7667303e0c..673431b6ab 100644--- a/saleor/checkout/tests/test_utils.py+++ b/saleor/checkout/tests/test_utils.py@@ -3,7 +3,7 @@ from decimal import Decimalimport pytestfrom prices import Money, TaxedMoney-from ..utils import get_taxed_undiscounted_price+from ...tax.calculations import get_taxed_undiscounted_priceBASE = Money("35.00", "USD")diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex f123d8302f..8af0a461d2 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -7,11 +7,10 @@ import graphenefrom django.core.exceptions import ValidationErrorfrom django.db.models import prefetch_related_objectsfrom django.utils import timezone-from prices import Money, TaxedMoney+from prices import Moneyfrom ..account.models import Userfrom ..core.exceptions import ProductNotPublished-from ..core.prices import quantize_pricefrom ..core.taxes import zero_taxed_moneyfrom ..core.utils.promo_code import (InvalidPromoCode,@@ -37,7 +36,6 @@ from ..product import models as product_modelsfrom ..shipping.interface import ShippingMethodDatafrom ..shipping.models import ShippingMethod, ShippingMethodChannelListingfrom ..shipping.utils import convert_to_shipping_method_data-from ..tax.calculations import calculate_flat_rate_taxfrom ..warehouse.availability import check_stock_and_preorder_quantityfrom ..warehouse.models import Warehousefrom ..warehouse.reservations import reserve_stocks_and_preorders@@ -962,27 +960,3 @@ def get_checkout_metadata(checkout: "Checkout"):return checkout.metadata_storageelse:return CheckoutMetadata(checkout=checkout)---def get_taxed_undiscounted_price(- undiscounted_base_price: "Money",- price: "TaxedMoney",- tax_rate: "Decimal",- prices_entered_with_tax: bool,-):- """Apply taxes to undiscounted base price.-- This function also prevents rounding difference between prices from tax-app and- local calculations based on tax_rate that might occur in orders without discounts.- """- price_to_compare = price.gross if prices_entered_with_tax else price.net- if undiscounted_base_price == price_to_compare:- return price- return quantize_price(- calculate_flat_rate_tax(- money=undiscounted_base_price,- tax_rate=tax_rate * 100,- prices_entered_with_tax=prices_entered_with_tax,- ),- undiscounted_base_price.currency,- )diff --git a/saleor/order/calculations.py b/saleor/order/calculations.pyindex ebe93daed9..4b34b2e381 100644--- a/saleor/order/calculations.py+++ b/saleor/order/calculations.py@@ -13,7 +13,7 @@ from ..order import base_calculationsfrom ..payment.model_helpers import get_subtotalfrom ..plugins.manager import PluginsManagerfrom ..tax import TaxCalculationStrategy-from ..tax.calculations import calculate_flat_rate_tax+from ..tax.calculations import get_taxed_undiscounted_pricefrom ..tax.calculations.order import update_order_prices_with_flat_ratesfrom ..tax.utils import (calculate_tax_rate,@@ -22,7 +22,11 @@ from ..tax.utils import (normalize_tax_rate_for_db,)from . import ORDER_EDITABLE_STATUS-from .base_calculations import base_order_subtotal, base_order_total+from .base_calculations import (+ base_order_line_total,+ base_order_subtotal,+ base_order_total,+)from .interface import OrderTaxedPricesDatafrom .models import Order, OrderLine@@ -62,10 +66,18 @@ def _recalculate_order_prices(order, product, variant, None, line_unit.undiscounted_price)line.undiscounted_unit_price = get_undiscounted_price(- line_unit, line.tax_rate, prices_entered_with_tax, line.currency+ line_unit,+ line.undiscounted_base_unit_price,+ line.tax_rate,+ prices_entered_with_tax,)+line.undiscounted_total_price = get_undiscounted_price(- line_total, line.tax_rate, prices_entered_with_tax, line.currency+ line_total,+ # base_order_line_total returns equal gross and net+ base_order_line_total(line).undiscounted_price.net,+ line.tax_rate,+ prices_entered_with_tax,)except TaxError:pass@@ -84,23 +96,22 @@ def _recalculate_order_prices(def get_undiscounted_price(- line_price: OrderTaxedPricesData, tax_rate, prices_entered_with_tax, currency+ line_price: OrderTaxedPricesData,+ line_base_price: Money,+ tax_rate,+ prices_entered_with_tax,):if (tax_rate > 0and line_price.undiscounted_price.net == line_price.undiscounted_price.gross):- return quantize_price(- calculate_flat_rate_tax(- # It is calculated when net and gross are equal- money=line_price.undiscounted_price.net,- tax_rate=tax_rate,- prices_entered_with_tax=prices_entered_with_tax,- ),- currency,+ get_taxed_undiscounted_price(+ line_base_price,+ line_price.undiscounted_price,+ tax_rate,+ prices_entered_with_tax,)- else:- return line_price.undiscounted_price+ return line_price.undiscounted_pricedef _update_order_discounts_and_base_undiscounted_total(diff --git a/saleor/tax/calculations/__init__.py b/saleor/tax/calculations/__init__.pyindex a9ce7f6214..44fcc999f0 100644--- a/saleor/tax/calculations/__init__.py+++ b/saleor/tax/calculations/__init__.py@@ -2,6 +2,8 @@ from decimal import Decimalfrom prices import Money, TaxedMoney+from ...core.prices import quantize_price+def calculate_flat_rate_tax(money: "Money", tax_rate: "Decimal", prices_entered_with_tax: bool@@ -18,3 +20,27 @@ def calculate_flat_rate_tax(return TaxedMoney(net=Money(net_amount, currency), gross=Money(gross_amount, currency))+++def get_taxed_undiscounted_price(+ undiscounted_base_price: "Money",+ price: "TaxedMoney",+ tax_rate: "Decimal",+ prices_entered_with_tax: bool,+):+ """Apply taxes to undiscounted base price.++ This function also prevents rounding difference between prices from tax-app and+ local calculations based on tax_rate that might occur in orders without discounts.+ """+ price_to_compare = price.gross if prices_entered_with_tax else price.net+ if undiscounted_base_price == price_to_compare:+ return price+ return quantize_price(+ calculate_flat_rate_tax(+ money=undiscounted_base_price,+ tax_rate=tax_rate * 100,+ prices_entered_with_tax=prices_entered_with_tax,+ ),+ undiscounted_base_price.currency,+ )