Cancel Shopify fulfillment when customer shipment is cancelled

Closes #13954
This commit is contained in:
Cédric Krier
2025-04-02 13:16:08 +02:00
parent 2972cbb7f1
commit 3979c4b89a
2 changed files with 43 additions and 3 deletions
+10
View File
@@ -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
@@ -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'