forked from tryton/stock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocation.py
496 lines (438 loc) · 18.1 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
# 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
from decimal import Decimal
from trytond.model import ModelView, ModelSQL, fields, MatchMixin
from trytond.wizard import Wizard, StateView, Button, StateAction
from sql import Null
from sql.conditionals import Case
from trytond import backend
from trytond.pyson import Eval, PYSONEncoder, Date, If
from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta
from trytond.tools import grouped_slice
__all__ = ['Location', 'Party', 'ProductsByLocationsStart',
'ProductsByLocations', 'LocationLeadTime']
STATES = {
'readonly': ~Eval('active'),
}
DEPENDS = ['active']
class Location(ModelSQL, ModelView):
"Stock Location"
__name__ = 'stock.location'
name = fields.Char("Name", size=None, required=True, states=STATES,
depends=DEPENDS, translate=True)
code = fields.Char("Code", size=None, states=STATES, depends=DEPENDS,
select=True)
active = fields.Boolean('Active', select=True)
address = fields.Many2One("party.address", "Address",
states={
'invisible': Eval('type') != 'warehouse',
'readonly': ~Eval('active'),
},
depends=['type', 'active'])
type = fields.Selection([
('supplier', 'Supplier'),
('customer', 'Customer'),
('lost_found', 'Lost and Found'),
('warehouse', 'Warehouse'),
('storage', 'Storage'),
('production', 'Production'),
('drop', 'Drop'),
('view', 'View'),
], 'Location type', states=STATES, depends=DEPENDS)
parent = fields.Many2One("stock.location", "Parent", select=True,
left="left", right="right",
states={
'invisible': Eval('type') == 'warehouse',
},
depends=['type'])
left = fields.Integer('Left', required=True, select=True)
right = fields.Integer('Right', required=True, select=True)
childs = fields.One2Many("stock.location", "parent", "Children")
warehouse = fields.Function(fields.Many2One('stock.location', 'Warehouse'),
'get_warehouse')
input_location = fields.Many2One(
"stock.location", "Input", states={
'invisible': Eval('type') != 'warehouse',
'readonly': ~Eval('active'),
'required': Eval('type') == 'warehouse',
},
domain=[
('type', '=', 'storage'),
['OR',
('parent', 'child_of', [Eval('id')]),
('parent', '=', None),
],
],
depends=['type', 'active', 'id'])
output_location = fields.Many2One(
"stock.location", "Output", states={
'invisible': Eval('type') != 'warehouse',
'readonly': ~Eval('active'),
'required': Eval('type') == 'warehouse',
},
domain=[
('type', '=', 'storage'),
['OR',
('parent', 'child_of', [Eval('id')]),
('parent', '=', None)]],
depends=['type', 'active', 'id'])
storage_location = fields.Many2One(
"stock.location", "Storage", states={
'invisible': Eval('type') != 'warehouse',
'readonly': ~Eval('active'),
'required': Eval('type') == 'warehouse',
},
domain=[
('type', 'in', ['storage', 'view']),
['OR',
('parent', 'child_of', [Eval('id')]),
('parent', '=', None)]],
depends=['type', 'active', 'id'])
picking_location = fields.Many2One(
'stock.location', 'Picking', states={
'invisible': Eval('type') != 'warehouse',
'readonly': ~Eval('active'),
},
domain=[
('type', '=', 'storage'),
('parent', 'child_of', [Eval('storage_location', -1)]),
],
depends=['type', 'active', 'storage_location'],
help='If empty the Storage is used')
quantity = fields.Function(fields.Float('Quantity'), 'get_quantity')
forecast_quantity = fields.Function(fields.Float('Forecast Quantity'),
'get_quantity')
cost_value = fields.Function(fields.Numeric('Cost Value'),
'get_cost_value')
@classmethod
def __setup__(cls):
super(Location, cls).__setup__()
cls._order.insert(0, ('name', 'ASC'))
cls._error_messages.update({
'invalid_type_for_moves': ('Location "%s" with existing moves '
'cannot be changed to a type that does not support moves.'
),
'child_of_warehouse': ('Location "%(location)s" must be a '
'child of warehouse "%(warehouse)s".'),
})
parent_domain = []
childs_domain = []
childs_mapping = cls._childs_domain()
for type_, allowed_parents in cls._parent_domain().iteritems():
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
cls.childs.depends.append('type')
@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': [''],
}
@classmethod
def _childs_domain(cls):
childs_domain = {}
for type_, allowed_parents in cls._parent_domain().iteritems():
for parent in allowed_parents:
childs_domain.setdefault(parent, [])
childs_domain[parent].append(type_)
return childs_domain
@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
super(Location, cls).__register__(module_name)
table = TableHandler(cls, module_name)
table.index_action(['left', 'right'], 'add')
@classmethod
def validate(cls, locations):
super(Location, cls).validate(locations)
cls.check_recursion(locations)
for location in locations:
location.check_type_for_moves()
def check_type_for_moves(self):
""" Check locations with moves have types compatible with moves. """
invalid_move_types = ['warehouse', 'view']
Move = Pool().get('stock.move')
if (self.type in invalid_move_types
and Move.search([
['OR',
('to_location', '=', self.id),
('from_location', '=', self.id),
],
('state', 'not in', ['staging', 'draft']),
])):
self.raise_user_error('invalid_type_for_moves', (self.rec_name,))
@staticmethod
def default_active():
return True
@staticmethod
def default_left():
return 0
@staticmethod
def default_right():
return 0
@staticmethod
def default_type():
return 'storage'
@classmethod
def check_xml_record(self, 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 search_rec_name(cls, name, clause):
locations = cls.search([
('code', '=', clause[2]),
], order=[])
if locations:
return [('id', 'in', [l.id for l in locations])]
return [(cls._rec_name,) + tuple(clause[1:])]
@classmethod
def get_quantity(cls, locations, name):
pool = Pool()
Product = pool.get('product.product')
Date_ = pool.get('ir.date')
if (not Transaction().context.get('product')) \
or not (isinstance(Transaction().context['product'],
(int, long))):
return dict([(l.id, 0) for l in locations])
with Transaction().set_context(active_test=False):
if not Product.search([
('id', '=', Transaction().context['product']),
]):
return dict([(l.id, 0) for l in locations])
context = {}
if (name == 'quantity'
and Transaction().context.get('stock_date_end') >
Date_.today()):
context['stock_date_end'] = Date_.today()
if name == 'forecast_quantity':
context['forecast'] = True
if not Transaction().context.get('stock_date_end'):
context['stock_date_end'] = datetime.date.max
product_id = Transaction().context['product']
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=location_ids, product_ids=[product_id],
with_childs=True))
return dict((loc.id, pbl.get((loc.id, product_id), 0))
for loc in locations)
@classmethod
def get_cost_value(cls, locations, name):
Product = Pool().get('product.product')
trans_context = Transaction().context
product_id = trans_context.get('product')
if not product_id:
return dict((l.id, None) for l in locations)
cost_values, 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)
with Transaction().set_context(context):
product = Product(product_id)
for location in locations:
# The date could be before the product creation
if not isinstance(product.cost_price, Decimal):
cost_values[location.id] = None
else:
cost_values[location.id] = (Decimal(str(location.quantity))
* product.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()
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:
cls.write(list(to_update), {
'parent': location.id,
})
to_update.clear()
@classmethod
def create(cls, vlist):
locations = super(Location, cls).create(vlist)
cls._set_warehouse_parent(locations)
return locations
@classmethod
def write(cls, *args):
super(Location, cls).write(*args)
locations = sum(args[::2], [])
cls._set_warehouse_parent(locations)
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:
cls.raise_user_error('child_of_warehouse', {
'location': location.rec_name,
'warehouse': warehouse.rec_name,
})
@classmethod
def copy(cls, locations, default=None):
if default is None:
default = {}
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.iteritems():
if loc_id == location.id:
cls.write([cp_warehouse], {
field: new_location.id,
})
res.append(new_location)
return res
class Party:
__metaclass__ = PoolMeta
__name__ = 'party.party'
supplier_location = fields.Property(fields.Many2One('stock.location',
'Supplier Location', domain=[('type', '=', 'supplier')],
help='The default source location when receiving products from the '
'party.'))
customer_location = fields.Property(fields.Many2One('stock.location',
'Customer Location', domain=[('type', '=', 'customer')],
help='The default destination location when sending products to the '
'party.'))
class ProductsByLocationsStart(ModelView):
'Products by Locations'
__name__ = 'stock.products_by_locations.start'
forecast_date = fields.Date(
'At Date', help=('Allow to compute expected '
'stock quantities for this date.\n'
'* An empty value is an infinite date in the future.\n'
'* A date in the past will provide historical values.'))
@staticmethod
def default_forecast_date():
Date_ = Pool().get('ir.date')
return Date_.today()
class ProductsByLocations(Wizard):
'Products by Locations'
__name__ = 'stock.products_by_locations'
start = StateView('stock.products_by_locations.start',
'stock.products_by_locations_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Open', 'open', 'tryton-ok', True),
])
open = StateAction('stock.act_products_by_locations')
def do_open(self, action):
pool = Pool()
Location = pool.get('stock.location')
Lang = pool.get('ir.lang')
context = {}
context['locations'] = Transaction().context.get('active_ids')
date = self.start.forecast_date or datetime.date.max
context['stock_date_end'] = Date(date.year, date.month, date.day)
action['pyson_context'] = PYSONEncoder().encode(context)
locations = Location.browse(context['locations'])
for code in [Transaction().language, 'en_US']:
langs = Lang.search([
('code', '=', code),
])
if langs:
break
lang = langs[0]
date = Lang.strftime(date, lang.code, lang.date)
action['name'] += ' - (%s) @ %s' % (
','.join(l.name for l in locations), date)
return action, {}
class LocationLeadTime(ModelSQL, ModelView, MatchMixin):
'Location Lead Time'
__name__ = 'stock.location.lead_time'
sequence = fields.Integer('Sequence')
warehouse_from = fields.Many2One('stock.location', 'Warehouse From',
ondelete='CASCADE',
domain=[
('type', '=', 'warehouse'),
])
warehouse_to = fields.Many2One('stock.location', 'Warehouse To',
ondelete='CASCADE',
domain=[
('type', '=', 'warehouse'),
])
lead_time = fields.TimeDelta('Lead Time')
@classmethod
def __setup__(cls):
super(LocationLeadTime, cls).__setup__()
cls._order.insert(0, ('sequence', 'ASC'))
@classmethod
def order_sequence(cls, tables):
table, _ = tables[None]
return [Case((table.sequence == Null, 0), else_=1), table.sequence]
@classmethod
def get_lead_time(cls, pattern):
for record in cls.search([]):
if record.match(pattern):
return record.lead_time