Skip to content

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

Fix failing avalara when voucher with apply_once_per_order settings is used (#9959)

Iga Karbowiakfad5cdf464c0
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index 230e70ae2e..9a290e4d74 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -463,6 +463,15 @@ def generate_request_data_from_checkout(
):
address = checkout_info.shipping_address or checkout_info.billing_address
lines = get_checkout_lines_data(checkout_info, lines_info, config, discounts)
+ voucher = checkout_info.voucher
+ # for apply_once_per_order vouchers the discount is already applied on lines
+ discount_amount = (
+ checkout_info.checkout.discount.amount
+ if voucher
+ and voucher.type == VoucherType.ENTIRE_ORDER
+ and not voucher.apply_once_per_order
+ else 0
+ )
currency = checkout_info.checkout.currency
customer_email = cast(str, checkout_info.get_customer_email())
@@ -472,7 +481,7 @@ def generate_request_data_from_checkout(
transaction_token=transaction_token or str(checkout_info.checkout.token),
address=address.as_data() if address else {},
customer_email=customer_email,
- discount=checkout_info.checkout.discount.amount,
+ discount=discount_amount,
config=config,
currency=currency,
)
diff --git a/saleor/plugins/avatax/tests/cassettes/test_avatax/test_calculate_checkout_total_voucher_on_entire_order_applied_once_per_order.yaml b/saleor/plugins/avatax/tests/cassettes/test_avatax/test_calculate_checkout_total_voucher_on_entire_order_applied_once_per_order.yaml
new file mode 100644
index 0000000000..489c0f4924
--- /dev/null
+++ b/saleor/plugins/avatax/tests/cassettes/test_avatax/test_calculate_checkout_total_voucher_on_entire_order_applied_once_per_order.yaml
@@ -0,0 +1,72 @@
+interactions:
+- request:
+ body: '{"createTransactionModel": {"companyCode": "DEFAULT", "type": "SalesOrder",
+ "lines": [{"quantity": 3, "amount": "29.00", "taxCode": "O9999999", "taxIncluded":
+ true, "itemCode": "123", "discounted": false, "description": "Test product",
+ "ref1": "123"}, {"quantity": 1, "amount": "10.000", "taxCode": "FR000000", "taxIncluded":
+ true, "itemCode": "Shipping", "discounted": false, "description": null}], "code":
+ "c5a96402-e327-4f1b-83c0-a9a725f785cc", "date": "2022-06-09", "customerCode":
+ 0, "discount": null, "addresses": {"shipFrom": {"line1": "Teczowa 7", "line2":
+ null, "city": "Wroclaw", "region": "", "country": "PL", "postalCode": "53-601"},
+ "shipTo": {"line1": "O\u0142awska 10", "line2": "", "city": "WROC\u0141AW",
+ "region": "", "country": "PL", "postalCode": "53-105"}}, "commit": false, "currencyCode":
+ "USD", "email": "[email protected]"}}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate, br
+ Authorization:
+ - Basic Og==
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '846'
+ User-Agent:
+ - python-requests/2.27.1
+ method: POST
+ uri: https://rest.avatax.com/api/v2/transactions/createoradjust
+ response:
+ body:
+ string: '{"id":0,"code":"c5a96402-e327-4f1b-83c0-a9a725f785cc","companyId":242975,"date":"2022-06-09","paymentDate":"2022-06-09","status":"Temporary","type":"SalesOrder","batchCode":"","currencyCode":"USD","exchangeRateCurrencyCode":"USD","customerUsageType":"","entityUseCode":"","customerVendorCode":"0","customerCode":"0","exemptNo":"","reconciled":false,"locationCode":"","reportingLocationCode":"","purchaseOrderNo":"","referenceCode":"","salespersonCode":"","totalAmount":31.71,"totalExempt":0.0,"totalDiscount":0.0,"totalTax":7.29,"totalTaxable":31.71,"totalTaxCalculated":7.29,"adjustmentReason":"NotAdjusted","locked":false,"version":1,"exchangeRateEffectiveDate":"2022-06-09","exchangeRate":1.0,"email":"[email protected]","modifiedDate":"2022-06-09T11:04:15.2339743Z","modifiedUserId":283192,"taxDate":"2022-06-09","lines":[{"id":0,"transactionId":0,"lineNumber":"1","customerUsageType":"","entityUseCode":"","description":"Test
+ product","discountAmount":0.0,"exemptAmount":0.0,"exemptCertId":0,"exemptNo":"","isItemTaxable":true,"itemCode":"123","lineAmount":23.5800,"quantity":3.0,"ref1":"123","ref2":"","reportingDate":"2022-06-09","tax":5.42,"taxableAmount":23.58,"taxCalculated":5.42,"taxCode":"O9999999","taxCodeId":5340,"taxDate":"2022-06-09","taxIncluded":true,"details":[{"id":0,"transactionLineId":0,"transactionId":0,"country":"PL","region":"PL","exemptAmount":0.0,"jurisCode":"PL","jurisName":"POLAND","stateAssignedNo":"","jurisType":"CNT","jurisdictionType":"Country","nonTaxableAmount":0.0,"rate":0.230000,"tax":5.42,"taxableAmount":23.58,"taxType":"Output","taxSubTypeId":"O","taxName":"Standard
+ Rate","taxAuthorityTypeId":45,"taxCalculated":5.42,"rateType":"Standard","rateTypeCode":"S","unitOfBasis":"PerCurrencyUnit","isNonPassThru":false,"isFee":false,"reportingTaxableUnits":23.58,"reportingNonTaxableUnits":0.0,"reportingExemptUnits":0.0,"reportingTax":5.42,"reportingTaxCalculated":5.42,"liabilityType":"Seller"}],"nonPassthroughDetails":[],"hsCode":"","costInsuranceFreight":0.0,"vatCode":"PLS-230CPL","vatNumberTypeId":0},{"id":0,"transactionId":0,"lineNumber":"2","customerUsageType":"","entityUseCode":"","discountAmount":0.0,"exemptAmount":0.0,"exemptCertId":0,"exemptNo":"","isItemTaxable":true,"itemCode":"Shipping","lineAmount":8.1300,"quantity":1.0,"ref1":"","ref2":"","reportingDate":"2022-06-09","tax":1.87,"taxableAmount":8.13,"taxCalculated":1.87,"taxCode":"FR000000","taxCodeId":4779,"taxDate":"2022-06-09","taxIncluded":true,"details":[{"id":0,"transactionLineId":0,"transactionId":0,"country":"PL","region":"PL","exemptAmount":0.0,"jurisCode":"PL","jurisName":"POLAND","stateAssignedNo":"","jurisType":"CNT","jurisdictionType":"Country","nonTaxableAmount":0.0,"rate":0.230000,"tax":1.87,"taxableAmount":8.13,"taxType":"Output","taxSubTypeId":"O","taxName":"Standard
+ Rate","taxAuthorityTypeId":45,"taxCalculated":1.87,"rateType":"Standard","rateTypeCode":"S","unitOfBasis":"PerCurrencyUnit","isNonPassThru":false,"isFee":false,"reportingTaxableUnits":8.13,"reportingNonTaxableUnits":0.0,"reportingExemptUnits":0.0,"reportingTax":1.87,"reportingTaxCalculated":1.87,"liabilityType":"Seller"}],"nonPassthroughDetails":[],"hsCode":"","costInsuranceFreight":0.0,"vatCode":"PLS-230FPL","vatNumberTypeId":0}],"addresses":[{"id":0,"transactionId":0,"boundaryLevel":"Zip5","line1":"Olawska
+ 10","line2":"","line3":"","city":"WROCLAW","region":"","postalCode":"53-105","country":"PL","taxRegionId":205102,"latitude":"","longitude":""},{"id":0,"transactionId":0,"boundaryLevel":"Zip5","line1":"Teczowa
+ 7","line2":"","line3":"","city":"Wroclaw","region":"","postalCode":"53-601","country":"PL","taxRegionId":205102,"latitude":"","longitude":""}],"summary":[{"country":"PL","region":"PL","jurisType":"Country","jurisCode":"PL","jurisName":"POLAND","taxAuthorityType":45,"stateAssignedNo":"","taxType":"Output","taxSubType":"O","taxName":"Standard
+ Rate","rateType":"Standard","taxable":31.71,"rate":0.230000,"tax":7.29,"taxCalculated":7.29,"nonTaxable":0.0,"exemption":0.0}]}'
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Thu, 09 Jun 2022 11:04:15 GMT
+ Location:
+ - /api/v2/companies/242975/transactions/0
+ ServerDuration:
+ - '00:00:00.0140140'
+ Transfer-Encoding:
+ - chunked
+ X-Content-Type-Options:
+ - nosniff
+ cache-control:
+ - private, no-cache, no-store
+ referrer-policy:
+ - same-origin
+ strict-transport-security:
+ - max-age=31536000; includeSubdomains
+ x-avalara-uid:
+ - 0ac25b68-5b54-41bd-97d0-36fdf3de30ed
+ x-correlation-id:
+ - 0ac25b68-5b54-41bd-97d0-36fdf3de30ed
+ x-frame-options:
+ - sameorigin
+ x-permitted-cross-domain-policies:
+ - none
+ x-xss-protection:
+ - 1; mode=block
+ status:
+ code: 201
+ message: Created
+version: 1
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 027990aaca..a05c822a57 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -1161,6 +1161,83 @@ def test_calculate_checkout_total_voucher_on_entire_order(
)
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test_calculate_checkout_total_voucher_on_entire_order_applied_once_per_order(
+ checkout_with_item,
+ voucher_percentage,
+ stock,
+ monkeypatch,
+ site_settings,
+ ship_to_pl_address,
+ shipping_zone,
+ address,
+ plugin_configuration,
+):
+ """Ensure that the voucher is applied to entire order the total
+ is equal to the shipping price."""
+ # given
+ plugin_configuration()
+ variant = stock.product_variant
+ monkeypatch.setattr(
+ "saleor.plugins.avatax.plugin.get_cached_tax_codes_or_fetch",
+ lambda _: {"PC040156": "desc"},
+ )
+ manager = get_plugins_manager()
+ site_settings.company_address = address
+ site_settings.save()
+
+ channel = checkout_with_item.channel
+ channel_listing = variant.channel_listings.get(channel=channel)
+ net = (
+ variant.get_price(
+ variant.product, [], checkout_with_item.channel, channel_listing
+ )
+ * checkout_with_item.lines.first().quantity
+ )
+
+ voucher_percentage.apply_once_per_order = True
+ voucher_percentage.save(update_fields=["apply_once_per_order"])
+
+ voucher_listing = voucher_percentage.channel_listings.get(
+ channel=checkout_with_item.channel
+ )
+ discount_value = voucher_listing.discount_value
+
+ shipping_method = shipping_zone.shipping_methods.get()
+ checkout_with_item.shipping_address = ship_to_pl_address
+ checkout_with_item.shipping_method = shipping_method
+ checkout_with_item.voucher_code = voucher_percentage.code
+ checkout_with_item.discount_amount = net.amount
+ checkout_with_item.save()
+
+ shipping_channel_listings = shipping_method.channel_listings.get(channel=channel)
+ shipping_price = shipping_channel_listings.price
+
+ discounts = None
+ checkout_info = fetch_checkout_info(checkout_with_item, [], discounts, manager)
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+
+ # when
+ total = manager.calculate_checkout_total(
+ checkout_info, lines, ship_to_pl_address, discounts
+ )
+
+ # then
+ line = checkout_with_item.lines.first()
+ channel_listing = variant.channel_listings.get(channel=channel)
+ expected_amount = (
+ channel_listing.price.amount * ((100 - discount_value) / 100) # discounted line
+ + ((line.quantity - 1) * channel_listing.price).amount # undiscounted lines
+ + shipping_price.amount # shipping price
+ )
+ total = quantize_price(total, total.currency)
+ assert total == TaxedMoney(
+ net=quantize_price(Money(expected_amount / Decimal(1.23), "USD"), "USD"),
+ gross=quantize_price(Money(expected_amount, "USD"), "USD"),
+ )
+
+
@pytest.mark.vcr
@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
def test_calculate_checkout_total_voucher_on_entire_order_product_without_taxes(