Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Saleor 5715 fix breaking calculate order shipping avatax method 3.1 (#9109)
Iga Karbowiakcf8c3764ca49
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.pyindex f4c629b968..d524426be1 100644--- a/saleor/graphql/order/mutations/orders.py+++ b/saleor/graphql/order/mutations/orders.py@@ -370,12 +370,24 @@ class OrderUpdateShipping(EditableOrderValidationMixin, BaseMutation):"postal_code_rules"),)+ shipping_channel_listing = (+ shipping_models.ShippingMethodChannelListing.objects.filter(+ shipping_method=method, channel=order.channel+ ).first()+ )+ if not shipping_channel_listing:+ raise ValidationError(+ {+ "shipping_method": ValidationError(+ "Shipping method not available in the given channel.",+ code=OrderErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.value,+ )+ }+ )+shipping_method_data = convert_to_shipping_method_data(method,- shipping_models.ShippingMethodChannelListing.objects.filter(- shipping_method=method,- channel=order.channel,- ).first(),+ shipping_channel_listing,)clean_order_update_shipping(order, shipping_method_data)diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex 672da78e83..9c7c3cf81b 100644--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@ -2626,17 +2626,12 @@ def test_draft_order_create_invalid_shipping_address(DRAFT_UPDATE_QUERY = """mutation draftUpdate($id: ID!,- $voucher: ID,- $channel: ID,- $customerNote: String+ $input: DraftOrderInput!,) {draftOrderUpdate(id: $id,- input: {- voucher: $voucher,- customerNote: $customerNote- channelId: $channel- }) {+ input: $input+ ) {errors {fieldcode@@ -2664,7 +2659,9 @@ def test_draft_order_update_existing_channel_id(channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)variables = {"id": order_id,- "channel": channel_id,+ "input": {+ "channelId": channel_id,+ },}response = staff_api_client.post_graphql(@@ -2690,7 +2687,9 @@ def test_draft_order_update_voucher_not_available(voucher.channel_listings.all().delete()variables = {"id": order_id,- "voucher": voucher_id,+ "input": {+ "voucher": voucher_id,+ },}response = staff_api_client.post_graphql(@@ -5765,6 +5764,35 @@ def test_order_update_shipping(assert order.shipping_method_name == shipping_method.name[email protected]("status", [OrderStatus.UNCONFIRMED, OrderStatus.DRAFT])+def test_order_update_shipping_no_shipping_method_channel_listings(+ status,+ staff_api_client,+ permission_manage_orders,+ order_with_lines,+ shipping_method,+ staff_user,+):+ order = order_with_lines+ order.status = status+ order.save()+ assert order.shipping_method != shipping_method+ query = ORDER_UPDATE_SHIPPING_QUERY+ order_id = graphene.Node.to_global_id("Order", order.id)+ method_id = graphene.Node.to_global_id("ShippingMethod", shipping_method.id)+ shipping_method.channel_listings.all().delete()+ variables = {"order": order_id, "shippingMethod": method_id}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_orders]+ )+ content = get_graphql_content(response)+ data = content["data"]["orderUpdateShipping"]+ errors = data["errors"]+ assert len(errors) == 1+ assert errors[0]["code"] == OrderErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.name+ assert errors[0]["field"] == "shippingMethod"++def test_order_update_shipping_tax_included(staff_api_client,permission_manage_orders,diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.pyindex 5571d4627a..f9eb7ede58 100644--- a/saleor/plugins/avatax/__init__.py+++ b/saleor/plugins/avatax/__init__.py@@ -149,9 +149,18 @@ def _validate_order(order: "Order") -> bool:shipping_address = order.shipping_addressshipping_required = order.is_shipping_required()address = shipping_address or order.billing_address- return _validate_adddress_details(- shipping_address, shipping_required, address, order.shipping_method+ shipping_method = order.shipping_method+ valid_address_details = _validate_adddress_details(+ shipping_address, shipping_required, address, shipping_method)+ if not valid_address_details:+ return False+ channel_listing = shipping_method.channel_listings.filter( # type: ignore+ channel_id=order.channel_id+ ).first()+ if not channel_listing:+ return False+ return Truedef _validate_checkout(diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.pyindex 454338247d..1033926a07 100644--- a/saleor/plugins/avatax/plugin.py+++ b/saleor/plugins/avatax/plugin.py@@ -657,10 +657,17 @@ class AvataxPlugin(BasePlugin):gross = Money(amount=net + tax, currency=currency)net = Money(amount=net, currency=currency)return TaxedMoney(net=net, gross=gross)++ # Ignore typing checks because it is checked in _validate_order+ channel_listing = order.shipping_method.channel_listings.filter( # type: ignore+ channel_id=order.channel_id+ ).first()+ if not channel_listing:+ return previous_value+ price = channel_listing.pricereturn TaxedMoney(- # Ignore typing checks because it is checked in _validate_order- net=order.shipping_method.price, # type: ignore- gross=order.shipping_method.price, # type: ignore+ net=price,+ gross=price,)def get_tax_rate_type_choices(self, previous_value: Any) -> List[TaxType]:diff --git a/saleor/plugins/avatax/tests/cassettes/test_calculate_order_shipping_zero_shipping_amount.yaml b/saleor/plugins/avatax/tests/cassettes/test_calculate_order_shipping_zero_shipping_amount.yamlnew file mode 100644index 0000000000..881317d2e0--- /dev/null+++ b/saleor/plugins/avatax/tests/cassettes/test_calculate_order_shipping_zero_shipping_amount.yaml@@ -0,0 +1,106 @@+interactions:+- request:+ body: '{"createTransactionModel": {"companyCode": "DEFAULT", "type": "SalesInvoice",+ "lines": [{"quantity": 3, "amount": "36.900", "taxCode": "O9999999", "taxIncluded":+ true, "itemCode": "SKU_A", "description": "Test product"}], "code": "caeb3dd0-d243-4aa0-8eeb-40eac6e313a4",+ "date": "2022-01-28", "customerCode": 0, "addresses": {"shipFrom": {"line1":+ "Teczowa 7", "line2": null, "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": "USD", "email": "[email protected]"}}'+ headers:+ Accept:+ - '*/*'+ Accept-Encoding:+ - gzip, deflate, br+ Authorization:+ - Basic Og==+ Connection:+ - keep-alive+ Content-Length:+ - '651'+ User-Agent:+ - python-requests/2.27.1+ method: POST+ uri: https://rest.avatax.com/api/v2/transactions/createoradjust+ response:+ body:+ string: !!binary |+ i/8BAICqqqrq/3SXs5qB2uZurqd0cI+DAwQsEO4eh7yJq7K5a6aZqoGqmKc5pO9voz9AqHUPc9vd+ +RaNRqJxkXjktkgkmqLu0D1HhWBv/0/ekdlstda60fVuV7U7RTY6kCHLuDXO6cLVbVO0zLrogVvR+ arDdoKkabkmRjdPM4XVyZOq23m07RY4FZKjWdV3oqqh7UpSFZclk6MxPOFIkr7kS1s48Ip/CM3oL+ UnRjsY+DiSAF7JISgn2hceV6PpIirPbB4Y5vFhxEO1O7ZIkT0jXzHZfXXAsS2wXx8rpmZIxK6mx++ EFxMXFRNln+4x4pplo9I0fQkwcZg/QhHZuAxQ9EYLYuPIS4qCdHNMYkP93e65s9Lsg/O+EwOKXE2+ AxKCdVk0zwhtRsq5mSO8fj6RknctpvsjBhDJvf0UlyBkdKk5br/BOQbySLdE4VGctFEHgfWtXTri+ BW1Hn21OJrhz4ZXMptwpkpG98Mq3Ecm4e+H1wKNdRhZcGyTAYfdnyTIhiJ2tfUTZM3TBkYjZI7JN+ fh4svqbbGO3fZPhNvSXcPZ0+FifpRYa+3knREynz5FaKchzkHyf84Kmjui6rsi63jZWY/N2HvXMJ+ OZ8cmb76Eph2fdu13a5V5JDFh+3SoBShamVWHGbx34YBVvwTRxZUxPAZBvFFptoQ1LVaeYyJ/UiG+ BFl+YeVpHlHaOJGi25J9cEty0ufgzy4cR9RN0fnBwxWvLV50bxptdFt2Xb+pttvfpGjC/wAAQFVV+ VfV/ugbwWcNBTM3Mw01vDh6ZAAYZ4OEQ7n7KQ4C6qbiBApipWqqKBphBmrrYdRtvNR8IWmxjmZys+ rKysfPkJ2lRWvkQiJG6pZRh7DX6o5F+j80/P7p459Q5GH+qq0wpi13crDANNWr9S9aoPUJh84Azz+ 9z+8g9lrIqqoaxvdHVoFSTZkO4iPIR+i/RsRUU2666qmUwnou3OZH5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Lzcw4=+ headers:+ Connection:+ - keep-alive+ Content-Encoding:+ - br+ Content-Type:+ - application/json; charset=utf-8+ Date:+ - Fri, 28 Jan 2022 08:30:04 GMT+ Location:+ - /api/v2/companies/242975/transactions/67000030299149+ ServerDuration:+ - '00:00:00.0769519'+ Transfer-Encoding:+ - chunked+ Vary:+ - Accept-Encoding+ X-Content-Type-Options:+ - nosniff+ referrer-policy:+ - same-origin+ strict-transport-security:+ - max-age=31536000; includeSubdomains+ x-avalara-uid:+ - 0727935d-d2c5-4401-9e98-fe98ca09cac7+ x-correlation-id:+ - 0727935d-d2c5-4401-9e98-fe98ca09cac7+ x-frame-options:+ - sameorigin+ x-permitted-cross-domain-policies:+ - none+ x-xss-protection:+ - 1; mode=block+ status:+ code: 201+ message: Created+version: 1diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.pyindex d031cf370d..6d3a54c6af 100644--- a/saleor/plugins/avatax/tests/test_avatax.py+++ b/saleor/plugins/avatax/tests/test_avatax.py@@ -18,7 +18,7 @@ from ....checkout.fetch import ()from ....checkout.utils import add_variant_to_checkoutfrom ....core.prices import quantize_price-from ....core.taxes import TaxError, TaxType+from ....core.taxes import TaxError, TaxType, zero_taxed_moneyfrom ....discount import DiscountValueType, OrderDiscountType, VoucherTypefrom ....order import OrderStatusfrom ....product import ProductTypeKind@@ -1138,6 +1138,55 @@ def test_calculate_order_shipping(assert price == TaxedMoney(net=Money("8.13", "USD"), gross=Money("10.00", "USD"))+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])+def test_calculate_order_shipping_zero_shipping_amount(+ order_line, shipping_zone, site_settings, address, plugin_configuration+):+ plugin_configuration()+ manager = get_plugins_manager()+ order = order_line.order+ method = shipping_zone.shipping_methods.get()+ order.shipping_address = order.billing_address.get_copy()+ order.shipping_method_name = method.name+ order.shipping_method = method+ order.save()++ channel_listing = method.channel_listings.get(channel=order.channel)+ channel_listing.price_amount = 0+ channel_listing.save(update_fields=["price_amount"])++ site_settings.company_address = address+ site_settings.save()++ price = manager.calculate_order_shipping(order)+ price = quantize_price(price, price.currency)+ assert price == TaxedMoney(net=Money("0.00", "USD"), gross=Money("0.00", "USD"))+++@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])+def test_calculate_order_shipping_no_channel_listing(+ order_line, shipping_zone, site_settings, address, plugin_configuration+):+ plugin_configuration()+ manager = get_plugins_manager()+ order = order_line.order+ method = shipping_zone.shipping_methods.get()+ order.shipping_address = order.billing_address.get_copy()+ order.shipping_method_name = method.name+ order.shipping_method = method+ order.save()++ method.channel_listings.all().delete()++ site_settings.company_address = address+ site_settings.save()++ price = manager.calculate_order_shipping(order)+ price = quantize_price(price, price.currency)+ assert price == zero_taxed_money(order.currency)++@pytest.mark.vcr@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])def test_calculate_order_line_unit(diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.pyindex 2fa32f6cbc..4e96997389 100644--- a/saleor/plugins/manager.py+++ b/saleor/plugins/manager.py@@ -276,9 +276,12 @@ class PluginsManager(PaymentInterface):def calculate_order_shipping(self, order: "Order") -> TaxedMoney:if not order.shipping_method:return zero_taxed_money(order.currency)- shipping_price = order.shipping_method.channel_listings.get(+ channel_listing = order.shipping_method.channel_listings.filter(channel_id=order.channel_id- ).price+ ).first()+ if not channel_listing:+ return zero_taxed_money(order.currency)+ shipping_price = channel_listing.pricedefault_value = quantize_price(TaxedMoney(net=shipping_price, gross=shipping_price),shipping_price.currency,