Skip to content

Commit

Permalink
Set unit_discount_reason on line when the entire order voucher is app…
Browse files Browse the repository at this point in the history
…lied (#16084)

* Set unit_discount_reason on line when the entire order voucher is applied

* Fix e2e tests
  • Loading branch information
IKarbowiak committed Jun 6, 2024
1 parent f98889f commit 186077d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
35 changes: 29 additions & 6 deletions saleor/checkout/complete_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,22 @@ def _create_line_for_order(
prices_entered_with_tax,
)

voucher_code = None
line_voucher_code = None
if checkout_line_info.voucher:
voucher_code = checkout_line_info.voucher.code
line_voucher_code = checkout_line_info.voucher.code
order_voucher_code = None
if checkout_info.voucher:
order_voucher_code = checkout_info.voucher.code

discount_price = undiscounted_unit_price - unit_price
if prices_entered_with_tax:
discount_amount = discount_price.gross
else:
discount_amount = discount_price.net

unit_discount_reason = None
if voucher_code:
unit_discount_reason = f"Voucher code: {voucher_code}"
unit_discount_reason = _get_unit_discount_reason(
line_voucher_code, order_voucher_code
)

tax_class = None
if product.tax_class_id:
Expand All @@ -344,7 +347,7 @@ def _create_line_for_order(
undiscounted_total_price=undiscounted_total_price, # money field not supported by mypy_django_plugin # noqa: E501
total_price=total_line_price,
tax_rate=tax_rate,
voucher_code=voucher_code,
voucher_code=line_voucher_code,
unit_discount=discount_amount, # money field not supported by mypy_django_plugin # noqa: E501
unit_discount_reason=unit_discount_reason,
unit_discount_value=discount_amount.amount, # we store value as fixed discount
Expand Down Expand Up @@ -385,6 +388,26 @@ def _create_line_for_order(
return line_info


def _get_unit_discount_reason(
line_voucher_code: Optional[str],
order_voucher_code: Optional[str],
) -> Optional[str]:
unit_discount_reason = None
if line_voucher_code:
if unit_discount_reason:
unit_discount_reason += f" & Voucher code: {line_voucher_code}"
else:
unit_discount_reason = f"Voucher code: {line_voucher_code}"
elif order_voucher_code:
if unit_discount_reason:
msg = f" & Entire order voucher code: {order_voucher_code}"
unit_discount_reason += msg
else:
unit_discount_reason = f"Entire order voucher code: {order_voucher_code}"

return unit_discount_reason


def _create_order_line_discounts(
checkout_line_info: "CheckoutLineInfo", order_line: "OrderLine"
) -> list["OrderLineDiscount"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,11 @@ def test_checkout_with_voucher_complete(
order_payment = order.payments.first()
assert order_payment == payment
assert payment.transactions.count() == 1
assert (
order_line.unit_discount_amount
== (discount_amount / checkout_line_quantity).amount
)
assert order_line.unit_discount_reason

code.refresh_from_db()
assert code.used == voucher_used_count + 1
Expand Down Expand Up @@ -1167,7 +1172,9 @@ def test_checkout_with_voucher_complete_product_on_promotion(
assert order_line.sale_id == graphene.Node.to_global_id(
"Promotion", catalogue_promotion_without_rules.id
)
assert order_line.unit_discount_reason == f"Promotion: {order_line.sale_id}"
assert order_line.unit_discount_reason == (
f"Entire order voucher code: {code.code} & Promotion: {order_line.sale_id}"
)

assert checkout_line_quantity == order_line.quantity
assert checkout_line_variant == order_line.variant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,13 @@ def test_checkout_with_voucher_complete(
pk=checkout.pk
).exists(), "Checkout should have been deleted"

order_line = order.lines.first()
assert (
order_line.unit_discount_amount
== (discount_amount / order_line.quantity).amount
)
assert order_line.unit_discount_reason


@pytest.mark.integration
def test_checkout_complete_with_voucher_apply_once_per_order(
Expand Down Expand Up @@ -1956,7 +1963,9 @@ def test_checkout_with_voucher_complete_product_on_sale(
assert order_line.sale_id == graphene.Node.to_global_id(
"Sale", catalogue_promotion_without_rules.old_sale_id
)
assert order_line.unit_discount_reason == f"Sale: {order_line.sale_id}"
assert order_line.unit_discount_reason == (
f"Entire order voucher code: {code.code} & Sale: {order_line.sale_id}"
)

code.refresh_from_db()
assert code.used == voucher_used_count + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def test_checkout_calculate_discount_for_sale_and_voucher_1014(
order_line = order_data["lines"][0]
assert order_line["unitDiscountType"] == "FIXED"
assert order_line["unitPrice"]["gross"]["amount"] == expected_unit_price
assert order_line["unitDiscountReason"] == f"Sale: {sale_id}"
assert order_line["unitDiscountReason"] == (
f"Entire order voucher code: {voucher_code} & Sale: {sale_id}"
)
assert order_data["total"]["gross"]["amount"] == total_gross_amount
assert order_data["subtotal"]["gross"]["amount"] == subtotal_amount
assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,6 @@ def test_checkout_with_promotion_and_voucher_CORE_2107(
assert order_line["undiscountedUnitPrice"]["gross"]["amount"] == float(
product_variant_price
)
assert order_line["unitDiscountReason"] == f"Promotion: {promotion_id}"
assert order_line["unitDiscountReason"] == (
f"Entire order voucher code: {voucher_code} & Promotion: {promotion_id}"
)

0 comments on commit 186077d

Please sign in to comment.