Skip to content

Commit

Permalink
Merge PR #803 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Sep 29, 2024
2 parents a413546 + 401e673 commit e8550c8
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
20 changes: 20 additions & 0 deletions delivery_deliverea/i18n/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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
Expand Down Expand Up @@ -543,6 +556,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 ""
Expand Down
10 changes: 10 additions & 0 deletions delivery_deliverea/models/deliverea_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
28 changes: 24 additions & 4 deletions delivery_deliverea/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions delivery_deliverea/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
34 changes: 34 additions & 0 deletions delivery_deliverea/tests/test_delivery_deliverea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
5 changes: 5 additions & 0 deletions delivery_deliverea/views/stock_picking_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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'))]}</attribute>
</xpath>
<xpath expr="//button[@name='print_return_label']" position="attributes">
<attribute
name="attrs"
>{'invisible':['|', '|', '|', ('is_return_picking', '=', False),('state', '=', 'done'),('picking_type_code', '!=', 'incoming'),('delivery_type','=', 'deliverea')]}</attribute>
</xpath>
</field>
</record>
</odoo>

0 comments on commit e8550c8

Please sign in to comment.