Skip to content

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

Fix order shipping Avatax tax calculations for JPY currency (#12235)

Iga Karbowiak41ef02c57ae4
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index 473778fcda..20401629af 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -580,12 +580,7 @@ class AvataxPlugin(BasePlugin):
/ quantity,
)
- def _calculate_order_shipping(
- self, order, taxes_data, previous_value
- ) -> TaxedMoney:
- if taxes_data is None:
- return previous_value
-
+ def _calculate_order_shipping(self, order, taxes_data) -> TaxedMoney:
prices_entered_with_tax = partial(_get_prices_entered_with_tax_for_order, order)
currency = taxes_data.get("currencyCode")
for line in taxes_data.get("lines", []):
@@ -594,7 +589,7 @@ class AvataxPlugin(BasePlugin):
discount_amount = Decimal(line.get("discountAmount", 0.0))
net = Decimal(line.get("lineAmount", 0.0)) - discount_amount
if currency == "JPY" and prices_entered_with_tax():
- gross = previous_value.gross
+ gross = order.base_shipping_price
net = Money(amount=gross.amount - tax, currency=currency)
else:
gross = Money(amount=net + tax, currency=currency)
@@ -611,7 +606,9 @@ class AvataxPlugin(BasePlugin):
self, order: "Order", previous_value: TaxedMoney
) -> TaxedMoney:
taxes_data = self._get_order_tax_data(order, previous_value)
- return self._calculate_order_shipping(order, taxes_data, previous_value)
+ if taxes_data is None:
+ return previous_value
+ return self._calculate_order_shipping(order, taxes_data)
def calculate_order_total(
self,
@@ -641,10 +638,9 @@ class AvataxPlugin(BasePlugin):
).price_with_discounts
taxed_subtotal += taxed_line_total_data
- base_shipping_price = order.base_shipping_price
- shipping_price = self._calculate_order_shipping(
- order, taxes_data, base_shipping_price
- )
+ shipping_price = order.base_shipping_price
+ if taxes_data is not None:
+ shipping_price = self._calculate_order_shipping(order, taxes_data)
taxed_total = taxed_subtotal + shipping_price
diff --git a/saleor/plugins/avatax/tests/cassettes/test_avatax/test_calculate_order_total_for_JPY.yaml b/saleor/plugins/avatax/tests/cassettes/test_avatax/test_calculate_order_total_for_JPY.yaml
new file mode 100644
index 0000000000..c268bdab0b
--- /dev/null
+++ b/saleor/plugins/avatax/tests/cassettes/test_avatax/test_calculate_order_total_for_JPY.yaml
@@ -0,0 +1,74 @@
+interactions:
+- request:
+ body: '{"createTransactionModel": {"companyCode": "DEFAULT", "type": "SalesInvoice",
+ "lines": [{"quantity": 3, "amount": "3600.000", "taxCode": "O9999999", "taxIncluded":
+ true, "itemCode": "123", "discounted": false, "description": "Test product"},
+ {"quantity": 1, "amount": "700.000", "taxCode": "FR000000", "taxIncluded": true,
+ "itemCode": "Shipping", "discounted": false, "description": null}], "code":
+ "acd43357-0b7e-4f75-8e50-4847acce9dfd", "date": "2023-03-06", "customerCode":
+ 0, "discount": null, "addresses": {"shipFrom": {"line1": "Teczowa 7", "line2":
+ "", "city": "Wroclaw", "region": "", "country": "PL", "postalCode": "53-601"},
+ "shipTo": {"line1": "T\u0119czowa 7", "line2": "", "city": "WROC\u0141AW", "region":
+ "", "country": "PL", "postalCode": "53-601"}}, "commit": false, "currencyCode":
+ "JPY", "email": "[email protected]"}}'
+ headers:
+ Accept:
+ - '*/*'
+ Accept-Encoding:
+ - gzip, deflate, br
+ Authorization:
+ - Basic Og==
+ Connection:
+ - keep-alive
+ Content-Length:
+ - '836'
+ User-Agent:
+ - python-requests/2.28.2
+ method: POST
+ uri: https://sandbox-rest.avatax.com/api/v2/transactions/createoradjust
+ response:
+ body:
+ string: '{"id":85020701818896,"code":"acd43357-0b7e-4f75-8e50-4847acce9dfd","companyId":7799660,"date":"2023-03-06","status":"Saved","type":"SalesInvoice","batchCode":"","currencyCode":"JPY","exchangeRateCurrencyCode":"JPY","customerUsageType":"","entityUseCode":"","customerVendorCode":"0","customerCode":"0","exemptNo":"","reconciled":false,"locationCode":"","reportingLocationCode":"","purchaseOrderNo":"","referenceCode":"","salespersonCode":"","taxOverrideType":"None","taxOverrideAmount":0.0,"taxOverrideReason":"","totalAmount":3496.11,"totalExempt":0.17,"totalDiscount":0.0,"totalTax":803.0,"totalTaxable":3495.94,"totalTaxCalculated":803.0,"adjustmentReason":"NotAdjusted","adjustmentDescription":"","locked":false,"region":"","country":"PL","version":1,"softwareVersion":"23.2.3.0","originAddressId":85020701818898,"destinationAddressId":85020701818897,"exchangeRateEffectiveDate":"2023-03-06","exchangeRate":1.0,"description":"","email":"[email protected]","businessIdentificationNo":"","modifiedDate":"2023-03-06T10:49:16.1930736Z","modifiedUserId":6479978,"taxDate":"2023-03-06","lines":[{"id":85020701818902,"transactionId":85020701818896,"lineNumber":"1","boundaryOverrideId":0,"customerUsageType":"","entityUseCode":"","description":"Test
+ product","destinationAddressId":85020701818897,"originAddressId":85020701818898,"discountAmount":0.0,"discountTypeId":0,"exemptAmount":0.17,"exemptCertId":0,"exemptNo":"","isItemTaxable":true,"isSSTP":false,"itemCode":"123","lineAmount":2927.0000,"quantity":3.0,"ref1":"","ref2":"","reportingDate":"2023-03-06","revAccount":"","sourcing":"Destination","tax":673.0,"taxableAmount":2926.83,"taxCalculated":673.0,"taxCode":"O9999999","taxCodeId":9111,"taxDate":"2023-03-06","taxEngine":"","taxOverrideType":"None","businessIdentificationNo":"","taxOverrideAmount":0.0,"taxOverrideReason":"","taxIncluded":true,"details":[{"id":85020701818923,"transactionLineId":85020701818902,"transactionId":85020701818896,"addressId":85020701818897,"country":"PL","region":"PL","countyFIPS":"","stateFIPS":"","exemptAmount":0.0000,"exemptReasonId":4,"inState":true,"jurisCode":"PL","jurisName":"POLAND","jurisdictionId":200102,"signatureCode":"","stateAssignedNo":"","jurisType":"CNT","jurisdictionType":"Country","nonTaxableAmount":0.0000,"nonTaxableRuleId":0,"nonTaxableType":"RateRule","rate":0.230000,"rateRuleId":411502,"rateSourceId":0,"serCode":"","sourcing":"Destination","tax":673.1700,"taxableAmount":2926.8300,"taxType":"Output","taxSubTypeId":"O","taxTypeGroupId":"InputAndOutput","taxName":"Standard
+ Rate","taxAuthorityTypeId":45,"taxRegionId":205102,"taxCalculated":673.1700,"taxOverride":0.0000,"rateType":"Standard","rateTypeCode":"S","taxableUnits":2926.8300,"nonTaxableUnits":0.0000,"exemptUnits":0.0000,"unitOfBasis":"PerCurrencyUnit","isNonPassThru":false,"isFee":false,"reportingTaxableUnits":2926.83,"reportingNonTaxableUnits":0.0,"reportingExemptUnits":0.0,"reportingTax":673.17,"reportingTaxCalculated":673.17,"liabilityType":"Seller"}],"nonPassthroughDetails":[],"lineLocationTypes":[{"documentLineLocationTypeId":85020701818905,"documentLineId":85020701818902,"documentAddressId":85020701818898,"locationTypeCode":"ShipFrom"},{"documentLineLocationTypeId":85020701818906,"documentLineId":85020701818902,"documentAddressId":85020701818897,"locationTypeCode":"ShipTo"}],"parameters":[{"name":"Transport","value":"None"},{"name":"IsMarketplace","value":"False"},{"name":"IsTriangulation","value":"false"},{"name":"IsGoodsSecondHand","value":"false"}],"hsCode":"","costInsuranceFreight":0.0,"vatCode":"PLS-230C","vatNumberTypeId":0},{"id":85020701818903,"transactionId":85020701818896,"lineNumber":"2","boundaryOverrideId":0,"customerUsageType":"","entityUseCode":"","description":"","destinationAddressId":85020701818897,"originAddressId":85020701818898,"discountAmount":0.0,"discountTypeId":0,"exemptAmount":0.0,"exemptCertId":0,"exemptNo":"","isItemTaxable":true,"isSSTP":false,"itemCode":"Shipping","lineAmount":569.1100,"quantity":1.0,"ref1":"","ref2":"","reportingDate":"2023-03-06","revAccount":"","sourcing":"Destination","tax":130.0,"taxableAmount":569.11,"taxCalculated":130.0,"taxCode":"FR000000","taxCodeId":8550,"taxDate":"2023-03-06","taxEngine":"","taxOverrideType":"None","businessIdentificationNo":"","taxOverrideAmount":0.0,"taxOverrideReason":"","taxIncluded":true,"details":[{"id":85020701818943,"transactionLineId":85020701818903,"transactionId":85020701818896,"addressId":85020701818897,"country":"PL","region":"PL","countyFIPS":"","stateFIPS":"","exemptAmount":0.0000,"exemptReasonId":4,"inState":true,"jurisCode":"PL","jurisName":"POLAND","jurisdictionId":200102,"signatureCode":"","stateAssignedNo":"","jurisType":"CNT","jurisdictionType":"Country","nonTaxableAmount":0.0000,"nonTaxableRuleId":0,"nonTaxableType":"RateRule","rate":0.230000,"rateRuleId":411502,"rateSourceId":0,"serCode":"","sourcing":"Destination","tax":130.8900,"taxableAmount":569.1100,"taxType":"Output","taxSubTypeId":"O","taxTypeGroupId":"InputAndOutput","taxName":"Standard
+ Rate","taxAuthorityTypeId":45,"taxRegionId":205102,"taxCalculated":130.8900,"taxOverride":0.0000,"rateType":"Standard","rateTypeCode":"S","taxableUnits":569.1100,"nonTaxableUnits":0.0000,"exemptUnits":0.0000,"unitOfBasis":"PerCurrencyUnit","isNonPassThru":false,"isFee":false,"reportingTaxableUnits":569.11,"reportingNonTaxableUnits":0.0,"reportingExemptUnits":0.0,"reportingTax":130.89,"reportingTaxCalculated":130.89,"liabilityType":"Seller"}],"nonPassthroughDetails":[],"lineLocationTypes":[{"documentLineLocationTypeId":85020701818925,"documentLineId":85020701818903,"documentAddressId":85020701818898,"locationTypeCode":"ShipFrom"},{"documentLineLocationTypeId":85020701818926,"documentLineId":85020701818903,"documentAddressId":85020701818897,"locationTypeCode":"ShipTo"}],"parameters":[{"name":"Transport","value":"None"},{"name":"IsMarketplace","value":"False"},{"name":"IsTriangulation","value":"false"},{"name":"IsGoodsSecondHand","value":"false"}],"hsCode":"","costInsuranceFreight":0.0,"vatCode":"PLS-230D","vatNumberTypeId":0}],"addresses":[{"id":85020701818897,"transactionId":85020701818896,"boundaryLevel":"Zip5","line1":"Teczowa
+ 7","line2":"","line3":"","city":"WROCLAW","region":"","postalCode":"53-601","country":"PL","taxRegionId":205102},{"id":85020701818898,"transactionId":85020701818896,"boundaryLevel":"Zip5","line1":"Teczowa
+ 7","line2":"","line3":"","city":"Wroclaw","region":"","postalCode":"53-601","country":"PL","taxRegionId":205102}],"locationTypes":[{"documentLocationTypeId":85020701818900,"documentId":85020701818896,"documentAddressId":85020701818898,"locationTypeCode":"ShipFrom"},{"documentLocationTypeId":85020701818901,"documentId":85020701818896,"documentAddressId":85020701818897,"locationTypeCode":"ShipTo"}],"summary":[{"country":"PL","region":"PL","jurisType":"Country","jurisCode":"PL","jurisName":"POLAND","taxAuthorityType":45,"stateAssignedNo":"","taxType":"Output","taxSubType":"O","taxName":"Standard
+ Rate","rateType":"Standard","taxable":3495.94,"rate":0.230000,"tax":804.06,"taxCalculated":804.06,"nonTaxable":0.00,"exemption":0.00}]}'
+ headers:
+ Connection:
+ - keep-alive
+ Content-Type:
+ - application/json; charset=utf-8
+ Date:
+ - Mon, 06 Mar 2023 10:49:16 GMT
+ Location:
+ - /api/v2/companies/7799660/transactions/85020701818896
+ ServerDuration:
+ - '00:00:00.0447832'
+ 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:
+ - 6edeb815-87c2-4530-bda8-fee6389b8812
+ x-correlation-id:
+ - 6edeb815-87c2-4530-bda8-fee6389b8812
+ 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 da464457af..8f2cbe7e55 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -1877,6 +1877,36 @@ def test_calculate_order_total(
assert price == TaxedMoney(net=Money("38.13", "USD"), gross=Money("46.90", "USD"))
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test_calculate_order_total_for_JPY(
+ order_line_JPY,
+ shipping_zone_JPY,
+ channel_JPY,
+ site_settings,
+ address,
+ plugin_configuration,
+):
+ # given
+ plugin_configuration(channel=channel_JPY)
+ manager = get_plugins_manager()
+ order = order_line_JPY.order
+ method = shipping_zone_JPY.shipping_methods.get()
+ order.shipping_address = order.billing_address.get_copy()
+ order_set_shipping_method(order, method)
+ order.save()
+
+ site_settings.company_address = address
+ site_settings.save()
+
+ # when
+ price = manager.calculate_order_total(order, order.lines.all())
+
+ # then
+ price = quantize_price(price, price.currency)
+ assert price == TaxedMoney(net=Money("3497", "JPY"), gross=Money("4300", "JPY"))
+
+
@pytest.mark.vcr
@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
def test_calculate_order_shipping_entire_order_voucher(