Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix propagating voucher value when tax calculation is disabled (#11632)
Marcin Gębala61c8db6f86b0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 982a248b62..78e33ff7f4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,7 @@ All notable, unreleased changes to this project will be documented in this file.- Add ability to set a custom Celery queue for async webhook - #11511 by @NyanKiyoshi- Remove `CUSTOMER_UPDATED` webhook trigger from address mutations - #11395 by @jakubkuc- Drop `Django.Auth` - #11305 by @fowczarek+- Propagate voucher discount between checkout lines when charge_taxes is disabled - #11632 by @maarcingebala# 3.9.0diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.pyindex b18cf7258a..4445e91846 100644--- a/saleor/checkout/base_calculations.py+++ b/saleor/checkout/base_calculations.py@@ -235,3 +235,85 @@ def base_checkout_subtotal(]return sum(line_totals, zero_money(currency))+++def apply_checkout_discount_on_checkout_line(+ checkout_info: "CheckoutInfo",+ lines: Iterable["CheckoutLineInfo"],+ checkout_line_info: "CheckoutLineInfo",+ discounts: Iterable["DiscountInfo"],+ line_unit_price: Money,+):+ """Calculate the checkout line price with discounts.++ Include the entire order voucher discount.+ The discount amount is calculated for every line proportionally to+ the rate of total line price to checkout total price.+ """+ voucher = checkout_info.voucher+ if (+ not voucher+ or voucher.apply_once_per_order+ or voucher.type in [VoucherType.SHIPPING, VoucherType.SPECIFIC_PRODUCT]+ ):+ return line_unit_price++ line_quantity = checkout_line_info.line.quantity+ total_discount_amount = checkout_info.checkout.discount_amount+ line_total_price = line_unit_price * line_quantity+ currency = checkout_info.checkout.currency++ lines = list(lines)++ # if the checkout has a single line, the whole discount amount will be applied+ # to this line+ if len(lines) == 1:+ return max(+ (line_total_price - Money(total_discount_amount, currency)) / line_quantity,+ zero_money(currency),+ )++ # if the checkout has more lines we need to propagate the discount amount+ # proportionally to total prices of items+ lines_total_prices = [+ calculate_base_line_unit_price(+ line_info,+ checkout_info.channel,+ discounts,+ ).amount+ * line_info.line.quantity+ for line_info in lines+ if line_info.line.id != checkout_line_info.line.id+ ]++ total_price = sum(lines_total_prices) + line_total_price.amount++ last_element = lines[-1].line.id == checkout_line_info.line.id+ if last_element:+ discount_amount = _calculate_discount_for_last_element(+ lines_total_prices, total_price, total_discount_amount, currency+ )+ else:+ discount_amount = line_total_price.amount / total_price * total_discount_amount+ return max(+ (line_total_price - Money(discount_amount, currency)) / line_quantity,+ zero_money(currency),+ )+++def _calculate_discount_for_last_element(+ lines_total_prices, total_price, total_discount_amount, currency+):+ """Calculate the discount for last element.++ If the given line is last on the list we should calculate the discount by difference+ between total discount amount and sum of discounts applied to rest of the lines,+ otherwise the sum of discounts won't be equal to the discount amount.+ """+ sum_of_discounts_other_elements = sum(+ [+ line_total_price / total_price * total_discount_amount+ for line_total_price in lines_total_prices+ ]+ )+ return total_discount_amount - sum_of_discounts_other_elementsdiff --git a/saleor/checkout/calculations.py b/saleor/checkout/calculations.pyindex 82be6a25d0..4d7d1f57d8 100644--- a/saleor/checkout/calculations.py+++ b/saleor/checkout/calculations.py@@ -482,23 +482,21 @@ def _get_checkout_base_prices(for line_info in lines:line = line_info.line+ quantity = line.quantity- total_price_default = base_calculations.calculate_base_line_total_price(- line_info,- checkout_info.channel,- discounts,+ unit_price = base_calculations.calculate_base_line_unit_price(+ line_info, checkout_info.channel, discounts)- line.total_price = quantize_price(- TaxedMoney(net=total_price_default, gross=total_price_default), currency+ 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)- unit_price_default = base_calculations.calculate_base_line_unit_price(- line_info, checkout_info.channel, discounts- )- unit_price = quantize_price(- TaxedMoney(net=unit_price_default, gross=unit_price_default), currency+ line.total_price = quantize_price(+ TaxedMoney(net=line_total_price, gross=line_total_price), currency)- line.tax_rate = calculate_tax_rate(unit_price)+ # 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(checkout_info, linesdiff --git a/saleor/checkout/tests/test_calculations.py b/saleor/checkout/tests/test_calculations.pyindex 42f729c464..f750da6de8 100644--- a/saleor/checkout/tests/test_calculations.py+++ b/saleor/checkout/tests/test_calculations.py@@ -7,6 +7,7 @@ from django.utils import timezonefrom freezegun import freeze_timefrom prices import Money, TaxedMoney+from ...checkout.utils import add_promo_code_to_checkoutfrom ...core.prices import quantize_pricefrom ...core.taxes import TaxData, TaxLineData, zero_taxed_moneyfrom ...plugins.manager import get_plugins_manager@@ -16,7 +17,11 @@ from ..base_calculations import (base_checkout_delivery_price,calculate_base_line_total_price,)-from ..calculations import _apply_tax_data, fetch_checkout_prices_if_expired+from ..calculations import (+ _apply_tax_data,+ _get_checkout_base_prices,+ fetch_checkout_prices_if_expired,+)from ..fetch import CheckoutLineInfo, fetch_checkout_info, fetch_checkout_lines@@ -259,6 +264,61 @@ def test_fetch_checkout_prices_if_expired_flat_rates_and_no_tax_calc_strategy(assert checkout.shipping_tax_rate == Decimal("0.2300")+def test_get_checkout_base_prices_no_charge_taxes_with_voucher(+ checkout_with_item, voucher_percentage+):+ # given+ checkout = checkout_with_item+ channel = checkout.channel+ lines, _ = fetch_checkout_lines(checkout)+ line_info = list(lines)[0]+ variant = line_info.variant+ product_price = variant.get_price(+ line_info.product, [], channel, line_info.channel_listing, []+ )++ manager = get_plugins_manager()+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)++ add_promo_code_to_checkout(+ manager,+ checkout_info,+ lines,+ voucher_percentage.code,+ [],+ )+ voucher_value = voucher_percentage.channel_listings.get(+ channel=channel.pk+ ).discount_value++ checkout.refresh_from_db()+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)++ # when+ _get_checkout_base_prices(checkout, checkout_info, lines, [])+ checkout.save()+ checkout.lines.bulk_update(+ [line_info.line for line_info in lines],+ [+ "total_price_net_amount",+ "total_price_gross_amount",+ "tax_rate",+ ],+ )+ checkout.refresh_from_db()++ line = checkout.lines.all()[0]++ # then+ expected_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++@freeze_time("2020-12-12 12:00:00")def test_fetch_checkout_prices_if_expired_webhooks_success(plugins_manager,diff --git a/saleor/tax/calculations/checkout.py b/saleor/tax/calculations/checkout.pyindex 0a0057b123..cd91f999d6 100644--- a/saleor/tax/calculations/checkout.py+++ b/saleor/tax/calculations/checkout.py@@ -1,12 +1,12 @@from decimal import Decimalfrom typing import TYPE_CHECKING, Iterable, Optional-from prices import Money, TaxedMoney+from prices import TaxedMoneyfrom ...checkout import base_calculationsfrom ...core.prices import quantize_price-from ...core.taxes import zero_money, zero_taxed_money-from ...discount import DiscountInfo, VoucherType+from ...core.taxes import zero_taxed_money+from ...discount import DiscountInfofrom ..models import TaxClassCountryRatefrom ..utils import get_tax_rate_for_tax_class, normalize_tax_rate_for_dbfrom . import calculate_flat_rate_tax@@ -134,7 +134,7 @@ def _calculate_checkout_line_unit_price(channel,discounts,)- unit_price = apply_checkout_discount_on_checkout_line(+ unit_price = base_calculations.apply_checkout_discount_on_checkout_line(checkout_info,lines,checkout_line_info,@@ -142,85 +142,3 @@ def _calculate_checkout_line_unit_price(unit_price,)return calculate_flat_rate_tax(unit_price, tax_rate, prices_entered_with_tax)---def apply_checkout_discount_on_checkout_line(- checkout_info: "CheckoutInfo",- lines: Iterable["CheckoutLineInfo"],- checkout_line_info: "CheckoutLineInfo",- discounts: Iterable["DiscountInfo"],- line_price: Money,-):- """Calculate the checkout line price with discounts.-- Include the entire order voucher discount.- The discount amount is calculated for every line proportionally to- the rate of total line price to checkout total price.- """- voucher = checkout_info.voucher- if (- not voucher- or voucher.apply_once_per_order- or voucher.type in [VoucherType.SHIPPING, VoucherType.SPECIFIC_PRODUCT]- ):- return line_price-- line_quantity = checkout_line_info.line.quantity- total_discount_amount = checkout_info.checkout.discount_amount- line_total_price = line_price * line_quantity- currency = checkout_info.checkout.currency-- lines = list(lines)-- # if the checkout has a single line, the whole discount amount will be applied- # to this line- if len(lines) == 1:- return max(- (line_total_price - Money(total_discount_amount, currency)) / line_quantity,- zero_money(currency),- )-- # if the checkout has more lines we need to propagate the discount amount- # proportionally to total prices of items- lines_total_prices = [- base_calculations.calculate_base_line_unit_price(- line_info,- checkout_info.channel,- discounts,- ).amount- * line_info.line.quantity- for line_info in lines- if line_info.line.id != checkout_line_info.line.id- ]-- total_price = sum(lines_total_prices) + line_total_price.amount-- last_element = lines[-1].line.id == checkout_line_info.line.id- if last_element:- discount_amount = _calculate_discount_for_last_element(- lines_total_prices, total_price, total_discount_amount, currency- )- else:- discount_amount = line_total_price.amount / total_price * total_discount_amount- return max(- (line_total_price - Money(discount_amount, currency)) / line_quantity,- zero_money(currency),- )---def _calculate_discount_for_last_element(- lines_total_prices, total_price, total_discount_amount, currency-):- """Calculate the discount for last element.-- If the given line is last on the list we should calculate the discount by difference- between total discount amount and sum of discounts applied to rest of the lines,- otherwise the sum of discounts won't be equal to the discount amount.- """- sum_of_discounts_other_elements = sum(- [- line_total_price / total_price * total_discount_amount- for line_total_price in lines_total_prices- ]- )- return total_discount_amount - sum_of_discounts_other_elements