Skip to content

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

Fix order-level discounts handling when using tax app for tax calculation. (#16696)

Piotr Zabieglik9798596564fe
diff --git a/CHANGELOG.md b/CHANGELOG.md
index edb7127674..b58321bb56 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,3 +28,4 @@ All notable, unreleased changes to this project will be documented in this file.
- Fixed a crash when the Decimal scalar is passed a non-normal value - #16520 by @patrys
- Fixed a bug when saving webhook payload to Azure Storage - #16585 by @delemeator
- Added validation for tax data received from tax app - #16720 by @zedzior
+- Fixed order-level discounts handling when using tax app for tax calculation - #16696 by @zedzior
diff --git a/saleor/discount/utils/manual_discount.py b/saleor/discount/utils/manual_discount.py
index 844e42c889..9259a421cf 100644
--- a/saleor/discount/utils/manual_discount.py
+++ b/saleor/discount/utils/manual_discount.py
@@ -36,33 +36,17 @@ def split_manual_discount(
"""Discounts sent to tax app must be split into subtotal and shipping portion."""
currency = subtotal.currency
subtotal_discount, shipping_discount = zero_money(currency), zero_money(currency)
- if discount.value_type == DiscountValueType.PERCENTAGE:
- discounted_subtotal = apply_discount_to_value(
+ total = subtotal + shipping_price
+ if total.amount > 0:
+ discounted_total = apply_discount_to_value(
value=discount.value,
value_type=discount.value_type,
currency=currency,
- price_to_discount=subtotal,
+ price_to_discount=total,
)
- subtotal_discount = subtotal - discounted_subtotal
- discounted_shipping_price = apply_discount_to_value(
- value=discount.value,
- value_type=discount.value_type,
- currency=currency,
- price_to_discount=shipping_price,
- )
- shipping_discount = shipping_price - discounted_shipping_price
- elif discount.value_type == DiscountValueType.FIXED:
- total = subtotal + shipping_price
- if total.amount > 0:
- discounted_total = apply_discount_to_value(
- value=discount.value,
- value_type=discount.value_type,
- currency=currency,
- price_to_discount=total,
- )
- total_discount = total - discounted_total
- subtotal_discount = subtotal / total * total_discount
- shipping_discount = total_discount - subtotal_discount
+ total_discount = total - discounted_total
+ subtotal_discount = subtotal / total * total_discount
+ shipping_discount = total_discount - subtotal_discount
return quantize_price(subtotal_discount, currency), quantize_price(
shipping_discount, currency
diff --git a/saleor/discount/utils/voucher.py b/saleor/discount/utils/voucher.py
index 3cce524684..504001c6c8 100644
--- a/saleor/discount/utils/voucher.py
+++ b/saleor/discount/utils/voucher.py
@@ -322,12 +322,15 @@ def create_or_update_discount_object_from_order_level_voucher(
order, database_connection_name
):
"""Create or update discount object for ENTIRE_ORDER and SHIPPING voucher."""
- if not order.voucher_id:
+ voucher = order.voucher
+ if not order.voucher_id or (
+ is_order_level_voucher(voucher)
+ and order.discounts.filter(type=DiscountType.MANUAL)
+ ):
with allow_writer():
order.discounts.filter(type=DiscountType.VOUCHER).delete()
return
- voucher = order.voucher
if not is_order_level_voucher(voucher) and not is_shipping_voucher(voucher):
return
@@ -339,13 +342,22 @@ def create_or_update_discount_object_from_order_level_voucher(
if not voucher_channel_listing:
return
- price_to_discount = zero_money(order.currency)
+ discount_amount = zero_money(order.currency)
if is_order_level_voucher(voucher):
- price_to_discount = order.subtotal.net
+ discount_amount = voucher.get_discount_amount_for(
+ order.subtotal.net, order.channel
+ )
if is_shipping_voucher(voucher):
- price_to_discount = order.shipping_price_net
+ discount_amount = voucher.get_discount_amount_for(
+ order.undiscounted_base_shipping_price, order.channel
+ )
+ # Shipping voucher is tricky: it is associated with an order, but it
+ # decreases base price, similar to line level discounts
+ order.base_shipping_price = max(
+ order.undiscounted_base_shipping_price - discount_amount,
+ zero_money(order.currency),
+ )
- discount_amount = voucher.get_discount_amount_for(price_to_discount, order.channel)
discount_reason = f"Voucher code: {order.voucher_code}"
discount_name = voucher.name or ""
diff --git a/saleor/graphql/core/types/taxes.py b/saleor/graphql/core/types/taxes.py
index 17831b0bd7..b517d7abad 100644
--- a/saleor/graphql/core/types/taxes.py
+++ b/saleor/graphql/core/types/taxes.py
@@ -12,6 +12,7 @@ from ....discount import DiscountType
from ....discount.utils.checkout import has_checkout_order_promotion
from ....discount.utils.manual_discount import split_manual_discount
from ....discount.utils.voucher import is_order_level_voucher
+from ....order.base_calculations import base_order_subtotal
from ....order.models import Order, OrderLine
from ....order.utils import get_order_country
from ....tax.utils import get_charge_taxes
@@ -404,17 +405,21 @@ class TaxableObject(BaseObjectType):
.then(calculate_checkout_discounts)
)
- def map_discounts(discounts):
+ discounts = OrderDiscountsByOrderIDLoader(info.context).load(root.id)
+ order_lines = OrderLinesByOrderIdLoader(info.context).load(root.id)
+
+ def calculate_order_discounts(results):
# Only order level discounts, like entire order vouchers,
# order promotions and manual discounts should be taken into account.
# Manual discount needs to be split into subtotal and shipping portions.
+ (discounts, order_lines) = results
taxable_discounts = []
currency = root.currency
for discount in discounts:
shipping_discount = Money(0, currency)
subtotal_discount = Money(0, currency)
if discount.type == DiscountType.MANUAL:
- subtotal = root.subtotal.net
+ subtotal = base_order_subtotal(root, order_lines)
shipping = root.base_shipping_price
subtotal_discount, shipping_discount = split_manual_discount(
discount, subtotal, shipping
@@ -444,11 +449,7 @@ class TaxableObject(BaseObjectType):
return taxable_discounts
- return (
- OrderDiscountsByOrderIDLoader(info.context)
- .load(root.id)
- .then(map_discounts)
- )
+ return Promise.all([discounts, order_lines]).then(calculate_order_discounts)
@staticmethod
def resolve_lines(root: Union[Checkout, Order], info: ResolveInfo):
diff --git a/saleor/order/base_calculations.py b/saleor/order/base_calculations.py
index 04f293a414..8a5c720604 100644
--- a/saleor/order/base_calculations.py
+++ b/saleor/order/base_calculations.py
@@ -7,10 +7,10 @@ from prices import Money, TaxedMoney
from ..core.db.connection import allow_writer
from ..core.prices import quantize_price
from ..core.taxes import zero_money
-from ..discount import DiscountType, DiscountValueType, VoucherType
+from ..discount import DiscountType, DiscountValueType
from ..discount.models import OrderDiscount
from ..discount.utils.manual_discount import apply_discount_to_value
-from ..discount.utils.voucher import is_order_level_voucher
+from ..discount.utils.voucher import is_order_level_voucher, is_shipping_voucher
from ..shipping.models import ShippingMethodChannelListing
from .interface import OrderTaxedPricesData
@@ -95,11 +95,10 @@ def propagate_order_discount_on_order_prices(
"""
base_subtotal = base_order_subtotal(order, lines)
subtotal = base_subtotal
- shipping_price = order.undiscounted_base_shipping_price
+ shipping_price = order.base_shipping_price
currency = order.currency
order_discounts_to_update = []
- shipping_voucher_discount = None
for order_discount in order.discounts.all():
subtotal_before_discount = subtotal
shipping_price_before_discount = shipping_price
@@ -112,8 +111,12 @@ def propagate_order_discount_on_order_prices(
currency=currency,
price_to_discount=subtotal,
)
- elif voucher and voucher.type == VoucherType.SHIPPING:
- shipping_voucher_discount = order_discount
+ # Shipping voucher is tricky: it is associated with an order, but it
+ # decreases base price, similar to line level discounts, so we should
+ # exclude it
+ if is_shipping_voucher(order_discount.voucher):
+ continue
+
elif order_discount.type == DiscountType.ORDER_PROMOTION:
subtotal = apply_discount_to_value(
value=order_discount.value,
@@ -159,19 +162,6 @@ def propagate_order_discount_on_order_prices(
order_discount.amount = total_discount_amount
order_discounts_to_update.append(order_discount)
- # Apply shipping voucher discount
- if shipping_voucher_discount:
- shipping_price = apply_discount_to_value(
- value=shipping_voucher_discount.value,
- value_type=shipping_voucher_discount.value_type,
- currency=currency,
- price_to_discount=shipping_price,
- )
- discount_amount = shipping_price_before_discount - shipping_price
- if shipping_voucher_discount.amount != discount_amount:
- shipping_voucher_discount.amount = discount_amount
- order_discounts_to_update.append(shipping_voucher_discount)
-
if order_discounts_to_update:
OrderDiscount.objects.bulk_update(order_discounts_to_update, ["amount_value"])
@@ -326,7 +316,6 @@ def assign_order_prices(
database_connection_name: str = settings.DATABASE_CONNECTION_DEFAULT_NAME,
):
shipping_price = quantize_price(shipping_price, order.currency)
- order.base_shipping_price_amount = shipping_price.amount
order.shipping_price_net_amount = shipping_price.amount
order.shipping_price_gross_amount = shipping_price.amount
order.total_net_amount = subtotal.amount + shipping_price.amount
diff --git a/saleor/order/calculations.py b/saleor/order/calculations.py
index 318f8bb9cb..b442a70cc1 100644
--- a/saleor/order/calculations.py
+++ b/saleor/order/calculations.py
@@ -148,11 +148,17 @@ def _recalculate_prices(
order.tax_error = None
- # propagate the order level discount on the prices without taxes.
+ # Flat rates, plugin and tax exemption strategies require to propagate order-level
+ # discounts to order lines. Tax app does it itself.
+ using_tax_app = (
+ tax_calculation_strategy == TaxCalculationStrategy.TAX_APP
+ and tax_app_identifier
+ and not tax_app_identifier.startswith(PLUGIN_IDENTIFIER_PREFIX)
+ )
apply_order_discounts(
order,
lines,
- assign_prices=True,
+ assign_prices=not using_tax_app,
database_connection_name=database_connection_name,
)
if prices_entered_with_tax:
@@ -215,9 +221,10 @@ def _calculate_and_add_tax(
# If taxAppId is provided run tax plugin or Tax App. taxAppId can be
# configured with Avatax plugin identifier.
if not tax_app_identifier:
- # Get the taxes calculated with plugins.
+ # This is deprecated flow, kept to maintain backward compatibility.
+ # In Saleor 4.0 `tax_app_identifier` should be required and the flow should
+ # be dropped.
_recalculate_with_plugins(manager, order, lines, prices_entered_with_tax)
- # Get the taxes calculated with apps and apply to order.
tax_data = manager.get_taxes_for_order(order, tax_app_identifier)
if not tax_data:
log_address_if_validation_skipped_for_order(order, logger)
diff --git a/saleor/order/tests/test_apply_order_discount.py b/saleor/order/tests/test_apply_order_discount.py
index 52356fed5a..03d8315a60 100644
--- a/saleor/order/tests/test_apply_order_discount.py
+++ b/saleor/order/tests/test_apply_order_discount.py
@@ -192,7 +192,7 @@ def test_apply_order_discounts_shipping_voucher(
subtotal += line.base_unit_price * line.quantity
discount_amount = shipping_price.amount
- order_discount = order.discounts.create(
+ order.discounts.create(
type=DiscountType.VOUCHER,
value_type=DiscountValueType.FIXED,
value=discount_amount,
@@ -207,18 +207,8 @@ def test_apply_order_discounts_shipping_voucher(
discounted_subtotal, discounted_shipping_price = apply_order_discounts(order, lines)
# then
- assert discounted_shipping_price == zero_money(currency)
+ assert discounted_shipping_price == shipping_price
assert discounted_subtotal == subtotal
- assert order.subtotal_net_amount == discounted_subtotal.amount
- assert order.subtotal_gross_amount == discounted_subtotal.amount
- assert order.total_net == discounted_subtotal + discounted_shipping_price
- assert order.total_gross == discounted_subtotal + discounted_shipping_price
- assert order.shipping_price_net == discounted_shipping_price
- assert order.shipping_price_gross == discounted_shipping_price
- assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
- order_discount.refresh_from_db()
- assert order_discount.amount_value == shipping_price.amount
def test_apply_order_discounts_zero_discount(order_with_lines):
diff --git a/saleor/order/tests/test_fetch_order_prices.py b/saleor/order/tests/test_fetch_order_prices.py
index 239220fa95..5b1655852e 100644
--- a/saleor/order/tests/test_fetch_order_prices.py
+++ b/saleor/order/tests/test_fetch_order_prices.py
@@ -12,38 +12,28 @@ from ...discount.models import (
OrderLineDiscount,
PromotionRule,
)
-from ...tax import TaxCalculationStrategy
from ...tests.utils import round_down, round_up
from .. import OrderStatus, calculations
-def order_with_lines(order_with_lines):
- order_with_lines.status = OrderStatus.UNCONFIRMED
- return order_with_lines
-
-
@pytest.mark.parametrize("create_new_discounts", [True, False])
def test_fetch_order_prices_catalogue_discount_flat_rates(
order_with_lines_and_catalogue_promotion,
plugins_manager,
create_new_discounts,
+ tax_configuration_flat_rates,
):
# given
if create_new_discounts:
OrderLineDiscount.objects.all().delete()
order = order_with_lines_and_catalogue_promotion
+ order.status = OrderStatus.UNCONFIRMED
channel = order.channel
rule = PromotionRule.objects.get()
promotion_id = graphene.Node.to_global_id("Promotion", rule.promotion_id)
reward_value = rule.reward_value
- tc = channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
# when
@@ -154,22 +144,19 @@ def test_fetch_order_prices_order_discount_flat_rates(
order_with_lines_and_order_promotion,
plugins_manager,
create_new_discounts,
+ tax_configuration_flat_rates,
):
# given
if create_new_discounts:
OrderDiscount.objects.all().delete()
order = order_with_lines_and_order_promotion
+ order.status = OrderStatus.UNCONFIRMED
currency = order.currency
rule = PromotionRule.objects.get()
reward_amount = rule.reward_value
promotion_id = graphene.Node.to_global_id("Promotion", rule.promotion_id)
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
# when
@@ -289,20 +276,17 @@ def test_fetch_order_prices_gift_discount_flat_rates(
order_with_lines_and_gift_promotion,
plugins_manager,
create_new_discounts,
+ tax_configuration_flat_rates,
):
# given
if create_new_discounts:
OrderLineDiscount.objects.all().delete()
order = order_with_lines_and_gift_promotion
+ order.status = OrderStatus.UNCONFIRMED
rule = PromotionRule.objects.get()
promotion_id = graphene.Node.to_global_id("Promotion", rule.promotion_id)
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
# when
@@ -420,6 +404,7 @@ def test_fetch_order_prices_gift_discount_flat_rates(
def test_fetch_order_prices_catalogue_and_order_discounts_flat_rates(
draft_order_and_promotions,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order, rule_catalogue, rule_total, _ = draft_order_and_promotions
@@ -433,11 +418,6 @@ def test_fetch_order_prices_catalogue_and_order_discounts_flat_rates(
rule_total_reward = rule_total.reward_value
currency = order.currency
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
# when
@@ -577,6 +557,7 @@ def test_fetch_order_prices_catalogue_and_order_discounts_flat_rates(
def test_fetch_order_prices_catalogue_and_gift_discounts_flat_rates(
draft_order_and_promotions,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order, rule_catalogue, rule_total, rule_gift = draft_order_and_promotions
@@ -590,11 +571,6 @@ def test_fetch_order_prices_catalogue_and_gift_discounts_flat_rates(
rule_catalogue_reward = rule_catalogue.reward_value
currency = order.currency
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
# when
@@ -740,6 +716,7 @@ def test_fetch_order_prices_catalogue_and_gift_discounts_flat_rates(
def test_fetch_order_prices_catalogue_and_order_discounts_exceed_total_flat_rates(
draft_order_and_promotions,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order, rule_catalogue, rule_total, _ = draft_order_and_promotions
@@ -754,11 +731,6 @@ def test_fetch_order_prices_catalogue_and_order_discounts_exceed_total_flat_rate
rule_catalogue_reward = rule_catalogue.reward_value
currency = order.currency
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
# when
@@ -864,17 +836,14 @@ def test_fetch_order_prices_catalogue_and_order_discounts_exceed_total_flat_rate
def test_fetch_order_prices_manual_discount_and_order_discount_flat_rates(
order_with_lines_and_order_promotion,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines_and_order_promotion
+ order.status = OrderStatus.UNCONFIRMED
assert OrderDiscount.objects.exists()
currency = order.currency
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
discount_value = Decimal("50")
@@ -1003,17 +972,14 @@ def test_fetch_order_prices_manual_discount_and_order_discount_flat_rates(
def test_fetch_order_prices_manual_discount_and_gift_discount_flat_rates(
order_with_lines_and_gift_promotion,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines_and_gift_promotion
+ order.status = OrderStatus.UNCONFIRMED
assert OrderLineDiscount.objects.exists()
currency = order.currency
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
discount_value = Decimal("50")
@@ -1151,19 +1117,16 @@ def test_fetch_order_prices_manual_discount_and_gift_discount_flat_rates(
def test_fetch_order_prices_manual_discount_and_catalogue_discount_flat_rates(
order_with_lines_and_catalogue_promotion,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines_and_catalogue_promotion
+ order.status = OrderStatus.UNCONFIRMED
currency = order.currency
rule = PromotionRule.objects.get()
rule_catalogue_reward = rule.reward_value
promotion_id = graphene.Node.to_global_id("Promotion", rule.promotion_id)
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
tax_rate = Decimal("1.23")
manual_discount_value = Decimal("50")
@@ -1321,15 +1284,11 @@ def test_fetch_order_prices_manual_discount_and_catalogue_discount_flat_rates(
def test_fetch_order_prices_manual_line_discount_and_catalogue_discount_flat_rates(
order_with_lines_and_catalogue_promotion,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines_and_catalogue_promotion
-
- tc = order.channel.tax_configuration
- tc.country_exceptions.all().delete()
- tc.prices_entered_with_tax = False
- tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
- tc.save()
+ order.status = OrderStatus.UNCONFIRMED
line_1 = order.lines.get(quantity=3)
variant_1 = line_1.variant
@@ -1376,11 +1335,16 @@ def test_fetch_order_prices_manual_line_discount_and_catalogue_discount_flat_rat
def test_fetch_order_prices_voucher_specific_product_fixed(
- order_with_lines, voucher_specific_product_type, plugins_manager
+ order_with_lines,
+ voucher_specific_product_type,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
voucher = voucher_specific_product_type
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
unit_discount_amount = Decimal("5")
@@ -1411,19 +1375,19 @@ def test_fetch_order_prices_voucher_specific_product_fixed(
discount_amount = unit_discount_amount * discounted_line.quantity
assert order.base_shipping_price == shipping_price
assert order.shipping_price_net == shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount - discount_amount
- assert order.subtotal_gross_amount == subtotal.amount - discount_amount
+ assert order.subtotal_gross_amount == (subtotal.amount - discount_amount) * tax_rate
assert (
order.total_net_amount
== order.subtotal_net_amount + order.base_shipping_price_amount
)
assert (
order.total_gross_amount
- == order.subtotal_net_amount + order.base_shipping_price_amount
+ == (order.subtotal_net_amount + order.base_shipping_price_amount) * tax_rate
)
assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
assert (
discounted_line.base_unit_price_amount
@@ -1431,12 +1395,13 @@ def test_fetch_order_prices_voucher_specific_product_fixed(
)
assert (
discounted_line.total_price_gross_amount
- == discounted_line.base_unit_price_amount * discounted_line.quantity
+ == discounted_line.base_unit_price_amount * discounted_line.quantity * tax_rate
)
assert (
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert discounted_line.unit_discount_amount == unit_discount_amount
assert discounted_line.unit_discount_type == DiscountValueType.FIXED
@@ -1445,11 +1410,11 @@ def test_fetch_order_prices_voucher_specific_product_fixed(
assert line_1.base_unit_price_amount == line_1.undiscounted_base_unit_price_amount
assert (
line_1.total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -1465,11 +1430,16 @@ def test_fetch_order_prices_voucher_specific_product_fixed(
def test_fetch_order_prices_voucher_specific_product_discount_line_object_updated(
- order_with_lines, voucher_specific_product_type, plugins_manager
+ order_with_lines,
+ voucher_specific_product_type,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
voucher = voucher_specific_product_type
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
unit_discount_amount = Decimal("5")
@@ -1507,19 +1477,19 @@ def test_fetch_order_prices_voucher_specific_product_discount_line_object_update
discount_amount = unit_discount_amount * discounted_line.quantity
assert order.base_shipping_price == shipping_price
assert order.shipping_price_net == shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount - discount_amount
- assert order.subtotal_gross_amount == subtotal.amount - discount_amount
+ assert order.subtotal_gross_amount == (subtotal.amount - discount_amount) * tax_rate
assert (
order.total_net_amount
== order.subtotal_net_amount + order.base_shipping_price_amount
)
assert (
order.total_gross_amount
- == order.subtotal_net_amount + order.base_shipping_price_amount
+ == (order.subtotal_net_amount + order.base_shipping_price_amount) * tax_rate
)
assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
assert (
discounted_line.base_unit_price_amount
@@ -1527,12 +1497,13 @@ def test_fetch_order_prices_voucher_specific_product_discount_line_object_update
)
assert (
discounted_line.total_price_gross_amount
- == discounted_line.base_unit_price_amount * discounted_line.quantity
+ == discounted_line.base_unit_price_amount * discounted_line.quantity * tax_rate
)
assert (
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert discounted_line.unit_discount_amount == unit_discount_amount
assert discounted_line.unit_discount_type == DiscountValueType.FIXED
@@ -1541,11 +1512,11 @@ def test_fetch_order_prices_voucher_specific_product_discount_line_object_update
assert line_1.base_unit_price_amount == line_1.undiscounted_base_unit_price_amount
assert (
line_1.total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -1561,11 +1532,16 @@ def test_fetch_order_prices_voucher_specific_product_discount_line_object_update
def test_fetch_order_prices_voucher_specific_product_percentage(
- order_with_lines, voucher_specific_product_type, plugins_manager
+ order_with_lines,
+ voucher_specific_product_type,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
voucher = voucher_specific_product_type
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_value = Decimal("10")
@@ -1601,19 +1577,19 @@ def test_fetch_order_prices_voucher_specific_product_percentage(
)
assert order.base_shipping_price == shipping_price
assert order.shipping_price_net == shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount - discount_amount
- assert order.subtotal_gross_amount == subtotal.amount - discount_amount
+ assert order.subtotal_gross_amount == (subtotal.amount - discount_amount) * tax_rate
assert (
order.total_net_amount
== order.subtotal_net_amount + order.base_shipping_price_amount
)
assert (
order.total_gross_amount
- == order.subtotal_net_amount + order.base_shipping_price_amount
+ == (order.subtotal_net_amount + order.base_shipping_price_amount) * tax_rate
)
assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
unit_discount_amount = discount_amount / discounted_line.quantity
assert (
@@ -1622,12 +1598,13 @@ def test_fetch_order_prices_voucher_specific_product_percentage(
)
assert (
discounted_line.total_price_gross_amount
- == discounted_line.base_unit_price_amount * discounted_line.quantity
+ == discounted_line.base_unit_price_amount * discounted_line.quantity * tax_rate
)
assert (
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert discounted_line.unit_discount_amount == unit_discount_amount
assert discounted_line.unit_discount_type == DiscountValueType.FIXED
@@ -1636,11 +1613,11 @@ def test_fetch_order_prices_voucher_specific_product_percentage(
assert line_1.base_unit_price_amount == line_1.undiscounted_base_unit_price_amount
assert (
line_1.total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -1657,10 +1634,15 @@ def test_fetch_order_prices_voucher_specific_product_percentage(
def test_fetch_order_prices_voucher_apply_once_per_order_fixed(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_amount = Decimal("5")
@@ -1690,19 +1672,19 @@ def test_fetch_order_prices_voucher_apply_once_per_order_fixed(
discounted_line, line_1 = lines
assert order.base_shipping_price == shipping_price
assert order.shipping_price_net == shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount - discount_amount
- assert order.subtotal_gross_amount == subtotal.amount - discount_amount
+ assert order.subtotal_gross_amount == (subtotal.amount - discount_amount) * tax_rate
assert (
order.total_net_amount
== order.subtotal_net_amount + order.base_shipping_price_amount
)
assert (
order.total_gross_amount
- == order.subtotal_net_amount + order.base_shipping_price_amount
+ == (order.subtotal_net_amount + order.base_shipping_price_amount) * tax_rate
)
assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
unit_discount_amount = discount_amount / discounted_line.quantity
assert (
@@ -1711,12 +1693,13 @@ def test_fetch_order_prices_voucher_apply_once_per_order_fixed(
)
assert (
discounted_line.total_price_gross_amount
- == discounted_line.base_unit_price_amount * discounted_line.quantity
+ == discounted_line.base_unit_price_amount * discounted_line.quantity * tax_rate
)
assert (
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert discounted_line.unit_discount_amount == unit_discount_amount
assert discounted_line.unit_discount_type == DiscountValueType.FIXED
@@ -1725,11 +1708,11 @@ def test_fetch_order_prices_voucher_apply_once_per_order_fixed(
assert line_1.base_unit_price_amount == line_1.undiscounted_base_unit_price_amount
assert (
line_1.total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -1745,10 +1728,15 @@ def test_fetch_order_prices_voucher_apply_once_per_order_fixed(
def test_fetch_order_prices_voucher_apply_once_per_order_percentage(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_value = Decimal("10")
@@ -1781,19 +1769,19 @@ def test_fetch_order_prices_voucher_apply_once_per_order_percentage(
)
assert order.base_shipping_price == shipping_price
assert order.shipping_price_net == shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount - discount_amount
- assert order.subtotal_gross_amount == subtotal.amount - discount_amount
+ assert order.subtotal_gross_amount == (subtotal.amount - discount_amount) * tax_rate
assert (
order.total_net_amount
== order.subtotal_net_amount + order.base_shipping_price_amount
)
assert (
order.total_gross_amount
- == order.subtotal_net_amount + order.base_shipping_price_amount
+ == (order.subtotal_net_amount + order.base_shipping_price_amount) * tax_rate
)
assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
unit_discount_amount = discount_amount / discounted_line.quantity
assert (
@@ -1802,12 +1790,13 @@ def test_fetch_order_prices_voucher_apply_once_per_order_percentage(
)
assert (
discounted_line.total_price_gross_amount
- == discounted_line.base_unit_price_amount * discounted_line.quantity
+ == discounted_line.base_unit_price_amount * discounted_line.quantity * tax_rate
)
assert (
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert discounted_line.unit_discount_amount == unit_discount_amount
assert discounted_line.unit_discount_type == DiscountValueType.FIXED
@@ -1816,11 +1805,11 @@ def test_fetch_order_prices_voucher_apply_once_per_order_percentage(
assert line_1.base_unit_price_amount == line_1.undiscounted_base_unit_price_amount
assert (
line_1.total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -1836,11 +1825,16 @@ def test_fetch_order_prices_voucher_apply_once_per_order_percentage(
def test_fetch_order_prices_manual_order_discount_voucher_specific_product(
- order_with_lines, voucher_specific_product_type, plugins_manager
+ order_with_lines,
+ voucher_specific_product_type,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
voucher = voucher_specific_product_type
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
unit_discount_amount = Decimal("2")
@@ -1879,22 +1873,30 @@ def test_fetch_order_prices_manual_order_discount_voucher_specific_product(
# then
discounted_line, line_1 = lines
voucher_discount_amount = unit_discount_amount * discounted_line.quantity
- assert (
- order.total_gross_amount
- == subtotal.amount
- + shipping_price.amount
- - voucher_discount_amount
- - order_discount_amount
+ assert order.total_gross_amount == quantize_price(
+ (
+ subtotal.amount
+ + shipping_price.amount
+ - voucher_discount_amount
+ - order_discount_amount
+ )
+ * tax_rate,
+ currency,
)
- shipping_discount = shipping_price - order.shipping_price_gross
+ shipping_discount = shipping_price - order.shipping_price_net
subtotal_order_discount = order_discount_amount - shipping_discount.amount
- assert (
- order.subtotal_gross_amount
- == subtotal.amount - subtotal_order_discount - voucher_discount_amount
+ assert order.subtotal_gross_amount == quantize_price(
+ (subtotal.amount - subtotal_order_discount - voucher_discount_amount)
+ * tax_rate,
+ currency,
+ )
+ assert order.undiscounted_total_gross == quantize_price(
+ (subtotal + shipping_price) * tax_rate, currency
)
- assert order.undiscounted_total_gross == subtotal + shipping_price
- assert order.shipping_price_gross == shipping_price - shipping_discount
- assert order.base_shipping_price == shipping_price - shipping_discount
+ assert order.shipping_price_gross == quantize_price(
+ (shipping_price - shipping_discount) * tax_rate, currency
+ )
+ assert order.base_shipping_price == shipping_price
assert order.undiscounted_base_shipping_price == shipping_price
assert (
@@ -1909,6 +1911,7 @@ def test_fetch_order_prices_manual_order_discount_voucher_specific_product(
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert discounted_line.unit_discount_amount == unit_discount_amount
assert discounted_line.unit_discount_type == DiscountValueType.FIXED
@@ -1921,7 +1924,7 @@ def test_fetch_order_prices_manual_order_discount_voucher_specific_product(
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -1940,10 +1943,15 @@ def test_fetch_order_prices_manual_order_discount_voucher_specific_product(
def test_fetch_order_prices_manual_order_discount_and_voucher_apply_once_per_order(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_amount = Decimal("3")
@@ -1960,7 +1968,7 @@ def test_fetch_order_prices_manual_order_discount_and_voucher_apply_once_per_ord
order.save(update_fields=["voucher", "voucher_code"])
# create manual order discount
- order_discount_amount = Decimal("10")
+ order_discount_amount = Decimal("8")
order_discount = order.discounts.create(
value_type=DiscountValueType.FIXED,
value=order_discount_amount,
@@ -1983,22 +1991,30 @@ def test_fetch_order_prices_manual_order_discount_and_voucher_apply_once_per_ord
discounted_line, line_1 = lines
voucher_discount_amount = discount_amount
order.refresh_from_db()
- assert (
- order.total_gross_amount
- == subtotal.amount
- + shipping_price.amount
- - voucher_discount_amount
- - order_discount_amount
+ assert order.total_gross_amount == quantize_price(
+ (
+ subtotal.amount
+ + shipping_price.amount
+ - voucher_discount_amount
+ - order_discount_amount
+ )
+ * tax_rate,
+ currency,
)
- shipping_discount = shipping_price - order.shipping_price_gross
+ shipping_discount = shipping_price - order.shipping_price_net
subtotal_order_discount = order_discount_amount - shipping_discount.amount
- assert (
- order.subtotal_gross_amount
- == subtotal.amount - subtotal_order_discount - voucher_discount_amount
+ assert order.subtotal_gross_amount == quantize_price(
+ (subtotal.amount - subtotal_order_discount - voucher_discount_amount)
+ * tax_rate,
+ currency,
+ )
+ assert order.undiscounted_total_gross == quantize_price(
+ (subtotal + shipping_price) * tax_rate, currency
+ )
+ assert order.shipping_price_gross == quantize_price(
+ (shipping_price - shipping_discount) * tax_rate, currency
)
- assert order.undiscounted_total_gross == subtotal + shipping_price
- assert order.shipping_price_gross == shipping_price - shipping_discount
- assert order.base_shipping_price == shipping_price - shipping_discount
+ assert order.base_shipping_price == shipping_price
assert order.undiscounted_base_shipping_price == shipping_price
unit_discount_amount = discount_amount / discounted_line.quantity
@@ -2014,6 +2030,7 @@ def test_fetch_order_prices_manual_order_discount_and_voucher_apply_once_per_ord
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert discounted_line.unit_discount_amount == unit_discount_amount
assert discounted_line.unit_discount_type == DiscountValueType.FIXED
@@ -2026,7 +2043,7 @@ def test_fetch_order_prices_manual_order_discount_and_voucher_apply_once_per_ord
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -2045,11 +2062,16 @@ def test_fetch_order_prices_manual_order_discount_and_voucher_apply_once_per_ord
def test_fetch_order_prices_manual_line_discount_voucher_specific_product(
- order_with_lines, voucher_specific_product_type, plugins_manager
+ order_with_lines,
+ voucher_specific_product_type,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
voucher = voucher_specific_product_type
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
unit_discount_amount = Decimal("2")
@@ -2092,17 +2114,20 @@ def test_fetch_order_prices_manual_line_discount_voucher_specific_product(
manual_discount_amount = manual_line_discount_amount * discounted_line.quantity
assert (
order.total_gross_amount
- == subtotal.amount
- + shipping_price.amount
- - voucher_discount_amount
- - manual_discount_amount
+ == (
+ subtotal.amount
+ + shipping_price.amount
+ - voucher_discount_amount
+ - manual_discount_amount
+ )
+ * tax_rate
)
- assert (
- order.subtotal_gross_amount
- == subtotal.amount - manual_discount_amount - voucher_discount_amount
+ assert order.subtotal_gross_amount == quantize_price(
+ (subtotal.amount - manual_discount_amount - voucher_discount_amount) * tax_rate,
+ currency,
)
- assert order.undiscounted_total_gross == subtotal + shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.base_shipping_price == shipping_price
assert (
@@ -2119,6 +2144,7 @@ def test_fetch_order_prices_manual_line_discount_voucher_specific_product(
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert (
discounted_line.unit_discount_amount
@@ -2134,7 +2160,7 @@ def test_fetch_order_prices_manual_line_discount_voucher_specific_product(
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -2154,10 +2180,15 @@ def test_fetch_order_prices_manual_line_discount_voucher_specific_product(
def test_fetch_order_prices_manual_line_discount_and_voucher_apply_once_per_order(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_amount = Decimal("3")
@@ -2207,19 +2238,23 @@ def test_fetch_order_prices_manual_line_discount_and_voucher_apply_once_per_orde
manual_discount_amount = manual_line_discount_amount * discounted_line.quantity
assert (
order.total_gross_amount
- == subtotal.amount
- + shipping_price.amount
- - voucher_discount_amount
- - manual_discount_amount
- )
- shipping_discount = shipping_price - order.shipping_price_gross
+ == (
+ subtotal.amount
+ + shipping_price.amount
+ - voucher_discount_amount
+ - manual_discount_amount
+ )
+ * tax_rate
+ )
+ shipping_discount = shipping_price - order.shipping_price_net
subtotal_order_discount = manual_discount_amount - shipping_discount.amount
- assert (
- order.subtotal_gross_amount
- == subtotal.amount - subtotal_order_discount - voucher_discount_amount
+ assert order.subtotal_gross_amount == quantize_price(
+ (subtotal.amount - subtotal_order_discount - voucher_discount_amount)
+ * tax_rate,
+ currency,
)
- assert order.undiscounted_total_gross == subtotal + shipping_price
- assert order.shipping_price_gross == shipping_price - shipping_discount
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
+ assert order.shipping_price_gross == (shipping_price - shipping_discount) * tax_rate
assert order.base_shipping_price == shipping_price
unit_discount_amount = discount_amount / discounted_line.quantity
@@ -2237,6 +2272,7 @@ def test_fetch_order_prices_manual_line_discount_and_voucher_apply_once_per_orde
discounted_line.undiscounted_total_price_gross_amount
== discounted_line.undiscounted_base_unit_price_amount
* discounted_line.quantity
+ * tax_rate
)
assert (
discounted_line.unit_discount_amount
@@ -2255,7 +2291,7 @@ def test_fetch_order_prices_manual_line_discount_and_voucher_apply_once_per_orde
)
assert (
line_1.undiscounted_total_price_gross_amount
- == line_1.undiscounted_base_unit_price_amount * line_1.quantity
+ == line_1.undiscounted_base_unit_price_amount * line_1.quantity * tax_rate
)
assert line_1.unit_discount_amount == 0
assert line_1.unit_discount_type is None
@@ -2277,9 +2313,11 @@ def test_fetch_order_prices_manual_line_discount_and_voucher_apply_once_per_orde
def test_fetch_order_prices_catalogue_discount_race_condition(
order_with_lines_and_catalogue_promotion,
plugins_manager,
+ tax_configuration_flat_rates,
):
# given
order = order_with_lines_and_catalogue_promotion
+ order.status = OrderStatus.UNCONFIRMED
OrderLineDiscount.objects.all().delete()
# when
@@ -2298,13 +2336,18 @@ def test_fetch_order_prices_catalogue_discount_race_condition(
def test_fetch_order_prices_voucher_entire_order_fixed(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
assert voucher.type == VoucherType.ENTIRE_ORDER
assert voucher.discount_value_type == DiscountValueType.FIXED
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_amount = Decimal("35")
@@ -2346,19 +2389,19 @@ def test_fetch_order_prices_voucher_entire_order_fixed(
assert order.base_shipping_price == shipping_price
assert order.shipping_price_net == shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount - discount_amount
- assert order.subtotal_gross_amount == subtotal.amount - discount_amount
+ assert order.subtotal_gross_amount == (subtotal.amount - discount_amount) * tax_rate
assert (
order.total_net_amount
== order.subtotal_net_amount + order.base_shipping_price_amount
)
assert (
order.total_gross_amount
- == order.subtotal_net_amount + order.base_shipping_price_amount
+ == (order.subtotal_net_amount + order.base_shipping_price_amount) * tax_rate
)
assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
lines = order.lines.all()
line_1 = [line for line in lines if line.quantity == 3][0]
@@ -2408,7 +2451,10 @@ def test_fetch_order_prices_voucher_entire_order_fixed(
def test_fetch_order_prices_voucher_entire_order_percentage(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
assert voucher.type == VoucherType.ENTIRE_ORDER
@@ -2416,6 +2462,8 @@ def test_fetch_order_prices_voucher_entire_order_percentage(
voucher.save(update_fields=["discount_value_type"])
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_value = Decimal("50")
@@ -2457,19 +2505,19 @@ def test_fetch_order_prices_voucher_entire_order_percentage(
assert order.base_shipping_price == shipping_price
assert order.shipping_price_net == shipping_price
- assert order.shipping_price_gross == shipping_price
+ assert order.shipping_price_gross == shipping_price * tax_rate
assert order.subtotal_net_amount == Decimal(subtotal.amount / 2)
- assert order.subtotal_gross_amount == Decimal(subtotal.amount / 2)
+ assert order.subtotal_gross_amount == Decimal(subtotal.amount / 2) * tax_rate
assert (
order.total_net_amount
== order.subtotal_net_amount + order.base_shipping_price_amount
)
assert (
order.total_gross_amount
- == order.subtotal_net_amount + order.base_shipping_price_amount
+ == (order.subtotal_net_amount + order.base_shipping_price_amount) * tax_rate
)
assert order.undiscounted_total_net == subtotal + shipping_price
- assert order.undiscounted_total_gross == subtotal + shipping_price
+ assert order.undiscounted_total_gross == (subtotal + shipping_price) * tax_rate
lines = order.lines.all()
line_1 = [line for line in lines if line.quantity == 3][0]
@@ -2513,7 +2561,10 @@ def test_fetch_order_prices_voucher_entire_order_percentage(
def test_fetch_order_prices_voucher_shipping_fixed(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
voucher.type = VoucherType.SHIPPING
@@ -2521,6 +2572,8 @@ def test_fetch_order_prices_voucher_shipping_fixed(
voucher.save(update_fields=["type", "discount_value_type"])
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_value = Decimal("5")
@@ -2566,12 +2619,13 @@ def test_fetch_order_prices_voucher_shipping_fixed(
assert order.undiscounted_base_shipping_price_amount == undiscounted_shipping_price
assert order.base_shipping_price_amount == expected_shipping_price
assert order.shipping_price_net_amount == expected_shipping_price
- assert order.shipping_price_gross.amount == expected_shipping_price
+ assert order.shipping_price_gross.amount == expected_shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount
- assert order.subtotal_gross_amount == subtotal.amount
+ assert order.subtotal_gross_amount == subtotal.amount * tax_rate
assert order.total_net_amount == order.subtotal_net_amount + expected_shipping_price
assert (
- order.total_gross_amount == order.subtotal_net_amount + expected_shipping_price
+ order.total_gross_amount
+ == (order.subtotal_net_amount + expected_shipping_price) * tax_rate
)
assert (
order.undiscounted_total_net_amount
@@ -2579,12 +2633,15 @@ def test_fetch_order_prices_voucher_shipping_fixed(
)
assert (
order.undiscounted_total_gross_amount
- == subtotal.amount + undiscounted_shipping_price
+ == (subtotal.amount + undiscounted_shipping_price) * tax_rate
)
def test_fetch_order_prices_voucher_shipping_percentage(
- order_with_lines, voucher, plugins_manager
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
):
# given
voucher.type = VoucherType.SHIPPING
@@ -2592,6 +2649,8 @@ def test_fetch_order_prices_voucher_shipping_percentage(
voucher.save(update_fields=["type", "discount_value_type"])
order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
voucher_listing = voucher.channel_listings.get(channel=order.channel)
discount_value = Decimal("50")
@@ -2637,12 +2696,13 @@ def test_fetch_order_prices_voucher_shipping_percentage(
assert order.undiscounted_base_shipping_price.amount == undiscounted_shipping_price
assert order.base_shipping_price.amount == expected_shipping_price
assert order.shipping_price_net_amount == expected_shipping_price
- assert order.shipping_price_gross.amount == expected_shipping_price
+ assert order.shipping_price_gross.amount == expected_shipping_price * tax_rate
assert order.subtotal_net_amount == subtotal.amount
- assert order.subtotal_gross_amount == subtotal.amount
+ assert order.subtotal_gross_amount == subtotal.amount * tax_rate
assert order.total_net_amount == order.subtotal_net_amount + expected_shipping_price
assert (
- order.total_gross_amount == order.subtotal_net_amount + expected_shipping_price
+ order.total_gross_amount
+ == (order.subtotal_net_amount + expected_shipping_price) * tax_rate
)
assert (
order.undiscounted_total_net_amount
@@ -2650,5 +2710,285 @@ def test_fetch_order_prices_voucher_shipping_percentage(
)
assert (
order.undiscounted_total_gross_amount
- == subtotal.amount + undiscounted_shipping_price
+ == (subtotal.amount + undiscounted_shipping_price) * tax_rate
+ )
+
+
+def test_fetch_order_prices_voucher_shipping_and_manual_discount_fixed(
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
+):
+ # given
+ voucher.type = VoucherType.SHIPPING
+ voucher.discount_value_type = DiscountValueType.FIXED
+ voucher.save(update_fields=["type", "discount_value_type"])
+
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
+
+ voucher_listing = voucher.channel_listings.get(channel=order.channel)
+ voucher_discount_amount = Decimal("4")
+ voucher_listing.discount_value = voucher_discount_amount
+ voucher_listing.save(update_fields=["discount_value"])
+
+ order.voucher = voucher
+ code = voucher.codes.first().code
+ order.voucher_code = code
+
+ undiscounted_shipping_price = order.shipping_price.net.amount
+ base_shipping_price = Decimal(undiscounted_shipping_price - voucher_discount_amount)
+ currency = order.currency
+ undiscounted_subtotal = Decimal(0)
+ lines = order.lines.all()
+ for line in lines:
+ undiscounted_subtotal += line.base_unit_price_amount * line.quantity
+
+ # create manual order discount
+ manual_discount_amount = Decimal("10")
+ manual_discount = order.discounts.create(
+ value_type=DiscountValueType.FIXED,
+ value=manual_discount_amount,
+ name="Manual order discount",
+ type=DiscountType.MANUAL,
+ currency=currency,
+ )
+
+ total_with_shipping_voucher_discount = undiscounted_subtotal + base_shipping_price
+ subtotal_manual_reward_portion = round(
+ (undiscounted_subtotal / total_with_shipping_voucher_discount)
+ * manual_discount_amount,
+ 2,
+ )
+ shipping_manual_reward_portion = round(
+ manual_discount_amount - subtotal_manual_reward_portion, 2
+ )
+
+ expected_subtotal = undiscounted_subtotal - subtotal_manual_reward_portion
+ expected_shipping = base_shipping_price - shipping_manual_reward_portion
+ expected_total = expected_subtotal + expected_shipping
+
+ # when
+ order, lines = calculations.fetch_order_prices_if_expired(
+ order, plugins_manager, None, True
+ )
+
+ # then
+ assert OrderDiscount.objects.count() == 2
+ voucher_discount = order.discounts.filter(type=DiscountType.VOUCHER).first()
+ assert voucher_discount.voucher == voucher
+ assert voucher_discount.voucher_code == code
+ assert (
+ voucher_discount.amount_value
+ == undiscounted_shipping_price - base_shipping_price
+ )
+
+ manual_discount.refresh_from_db()
+ assert manual_discount.amount.amount == manual_discount_amount
+
+ assert order.undiscounted_base_shipping_price_amount == undiscounted_shipping_price
+ assert order.base_shipping_price_amount == base_shipping_price
+ assert order.shipping_price_net_amount == expected_shipping
+ assert order.shipping_price_gross.amount == round(expected_shipping * tax_rate, 2)
+ assert order.subtotal_net_amount == expected_subtotal
+ assert order.subtotal_gross_amount == round(expected_subtotal * tax_rate, 2)
+ assert order.total_net_amount == expected_total
+ assert order.total_gross_amount == round(expected_total * tax_rate, 2)
+ assert (
+ order.undiscounted_total_net_amount
+ == undiscounted_subtotal + undiscounted_shipping_price
+ )
+ assert order.undiscounted_total_gross_amount == round(
+ (undiscounted_subtotal + undiscounted_shipping_price) * tax_rate, 2
+ )
+
+
+def test_fetch_order_prices_voucher_shipping_and_manual_discount_percentage(
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
+):
+ # given
+ voucher.type = VoucherType.SHIPPING
+ voucher.discount_value_type = DiscountValueType.FIXED
+ voucher.save(update_fields=["type", "discount_value_type"])
+
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
+
+ voucher_listing = voucher.channel_listings.get(channel=order.channel)
+ voucher_discount_amount = Decimal("4")
+ voucher_listing.discount_value = voucher_discount_amount
+ voucher_listing.save(update_fields=["discount_value"])
+
+ order.voucher = voucher
+ code = voucher.codes.first().code
+ order.voucher_code = code
+
+ undiscounted_shipping_price = order.shipping_price.net.amount
+ base_shipping_price = Decimal(undiscounted_shipping_price - voucher_discount_amount)
+ currency = order.currency
+ undiscounted_subtotal = Decimal(0)
+ lines = order.lines.all()
+ for line in lines:
+ undiscounted_subtotal += line.base_unit_price_amount * line.quantity
+
+ # create manual order discount
+ manual_discount_value = Decimal("10")
+ manual_discount = order.discounts.create(
+ value_type=DiscountValueType.PERCENTAGE,
+ value=manual_discount_value,
+ name="Manual order discount",
+ type=DiscountType.MANUAL,
+ currency=currency,
+ )
+
+ total_with_shipping_voucher_discount = undiscounted_subtotal + base_shipping_price
+ manual_discount_amount = round(
+ total_with_shipping_voucher_discount * manual_discount_value / 100, 2
+ )
+ subtotal_manual_reward_portion = round(
+ (undiscounted_subtotal / total_with_shipping_voucher_discount)
+ * manual_discount_amount,
+ 2,
+ )
+ shipping_manual_reward_portion = round(
+ manual_discount_amount - subtotal_manual_reward_portion, 2
+ )
+
+ expected_subtotal = undiscounted_subtotal - subtotal_manual_reward_portion
+ expected_shipping = base_shipping_price - shipping_manual_reward_portion
+ expected_total = expected_subtotal + expected_shipping
+
+ # when
+ order, lines = calculations.fetch_order_prices_if_expired(
+ order, plugins_manager, None, True
+ )
+
+ # then
+ assert OrderDiscount.objects.count() == 2
+ voucher_discount = order.discounts.filter(type=DiscountType.VOUCHER).first()
+ assert voucher_discount.voucher == voucher
+ assert voucher_discount.voucher_code == code
+ assert (
+ voucher_discount.amount_value
+ == undiscounted_shipping_price - base_shipping_price
+ )
+
+ manual_discount.refresh_from_db()
+ assert manual_discount.amount.amount == manual_discount_amount
+
+ assert order.undiscounted_base_shipping_price_amount == undiscounted_shipping_price
+ assert order.base_shipping_price_amount == base_shipping_price
+ assert order.shipping_price_net_amount == expected_shipping
+ assert order.shipping_price_gross.amount == round(expected_shipping * tax_rate, 2)
+ assert order.subtotal_net_amount == expected_subtotal
+ assert order.subtotal_gross_amount == round(expected_subtotal * tax_rate, 2)
+ assert order.total_net_amount == expected_total
+ assert order.total_gross_amount == round(expected_total * tax_rate, 2)
+ assert (
+ order.undiscounted_total_net_amount
+ == undiscounted_subtotal + undiscounted_shipping_price
+ )
+ assert order.undiscounted_total_gross_amount == round(
+ (undiscounted_subtotal + undiscounted_shipping_price) * tax_rate, 2
)
+
+
+def test_fetch_order_prices_voucher_shipping_and_manual_discount_fixed_exceed_total(
+ order_with_lines,
+ voucher,
+ plugins_manager,
+ tax_configuration_flat_rates,
+):
+ # given
+ voucher.type = VoucherType.SHIPPING
+ voucher.discount_value_type = DiscountValueType.FIXED
+ voucher.save(update_fields=["type", "discount_value_type"])
+
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ tax_rate = Decimal("1.23")
+
+ voucher_listing = voucher.channel_listings.get(channel=order.channel)
+ voucher_discount_amount = Decimal("4")
+ voucher_listing.discount_value = voucher_discount_amount
+ voucher_listing.save(update_fields=["discount_value"])
+
+ order.voucher = voucher
+ code = voucher.codes.first().code
+ order.voucher_code = code
+
+ undiscounted_shipping_price = order.shipping_price.net.amount
+ base_shipping_price = Decimal(undiscounted_shipping_price - voucher_discount_amount)
+ currency = order.currency
+ undiscounted_subtotal = Decimal(0)
+ lines = order.lines.all()
+ for line in lines:
+ undiscounted_subtotal += line.base_unit_price_amount * line.quantity
+
+ # create manual order discount
+ manual_discount_value = Decimal("1000")
+ manual_discount = order.discounts.create(
+ value_type=DiscountValueType.FIXED,
+ value=manual_discount_value,
+ name="Manual order discount",
+ type=DiscountType.MANUAL,
+ currency=currency,
+ )
+
+ total_with_shipping_voucher_discount = undiscounted_subtotal + base_shipping_price
+ assert manual_discount_value > total_with_shipping_voucher_discount
+ manual_discount_amount = total_with_shipping_voucher_discount
+ subtotal_manual_reward_portion = round(
+ (undiscounted_subtotal / total_with_shipping_voucher_discount)
+ * manual_discount_amount,
+ 2,
+ )
+ shipping_manual_reward_portion = round(
+ manual_discount_amount - subtotal_manual_reward_portion, 2
+ )
+
+ expected_subtotal = undiscounted_subtotal - subtotal_manual_reward_portion
+ expected_shipping = base_shipping_price - shipping_manual_reward_portion
+ expected_total = expected_subtotal + expected_shipping
+
+ # when
+ order, lines = calculations.fetch_order_prices_if_expired(
+ order, plugins_manager, None, True
+ )
+
+ # then
+ assert OrderDiscount.objects.count() == 2
+ voucher_discount = order.discounts.filter(type=DiscountType.VOUCHER).first()
+ assert voucher_discount.voucher == voucher
+ assert voucher_discount.voucher_code == code
+ assert (
+ voucher_discount.amount_value
+ == undiscounted_shipping_price - base_shipping_price
+ )
+
+ manual_discount.refresh_from_db()
+ assert manual_discount.amount.amount == total_with_shipping_voucher_discount
+
+ assert order.undiscounted_base_shipping_price_amount == undiscounted_shipping_price
+ assert order.base_shipping_price_amount == base_shipping_price
+ assert order.shipping_price_net_amount == expected_shipping
+ assert order.shipping_price_gross.amount == round(expected_shipping * tax_rate, 2)
+ assert order.subtotal_net_amount == expected_subtotal
+ assert order.subtotal_gross_amount == round(expected_subtotal * tax_rate, 2)
+ assert order.total_net_amount == expected_total
+ assert order.total_gross_amount == round(expected_total * tax_rate, 2)
+ # FIXME (SHOPX-1390): If discounts exceed total value and flat rates are used
+ # undiscounted prices are set to 0
+ # assert (
+ # order.undiscounted_total_net_amount
+ # == undiscounted_subtotal + undiscounted_shipping_price
+ # )
+ # assert order.undiscounted_total_gross_amount == round(
+ # (undiscounted_subtotal + undiscounted_shipping_price) * tax_rate, 2
+ # )
diff --git a/saleor/tests/e2e/orders/discounts/test_order_with_shipping_voucher_and_manual_order_discount.py b/saleor/tests/e2e/orders/discounts/test_order_with_shipping_voucher_and_manual_order_discount.py
new file mode 100644
index 0000000000..ec4af9d5b3
--- /dev/null
+++ b/saleor/tests/e2e/orders/discounts/test_order_with_shipping_voucher_and_manual_order_discount.py
@@ -0,0 +1,177 @@
+from decimal import Decimal
+
+import pytest
+
+from ... import DEFAULT_ADDRESS
+from ...product.utils.preparing_product import prepare_product
+from ...shop.utils.preparing_shop import prepare_default_shop
+from ...utils import assign_permissions
+from ...vouchers.utils import create_voucher, create_voucher_channel_listing
+from ..utils import (
+ draft_order_complete,
+ draft_order_create,
+ draft_order_update,
+ order_discount_add,
+ order_lines_create,
+)
+
+
+def prepare_shipping_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ voucher_code,
+ voucher_discount_type,
+ voucher_discount_value,
+):
+ input = {
+ "code": voucher_code,
+ "discountValueType": voucher_discount_type,
+ "type": "SHIPPING",
+ }
+ voucher_data = create_voucher(e2e_staff_api_client, input)
+ voucher_id = voucher_data["id"]
+ channel_listing = [
+ {
+ "channelId": channel_id,
+ "discountValue": voucher_discount_value,
+ }
+ ]
+ create_voucher_channel_listing(
+ e2e_staff_api_client,
+ voucher_id,
+ channel_listing,
+ )
+
+ return voucher_discount_value, voucher_code
+
+
+def test_order_with_shipping_voucher_and_manual_order_discount(
+ e2e_staff_api_client,
+ shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ permission_manage_orders,
+):
+ permissions = [
+ *shop_permissions,
+ permission_manage_product_types_and_attributes,
+ permission_manage_discounts,
+ permission_manage_orders,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ shop_data = prepare_default_shop(e2e_staff_api_client)
+ channel_id = shop_data["channel"]["id"]
+ warehouse_id = shop_data["warehouse"]["id"]
+ shipping_method_id = shop_data["shipping_method"]["id"]
+ undiscounted_shipping_price = Decimal(10)
+ (
+ product_id,
+ product_variant_id,
+ product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ variant_price=Decimal(15),
+ )
+
+ voucher_code = "free-shipping"
+ voucher_discount_value = Decimal(50)
+ prepare_shipping_voucher(
+ e2e_staff_api_client,
+ channel_id,
+ voucher_code,
+ "PERCENTAGE",
+ voucher_discount_value,
+ )
+
+ # Step 1 - Create a draft order with shipping voucher
+ input = {
+ "channelId": channel_id,
+ "billingAddress": DEFAULT_ADDRESS,
+ "shippingAddress": DEFAULT_ADDRESS,
+ "shippingMethod": shipping_method_id,
+ "voucherCode": voucher_code,
+ }
+ data = draft_order_create(e2e_staff_api_client, input)
+ order_id = data["order"]["id"]
+ assert data["order"]["billingAddress"] is not None
+ assert data["order"]["shippingAddress"] is not None
+ assert data["order"]["voucher"]["code"] == voucher_code
+
+ # Step 2 - Add order line
+ quantity = 3
+ lines = [{"variantId": product_variant_id, "quantity": quantity}]
+ data = order_lines_create(e2e_staff_api_client, order_id, lines)
+ undiscounted_subtotal = Decimal(quantity * product_variant_price)
+ undiscounted_total = undiscounted_subtotal + undiscounted_shipping_price
+
+ assert data["order"]["subtotal"]["gross"]["amount"] == undiscounted_subtotal
+ # TODO (SHOPX-1388): Shipping price is not updated when running `orderLinesCreate`
+ # assert undiscounted_shipping_price == data["order"]["undiscountedShippingPrice"]["amount"]
+
+ # Step 3 - Add shipping method to the order
+ input = {"shippingMethod": shipping_method_id}
+ data = draft_order_update(e2e_staff_api_client, order_id, input)
+ assert (
+ undiscounted_shipping_price
+ == data["order"]["undiscountedShippingPrice"]["amount"]
+ )
+ shipping_price = Decimal(data["order"]["shippingPrice"]["gross"]["amount"])
+ assert shipping_price == undiscounted_shipping_price * voucher_discount_value / 100
+ voucher_discount_amount = undiscounted_shipping_price - shipping_price
+ subtotal = Decimal(data["order"]["subtotal"]["gross"]["amount"])
+ assert undiscounted_subtotal == subtotal
+ total = Decimal(data["order"]["total"]["gross"]["amount"])
+ assert total == undiscounted_total - voucher_discount_amount
+
+ # Step 4 - Add manual discount to the order
+ manual_discount_value = Decimal(15)
+ manual_discount_input = {
+ "valueType": "FIXED",
+ "value": manual_discount_value,
+ }
+ data = order_discount_add(
+ e2e_staff_api_client,
+ order_id,
+ manual_discount_input,
+ )
+ manual_discount_subtotal_share = Decimal(subtotal / total * manual_discount_value)
+ manual_discount_shipping_share = (
+ manual_discount_value - manual_discount_subtotal_share
+ )
+ shipping_price = Decimal(data["order"]["shippingPrice"]["gross"]["amount"])
+
+ assert (
+ shipping_price
+ == undiscounted_shipping_price * voucher_discount_value / 100
+ - manual_discount_shipping_share
+ )
+ subtotal = Decimal(data["order"]["subtotal"]["gross"]["amount"])
+ assert subtotal == undiscounted_subtotal - manual_discount_subtotal_share
+ total = Decimal(data["order"]["total"]["gross"]["amount"])
+ assert total == undiscounted_total - voucher_discount_amount - manual_discount_value
+
+ # Step 5 - Complete the draft order
+ data = draft_order_complete(e2e_staff_api_client, order_id)
+ assert (
+ Decimal(data["order"]["undiscountedShippingPrice"]["amount"])
+ == undiscounted_shipping_price
+ )
+ assert Decimal(data["order"]["shippingPrice"]["gross"]["amount"]) == shipping_price
+ assert Decimal(data["order"]["subtotal"]["gross"]["amount"]) == subtotal
+ assert (
+ Decimal(data["order"]["undiscountedTotal"]["gross"]["amount"])
+ == undiscounted_total
+ )
+ assert Decimal(data["order"]["total"]["gross"]["amount"]) == total
+
+ order_line = data["order"]["lines"][0]
+ assert (
+ order_line["undiscountedUnitPrice"]["gross"]["amount"] == product_variant_price
+ )
+ assert order_line["unitPrice"]["gross"]["amount"] == Decimal(
+ product_variant_price
+ ) - Decimal(manual_discount_subtotal_share / quantity)
diff --git a/saleor/tests/e2e/orders/utils/draft_order_complete.py b/saleor/tests/e2e/orders/utils/draft_order_complete.py
index 8415cf52c9..5a155a524c 100644
--- a/saleor/tests/e2e/orders/utils/draft_order_complete.py
+++ b/saleor/tests/e2e/orders/utils/draft_order_complete.py
@@ -22,6 +22,9 @@ mutation DraftOrderComplete($id: ID!) {
subtotal {
...BaseTaxedMoney
}
+ undiscountedShippingPrice {
+ amount
+ }
shippingPrice {
...BaseTaxedMoney
}
diff --git a/saleor/tests/e2e/orders/utils/order_discount_add.py b/saleor/tests/e2e/orders/utils/order_discount_add.py
index 69411191e6..549c88e51b 100644
--- a/saleor/tests/e2e/orders/utils/order_discount_add.py
+++ b/saleor/tests/e2e/orders/utils/order_discount_add.py
@@ -31,6 +31,15 @@ mutation OrderDiscountAdd($input: OrderDiscountCommonInput!, $id: ID!) {
amount
}
}
+ shippingPrice {
+ ...BaseTaxedMoney
+ }
+ total {
+ ...BaseTaxedMoney
+ }
+ subtotal {
+ ...BaseTaxedMoney
+ }
}
}
}
diff --git a/saleor/tests/e2e/orders/utils/order_lines_create.py b/saleor/tests/e2e/orders/utils/order_lines_create.py
index 572ae1f78f..cd7f881157 100644
--- a/saleor/tests/e2e/orders/utils/order_lines_create.py
+++ b/saleor/tests/e2e/orders/utils/order_lines_create.py
@@ -11,6 +11,12 @@ mutation orderLinesCreate($id: ID!, $input: [OrderLineCreateInput!]!) {
amount
}
}
+ undiscountedShippingPrice {
+ amount
+ }
+ shippingPrice {
+ ...BaseTaxedMoney
+ }
total {
...BaseTaxedMoney
}
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 37716765fa..948902afa5 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -81,6 +81,7 @@ from ..discount.models import (
VoucherTranslation,
)
from ..discount.utils.voucher import (
+ create_or_update_discount_object_from_order_level_voucher,
get_products_voucher_discount,
validate_voucher_in_order,
)
@@ -90,7 +91,7 @@ from ..graphql.core.utils import to_global_id_or_none
from ..menu.models import Menu, MenuItem, MenuItemTranslation
from ..order import OrderOrigin, OrderStatus
from ..order.actions import cancel_fulfillment, fulfill_order_lines
-from ..order.base_calculations import apply_order_discounts
+from ..order.base_calculations import base_order_subtotal
from ..order.events import (
OrderEvents,
fulfillment_refunded_event,
@@ -147,6 +148,7 @@ from ..shipping.models import (
)
from ..shipping.utils import convert_to_shipping_method_data
from ..site.models import SiteSettings
+from ..tax import TaxCalculationStrategy
from ..tax.utils import calculate_tax_rate, get_tax_class_kwargs_for_order_line
from ..thumbnail.models import Thumbnail
from ..warehouse import WarehouseClickAndCollectOption
@@ -5805,7 +5807,12 @@ def draft_order_with_free_shipping_voucher(
order.voucher = voucher_free_shipping
order.voucher_code = voucher_code.code
- subtotal, shipping_price = apply_order_discounts(order, order.lines.all())
+ create_or_update_discount_object_from_order_level_voucher(
+ order, settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
+
+ subtotal = base_order_subtotal(order, order.lines.all())
+ shipping_price = order.base_shipping_price
order.subtotal = TaxedMoney(gross=subtotal, net=subtotal)
order.shipping_price = TaxedMoney(net=shipping_price, gross=shipping_price)
total = subtotal + shipping_price
@@ -9870,3 +9877,25 @@ def setup_order_webhooks(
)
return _setup
+
+
+def tax_configuration_flat_rates(channel_USD):
+ tc = channel_USD.tax_configuration
+ tc.country_exceptions.all().delete()
+ tc.prices_entered_with_tax = False
+ tc.tax_calculation_strategy = TaxCalculationStrategy.FLAT_RATES
+ tc.tax_app_id = "avatax.app"
+ tc.save()
+ return tc
+
+
+def tax_configuration_tax_app(channel_USD):
+ tc = channel_USD.tax_configuration
+ tc.country_exceptions.all().delete()
+ tc.prices_entered_with_tax = False
+ tc.tax_calculation_strategy = TaxCalculationStrategy.TAX_APP
+ tc.tax_app_id = "avatax.app"
+ tc.save()
+ return tc
diff --git a/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
index f0a2e01956..93a92b7347 100644
--- a/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py
@@ -1,23 +1,30 @@
import json
from decimal import Decimal
-from functools import partial
-from unittest.mock import ANY
+from unittest.mock import ANY, Mock
import pytest
from freezegun import freeze_time
-from prices import Money, TaxedMoney, fixed_discount
+from prices import Money, TaxedMoney
from ....core.prices import quantize_price
-from ....discount import DiscountValueType, VoucherType
+from ....discount import (
+ DiscountType,
+ DiscountValueType,
+ RewardType,
+ RewardValueType,
+ VoucherType,
+)
from ....discount.models import PromotionRule
from ....graphql.core.utils import to_global_id_or_none
from ....order import OrderStatus
from ....order.calculations import fetch_order_prices_if_expired
from ....order.models import Order
-from ....order.utils import update_discount_for_order_line
+from ....order.utils import (
+ create_order_discount_for_order,
+ update_discount_for_order_line,
+)
from ....plugins.manager import get_plugins_manager
from ....tax import TaxableObjectDiscountType
-from ....tests.fixtures import recalculate_order
from ...event_types import WebhookEventSyncType
from ...models import Webhook
from ...transport.synchronous.transport import (
@@ -90,6 +97,13 @@ subscription {
"""
+def subscription_order_calculate_taxes(subscription_webhook):
+ return subscription_webhook(
+ TAXES_SUBSCRIPTION_QUERY, WebhookEventSyncType.ORDER_CALCULATE_TAXES
+ )
+
+
@freeze_time("2020-03-18 12:00:00")
@pytest.mark.parametrize("charge_taxes", [True, False])
def test_checkout_calculate_taxes(
@@ -1008,33 +1022,31 @@ def test_draft_order_calculate_taxes_free_shipping_voucher(
@freeze_time("2020-03-18 12:00:00")
def test_order_calculate_taxes_with_manual_discount(
- order_line,
+ order_with_lines,
subscription_calculate_taxes_for_order,
+ tax_configuration_tax_app,
):
# given
- order = order_line.order
- currency = order.currency
-
- order.total = order_line.total_price + order.shipping_price
- order.undiscounted_total = order.total
- order.save()
+ order = order_with_lines
+ shipping_price_amount = order.base_shipping_price_amount
- value = Decimal("20")
- discount = partial(fixed_discount, discount=Money(value, order.currency))
- order.total = discount(order.total)
- order.save()
+ discount_value = Decimal("20")
order.discounts.create(
value_type=DiscountValueType.FIXED,
- value=value,
+ value=discount_value,
reason="Discount reason",
- amount=(order.undiscounted_total - order.total).gross,
)
- shipping_price = Money(Decimal("10"), currency)
- order.base_shipping_price = shipping_price
+ line_1, line_2 = order.lines.all()
+ line_1_unit_price = line_1.base_unit_price_amount
+ line_2_unit_price = line_2.base_unit_price_amount
+ subtotal_amount = (
+ line_1_unit_price * line_1.quantity + line_2_unit_price * line_2.quantity
+ )
+ total_amount = subtotal_amount + shipping_price_amount
- recalculate_order(order)
- order.refresh_from_db()
+ manager = Mock(get_taxes_for_order=Mock(return_value={}))
+ fetch_order_prices_if_expired(order, manager, [line_1, line_2], True)
webhook = subscription_calculate_taxes_for_order
webhook.subscription_query = TAXES_SUBSCRIPTION_QUERY
@@ -1042,10 +1054,8 @@ def test_order_calculate_taxes_with_manual_discount(
# Manual discount applies both to subtotal and shipping. For tax calculation it
# requires to be split into subtotal and shipping portion.
- subtotal = order.subtotal.net
- total = subtotal + shipping_price
- manual_discount_subtotal_portion = subtotal / total * value
- manual_discount_shipping_portion = value - manual_discount_subtotal_portion
+ manual_discount_subtotal_portion = subtotal_amount / total_amount * discount_value
+ manual_discount_shipping_portion = discount_value - manual_discount_subtotal_portion
# when
deliveries = create_delivery_for_subscription_sync_event(
@@ -1072,20 +1082,37 @@ def test_order_calculate_taxes_with_manual_discount(
"lines": [
{
"chargeTaxes": True,
- "productName": "Test product",
- "productSku": "SKU_A",
- "quantity": 3,
+ "productName": line_1.product_name,
+ "productSku": line_1.product_sku,
+ "quantity": line_1.quantity,
"sourceLine": {
"__typename": "OrderLine",
- "id": to_global_id_or_none(order_line),
+ "id": to_global_id_or_none(line_1),
},
- "totalPrice": {"amount": 36.9},
- "unitPrice": {"amount": 12.3},
- "variantName": "SKU_A",
- }
+ "totalPrice": {
+ "amount": float(line_1_unit_price * line_1.quantity)
+ },
+ "unitPrice": {"amount": float(line_1_unit_price)},
+ "variantName": line_1.variant_name,
+ },
+ {
+ "chargeTaxes": True,
+ "productName": line_2.product_name,
+ "productSku": line_2.product_sku,
+ "quantity": line_2.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_2),
+ },
+ "totalPrice": {
+ "amount": float(line_2_unit_price * line_2.quantity)
+ },
+ "unitPrice": {"amount": float(line_2_unit_price)},
+ "variantName": line_2.variant_name,
+ },
],
- "pricesEnteredWithTax": True,
- "shippingPrice": {"amount": float(shipping_price.amount)},
+ "pricesEnteredWithTax": False,
+ "shippingPrice": {"amount": float(shipping_price_amount)},
"sourceObject": {"__typename": "Order", "id": to_global_id_or_none(order)},
},
}
@@ -1129,34 +1156,140 @@ def test_order_calculate_taxes_empty_order(
}
-@freeze_time("2020-03-18 12:00:00")
def test_order_calculate_taxes_order_promotion(
- order_with_lines_and_order_promotion, subscription_calculate_taxes_for_order
+ order_with_lines,
+ order_promotion_with_rule,
+ subscription_order_calculate_taxes,
+ tax_configuration_tax_app,
):
# given
- order = order_with_lines_and_order_promotion
- order.status = OrderStatus.DRAFT
- order.save(update_fields=["status"])
- webhook = subscription_calculate_taxes_for_order
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ webhook = subscription_order_calculate_taxes
+ channel = order.channel
+ shipping_price_amount = order.base_shipping_price_amount
+
+ line_1, line_2 = order.lines.all()
+ line_1_unit_price = line_1.base_unit_price_amount
+ line_2_unit_price = line_2.base_unit_price_amount
+
+ promotion = order_promotion_with_rule
+ rule = promotion.rules.get()
+ rule.order_predicate = {
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
+ }
+ rule.save(update_fields=["order_predicate"])
+ reward_value = Decimal(5)
+ assert rule.reward_value == reward_value
+ assert rule.reward_value_type == RewardValueType.FIXED
+ assert rule.reward_type == RewardType.SUBTOTAL_DISCOUNT
- webhook.subscription_query = TAXES_SUBSCRIPTION_QUERY
- webhook.save(update_fields=["subscription_query"])
+ manager = Mock(get_taxes_for_order=Mock(return_value={}))
- discount_amount = PromotionRule.objects.get().reward_value
+ # when
+ fetch_order_prices_if_expired(order, manager, None, True)
+ deliveries = create_delivery_for_subscription_sync_event(
+ WebhookEventSyncType.ORDER_CALCULATE_TAXES, order, webhook
+ )
- expected_shipping_price = Money("2.00", order.currency)
- order.undiscounted_base_shipping_price = expected_shipping_price
- order.base_shipping_price = expected_shipping_price
- order.shipping_price = TaxedMoney(
- net=expected_shipping_price, gross=expected_shipping_price
+ # then
+ assert json.loads(deliveries.payload.get_payload()) == {
+ "__typename": "CalculateTaxes",
+ "taxBase": {
+ "address": {"id": to_global_id_or_none(order.shipping_address)},
+ "currency": "USD",
+ "discounts": [
+ {
+ "amount": {"amount": reward_value},
+ "type": TaxableObjectDiscountType.SUBTOTAL,
+ }
+ ],
+ "channel": {"id": to_global_id_or_none(channel)},
+ "lines": [
+ {
+ "chargeTaxes": True,
+ "productName": line_1.product_name,
+ "productSku": line_1.product_sku,
+ "quantity": line_1.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_1),
+ },
+ "totalPrice": {"amount": line_1_unit_price * line_1.quantity},
+ "unitPrice": {"amount": line_1_unit_price},
+ "variantName": line_1.variant_name,
+ },
+ {
+ "chargeTaxes": True,
+ "productName": line_2.product_name,
+ "productSku": line_2.product_sku,
+ "quantity": line_2.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_2),
+ },
+ "totalPrice": {"amount": line_2_unit_price * line_2.quantity},
+ "unitPrice": {"amount": line_2_unit_price},
+ "variantName": line_2.variant_name,
+ },
+ ],
+ "pricesEnteredWithTax": False,
+ "shippingPrice": {"amount": shipping_price_amount},
+ "sourceObject": {"__typename": "Order", "id": to_global_id_or_none(order)},
+ },
+ }
+
+
+def test_order_calculate_taxes_order_voucher_and_manual_discount(
+ order_with_lines,
+ voucher,
+ subscription_order_calculate_taxes,
+ tax_configuration_tax_app,
+):
+ # given
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ webhook = subscription_order_calculate_taxes
+ channel = order.channel
+ shipping_price_amount = order.base_shipping_price_amount
+
+ line_1, line_2 = order.lines.all()
+ line_1_unit_price = line_1.base_unit_price_amount
+ line_2_unit_price = line_2.base_unit_price_amount
+ subtotal_amount = (
+ line_1_unit_price * line_1.quantity + line_2_unit_price * line_2.quantity
+ )
+ total_amount = subtotal_amount + shipping_price_amount
+
+ assert voucher.type == VoucherType.ENTIRE_ORDER
+ order.voucher = voucher
+ order.save(update_fields=["voucher_id"])
+
+ manual_reward = Decimal(10)
+ create_order_discount_for_order(
+ order=order,
+ reason="Manual discount",
+ value_type=DiscountValueType.FIXED,
+ value=manual_reward,
+ type=DiscountType.MANUAL,
)
+ subtotal_manual_reward_portion = (subtotal_amount / total_amount) * manual_reward
+ shipping_manual_reward_portion = manual_reward - subtotal_manual_reward_portion
+
+ manager = Mock(get_taxes_for_order=Mock(return_value={}))
+
# when
+ fetch_order_prices_if_expired(order, manager, None, True)
deliveries = create_delivery_for_subscription_sync_event(
WebhookEventSyncType.ORDER_CALCULATE_TAXES, order, webhook
)
# then
+ manual_discount = order.discounts.get(type=DiscountType.MANUAL)
+ assert manual_discount.amount_value == manual_reward
+ assert not order.discounts.filter(type=DiscountType.VOUCHER).first()
+
assert json.loads(deliveries.payload.get_payload()) == {
"__typename": "CalculateTaxes",
"taxBase": {
@@ -1164,34 +1297,346 @@ def test_order_calculate_taxes_order_promotion(
"currency": "USD",
"discounts": [
{
- "amount": {"amount": float(discount_amount)},
+ "amount": {"amount": subtotal_manual_reward_portion},
"type": TaxableObjectDiscountType.SUBTOTAL,
- }
+ },
+ {
+ "amount": {"amount": shipping_manual_reward_portion},
+ "type": TaxableObjectDiscountType.SHIPPING,
+ },
],
- "channel": {"id": to_global_id_or_none(order.channel)},
+ "channel": {"id": to_global_id_or_none(channel)},
"lines": [
{
"chargeTaxes": True,
- "productName": line.product_name,
- "productSku": line.product_sku,
- "quantity": line.quantity,
+ "productName": line_1.product_name,
+ "productSku": line_1.product_sku,
+ "quantity": line_1.quantity,
"sourceLine": {
"__typename": "OrderLine",
- "id": to_global_id_or_none(line),
+ "id": to_global_id_or_none(line_1),
},
- "totalPrice": {
- "amount": float(line.base_unit_price_amount * line.quantity)
+ "totalPrice": {"amount": line_1_unit_price * line_1.quantity},
+ "unitPrice": {"amount": line_1_unit_price},
+ "variantName": line_1.variant_name,
+ },
+ {
+ "chargeTaxes": True,
+ "productName": line_2.product_name,
+ "productSku": line_2.product_sku,
+ "quantity": line_2.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_2),
},
- "unitPrice": {"amount": float(line.base_unit_price_amount)},
- "variantName": line.variant_name,
- }
- for line in order.lines.all()
+ "totalPrice": {"amount": line_2_unit_price * line_2.quantity},
+ "unitPrice": {"amount": line_2_unit_price},
+ "variantName": line_2.variant_name,
+ },
],
- "pricesEnteredWithTax": True,
- "shippingPrice": {"amount": float(float(expected_shipping_price.amount))},
- "sourceObject": {
- "__typename": "Order",
- "id": to_global_id_or_none(order),
- },
+ "pricesEnteredWithTax": False,
+ "shippingPrice": {"amount": shipping_price_amount},
+ "sourceObject": {"__typename": "Order", "id": to_global_id_or_none(order)},
+ },
+ }
+
+
+def test_order_calculate_taxes_order_promotion_and_manual_discount(
+ order_with_lines,
+ order_promotion_with_rule,
+ subscription_order_calculate_taxes,
+ tax_configuration_tax_app,
+):
+ # given
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ webhook = subscription_order_calculate_taxes
+ channel = order.channel
+ shipping_price_amount = order.base_shipping_price_amount
+
+ line_1, line_2 = order.lines.all()
+ line_1_unit_price = line_1.base_unit_price_amount
+ line_2_unit_price = line_2.base_unit_price_amount
+ subtotal_amount = (
+ line_1_unit_price * line_1.quantity + line_2_unit_price * line_2.quantity
+ )
+ total_amount = subtotal_amount + shipping_price_amount
+
+ promotion = order_promotion_with_rule
+ rule = promotion.rules.get()
+ rule.order_predicate = {
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
+ }
+ rule.save(update_fields=["order_predicate"])
+ assert rule.reward_type == RewardType.SUBTOTAL_DISCOUNT
+
+ manual_reward = Decimal(10)
+ create_order_discount_for_order(
+ order=order,
+ reason="Manual discount",
+ value_type=DiscountValueType.FIXED,
+ value=manual_reward,
+ type=DiscountType.MANUAL,
+ )
+
+ subtotal_manual_reward_portion = (subtotal_amount / total_amount) * manual_reward
+ shipping_manual_reward_portion = manual_reward - subtotal_manual_reward_portion
+
+ manager = Mock(get_taxes_for_order=Mock(return_value={}))
+
+ # when
+ fetch_order_prices_if_expired(order, manager, None, True)
+ deliveries = create_delivery_for_subscription_sync_event(
+ WebhookEventSyncType.ORDER_CALCULATE_TAXES, order, webhook
+ )
+
+ # then
+ manual_discount = order.discounts.get(type=DiscountType.MANUAL)
+ assert manual_discount.amount_value == manual_reward
+ assert not order.discounts.filter(type=DiscountType.ORDER_PROMOTION).first()
+
+ assert json.loads(deliveries.payload.get_payload()) == {
+ "__typename": "CalculateTaxes",
+ "taxBase": {
+ "address": {"id": to_global_id_or_none(order.shipping_address)},
+ "currency": "USD",
+ "discounts": [
+ {
+ "amount": {"amount": subtotal_manual_reward_portion},
+ "type": TaxableObjectDiscountType.SUBTOTAL,
+ },
+ {
+ "amount": {"amount": shipping_manual_reward_portion},
+ "type": TaxableObjectDiscountType.SHIPPING,
+ },
+ ],
+ "channel": {"id": to_global_id_or_none(channel)},
+ "lines": [
+ {
+ "chargeTaxes": True,
+ "productName": line_1.product_name,
+ "productSku": line_1.product_sku,
+ "quantity": line_1.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_1),
+ },
+ "totalPrice": {"amount": line_1_unit_price * line_1.quantity},
+ "unitPrice": {"amount": line_1_unit_price},
+ "variantName": line_1.variant_name,
+ },
+ {
+ "chargeTaxes": True,
+ "productName": line_2.product_name,
+ "productSku": line_2.product_sku,
+ "quantity": line_2.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_2),
+ },
+ "totalPrice": {"amount": line_2_unit_price * line_2.quantity},
+ "unitPrice": {"amount": line_2_unit_price},
+ "variantName": line_2.variant_name,
+ },
+ ],
+ "pricesEnteredWithTax": False,
+ "shippingPrice": {"amount": shipping_price_amount},
+ "sourceObject": {"__typename": "Order", "id": to_global_id_or_none(order)},
+ },
+ }
+
+
+def test_order_calculate_taxes_free_shipping_voucher_and_manual_discount_fixed(
+ order_with_lines,
+ voucher_free_shipping,
+ subscription_order_calculate_taxes,
+ tax_configuration_tax_app,
+):
+ # given
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ voucher = voucher_free_shipping
+ webhook = subscription_order_calculate_taxes
+ channel = order.channel
+ shipping_price_amount = order.base_shipping_price_amount
+
+ line_1, line_2 = order.lines.all()
+ line_1_unit_price = line_1.base_unit_price_amount
+ line_2_unit_price = line_2.base_unit_price_amount
+
+ assert voucher.type == VoucherType.SHIPPING
+ order.voucher = voucher
+ order.save(update_fields=["voucher_id"])
+
+ manual_reward = Decimal(10)
+ create_order_discount_for_order(
+ order=order,
+ reason="Manual discount",
+ value_type=DiscountValueType.FIXED,
+ value=manual_reward,
+ type=DiscountType.MANUAL,
+ )
+
+ # Since shipping is free, whole manual discount should be applied to subtotal
+ subtotal_manual_reward_portion = manual_reward
+
+ manager = Mock(get_taxes_for_order=Mock(return_value={}))
+
+ # when
+ fetch_order_prices_if_expired(order, manager, None, True)
+ deliveries = create_delivery_for_subscription_sync_event(
+ WebhookEventSyncType.ORDER_CALCULATE_TAXES, order, webhook
+ )
+
+ # then
+ manual_discount = order.discounts.get(type=DiscountType.MANUAL)
+ assert manual_discount.amount_value == subtotal_manual_reward_portion
+ voucher_discount = order.discounts.get(type=DiscountType.VOUCHER)
+ assert voucher_discount.amount_value == shipping_price_amount
+
+ assert json.loads(deliveries.payload.get_payload()) == {
+ "__typename": "CalculateTaxes",
+ "taxBase": {
+ "address": {"id": to_global_id_or_none(order.shipping_address)},
+ "currency": "USD",
+ "discounts": [
+ {
+ "amount": {"amount": subtotal_manual_reward_portion},
+ "type": TaxableObjectDiscountType.SUBTOTAL,
+ },
+ ],
+ "channel": {"id": to_global_id_or_none(channel)},
+ "lines": [
+ {
+ "chargeTaxes": True,
+ "productName": line_1.product_name,
+ "productSku": line_1.product_sku,
+ "quantity": line_1.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_1),
+ },
+ "totalPrice": {"amount": line_1_unit_price * line_1.quantity},
+ "unitPrice": {"amount": line_1_unit_price},
+ "variantName": line_1.variant_name,
+ },
+ {
+ "chargeTaxes": True,
+ "productName": line_2.product_name,
+ "productSku": line_2.product_sku,
+ "quantity": line_2.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_2),
+ },
+ "totalPrice": {"amount": line_2_unit_price * line_2.quantity},
+ "unitPrice": {"amount": line_2_unit_price},
+ "variantName": line_2.variant_name,
+ },
+ ],
+ "pricesEnteredWithTax": False,
+ "shippingPrice": {"amount": 0},
+ "sourceObject": {"__typename": "Order", "id": to_global_id_or_none(order)},
+ },
+ }
+
+
+def test_order_calculate_taxes_free_shipping_voucher_and_manual_discount_percentage(
+ order_with_lines,
+ voucher_free_shipping,
+ subscription_order_calculate_taxes,
+ tax_configuration_tax_app,
+):
+ # given
+ order = order_with_lines
+ order.status = OrderStatus.UNCONFIRMED
+ voucher = voucher_free_shipping
+ webhook = subscription_order_calculate_taxes
+ channel = order.channel
+ shipping_price_amount = order.base_shipping_price_amount
+
+ line_1, line_2 = order.lines.all()
+ line_1_unit_price = line_1.base_unit_price_amount
+ line_2_unit_price = line_2.base_unit_price_amount
+
+ subtotal_amount = (
+ line_1_unit_price * line_1.quantity + line_2_unit_price * line_2.quantity
+ )
+ total_amount = subtotal_amount + shipping_price_amount
+
+ assert voucher.type == VoucherType.SHIPPING
+ order.voucher = voucher
+ order.save(update_fields=["voucher_id"])
+ total_amount -= shipping_price_amount
+
+ manual_reward = Decimal(10)
+ create_order_discount_for_order(
+ order=order,
+ reason="Manual discount",
+ value_type=DiscountValueType.PERCENTAGE,
+ value=manual_reward,
+ type=DiscountType.MANUAL,
+ )
+
+ # Since shipping is free, whole manual discount should be applied to subtotal
+ subtotal_manual_reward_portion = manual_reward / 100 * total_amount
+
+ manager = Mock(get_taxes_for_order=Mock(return_value={}))
+
+ # when
+ fetch_order_prices_if_expired(order, manager, None, True)
+ deliveries = create_delivery_for_subscription_sync_event(
+ WebhookEventSyncType.ORDER_CALCULATE_TAXES, order, webhook
+ )
+
+ # then
+ manual_discount = order.discounts.get(type=DiscountType.MANUAL)
+ assert manual_discount.amount_value == subtotal_manual_reward_portion
+ voucher_discount = order.discounts.get(type=DiscountType.VOUCHER)
+ assert voucher_discount.amount_value == shipping_price_amount
+
+ assert json.loads(deliveries.payload.get_payload()) == {
+ "__typename": "CalculateTaxes",
+ "taxBase": {
+ "address": {"id": to_global_id_or_none(order.shipping_address)},
+ "currency": "USD",
+ "discounts": [
+ {
+ "amount": {"amount": subtotal_manual_reward_portion},
+ "type": TaxableObjectDiscountType.SUBTOTAL,
+ },
+ ],
+ "channel": {"id": to_global_id_or_none(channel)},
+ "lines": [
+ {
+ "chargeTaxes": True,
+ "productName": line_1.product_name,
+ "productSku": line_1.product_sku,
+ "quantity": line_1.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_1),
+ },
+ "totalPrice": {"amount": line_1_unit_price * line_1.quantity},
+ "unitPrice": {"amount": line_1_unit_price},
+ "variantName": line_1.variant_name,
+ },
+ {
+ "chargeTaxes": True,
+ "productName": line_2.product_name,
+ "productSku": line_2.product_sku,
+ "quantity": line_2.quantity,
+ "sourceLine": {
+ "__typename": "OrderLine",
+ "id": to_global_id_or_none(line_2),
+ },
+ "totalPrice": {"amount": line_2_unit_price * line_2.quantity},
+ "unitPrice": {"amount": line_2_unit_price},
+ "variantName": line_2.variant_name,
+ },
+ ],
+ "pricesEnteredWithTax": False,
+ "shippingPrice": {"amount": 0},
+ "sourceObject": {"__typename": "Order", "id": to_global_id_or_none(order)},
},
}