Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Rework checkout total calculation (#11648)
Marcin Gębala7f5db27a203c
diff --git a/saleor/checkout/calculations.py b/saleor/checkout/calculations.pyindex 4d7d1f57d8..ecbe7f92f2 100644--- a/saleor/checkout/calculations.py+++ b/saleor/checkout/calculations.py@@ -7,12 +7,11 @@ from prices import Money, TaxedMoneyfrom ..checkout import base_calculationsfrom ..core.prices import quantize_price-from ..core.taxes import TaxData, zero_taxed_money+from ..core.taxes import TaxData, zero_money, zero_taxed_moneyfrom ..discount import DiscountInfofrom ..tax import TaxCalculationStrategyfrom ..tax.calculations.checkout import update_checkout_prices_with_flat_ratesfrom ..tax.utils import (- calculate_tax_rate,get_charge_taxes_for_checkout,get_tax_calculation_strategy_for_checkout,normalize_tax_rate_for_db,@@ -479,6 +478,7 @@ def _get_checkout_base_prices(discounts = []currency = checkout_info.checkout.currency+ subtotal = zero_money(currency)for line_info in lines:line = line_info.line@@ -490,30 +490,26 @@ def _get_checkout_base_prices(unit_price = base_calculations.apply_checkout_discount_on_checkout_line(checkout_info, lines, line_info, discounts, unit_price)- line_total_price = quantize_price(unit_price * quantity, unit_price.currency)+ line_total_price = quantize_price(unit_price * quantity, currency)+ subtotal += line_total_price++ line.total_price = TaxedMoney(net=line_total_price, gross=line_total_price)- line.total_price = quantize_price(- TaxedMoney(net=line_total_price, gross=line_total_price), currency- )# Set zero tax rate since net and gross are equal.line.tax_rate = Decimal("0.0")- shipping_price_default = base_calculations.base_checkout_delivery_price(+ # Calculate shipping price+ shipping_price = base_calculations.base_checkout_delivery_price(checkout_info, lines)checkout.shipping_price = quantize_price(- TaxedMoney(shipping_price_default, shipping_price_default), currency+ TaxedMoney(shipping_price, shipping_price), currency)- checkout.shipping_tax_rate = calculate_tax_rate(checkout.shipping_price)+ checkout.shipping_tax_rate = Decimal("0.0")- subtotal_default = sum(- [line_info.line.total_price for line_info in lines], zero_taxed_money(currency)- )- checkout.subtotal = subtotal_default+ # Set subtotal+ checkout.subtotal = TaxedMoney(net=subtotal, gross=subtotal)- total_default = base_calculations.base_checkout_total(- checkout_info, discounts, lines- )- checkout.total = quantize_price(- TaxedMoney(net=total_default, gross=total_default), currency- )+ # Calculate checkout total+ total = subtotal + shipping_price+ checkout.total = quantize_price(TaxedMoney(net=total, gross=total), currency)diff --git a/saleor/checkout/tests/test_calculations.py b/saleor/checkout/tests/test_calculations.pyindex f750da6de8..932c0e34c1 100644--- a/saleor/checkout/tests/test_calculations.py+++ b/saleor/checkout/tests/test_calculations.py@@ -270,6 +270,23 @@ def test_get_checkout_base_prices_no_charge_taxes_with_voucher(# givencheckout = checkout_with_itemchannel = checkout.channel++ line = checkout.lines.first()+ variant = line.variant+ channel_listing = variant.channel_listings.get(channel=channel.pk)+ channel_listing.price_amount = Decimal("9.60")+ channel_listing.save()++ line.quantity = 7+ line.save()++ voucher_value = Decimal("3.00")+ voucher_channel_listing = voucher_percentage.channel_listings.get(+ channel=channel.pk+ )+ voucher_channel_listing.discount_value = voucher_value+ voucher_channel_listing.save()+lines, _ = fetch_checkout_lines(checkout)line_info = list(lines)[0]variant = line_info.variant@@ -287,9 +304,6 @@ def test_get_checkout_base_prices_no_charge_taxes_with_voucher(voucher_percentage.code,[],)- voucher_value = voucher_percentage.channel_listings.get(- channel=channel.pk- ).discount_valuecheckout.refresh_from_db()checkout_info = fetch_checkout_info(checkout, lines, [], manager)@@ -310,13 +324,16 @@ def test_get_checkout_base_prices_no_charge_taxes_with_voucher(line = checkout.lines.all()[0]# then- expected_price = quantize_price(+ assert line.tax_rate == Decimal("0.0")++ expected_unit_price = quantize_price((100 - voucher_value) / 100 * product_price, checkout.currency)- expected_price = TaxedMoney(net=expected_price, gross=expected_price)- assert line.tax_rate == Decimal("0.0")- line_unit_price = line.total_price / line.quantity- assert line_unit_price == expected_price+ line_unit_price = quantize_price(+ line.total_price / line.quantity, checkout.currency+ )+ assert line_unit_price.gross == expected_unit_price+ assert line.total_price == checkout.total@freeze_time("2020-12-12 12:00:00")