From 401e673201edcc3ad56286a80fb41bf3c737193a Mon Sep 17 00:00:00 2001 From: bencoronel Date: Tue, 16 Apr 2024 14:07:48 +0200 Subject: [PATCH] [FIX] delivery_deliverea: Generate a deliverea return when sending to shipper --- delivery_deliverea/i18n/es.po | 20 +++++++++++ .../models/deliverea_request.py | 10 ++++++ delivery_deliverea/models/delivery_carrier.py | 28 ++++++++++++--- delivery_deliverea/models/stock_picking.py | 7 ++++ .../tests/test_delivery_deliverea.py | 34 +++++++++++++++++++ .../views/stock_picking_views.xml | 5 +++ 6 files changed, 100 insertions(+), 4 deletions(-) diff --git a/delivery_deliverea/i18n/es.po b/delivery_deliverea/i18n/es.po index d7ca9963ca..b3adf24f9c 100644 --- a/delivery_deliverea/i18n/es.po +++ b/delivery_deliverea/i18n/es.po @@ -183,6 +183,19 @@ msgstr "Centro de distribución Deliverea" msgid "Deliverea Error" msgstr "Error de Deliverea" +#. module: delivery_deliverea +#. odoo-python +#: code:addons/delivery_deliverea/models/delivery_carrier.py:0 +#, python-format +msgid "" +"Deliverea Error:\n" +"\n" +"%s" +msgstr "" +"Error de Deliverea:\n" +"\n" +"%s" + #. module: delivery_deliverea #. odoo-python #: code:addons/delivery_deliverea/models/deliverea_request.py:0 @@ -531,6 +544,13 @@ msgstr "Uuid" msgid "Warehouse delivered" msgstr "Entregado por almacén" +#. module: delivery_deliverea +#. odoo-python +#: code:addons/delivery_deliverea/models/delivery_carrier.py:0 +#, python-format +msgid "References missing in response.\n" +msgstr "Faltan referencias en la respuesta.\n" + #. module: delivery_deliverea #: model:ir.model.fields,help:delivery_deliverea.field_delivery_carrier__deliverea_saturday_delivery msgid "" diff --git a/delivery_deliverea/models/deliverea_request.py b/delivery_deliverea/models/deliverea_request.py index d78eb8c3c8..aa303c41cd 100644 --- a/delivery_deliverea/models/deliverea_request.py +++ b/delivery_deliverea/models/deliverea_request.py @@ -30,6 +30,7 @@ def __init__(self, carrier): "create_return": path + "returns", "get_shipment_tracking": path + "shipments/{delivereaReference}/trackings", "get_return_tracking": path + "returns/{delivereaReference}/trackings", + "get_return_label": path + "returns/{delivereaReference}/label", } def _send_api_request(self, request_type, url, data=None, skip_auth=False): @@ -163,6 +164,15 @@ def get_shipment_label(self, deliverea_reference): ) return res.json() + def get_return_label(self, deliverea_reference): + res = self._send_api_request( + request_type="GET", + url=self.urls["get_return_label"].format( + delivereaReference=deliverea_reference, + ), + ) + return res.json() + def create_return(self, vals): res = self._send_api_request( request_type="POST", url=self.urls["create_return"], data=vals diff --git a/delivery_deliverea/models/delivery_carrier.py b/delivery_deliverea/models/delivery_carrier.py index 7a765d8daa..aefb9be9a6 100644 --- a/delivery_deliverea/models/delivery_carrier.py +++ b/delivery_deliverea/models/delivery_carrier.py @@ -448,22 +448,26 @@ def deliverea_send_shipping(self, pickings): ) return res - def deliverea_get_return_label(self, pickings): - self.deliverea_return_shipping(pickings) + def _deliverea_check_response(self, response): + if not response.get("carrierReference") or not response.get( + "delivereaReference" + ): + error_msg = _("References missing in response.\n") + raise UserError(_("Deliverea Error:\n\n%s") % error_msg) def deliverea_return_shipping(self, pickings): - res = [] deliverea_request = DelivereaRequest(self) for picking in pickings: vals = self._prepare_deliverea_order(picking) response = deliverea_request.create_return(vals) + self._deliverea_check_response(response) picking.write( { "carrier_tracking_ref": response.get("carrierReference", ""), "deliverea_reference": response.get("delivereaReference", ""), } ) - return res + return True def deliverea_cancel_shipment(self, pickings): deliverea_request = DelivereaRequest(self) @@ -495,6 +499,22 @@ def deliverea_get_label(self, pickings): self.deliverea_tracking_state_update(picking) return True + def deliverea_get_return_label(self, pickings): + deliverea_request = DelivereaRequest(self) + for picking in pickings.filtered("deliverea_reference"): + response = deliverea_request.get_return_label(picking.deliverea_reference) + if response: + file_type = response.get("type") + self.env["ir.attachment"].create( + { + "name": "Return_%s.%s" % (picking.name, file_type), + "type": "binary", + "datas": response.get("content"), + "res_model": picking._name, + "res_id": picking.id, + } + ) + def deliverea_get_tracking_link(self, picking): domain = ( self.deliverea_url_tracking diff --git a/delivery_deliverea/models/stock_picking.py b/delivery_deliverea/models/stock_picking.py index f962e210a8..ffc42b4ab4 100644 --- a/delivery_deliverea/models/stock_picking.py +++ b/delivery_deliverea/models/stock_picking.py @@ -19,6 +19,13 @@ def deliverea_get_label(self): return return self.carrier_id.deliverea_get_label(self) + def send_to_shipper(self): + self.ensure_one() + if self.delivery_type == "deliverea": + self.carrier_id.deliverea_return_shipping(self) + self.carrier_id.deliverea_get_return_label(self) + return super().send_to_shipper() + @api.model def deliverea_update_tracking_state(self, data): if data.get("delivereaReference"): diff --git a/delivery_deliverea/tests/test_delivery_deliverea.py b/delivery_deliverea/tests/test_delivery_deliverea.py index 0759bdf461..4f2c77e576 100644 --- a/delivery_deliverea/tests/test_delivery_deliverea.py +++ b/delivery_deliverea/tests/test_delivery_deliverea.py @@ -193,6 +193,7 @@ def _call_side_effect(*args, **kwargs): def test_01_create_order(self): create_shipment_value = { "delivereaReference": "Sbe2fd53e94869d", + "carrierReference": "test", "carrierCode": "dummy", "serviceCode": "standard", "costCenterCode": "DEFAULT", @@ -290,6 +291,7 @@ def _call_side_effect(*args, **kwargs): def test_04_create_return_and_delete(self): return_value = { "delivereaReference": "Sbe2fd53e94869d", + "carrierReference": "test", "carrierCode": "dummy", "serviceCode": "standard", "costCenterCode": "DEFAULT", @@ -352,6 +354,7 @@ def test_05_get_tracking_shipment(self): def test_06_create_return(self): return_value = { "delivereaReference": "Sbe2fd53e94869d", + "carrierReference": "test", "carrierCode": "dummy", "serviceCode": "standard", "costCenterCode": "DEFAULT", @@ -423,3 +426,34 @@ def test_11_get_exception(self): return_value = {"Error test": "Error"} with self.assertRaises(UserError): DelivereaRequest._check_error(DelivereaRequest, return_value) + + def test_12_get_exception(self): + return_value = { + "delivereaReference": "Sbe2fd53e94869d", + "carrierCode": "dummy", + "serviceCode": "standard", + "costCenterCode": "DEFAULT", + } + requests_mock_call = mock.MagicMock( + return_value=MockResponse(return_value, code=200) + ) + with self.assertRaises(UserError): + with mock.patch.object(requests, "post", new=requests_mock_call): + self.carrier.deliverea_return_shipping(self.picking) + + def test_13_get_return_label(self): + self.picking.deliverea_reference = "Sbe2fd53e94869d" + return_value = {"type": "zpl", "content": "TEST"} + requests_mock_call = mock.MagicMock( + return_value=MockResponse(return_value, code=200) + ) + with mock.patch.object(requests, "get", new=requests_mock_call): + self.carrier.deliverea_get_return_label(self.picking) + + attachment = self.env["ir.attachment"].search( + [ + ("res_model", "=", "stock.picking"), + ("res_id", "=", self.picking.id), + ] + ) + self.assertTrue(attachment) diff --git a/delivery_deliverea/views/stock_picking_views.xml b/delivery_deliverea/views/stock_picking_views.xml index 890009f562..7c6653c76c 100644 --- a/delivery_deliverea/views/stock_picking_views.xml +++ b/delivery_deliverea/views/stock_picking_views.xml @@ -27,6 +27,11 @@ name="attrs" >{'invisible': ['|','|','|',('carrier_tracking_ref','=',False),('delivery_type','in', ['fixed', 'base_on_rule']),('delivery_type','=',False),('state','not in',('done', 'assigned'))]} + + {'invisible':['|', '|', '|', ('is_return_picking', '=', False),('state', '=', 'done'),('picking_type_code', '!=', 'incoming'),('delivery_type','=', 'deliverea')]} +