Skip to content

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

Fix issue with checkout complete for free shippng voucher without tax class (#11796)

Iga Karbowiak4f6e08a79985
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index 7af2c8543e..f415cdbcaa 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -49,6 +49,8 @@ DEFAULT_TAX_DESCRIPTION = "Unmapped Other SKU - taxable default"
TAX_CODE_NON_TAXABLE_PRODUCT = "NT"
+SHIPPING_ITEM_CODE = "Shipping"
+
@dataclass
class AvataxConfiguration:
@@ -262,7 +264,7 @@ def append_shipping_to_data(
quantity=1,
amount=shipping_price_amount,
tax_code=shipping_tax_code,
- item_code="Shipping",
+ item_code=SHIPPING_ITEM_CODE,
prices_entered_with_tax=prices_entered_with_tax,
discounted=discounted,
)
@@ -515,15 +517,27 @@ def generate_request_data_from_checkout(
)
if not lines:
return {}
- 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.SPECIFIC_PRODUCT
- and not voucher.apply_once_per_order
- else 0
- )
+
+ discount_amount = Decimal("0")
+ if voucher := checkout_info.voucher:
+ # for apply_once_per_order vouchers the discount is already applied on lines
+ applicable_discount = (
+ voucher.type != VoucherType.SPECIFIC_PRODUCT
+ and not voucher.apply_once_per_order
+ )
+
+ if voucher.type == VoucherType.SHIPPING:
+ # when the taxes are not calculated on shipping method, the shipping
+ # discount cannot by applied by plugin
+ applicable_discount = applicable_discount and SHIPPING_ITEM_CODE in {
+ line["itemCode"] for line in lines
+ }
+
+ discount_amount = (
+ checkout_info.checkout.discount.amount
+ if applicable_discount
+ else Decimal("0")
+ )
currency = checkout_info.checkout.currency
customer_email = cast(str, checkout_info.get_customer_email())
diff --git a/saleor/plugins/avatax/tests/cassettes/test_avatax/test_preprocess_order_creation_shipping_voucher_no_tax_class_on_delivery_method.yaml b/saleor/plugins/avatax/tests/cassettes/test_avatax/test_preprocess_order_creation_shipping_voucher_no_tax_class_on_delivery_method.yaml
new file mode 100644
index 0000000000..5cc824bdf4
--- /dev/null
+++ b/saleor/plugins/avatax/tests/cassettes/test_avatax/test_preprocess_order_creation_shipping_voucher_no_tax_class_on_delivery_method.yaml
@@ -0,0 +1,71 @@
+interactions:
+- request:
+ body: '{"createTransactionModel": {"companyCode": "DEFAULT", "type": "SalesOrder",
+ "lines": [{"quantity": 3, "amount": "15.00", "taxCode": "O9999999", "taxIncluded":
+ true, "itemCode": "123", "discounted": false, "description": "Test product",
+ "ref1": "123"}], "code": "8ab4d1d2-13a5-4bb0-8994-c267e13e93f1", "date": "2023-01-23",
+ "customerCode": 0, "discount": null, "addresses": {"shipFrom": {"line1": "Teczowa
+ 7", "line2": "", "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:
+ - '697'
+ User-Agent:
+ - python-requests/2.28.2
+ method: POST
+ uri: https://sandbox-rest.avatax.com/api/v2/transactions/createoradjust
+ response:
+ body:
+ string: '{"id":0,"code":"8ab4d1d2-13a5-4bb0-8994-c267e13e93f1","companyId":7799660,"date":"2023-01-23","paymentDate":"2023-01-23","status":"Temporary","type":"SalesOrder","batchCode":"","currencyCode":"USD","exchangeRateCurrencyCode":"USD","customerUsageType":"","entityUseCode":"","customerVendorCode":"0","customerCode":"0","exemptNo":"","reconciled":false,"locationCode":"","reportingLocationCode":"","purchaseOrderNo":"","referenceCode":"","salespersonCode":"","totalAmount":12.2,"totalExempt":0.0,"totalDiscount":0.0,"totalTax":2.8,"totalTaxable":12.2,"totalTaxCalculated":2.8,"adjustmentReason":"NotAdjusted","locked":false,"version":1,"exchangeRateEffectiveDate":"2023-01-23","exchangeRate":1.0,"email":"[email protected]","modifiedDate":"2023-01-23T14:04:35.140459Z","modifiedUserId":6479978,"taxDate":"2023-01-23","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":12.2000,"quantity":3.0,"ref1":"123","ref2":"","reportingDate":"2023-01-23","tax":2.8,"taxableAmount":12.2,"taxCalculated":2.8,"taxCode":"O9999999","taxCodeId":9111,"taxDate":"2023-01-23","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":2.8,"taxableAmount":12.2,"taxType":"Output","taxSubTypeId":"O","taxName":"Standard
+ Rate","taxAuthorityTypeId":45,"taxCalculated":2.8,"rateType":"Standard","rateTypeCode":"S","unitOfBasis":"PerCurrencyUnit","isNonPassThru":false,"isFee":false,"reportingTaxableUnits":12.2,"reportingNonTaxableUnits":0.0,"reportingExemptUnits":0.0,"reportingTax":2.8,"reportingTaxCalculated":2.8,"liabilityType":"Seller"}],"nonPassthroughDetails":[],"hsCode":"","costInsuranceFreight":0.0,"vatCode":"PLS-230C","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":12.2,"rate":0.230000,"tax":2.8,"taxCalculated":2.8,"nonTaxable":0.0,"exemption":0.0}]}'
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Mon, 23 Jan 2023 14:04:35 GMT
+ Location:
+ - /api/v2/companies/7799660/transactions/0
+ ServerDuration:
+ - '00:00:00.0170292'
+ Transfer-Encoding:
+ - chunked
+ X-Content-Type-Options:
+ - nosniff
+ api-supported-versions:
+ - '2.0'
+ cache-control:
+ - private, no-cache, no-store
+ referrer-policy:
+ - same-origin
+ strict-transport-security:
+ - max-age=31536000; includeSubdomains
+ x-avalara-uid:
+ - 95c1e231-c726-4ecf-975c-559a4ab2e65a
+ x-correlation-id:
+ - 95c1e231-c726-4ecf-975c-559a4ab2e65a
+ 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 296b603081..3d2ae34de6 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -2620,6 +2620,46 @@ def test_preprocess_order_creation_wrong_data(
manager.preprocess_order_creation(checkout_info, discounts, lines)
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test_preprocess_order_creation_shipping_voucher_no_tax_class_on_delivery_method(
+ checkout_with_item,
+ monkeypatch,
+ address,
+ ship_to_pl_address,
+ site_settings,
+ shipping_zone,
+ discount_info,
+ plugin_configuration,
+ voucher_free_shipping,
+):
+ plugin_configuration()
+ 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()
+
+ 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_free_shipping.code
+ checkout_with_item.discount = shipping_method.channel_listings.first().price
+ checkout_with_item.save()
+
+ shipping_method.tax_class.delete()
+ shipping_method.refresh_from_db()
+ assert not shipping_method.tax_class
+
+ discounts = [discount_info]
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, discounts, manager)
+
+ manager.preprocess_order_creation(checkout_info, discounts)
+
+
@pytest.mark.vcr
def test_get_cached_tax_codes_or_fetch(monkeypatch, avatax_config):
monkeypatch.setattr("saleor.plugins.avatax.cache.get", lambda x, y: {})
diff --git a/saleor/site/models.py b/saleor/site/models.py
index 212a374d8d..621bba21b7 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -116,7 +116,7 @@ class SiteSettings(models.Model):
sender_address: Optional[str] = self.default_mail_sender_address
if not sender_address:
- sender_address = settings.DEFAULT_FROM_EMAIL # type: ignore[misc] # circular import # noqa: E501
+ sender_address = settings.DEFAULT_FROM_EMAIL # type: ignore[misc]
if not sender_address:
raise ImproperlyConfigured("No sender email address has been set-up")