From 3979c4b89ada2b70ebc2363e1f7c4c66f141a92a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 2 Apr 2025 13:16:08 +0200 Subject: [PATCH] Cancel Shopify fulfillment when customer shipment is cancelled Closes #13954 --- modules/web_shop_shopify/sale.py | 10 ++++++ .../tests/scenario_web_shop_shopify.rst | 36 +++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/modules/web_shop_shopify/sale.py b/modules/web_shop_shopify/sale.py index e8c06b1477..79446a08e0 100644 --- a/modules/web_shop_shopify/sale.py +++ b/modules/web_shop_shopify/sale.py @@ -6,6 +6,7 @@ from itertools import zip_longest import dateutil import shopify +from shopify.resources.fulfillment import FulfillmentV2 from trytond.i18n import gettext from trytond.model import ModelView, Unique, fields @@ -216,6 +217,15 @@ class Sale(IdentifierMixin, metaclass=PoolMeta): fulfillment.errors.full_messages()))) shipment.set_shopify_identifier(self, fulfillment.id) Transaction().commit() + elif shipment.state == 'cancelled': + fulfillment_id = shipment.get_shopify_identifier(self) + if fulfillment_id: + fulfillment = shopify.Fulfillment.find( + fulfillment_id, order_id=self.shopify_identifier) + if fulfillment.status != 'cancelled': + fulfillment = FulfillmentV2() + fulfillment.id = fulfillment_id + fulfillment.post('cancel') # TODO: manage drop shipment diff --git a/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst b/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst index 67c51d6bde..2f3d5ba8f0 100644 --- a/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst +++ b/modules/web_shop_shopify/tests/scenario_web_shop_shopify.rst @@ -553,9 +553,39 @@ Make a partial shipment:: >>> order.financial_status 'paid' -Cancel remaining shipment:: +Ship and cancel remaining shipment:: >>> shipment, = [s for s in sale.shipments if s.state != 'done'] + >>> shipment.click('pick') + >>> shipment.click('pack') + >>> shipment.click('ship') + >>> shipment.state + 'shipped' + + >>> order.reload() + >>> order.fulfillment_status + 'fulfilled' + >>> len(order.fulfillments) + 2 + + >>> shipment.click('cancel') + >>> shipment.state + 'cancelled' + + >>> order.reload() + >>> order.fulfillment_status + 'partial' + >>> len(order.fulfillments) + 2 + + >>> shipment_exception = sale.click('handle_shipment_exception') + >>> shipment_exception.form.recreate_moves.extend( + ... shipment_exception.form.ignore_moves.find()) + >>> shipment_exception.execute('handle') + +Cancel remaining shipment:: + + >>> shipment, = [s for s in sale.shipments if s.state not in {'done', 'cancelled'}] >>> shipment.click('cancel') >>> shipment.state 'cancelled' @@ -570,7 +600,7 @@ Cancel remaining shipment:: >>> order.fulfillment_status 'partial' >>> len(order.fulfillments) - 1 + 2 >>> order.financial_status 'paid' @@ -585,7 +615,7 @@ Ignore shipment exception:: >>> order.fulfillment_status 'fulfilled' >>> len(order.fulfillments) - 1 + 2 >>> order.financial_status 'partially_refunded'