forked from OCA/delivery-carrier
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdelivery_carrier.py
157 lines (140 loc) · 5.29 KB
/
delivery_carrier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
_logger = logging.getLogger(__name__)
class DeliveryCarrier(models.Model):
_inherit = "delivery.carrier"
delivery_type = fields.Selection(
selection_add=[("gls", "GLS")],
ondelete={
"gls": lambda recs: recs.write({"delivery_type": "fixed", "fixed_price": 0})
},
)
# Backport
carrier_account_id = fields.Many2one(
"carrier.account",
string="Account",
company_dependent=True,
domain="[('delivery_type', '=', delivery_type)]",
)
gls_contact_id = fields.Char(
string="International",
size=10,
help="Contact id for GLS International transportation (T8914)",
)
gls_url = fields.Char(string="Service Url")
gls_url_test = fields.Char(string="Test Service Url")
gls_url_tracking = fields.Char(
help="Root URL for parcel tracking. Needs a %s for the tracking reference."
)
gls_label_format = fields.Selection(
string="Label format",
selection=[
("pdf", "PDF"),
("zebra", "Zebra"),
("intermec", "Intermec"),
("datamax", "Datamax"),
("toshiba", "Toshiba"),
],
default="pdf",
)
gls_label_template = fields.Selection(
string="Label Template",
selection=[
("D_200", "D200"),
("PF_4_I", "PF4I"),
("PF_4_I_200", "PF4I200"),
("PF_4_I_300", "PF4I300"),
("PF_8_D_200", "PF8D200"),
("T_200_BF", "T200BF"),
("T_300_BF", "T300BF"),
("ZPL_200", "ZPL200"),
("ZPL_300", "ZPL300"),
],
default=False,
)
gls_return_partner_id = fields.Many2one(
"res.partner",
string="Return Address",
help="If set, this partner's address will be used on the return label.",
)
@api.constrains("gls_return_partner_id")
def _check_gls_return_partner_id(self):
records_to_check = self.filtered(
lambda c: c.delivery_type == "gls" and c.gls_return_partner_id
)
for carrier in records_to_check:
carrier.gls_return_partner_id._gls_prepare_address()
@api.constrains(
"delivery_type",
"gls_contact_id",
"gls_url_tracking",
"gls_label_format",
"carrier_account_id",
)
def _check_gls_fields(self):
gls_field_names = [
"gls_contact_id",
"gls_url_tracking",
"gls_label_format",
"carrier_account_id",
]
gls_records = self.filtered(lambda c: c.delivery_type == "gls")
for rec in gls_records:
for field_name in gls_field_names:
value = rec[field_name]
if not value:
field = rec._fields[field_name]
description_string = field._description_string(self.env)
raise ValidationError(
_(
f"The GLS field '{description_string}' is required for "
f"carrier {rec.name}"
)
)
for rec in self - gls_records:
if any(
rec[f]
for f in gls_field_names
if f not in ("carrier_account_id", "gls_label_format")
):
raise ValidationError(
_(f"Incorrect GLS parameters set on carrier {rec.name}.")
)
@api.constrains("delivery_type", "prod_environment", "gls_url", "gls_url_test")
def _check_gls_url(self):
for rec in self.filtered(lambda c: c.delivery_type == "gls"):
if not rec.prod_environment and not rec.gls_url_test:
raise ValidationError(
_("The GLS field 'Test Service Url' is required in test mode")
)
if rec.prod_environment and not rec.gls_url:
raise ValidationError(
_("The GLS field 'Service Url' is required in non test mode")
)
def gls_send_shipping(self, picking):
tracking_number = picking.gls_send_shipping(self)
return [{"exact_price": False, "tracking_number": tracking_number}]
@api.model
def gls_tracking_url(self, tracking_number):
return self.gls_url_tracking % tracking_number
def gls_get_tracking_link(self, pickings):
links = []
for picking in pickings:
for link in picking.package_ids.parcel_tracking:
if link:
links.append(self.gls_tracking_url(link))
return links
def gls_cancel_shipment(self, pickings):
if pickings.package_ids.report_id:
msg = _("Packages cannot be canceled after the End of Day report.")
raise ValidationError(msg)
for picking in pickings:
picking.gls_cancel_shipment()
def _get_gls_client(self):
"""Return a GLS connection client using this carrier configuration."""
# the client checks all parameters, so nothing needed here.
self.ensure_one()
return self.env["delivery.client.gls"].create({"carrier_id": self.id})