Skip to content

Commit 37aafe1

Browse files
committed
[MIG-IMP] delivery_ups_oca: Migration to 14.0 and oauth
1 parent 2c14c6d commit 37aafe1

16 files changed

+459
-203
lines changed

delivery_ups_oca/README.rst

+37-17
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ Delivery UPS OCA
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:9a05f41a837cc3dfd85e3ca794674ae7e2307fde3ee923412b565c7581842223
10+
!! source digest: sha256:b5553393572f0de609a108deeebcf658e18f2dcf8086d68aa9b8704050f9d7eb
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
13-
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1414
:target: https://odoo-community.org/page/development-status
15-
:alt: Production/Stable
15+
:alt: Beta
1616
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
1717
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1818
:alt: License: AGPL-3
1919
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fdelivery--carrier-lightgray.png?logo=github
20-
:target: https://github.com/OCA/delivery-carrier/tree/13.0/delivery_ups_oca
20+
:target: https://github.com/OCA/delivery-carrier/tree/14.0/delivery_ups_oca
2121
:alt: OCA/delivery-carrier
2222
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23-
:target: https://translation.odoo-community.org/projects/delivery-carrier-13-0/delivery-carrier-13-0-delivery_ups_oca
23+
:target: https://translation.odoo-community.org/projects/delivery-carrier-14-0/delivery-carrier-14-0-delivery_ups_oca
2424
:alt: Translate me on Weblate
2525
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26-
:target: https://runboat.odoo-community.org/builds?repo=OCA/delivery-carrier&target_branch=13.0
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/delivery-carrier&target_branch=14.0
2727
:alt: Try me on Runboat
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -48,22 +48,33 @@ Configuration
4848

4949
To configure this module, you need to:
5050

51-
#. Add a carrier account with delivery type ``ups`` and fill in your credentials
52-
#. Configure in Odoo all required fields of the UPS tab with your account data https://wwwapps.ups.com/ppc/ppc.html (Username WS , Password WS, Access license number, Shipper number, etc)
53-
#. If yo have "Tracking state update sync" checked all delivery orders state check will be done querying UPS services.
54-
55-
**NOTE** You need to request an access key from https://www.ups.com/upsdeveloperkit/manageaccesskeys?loc=es_ES
56-
for using the webservice.
51+
#. Add a carrier account with delivery type ``ups`` and fill in your credentials (UPS
52+
Client and UPS Client Secret)
53+
#. Configure in Odoo all required fields of the UPS tab with your account data
54+
https://wwwapps.ups.com/ppc/ppc.html (Shipper number, Default Packaging, Package
55+
Dimension Code, Package Weight Code and File Format).
56+
#. If yo have "Tracking state update sync" checked all delivery orders state check will
57+
be done querying UPS services.
58+
#. It is possible to create a UPS carrier for cash on delivery parcels. Select the
59+
``ups`` delivery type and check the "Cash on Delivery" checkbox under the "UPS" tab.
60+
It is required to select the "UPS COD Funds Code" when the "Cash on Delivery" option
61+
is selected.
62+
63+
**NOTE** You need to add an APP from https://developer.ups.com/ for using the
64+
webservice.
5765

5866
Usage
5967
=====
6068

6169
You have to set the created shipping method in the delivery order to ship:
6270

63-
* When the picking is 'Transferred', a *Create Shipping Label* button appears. Just click on it, and if all went well, the label will be 'attached'.
64-
* If the shipment creation process fails, a validation error will appear displaying UPS error.
71+
* When the picking is 'Transferred', a *Create Shipping Label* button appears. Just
72+
click on it, and if all went well, the label will be 'attached'.
73+
* If the shipment creation process fails, a validation error will appear displaying UPS
74+
error.
6575
* When the delivery order is cancelled, it's automatically cancelled too in UPS.
66-
* If you have "Tracking state update sync" checked in the shipping method, a periodical state check will be done querying UPS services.
76+
* If you have "Tracking state update sync" checked in the shipping method, a periodical
77+
state check will be done querying UPS services.
6778

6879
Known issues / Roadmap
6980
======================
@@ -77,7 +88,7 @@ Bug Tracker
7788
Bugs are tracked on `GitHub Issues <https://github.com/OCA/delivery-carrier/issues>`_.
7889
In case of trouble, please check there if your issue has already been reported.
7990
If you spotted it first, help us to smash it by providing a detailed and welcomed
80-
`feedback <https://github.com/OCA/delivery-carrier/issues/new?body=module:%20delivery_ups_oca%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
91+
`feedback <https://github.com/OCA/delivery-carrier/issues/new?body=module:%20delivery_ups_oca%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
8192

8293
Do not contact contributors directly about support or help with technical issues.
8394

@@ -89,6 +100,7 @@ Authors
89100

90101
* Hunki Enterprises BV
91102
* Tecnativa
103+
* ForgeFlow
92104

93105
Contributors
94106
~~~~~~~~~~~~
@@ -99,6 +111,14 @@ Contributors
99111
* Víctor Martínez
100112
* Pedro M. Baeza
101113

114+
* `ForgeFlow <https://www.forgeflow.com>`_:
115+
116+
* Jordi Ballester
117+
118+
* `Sygel <https://www.sygel.es>`_:
119+
120+
* Manuel Regidor
121+
102122
Maintainers
103123
~~~~~~~~~~~
104124

@@ -112,6 +132,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
112132
mission is to support the collaborative development of Odoo features and
113133
promote its widespread use.
114134

115-
This module is part of the `OCA/delivery-carrier <https://github.com/OCA/delivery-carrier/tree/13.0/delivery_ups_oca>`_ project on GitHub.
135+
This module is part of the `OCA/delivery-carrier <https://github.com/OCA/delivery-carrier/tree/14.0/delivery_ups_oca>`_ project on GitHub.
116136

117137
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

delivery_ups_oca/__manifest__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
{
55
"name": "Delivery UPS OCA",
66
"summary": "Integrate UPS webservice",
7-
"version": "13.0.2.0.1",
8-
"development_status": "Production/Stable",
7+
"version": "14.0.1.0.0",
8+
"development_status": "Beta",
99
"category": "Delivery",
1010
"website": "https://github.com/OCA/delivery-carrier",
11-
"author": "Hunki Enterprises BV, Tecnativa, Odoo Community Association (OCA)",
11+
"author": "Hunki Enterprises BV, Tecnativa, ForgeFlow, Odoo Community Association (OCA)",
1212
"license": "AGPL-3",
1313
"excludes": ["delivery_ups"],
1414
"depends": [
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

delivery_ups_oca/models/delivery_carrier.py

+75-58
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Copyright 2020 Hunki Enterprises BV
22
# Copyright 2021-2022 Tecnativa - Víctor Martínez
3+
# Copyright 2023 ForgeFlow, S.L. - Jordi Ballester
4+
# Copyright 2024 Sygel - Manuel Regidor
35
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
6+
47
from odoo import fields, models
58

69
from .ups_request import UpsRequest
@@ -9,15 +12,17 @@
912
class DeliveryCarrier(models.Model):
1013
_inherit = "delivery.carrier"
1114

12-
delivery_type = fields.Selection(selection_add=[("ups", "UPS")])
15+
delivery_type = fields.Selection(
16+
selection_add=[("ups", "UPS")],
17+
ondelete={
18+
"ups": lambda recs: recs.write({"delivery_type": "fixed", "fixed_price": 0})
19+
},
20+
)
1321
ups_file_format = fields.Selection(
14-
selection=[("GIF", "PDF"), ("ZPL", "ZPL"), ("EPL", "EPL"), ("SPL", "SPL")],
22+
selection=[("GIF", "GIF"), ("ZPL", "ZPL"), ("EPL", "EPL"), ("SPL", "SPL")],
1523
default="GIF",
1624
string="File format",
1725
)
18-
ups_ws_username = fields.Char(string="Username WS")
19-
ups_ws_password = fields.Char(string="Password WS")
20-
ups_access_license = fields.Char(string="Access license number")
2126
ups_shipper_number = fields.Char(string="Shipper number")
2227
ups_service_code = fields.Selection(
2328
selection=[
@@ -79,11 +84,61 @@ class DeliveryCarrier(models.Model):
7984
"webservice (you will necessary to activate tracking API)",
8085
)
8186
ups_use_packages_from_picking = fields.Boolean(string="Use packages from picking")
87+
ups_client_id = fields.Char()
88+
ups_client_secret = fields.Char()
89+
ups_token = fields.Char()
90+
ups_token_expiration_date = fields.Datetime(readonly=True)
91+
ups_cash_on_delivery = fields.Boolean(string="UPS Cash On Delivery")
92+
ups_cod_funds_code = fields.Selection(
93+
selection=[("1", "Cash"), ("9", "Check/Cashier Check/Money Order")],
94+
string="UPS Cod Funds Code",
95+
)
8296

83-
def ups_test_call(self, order):
97+
def _ups_get_response_price(self, total_charges, currency, company):
98+
"""We need to convert the price if the currency is different."""
99+
price = float(total_charges["MonetaryValue"])
100+
if total_charges["CurrencyCode"] != currency.name:
101+
price = currency._convert(
102+
price,
103+
self.env["res.currency"].search(
104+
[("name", "=", total_charges["CurrencyCode"])]
105+
),
106+
company,
107+
fields.Date.today(),
108+
)
109+
return price
110+
111+
def ups_rate_shipment(self, order):
112+
ups_request = UpsRequest(self)
113+
response = ups_request.rate_shipment(order)
114+
price = self._ups_get_response_price(
115+
response, order.currency_id, order.company_id
116+
)
117+
return {
118+
"success": True,
119+
"price": price,
120+
"error_message": False,
121+
"warning_message": False,
122+
}
123+
124+
def ups_create_shipping(self, picking):
125+
"""Send packages of the picking to UPS
126+
return a list of dicts {'exact_price': 'tracking_number':}
127+
suitable for delivery.carrier#send_shipping"""
84128
self.ensure_one()
85129
ups_request = UpsRequest(self)
86-
return ups_request.test_call(order)
130+
response = ups_request._send_shipping(picking)
131+
extra_price = self._ups_get_response_price(
132+
response["price"], picking.company_id.currency_id, picking.company_id
133+
)
134+
picking.carrier_tracking_ref = response["ShipmentIdentificationNumber"]
135+
# Create label from response
136+
self._create_ups_label(picking, response["labels"])
137+
# Return
138+
return {
139+
"exact_price": extra_price,
140+
"tracking_number": picking.carrier_tracking_ref,
141+
}
87142

88143
def ups_send_shipping(self, pickings):
89144
return [self.ups_create_shipping(p) for p in pickings]
@@ -97,21 +152,21 @@ def _prepare_ups_label_attachment(self, picking, values):
97152
"res_id": picking.id,
98153
}
99154

100-
def _create_ups_label(self, picking, data):
155+
def _create_ups_label(self, picking, labels):
101156
val_list = []
102-
for PackageResult in data["PackageResults"]:
103-
format_code = PackageResult["ShippingLabel"]["ImageFormat"]["Code"].upper()
157+
for label in labels:
158+
format_code = label["format_code"].upper()
104159
attachment_name = "%s-%s.%s" % (
105-
picking.carrier_tracking_ref,
160+
label["tracking_ref"],
161+
format_code,
106162
format_code,
107-
("txt" if format_code != "PDF" else "pdf"),
108163
)
109164
val_list.append(
110165
self._prepare_ups_label_attachment(
111166
picking,
112167
{
113168
"name": attachment_name,
114-
"datas": PackageResult["ShippingLabel"]["GraphicImage"],
169+
"datas": label["datas"],
115170
},
116171
)
117172
)
@@ -133,33 +188,17 @@ def ups_get_label(self, carrier_tracking_ref):
133188
)
134189
return self._create_ups_label(picking, response)
135190

136-
def ups_create_shipping(self, picking):
137-
"""Send packages of the picking to UPS
138-
return a list of dicts {'exact_price': 'tracking_number':}
139-
suitable for delivery.carrier#send_shipping"""
140-
self.ensure_one()
141-
ups_request = UpsRequest(self)
142-
response = ups_request._send_shipping(picking)
143-
extra_price = self._ups_get_response_price(
144-
response["price"], picking.company_id.currency_id, picking.company_id
145-
)
146-
picking.carrier_tracking_ref = response["ShipmentIdentificationNumber"]
147-
# Create label from response
148-
self._create_ups_label(picking, response)
149-
# Return
150-
return {
151-
"exact_price": extra_price,
152-
"tracking_number": picking.carrier_tracking_ref,
153-
}
154-
155191
def ups_get_tracking_link(self, picking):
156192
return "https://ups.com/WebTracking/track?trackingNumber=%s" % (
157193
picking.carrier_tracking_ref
158194
)
159195

160196
def ups_cancel_shipment(self, pickings):
161197
ups_request = UpsRequest(self)
162-
return ups_request.cancel_shipment(pickings)
198+
for picking in pickings.filtered(lambda a: a.carrier_tracking_ref):
199+
if ups_request.cancel_shipment(pickings):
200+
picking.write({"tracking_state_history": False})
201+
return True
163202

164203
def ups_tracking_state_update(self, picking):
165204
self.ensure_one()
@@ -172,29 +211,7 @@ def ups_tracking_state_update(self, picking):
172211
picking.delivery_state = response["delivery_state"]
173212
picking.tracking_state_history = response["tracking_state_history"]
174213

175-
def ups_rate_shipment(self, order):
214+
def ups_update_token(self):
215+
self.ensure_one()
176216
ups_request = UpsRequest(self)
177-
response = ups_request.rate_shipment(order)
178-
price = self._ups_get_response_price(
179-
response, order.currency_id, order.company_id
180-
)
181-
return {
182-
"success": True,
183-
"price": price,
184-
"error_message": False,
185-
"warning_message": False,
186-
}
187-
188-
def _ups_get_response_price(self, total_charges, currency, company):
189-
"""We need to convert the price if the currency is different."""
190-
price = float(total_charges["MonetaryValue"])
191-
if total_charges["CurrencyCode"] != currency.name:
192-
price = currency._convert(
193-
price,
194-
self.env["res.currency"].search(
195-
[("name", "=", total_charges["CurrencyCode"])]
196-
),
197-
company,
198-
fields.Date.today(),
199-
)
200-
return price
217+
ups_request._get_new_token()

0 commit comments

Comments
 (0)