-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmixin.py
386 lines (334 loc) · 13 KB
/
mixin.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# -*- coding: utf-8 -*-
"""
mixin.py
"""
from trytond.model import fields, ModelView
from trytond.pool import Pool
from trytond.pyson import Eval, Or, Bool
from trytond.transaction import Transaction
from trytond.modules.stock_package.stock import PackageMixin
__all__ = ['ShipmentCarrierMixin']
class ShipmentCarrierMixin(PackageMixin):
"""
Mixin class which implements all the fields and methods required for
getting shipping rates and generating labels
"""
is_international_shipping = fields.Function(
fields.Boolean("Is International Shipping"),
'get_is_international_shipping', loading="lazy"
)
weight = fields.Function(
fields.Float(
"Weight", digits=(16, Eval('weight_digits', 2)),
depends=['weight_digits'],
),
'get_weight'
)
weight_uom = fields.Function(
fields.Many2One('product.uom', 'Weight UOM'),
'get_weight_uom'
)
weight_digits = fields.Function(
fields.Integer('Weight Digits'), 'on_change_with_weight_digits'
)
tracking_number = fields.Many2One(
'shipment.tracking', 'Tracking Number', select=True,
states={'readonly': Eval('state') == 'done'}, depends=['state']
)
shipping_instructions = fields.Text(
'Shipping Instructions', states={
'readonly': Eval('state').in_(['cancel', 'done']),
}, depends=['state']
)
available_carrier_services = fields.Function(
fields.One2Many("carrier.service", None, "Available Carrier Services"),
getter="get_available_carrier_services"
)
carrier_service = fields.Many2One(
"carrier.service", "Carrier Service", domain=[
('id', 'in', Eval('available_carrier_services'))
], depends=['available_carrier_services', 'state']
)
carrier_cost_method = fields.Function(
fields.Char('Carrier Cost Method'),
"get_carrier_cost_method"
)
shipping_manifest = fields.Many2One(
"shipping.manifest", "Shipping Manifest", readonly=True, select=True
)
@property
def carrier_cost_moves(self):
"Moves to use for carrier cost calculation"
return []
@classmethod
def get_carrier_cost_method(cls, records, name):
res = {}
for record in records:
res[record.id] = record.carrier.carrier_cost_method \
if record.carrier else None
return res
@fields.depends("carrier")
def on_change_with_carrier_cost_method(self, name=None):
Model = Pool().get(self.__name__)
return Model.get_carrier_cost_method([self], name)[self.id]
@classmethod
def get_available_carrier_services(cls, records, name):
res = {}
for record in records:
res[record.id] = map(int, record.carrier.services) \
if record.carrier else []
return res
@fields.depends('carrier')
def on_change_with_available_carrier_services(self, name=None):
Model = Pool().get(self.__name__)
return Model.get_available_carrier_services([self], name)[self.id]
@classmethod
def get_weight(cls, records, name=None):
"""
Returns sum of weight associated with each package or
move line otherwise
"""
Uom = Pool().get('product.uom')
res = {}
for record in records:
weight_uom = record.weight_uom
if record.packages:
res[record.id] = sum([
Uom.compute_qty(p.weight_uom, p.weight, weight_uom)
for p in record.packages
])
else:
res[record.id] = sum([
move.get_weight(weight_uom, silent=True)
for move in record.carrier_cost_moves
])
return res
@fields.depends('weight_uom')
def on_change_with_weight_digits(self, name=None):
if self.weight_uom:
return self.weight_uom.digits
return 2
@classmethod
def __setup__(cls):
super(ShipmentCarrierMixin, cls).__setup__()
cls.carrier_service.states = {
'readonly': Eval('state') == 'done',
}
cls._buttons.update({
'label_wizard': {
'invisible': Or(
(~Eval('state').in_(['packed', 'done'])),
(Bool(Eval('tracking_number')))
),
'icon': 'tryton-executable',
},
})
cls._error_messages.update({
'no_shipments': 'There must be atleast one shipment.',
'too_many_shipments':
'The wizard can be called on only one shipment',
'tracking_number_already_present':
'Tracking Number is already present for this shipment.',
'invalid_state': 'Labels can only be generated when the '
'shipment is in Packed or Done states only',
'wrong_carrier':
'Carrier for selected shipment is not of %s',
'no_packages': 'Shipment %s has no packages',
'warehouse_address_missing': 'Warehouse address is missing',
})
# Following fields are already there in customer shipment, have
# them in mixin so other shipment model can also use it.
cls.carrier = fields.Many2One(
'carrier', 'Carrier', states={
'readonly': Eval('state') == 'done',
}, depends=['state']
)
cls.cost_currency = fields.Many2One(
'currency.currency', 'Cost Currency', states={
'invisible': ~Eval('carrier'),
'required': Bool(Eval('carrier')),
'readonly': Eval('state') == 'done',
}, depends=['carrier', 'state']
)
cls.cost_currency_digits = fields.Function(
fields.Integer('Cost Currency Digits'),
'on_change_with_cost_currency_digits'
)
cls.cost = fields.Numeric(
'Cost',
digits=(16, Eval('cost_currency_digits', 2)), states={
'invisible': ~Eval('carrier'),
'readonly': Eval('state') == 'done',
}, depends=['carrier', 'state', 'cost_currency_digits']
)
cls.packages.context = {'carrier': Eval('carrier')}
cls.packages.depends = ['carrier']
@fields.depends('currency')
def on_change_with_cost_currency_digits(self, name=None):
if self.cost_currency:
return self.cost_currency.digits
return 2
@classmethod
@ModelView.button_action('shipping.wizard_generate_shipping_label')
def label_wizard(cls, shipments):
if len(shipments) == 0:
cls.raise_user_error('no_shipments')
elif len(shipments) > 1:
cls.raise_user_error('too_many_shipments')
@classmethod
def copy(cls, shipments, default=None):
if default is None:
default = {}
default = default.copy()
default['tracking_number'] = None
return super(ShipmentCarrierMixin, cls).copy(shipments, default=default)
@classmethod
def get_is_international_shipping(cls, records, name):
res = dict.fromkeys([r.id for r in records], False)
for record in records:
from_address = record._get_ship_from_address(silent=True)
delivery_address = None
if hasattr(record, 'delivery_address'):
delivery_address = record.delivery_address
elif hasattr(record, 'contact_address'):
delivery_address = record.contact_address
if delivery_address and from_address and \
from_address.country and delivery_address.country and \
from_address.country != delivery_address.country:
res[record.id] = True
return res
@fields.depends('delivery_address', 'warehouse')
def on_change_with_is_international_shipping(self, name=None):
"""
Return True if international shipping
"""
Model = Pool().get(self.__name__)
return Model.get_is_international_shipping([self], name)[self.id]
def get_weight_uom(self, name):
"""
Returns weight uom for the shipment
"""
ModelData = Pool().get('ir.model.data')
return ModelData.get_id('product', 'uom_pound')
def _get_ship_from_address(self, silent=False):
"""
Usually the warehouse from which you ship
"""
# FIXME: from address is not always warehouse address, something
# like return shipment or supplier shipment makes warehouse
# to_address.
if self.warehouse and self.warehouse.address:
return self.warehouse.address
if not silent:
return self.raise_user_error('warehouse_address_missing')
def allow_label_generation(self):
"""
Shipment must be in the right states and tracking number must not
be present.
"""
if self.state not in ('packed', 'done'):
self.raise_user_error('invalid_state')
if self.tracking_number:
self.raise_user_error('tracking_number_already_present')
return True
def _create_default_package(self, box_type=None):
"""
Create a single stock package for the whole shipment
"""
Package = Pool().get('stock.package')
package, = Package.create([{
'shipment': '%s,%d' % (self.__name__, self.id),
'box_type': box_type and box_type.id,
'moves': [('add', self.carrier_cost_moves)],
}])
return package
def get_shipping_rates(self, carriers=None, silent=False):
"""
Gives a list of rates from carriers provided. If no carriers provided,
return rates from all the carriers.
List contains dictionary with following minimum keys:
[
{
'display_name': Name to display,
'carrier_service': carrier.service active record,
'cost': cost,
'cost_currency': currency.currency active repord,
'carrier': carrier active record,
}..
]
"""
Carrier = Pool().get('carrier')
if carriers is None:
carriers = Carrier.search([])
rates = []
for carrier in carriers:
rates.extend(self.get_shipping_rate(carrier=carrier, silent=silent))
return rates
def get_shipping_rate(self, carrier, carrier_service=None, silent=False):
"""
Gives a list of rates from provided carrier and carrier service.
List contains dictionary with following minimum keys:
[
{
'display_name': Name to display,
'carrier_service': carrier.service active record,
'cost': cost,
'cost_currency': currency.currency active repord,
'carrier': carrier active record,
}..
]
"""
Company = Pool().get('company.company')
if carrier.carrier_cost_method == 'product':
currency = Company(Transaction().context['company']).currency
rate_dict = {
'display_name': carrier.rec_name,
'carrier_service': carrier_service,
'cost': carrier.carrier_product.list_price,
'cost_currency': currency,
'carrier': carrier,
}
return [rate_dict]
return []
def apply_shipping_rate(self, rate):
"""
This method applies shipping rate. Rate is a dictionary with
following minimum keys:
{
'display_name': Name to display,
'carrier_service': carrier.service active record,
'cost': cost,
'cost_currency': currency.currency active repord,
'carrier': carrier active record,
}
"""
Currency = Pool().get('currency.currency')
shipment_cost = rate['cost_currency'].round(rate['cost'])
if self.cost_currency != rate['cost_currency']:
shipment_cost = Currency.compute(
rate['cost_currency'], shipment_cost, self.cost_currency
)
self.cost = shipment_cost
self.cost_currency = rate['cost_currency']
self.carrier = rate['carrier']
self.carrier_service = rate['carrier_service']
self.save()
def generate_shipping_labels(self, **kwargs):
"""
Generates shipment label for shipment and saves labels,
tracking numbers.
"""
self.raise_user_error(
"Shipping label generation feature is not available"
)
@staticmethod
def default_cost_currency():
Company = Pool().get('company.company')
company_id = Transaction().context.get('company')
return company_id and Company(company_id).currency.id or None
@property
def ship_from_address(self):
return None
@property
def ship_to_address(self):
return None