This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlocation.py
762 lines (687 loc) · 27.6 KB
/
location.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime
import operator
from decimal import Decimal
from sql import Column
from trytond.cache import Cache
from trytond.i18n import gettext
from trytond.model import (
DeactivableMixin, Index, MatchMixin, Model, ModelSQL, ModelView, fields,
sequence_ordered, tree)
from trytond.modules.product import price_digits, round_price
from trytond.pool import Pool
from trytond.pyson import Eval, If, TimeDelta
from trytond.tools import grouped_slice
from trytond.transaction import Transaction
from .exceptions import LocationValidationError
class WarehouseWasteLocation(ModelSQL):
"Warehouse Waste Location"
__name__ = 'stock.location.waste'
warehouse = fields.Many2One(
'stock.location', "Warehouse", required=True, ondelete='CASCADE',
domain=[('type', '=', 'warehouse')])
location = fields.Many2One(
'stock.location', "Waste Location", required=True, ondelete='CASCADE',
domain=[('type', '=', 'lost_found')])
class Location(DeactivableMixin, tree(), ModelSQL, ModelView):
"Stock Location"
__name__ = 'stock.location'
_default_warehouse_cache = Cache('stock.location.default_warehouse',
context=False)
name = fields.Char("Name", size=None, required=True, translate=True)
code = fields.Char(
"Code",
help="The internal identifier used for the location.")
address = fields.Many2One(
'party.address', "Address",
states={
'invisible': Eval('type') != 'warehouse',
})
type = fields.Selection([
('supplier', 'Supplier'),
('customer', 'Customer'),
('lost_found', 'Lost and Found'),
('warehouse', 'Warehouse'),
('storage', 'Storage'),
('production', 'Production'),
('drop', 'Drop'),
('view', 'View'),
], "Location type")
type_string = type.translated('type')
parent = fields.Many2One(
"stock.location", "Parent",
left="left", right="right",
help="Used to add structure above the location.")
left = fields.Integer('Left', required=True)
right = fields.Integer('Right', required=True)
childs = fields.One2Many("stock.location", "parent", "Children",
help="Used to add structure below the location.")
flat_childs = fields.Boolean(
"Flat Children",
help="Check to enforce a single level of children with no "
"grandchildren.")
warehouse = fields.Function(fields.Many2One('stock.location', 'Warehouse'),
'get_warehouse')
input_location = fields.Many2One(
"stock.location", "Input", states={
'invisible': Eval('type') != 'warehouse',
'required': Eval('type') == 'warehouse',
},
domain=[
('type', '=', 'storage'),
['OR',
('parent', 'child_of', [Eval('id', -1)]),
('parent', '=', None),
],
],
help="Where incoming stock is received.")
output_location = fields.Many2One(
"stock.location", "Output", states={
'invisible': Eval('type') != 'warehouse',
'required': Eval('type') == 'warehouse',
},
domain=[
('type', '=', 'storage'),
['OR',
('parent', 'child_of', [Eval('id', -1)]),
('parent', '=', None)]],
help="Where outgoing stock is sent from.")
storage_location = fields.Many2One(
"stock.location", "Storage", states={
'invisible': Eval('type') != 'warehouse',
'required': Eval('type') == 'warehouse',
},
domain=[
('type', 'in', ['storage', 'view']),
['OR',
('parent', 'child_of', [Eval('id', -1)]),
('parent', '=', None)]],
help="The top level location where stock is stored.")
picking_location = fields.Many2One(
'stock.location', 'Picking', states={
'invisible': Eval('type') != 'warehouse',
},
domain=[
('type', '=', 'storage'),
('parent', 'child_of', [Eval('storage_location', -1)]),
],
help="Where stock is picked from.\n"
"Leave empty to use the storage location.")
lost_found_location = fields.Many2One(
'stock.location', "Lost and Found",
states={
'invisible': Eval('type') != 'warehouse',
},
domain=[
('type', '=', 'lost_found'),
],
help="Used, by inventories, when correcting stock levels "
"in the warehouse.")
waste_locations = fields.Many2Many(
'stock.location.waste', 'warehouse', 'location', "Waste Locations",
states={
'invisible': Eval('type') != 'warehouse',
},
domain=[
('type', '=', 'lost_found'),
],
help="The locations used for waste products from the warehouse.")
waste_warehouses = fields.Many2Many(
'stock.location.waste', 'location', 'warehouse', "Waste Warehouses",
states={
'invisible': Eval('type') != 'lost_found',
},
domain=[
('type', '=', 'warehouse'),
],
help="The warehouses that use the location for waste products.")
allow_pickup = fields.Boolean(
"Allow Pickup",
states={
'invisible': (
(Eval('type') != 'warehouse')
& ~Eval('address')),
})
quantity = fields.Function(
fields.Float(
"Quantity", digits=(16, Eval('quantity_uom_digits', 2)),
help="The amount of stock in the location."),
'get_quantity', searcher='search_quantity')
forecast_quantity = fields.Function(
fields.Float(
"Forecast Quantity", digits=(16, Eval('quantity_uom_digits', 2)),
help="The amount of stock expected to be in the location."),
'get_quantity', searcher='search_quantity')
quantity_uom = fields.Function(fields.Many2One(
'product.uom', "Quantity UOM"),
'get_quantity_uom')
quantity_uom_digits = fields.Function(fields.Integer(
"Quantity UOM Digits"),
'get_quantity_uom')
cost_value = fields.Function(fields.Numeric(
"Cost Value", digits=price_digits,
help="The value of the stock in the location."),
'get_cost_value')
@classmethod
def __setup__(cls):
cls.code.search_unaccented = False
super(Location, cls).__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(t, (t.code, Index.Similarity())),
Index(
t,
(t.left, Index.Range()),
(t.right, Index.Range())),
})
cls._order.insert(0, ('name', 'ASC'))
parent_domain = [
['OR',
('parent.flat_childs', '=', False),
('parent', '=', None),
]
]
childs_domain = [
If(Eval('flat_childs', False),
('childs', '=', None),
()),
]
childs_mapping = cls._childs_domain()
for type_, allowed_parents in cls._parent_domain().items():
parent_domain.append(If(Eval('type') == type_,
('type', 'in', allowed_parents), ()))
childs_domain.append(If(Eval('type') == type_,
('type', 'in', childs_mapping[type_]), ()))
cls.parent.domain = parent_domain
cls.childs.domain = childs_domain
@classmethod
def _parent_domain(cls):
'''Returns a dict with location types as keys and a list of allowed
parent location types as values'''
return {
'customer': ['customer'],
'supplier': ['supplier'],
'production': ['production'],
'lost_found': ['lost_found'],
'view': ['warehouse', 'view', 'storage'],
'storage': ['warehouse', 'view', 'storage'],
'warehouse': ['view'],
}
@classmethod
def _childs_domain(cls):
childs_domain = {}
for type_, allowed_parents in cls._parent_domain().items():
for parent in allowed_parents:
childs_domain.setdefault(parent, [])
childs_domain[parent].append(type_)
return childs_domain
@classmethod
def validate_fields(cls, locations, field_names):
super().validate_fields(locations, field_names)
inactives = []
for location in locations:
location.check_type_for_moves(field_names)
if 'active' in field_names and not location.active:
inactives.append(location)
cls.check_inactive(inactives)
def check_type_for_moves(self, field_names=None):
""" Check locations with moves have types compatible with moves. """
pool = Pool()
Move = pool.get('stock.move')
if field_names and 'type' not in field_names:
return
invalid_move_types = ['warehouse', 'view']
if self.type in invalid_move_types:
# Use root to compute for all companies
with Transaction().set_user(0):
moves = Move.search([
['OR',
('to_location', '=', self.id),
('from_location', '=', self.id),
],
('state', 'not in', ['staging', 'draft']),
],
order=[], limit=1)
if moves:
raise LocationValidationError(
gettext('stock.msg_location_invalid_type_for_moves',
location=self.rec_name,
type=self.type_string))
@classmethod
def check_inactive(cls, locations):
"Check inactive location are empty"
assert all(not l.active for l in locations)
empty = cls.get_empty_locations(locations)
non_empty = set(locations) - set(empty)
if non_empty:
raise LocationValidationError(
gettext('stock.msg_location_inactive_not_empty',
location=next(iter(non_empty)).rec_name))
@classmethod
def get_empty_locations(cls, locations=None):
pool = Pool()
Move = pool.get('stock.move')
Product = pool.get('product.product')
if locations is None:
locations = cls.search([])
if not locations:
return []
location_ids = list(map(int, locations))
# Use root to compute for all companies
# and ensures inactive locations are in the query
with Transaction().set_user(0), \
Transaction().set_context(active_test=False):
query = Move.compute_quantities_query(
location_ids, with_childs=True)
quantities = Move.compute_quantities(
query, location_ids, with_childs=True)
empty = set(location_ids)
product_ids = [q[1] for q in quantities.keys()]
consumables = {
p.id for p in Product.browse(product_ids) if p.consumable}
for (location_id, product), quantity in quantities.items():
if quantity and product not in consumables:
empty.discard(location_id)
for sub_ids in grouped_slice(list(empty)):
sub_ids = list(sub_ids)
moves = Move.search([
('state', 'not in', ['done', 'cancelled']),
['OR',
('from_location', 'in', sub_ids),
('to_location', 'in', sub_ids),
],
])
for move in moves:
for location in [move.from_location, move.to_location]:
empty.discard(location.id)
return cls.browse(empty)
@staticmethod
def default_left():
return 0
@staticmethod
def default_right():
return 0
@classmethod
def default_flat_childs(cls):
return False
@staticmethod
def default_type():
return 'storage'
@classmethod
def check_xml_record(cls, records, values):
return True
def get_warehouse(self, name):
# Order by descending left to get the first one in the tree
with Transaction().set_context(active_test=False):
locations = self.search([
('parent', 'parent_of', [self.id]),
('type', '=', 'warehouse'),
], order=[('left', 'DESC')])
if locations:
return locations[0].id
@classmethod
def get_default_warehouse(cls):
warehouse = Transaction().context.get('warehouse')
if warehouse:
return warehouse
warehouse = cls._default_warehouse_cache.get(None, -1)
if warehouse == -1:
warehouses = cls.search([
('type', '=', 'warehouse'),
], limit=2)
if len(warehouses) == 1:
warehouse = warehouses[0].id
else:
warehouse = None
cls._default_warehouse_cache.set(None, warehouse)
return warehouse
@property
def lost_found_used(self):
if self.warehouse:
return self.warehouse.lost_found_location
@classmethod
def search_rec_name(cls, name, clause):
if clause[1].startswith('!') or clause[1].startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
return [bool_op,
(cls._rec_name,) + tuple(clause[1:]),
('code',) + tuple(clause[1:]),
]
@classmethod
def _get_quantity_grouping(cls):
context = Transaction().context
grouping, grouping_filter, key = (), (), []
if context.get('product') is not None:
grouping = ('product',)
grouping_filter = ([context['product']],)
key = (context['product'],)
elif context.get('product_template') is not None:
grouping = ('product.template',)
grouping_filter = ([context['product_template']],)
key = (context['product_template'],)
return grouping, grouping_filter, key
@classmethod
def get_quantity(cls, locations, name):
pool = Pool()
Product = pool.get('product.product')
Date_ = pool.get('ir.date')
trans_context = Transaction().context
def valid_context(name):
return (trans_context.get(name) is not None
and isinstance(trans_context[name], int))
context = {}
if (name == 'quantity'
and (trans_context.get('stock_date_end', datetime.date.max)
> Date_.today())):
context['stock_date_end'] = Date_.today()
if name == 'forecast_quantity':
context['forecast'] = True
if not trans_context.get('stock_date_end'):
context['stock_date_end'] = datetime.date.max
grouping, grouping_filter, key = cls._get_quantity_grouping()
if not grouping:
return {loc.id: None for loc in locations}
pbl = {}
for sub_locations in grouped_slice(locations):
location_ids = [l.id for l in sub_locations]
with Transaction().set_context(context):
pbl.update(Product.products_by_location(
location_ids,
grouping=grouping,
grouping_filter=grouping_filter,
with_childs=trans_context.get('with_childs', True)))
return dict((loc.id, pbl.get((loc.id,) + key, 0)) for loc in locations)
@classmethod
def search_quantity(cls, name, domain):
_, operator_, operand = domain
operator_ = {
'=': operator.eq,
'>=': operator.ge,
'>': operator.gt,
'<=': operator.le,
'<': operator.lt,
'!=': operator.ne,
'in': lambda v, l: v in l,
'not in': lambda v, l: v not in l,
}.get(operator_, lambda v, l: False)
ids = []
for location in cls.search([]):
if operator_(getattr(location, name), operand):
ids.append(location.id)
return [('id', 'in', ids)]
@classmethod
def get_quantity_uom(cls, locations, name):
pool = Pool()
Product = pool.get('product.product')
Template = pool.get('product.template')
context = Transaction().context
value = None
uom = None
if context.get('product') is not None:
product = Product(context['product'])
uom = product.default_uom
elif context.get('product_template') is not None:
template = Template(context['product_template'])
uom = template.default_uom
if uom:
if name == 'quantity_uom':
value = uom.id
elif name == 'quantity_uom_digits':
value = uom.digits
return {l.id: value for l in locations}
@classmethod
def get_cost_value(cls, locations, name):
pool = Pool()
Product = pool.get('product.product')
Template = pool.get('product.template')
trans_context = Transaction().context
cost_values = {l.id: None for l in locations}
def valid_context(name):
return (trans_context.get(name) is not None
and isinstance(trans_context[name], int))
if not any(map(valid_context, ['product', 'product_template'])):
return cost_values
def get_record():
if trans_context.get('product') is not None:
return Product(trans_context['product'])
else:
return Template(trans_context['product_template'])
context = {}
if 'stock_date_end' in trans_context:
# Use the last cost_price of the day
context['_datetime'] = datetime.datetime.combine(
trans_context['stock_date_end'], datetime.time.max)
# The date could be before the product creation
record = get_record()
if record.create_date > context['_datetime']:
return cost_values
with Transaction().set_context(context):
cost_price = get_record().cost_price
# The template may have more than one product
if cost_price is not None:
for location in locations:
cost_values[location.id] = round_price(
Decimal(str(location.quantity)) * cost_price)
return cost_values
@classmethod
def _set_warehouse_parent(cls, locations):
'''
Set the parent of child location of warehouse if not set
'''
to_update = set()
to_save = []
for location in locations:
if location.type == 'warehouse':
if not location.input_location.parent:
to_update.add(location.input_location)
if not location.output_location.parent:
to_update.add(location.output_location)
if not location.storage_location.parent:
to_update.add(location.storage_location)
if to_update:
for child_location in to_update:
child_location.parent = location
to_save.append(child_location)
to_update.clear()
cls.save(to_save)
@classmethod
def create(cls, vlist):
locations = super(Location, cls).create(vlist)
cls._set_warehouse_parent(locations)
cls._default_warehouse_cache.clear()
return locations
@classmethod
def write(cls, *args):
super(Location, cls).write(*args)
locations = sum(args[::2], [])
cls._set_warehouse_parent(locations)
cls._default_warehouse_cache.clear()
ids = [l.id for l in locations]
warehouses = cls.search([
('type', '=', 'warehouse'),
['OR',
('storage_location', 'in', ids),
('input_location', 'in', ids),
('output_location', 'in', ids),
]])
fields = ('storage_location', 'input_location', 'output_location')
wh2childs = {}
for warehouse in warehouses:
in_out_sto = (getattr(warehouse, f).id for f in fields)
for location in locations:
if location.id not in in_out_sto:
continue
childs = wh2childs.setdefault(warehouse.id, cls.search([
('parent', 'child_of', warehouse.id),
]))
if location not in childs:
raise LocationValidationError(
gettext('stock.msg_location_child_of_warehouse',
location=location.rec_name,
warehouse=warehouse.rec_name))
@classmethod
def delete(cls, *args):
super().delete(*args)
cls._default_warehouse_cache.clear()
@classmethod
def copy(cls, locations, default=None):
if default is None:
default = {}
else:
default = default.copy()
res = []
for location in locations:
if location.type == 'warehouse':
wh_default = default.copy()
wh_default['type'] = 'view'
wh_default['input_location'] = None
wh_default['output_location'] = None
wh_default['storage_location'] = None
wh_default['childs'] = None
new_location, = super(Location, cls).copy([location],
default=wh_default)
with Transaction().set_context(
cp_warehouse_locations={
'input_location': location.input_location.id,
'output_location': location.output_location.id,
'storage_location': location.storage_location.id,
},
cp_warehouse_id=new_location.id):
cls.copy(location.childs,
default={'parent': new_location.id})
cls.write([new_location], {
'type': 'warehouse',
})
else:
new_location, = super(Location, cls).copy([location],
default=default)
warehouse_locations = Transaction().context.get(
'cp_warehouse_locations') or {}
if location.id in warehouse_locations.values():
cp_warehouse = cls(
Transaction().context['cp_warehouse_id'])
for field, loc_id in warehouse_locations.items():
if loc_id == location.id:
cls.write([cp_warehouse], {
field: new_location.id,
})
res.append(new_location)
return res
@classmethod
def view_attributes(cls):
storage_types = Eval('type').in_(['storage', 'warehouse', 'view'])
return super().view_attributes() + [
('/tree/field[@name="quantity"]',
'visual', If(
storage_types & (Eval('quantity', 0) < 0), 'danger', ''),
['type']),
('/tree/field[@name="forecast_quantity"]',
'visual', If(
storage_types & (Eval('forecast_quantity', 0) < 0),
'warning', ''),
['type']),
]
class ProductsByLocationsContext(ModelView):
'Products by Locations'
__name__ = 'stock.products_by_locations.context'
forecast_date = fields.Date(
'At Date',
help="The date for which the stock quantity is calculated.\n"
"* An empty value calculates as far ahead as possible.\n"
"* A date in the past will provide historical values.")
stock_date_end = fields.Function(fields.Date('At Date'),
'on_change_with_stock_date_end')
@staticmethod
def default_forecast_date():
Date_ = Pool().get('ir.date')
return Date_.today()
@fields.depends('forecast_date')
def on_change_with_stock_date_end(self, name=None):
if self.forecast_date is None:
return datetime.date.max
return self.forecast_date
class ProductsByLocations(DeactivableMixin, ModelSQL, ModelView):
"Products by Locations"
__name__ = 'stock.products_by_locations'
product = fields.Many2One('product.product', "Product")
quantity = fields.Function(
fields.Float(
"Quantity", digits=(16, Eval('default_uom_digits', 2))),
'get_product', searcher='search_product')
forecast_quantity = fields.Function(
fields.Float(
"Forecast Quantity", digits=(16, Eval('default_uom_digits', 2))),
'get_product', searcher='search_product')
default_uom = fields.Function(
fields.Many2One('product.uom', "Default UOM"),
'get_product', searcher='search_product')
default_uom_digits = fields.Function(
fields.Integer("Default UOM Digits"), 'get_product')
cost_value = fields.Function(
fields.Numeric("Cost Value"), 'get_product')
consumable = fields.Function(
fields.Boolean("Consumable"), 'get_product',
searcher='search_product')
@classmethod
def __setup__(cls):
super().__setup__()
cls._order.insert(0, ('product', 'ASC'))
@classmethod
def table_query(cls):
pool = Pool()
Product = pool.get('product.product')
product = Product.__table__()
columns = []
for fname, field in cls._fields.items():
if not hasattr(field, 'set'):
if (isinstance(field, fields.Many2One)
and field.get_target() == Product):
column = Column(product, 'id')
else:
column = Column(product, fname)
columns.append(column.as_(fname))
return product.select(*columns)
def get_rec_name(self, name):
return self.product.rec_name
@classmethod
def search_rec_name(cls, name, clause):
return [('product.rec_name',) + tuple(clause[1:])]
def get_product(self, name):
value = getattr(self.product, name)
if isinstance(value, Model):
value = value.id
return value
@classmethod
def search_product(cls, name, clause):
nested = clause[0][len(name):]
return [('product.' + name + nested, *clause[1:])]
class LocationLeadTime(sequence_ordered(), ModelSQL, ModelView, MatchMixin):
'Location Lead Time'
__name__ = 'stock.location.lead_time'
warehouse_from = fields.Many2One('stock.location', 'Warehouse From',
ondelete='CASCADE',
domain=[
('type', '=', 'warehouse'),
('id', '!=', Eval('warehouse_to', -1)),
])
warehouse_to = fields.Many2One('stock.location', 'Warehouse To',
ondelete='CASCADE',
domain=[
('type', '=', 'warehouse'),
('id', '!=', Eval('warehouse_from', -1)),
])
lead_time = fields.TimeDelta(
"Lead Time",
domain=['OR',
('lead_time', '=', None),
('lead_time', '>=', TimeDelta()),
],
help="The time it takes to move stock between the warehouses.")
@classmethod
def get_lead_time(cls, pattern):
for record in cls.search([]):
if record.match(pattern):
return record.lead_time