From aa9f0cba52113c1596cb0320aa3c0be05948cc9b Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Tue, 20 Sep 2022 19:48:31 +0000 Subject: [PATCH] [IMP] delivery_purolator: add tracking and cancel shipment --- .../models/delivery_purolator.py | 25 +++++++++++++++++-- .../models/purolator_services.py | 6 +++++ delivery_purolator/tests/test_purolator.py | 4 +++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/delivery_purolator/models/delivery_purolator.py b/delivery_purolator/models/delivery_purolator.py index 4b1aca91..4aab75b9 100644 --- a/delivery_purolator/models/delivery_purolator.py +++ b/delivery_purolator/models/delivery_purolator.py @@ -339,5 +339,26 @@ class ProviderPurolator(models.Model): return res - # TODO cancel shipment - # TODO track shipment + def purolator_get_tracking_link(self, pickings): + res = [] + for picking in pickings: + ref = picking.carrier_tracking_ref + res = res + ['https://www.purolator.com/en/shipping/tracker?pins=%s' % ref] + return res + + def purolator_cancel_shipment(self, picking, packages=None): + service = self._purolator_service() + if packages: + for package in packages: + tracking_pin = package.carrier_tracking_ref + void_res = service.shipment_void(tracking_pin) + self._purolator_format_errors(void_res, raise_class=UserError) + package.write({'carrier_tracking_ref': ''}) + picking.message_post(body=_('Package N° %s has been cancelled' % tracking_pin)) + else: + tracking_pin = picking.carrier_tracking_ref + void_res = service.shipment_void(tracking_pin) + self._purolator_format_errors(void_res, raise_class=UserError) + picking.message_post(body=_('Shipment N° %s has been cancelled' % tracking_pin)) + picking.write({'carrier_tracking_ref': '', + 'carrier_price': 0.0}) diff --git a/delivery_purolator/models/purolator_services.py b/delivery_purolator/models/purolator_services.py index 417a0823..109ba771 100644 --- a/delivery_purolator/models/purolator_services.py +++ b/delivery_purolator/models/purolator_services.py @@ -275,6 +275,12 @@ class PurolatorClient(object): ) return response.body + def shipment_void(self, pin): + response = self.shipping_client.service.VoidShipment( + PIN={'Value': pin} + ) + return response.body + def document_by_pin(self, pin, document_type='', output_type='ZPL'): # TODO document_type? document_criterium = self.shipping_documents_factory.ArrayOfDocumentCriteria() diff --git a/delivery_purolator/tests/test_purolator.py b/delivery_purolator/tests/test_purolator.py index c5bd9f2d..47d39f44 100644 --- a/delivery_purolator/tests/test_purolator.py +++ b/delivery_purolator/tests/test_purolator.py @@ -132,3 +132,7 @@ class TestPurolator(TransactionCase): picking.send_to_shipper() self.assertTrue(picking.carrier_tracking_ref) self.assertEqual(picking.message_attachment_count, 1) # has tracking label now + + # Void + picking.cancel_shipment() + self.assertFalse(picking.carrier_tracking_ref)