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
|
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 TeMPO Consulting, MSF
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv
from osv import fields
from mx.DateTime import DateFrom
from mx.DateTime import now
from mx.DateTime import RelativeDate
class threshold_value(osv.osv):
_name = 'threshold.value'
_description = 'Threshold value'
def _get_product_ids(self, cr, uid, ids, field_name, arg, context=None):
'''
Returns a list of products for the rule
'''
res = {}
for rule in self.browse(cr, uid, ids, context=context):
res[rule.id] = []
for line in rule.line_ids:
res[rule.id].append(line.product_id.id)
return res
def _src_product_ids(self, cr, uid, obj, name, args, context=None):
if not context:
context = {}
res = []
for arg in args:
if arg[0] == 'product_ids':
rule_ids = []
line_ids = self.pool.get('threshold.value.line').search(cr, uid, [('product_id', arg[1], arg[2])])
for l in self.pool.get('threshold.value.line').browse(cr, uid, line_ids):
if l.threshold_value_id.id not in rule_ids:
rule_ids.append(l.threshold_value_id.id)
res.append(('id', 'in', rule_ids))
return res
_columns = {
'name': fields.char(size=128, string='Reference', required=True),
'active': fields.boolean(string='Active'),
'warehouse_id': fields.many2one('stock.warehouse', string='Warehouse', required=True),
'location_id': fields.many2one('stock.location', 'Location', required=True, ondelete="cascade",
domain="[('is_replenishment', '=', warehouse_id)]",
help='Location where the computation is made'),
'compute_method': fields.selection([('fixed', 'Fixed values'), ('computed', 'Computed values')],
string='Method of computation', required=True,
help="""If 'Fixed values', the scheduler will compare stock of product with the threshold value of the line. \n
If 'Computed values', the threshold value and the ordered quantity will be calculated according to defined parameters"""),
'consumption_method': fields.selection([('amc', 'Average Monthly Consumption'), ('fmc', 'Forecasted Monthly Consumption')],
string='Consumption Method',
help='Method used to compute the consumption of products.'),
'consumption_period_from': fields.date(string='Period of calculation',
help='This period is a number of past months the system has to consider for AMC calculation.'\
'By default this value is equal to the frequency in the Threshold.'),
'consumption_period_to': fields.date(string='-'),
'frequency': fields.float(digits=(16,2), string='Order frequency',
help='The time between two replenishments. Will be used to compute the quantity to order.'),
'safety_month': fields.float(digits=(16,2), string='Safety Stock in months',
help='In months. Period during the stock is not empty but need to be replenish. \
Used to compute the quantity to order.'),
'lead_time': fields.float(digits=(16,2), string='Fixed Lead Time in months',
help='In months. Time to be delivered after processing the purchase order.'),
'supplier_lt': fields.boolean(string='Product\'s supplier LT',
help='If checked, use the lead time set in the supplier form.'),
'line_ids': fields.one2many('threshold.value.line', 'threshold_value_id', string="Products"),
'fixed_line_ids': fields.one2many('threshold.value.line', 'threshold_value_id2', string="Products"),
'product_ids': fields.function(_get_product_ids, fnct_search=_src_product_ids,
type='many2many', relation='product.product', method=True, string='Products'),
'sublist_id': fields.many2one('product.list', string='List/Sublist'),
'nomen_manda_0': fields.many2one('product.nomenclature', 'Main Type'),
'nomen_manda_1': fields.many2one('product.nomenclature', 'Group'),
'nomen_manda_2': fields.many2one('product.nomenclature', 'Family'),
'nomen_manda_3': fields.many2one('product.nomenclature', 'Root'),
}
_defaults = {
'name': lambda obj, cr, uid, context=None: obj.pool.get('ir.sequence').get(cr, uid, 'threshold.value') or '',
'active': lambda *a: True,
'frequency': lambda *a: 3,
'consumption_method': lambda *a: 'amc',
'consumption_period_from': lambda *a: (now() + RelativeDate(day=1, months=-2)).strftime('%Y-%m-%d'),
'consumption_period_to': lambda *a: (now() + RelativeDate(day=1)).strftime('%Y-%m-%d'),
}
def copy(self, cr, uid, ids, defaults={}, context=None):
'''
Increment the sequence
'''
name = self.pool.get('ir.sequence').get(cr, uid, 'threshold.value') or ''
defaults.update({'name': name})
return super(threshold_value, self).copy(cr, uid, ids, defaults, context=context)
def default_get(self, cr, uid, fields, context=None):
'''
Get the default values for the replenishment rule
'''
res = super(threshold_value, self).default_get(cr, uid, fields, context=context)
company_id = res.get('company_id')
warehouse_id = res.get('warehouse_id')
if not 'company_id' in res:
company_id = self.pool.get('res.company')._company_default_get(cr, uid, 'stock.warehouse.automatic.supply', context=context)
res.update({'company_id': company_id})
if not 'warehouse_id' in res:
warehouse_id = self.pool.get('stock.warehouse').search(cr, uid, [('company_id', '=', company_id)], context=context)[0]
res.update({'warehouse_id': warehouse_id})
if not 'location_id' in res:
location_id = self.pool.get('stock.warehouse').browse(cr, uid, warehouse_id, context=context).lot_stock_id.id
res.update({'location_id': location_id})
return res
def onchange_warehouse_id(self, cr, uid, ids, warehouse_id, context=None):
""" Finds default stock location id for changed warehouse.
@param warehouse_id: Changed id of warehouse.
@return: Dictionary of values.
"""
if warehouse_id:
w = self.pool.get('stock.warehouse').browse(cr, uid, warehouse_id, context=context)
v = {'location_id': w.lot_stock_id.id}
return {'value': v}
return {}
def on_change_method(self, cr, uid, ids, method):
'''
Unfill the consumption period if the method is FMC
'''
res = {}
if method and method == 'fmc':
res.update({'consumption_period_from': False, 'consumption_period_to': False})
elif method and method == 'amc':
res.update({'consumption_period_from': (now() + RelativeDate(day=1, months=-2)).strftime('%Y-%m-%d'),
'consumption_period_to': (now() + RelativeDate(day=1, months=1, days=-1)).strftime('%Y-%m-%d')})
return {'value': res}
def on_change_period(self, cr, uid, ids, from_date, to_date):
'''
Check if the from date is younger than the to date
'''
warn = {}
val = {}
if from_date and to_date and from_date > to_date:
warn = {'title': 'Issue on date',
'message': 'The start date must be younger than end date'}
if from_date:
val.update({'consumption_period_from': (DateFrom(from_date) + RelativeDate(day=1)).strftime('%Y-%m-%d')})
if to_date:
val.update({'consumption_period_to': (DateFrom(to_date) + RelativeDate(months=1, day=1, days=-1)).strftime('%Y-%m-%d')})
return {'value': val, 'warning': warn}
##############################################################################################################################
# The code below aims to enable filtering products regarding their sublist or their nomenclature.
# Then, we fill lines of the one2many object 'threshold.value.line' according to the filtered products
##############################################################################################################################
def onChangeSearchNomenclature(self, cr, uid, id, position, type, nomen_manda_0, nomen_manda_1, nomen_manda_2, nomen_manda_3, num=True, context=None):
return self.pool.get('product.product').onChangeSearchNomenclature(cr, uid, 0, position, type, nomen_manda_0, nomen_manda_1, nomen_manda_2, nomen_manda_3, False, context={'withnum': 1})
def fill_lines(self, cr, uid, ids, context=None):
'''
Fill all lines according to defined nomenclature level and sublist
'''
if context is None:
context = {}
for report in self.browse(cr, uid, ids, context=context):
product_ids = []
products = []
nom = False
field = False
#Â Get all products for the defined nomenclature
if report.nomen_manda_3:
nom = report.nomen_manda_3.id
field = 'nomen_manda_3'
elif report.nomen_manda_2:
nom = report.nomen_manda_2.id
field = 'nomen_manda_2'
elif report.nomen_manda_1:
nom = report.nomen_manda_1.id
field = 'nomen_manda_1'
elif report.nomen_manda_0:
nom = report.nomen_manda_0.id
field = 'nomen_manda_0'
if nom:
product_ids.extend(self.pool.get('product.product').search(cr, uid, [(field, '=', nom)], context=context))
#Â Get all products for the defined list
if report.sublist_id:
for line in report.sublist_id.product_ids:
product_ids.append(line.name.id)
# Check if products in already existing lines are in domain
products = []
for line in report.line_ids:
if line.product_id.id in product_ids:
products.append(line.product_id.id)
else:
self.pool.get('threshold.value.line').unlink(cr, uid, line.id, context=context)
for product in self.pool.get('product.product').browse(cr, uid, product_ids, context=context):
# Check if the product is not already on the report
if product.type not in ('consu', 'service', 'service_recep') and product.id not in products:
self.pool.get('threshold.value.line').create(cr, uid, {'product_id': product.id,
'product_uom_id': product.uom_id.id,
'product_qty': 1.00,
'threshold_value_id': report.id})
return {'type': 'ir.actions.act_window',
'res_model': 'threshold.value',
'view_type': 'form',
'view_mode': 'form',
'res_id': ids[0],
'target': 'dummy',
'context': context}
def dummy(self, cr, uid, ids, context=None):
return True
def get_nomen(self, cr, uid, id, field):
return self.pool.get('product.nomenclature').get_nomen(cr, uid, self, id, field, context={'withnum': 1})
def write(self, cr, uid, ids, vals, context=None):
if vals.get('sublist_id',False):
vals.update({'nomen_manda_0':False,'nomen_manda_1':False,'nomen_manda_2':False,'nomen_manda_3':False})
if vals.get('nomen_manda_0',False):
vals.update({'sublist_id':False})
ret = super(threshold_value, self).write(cr, uid, ids, vals, context=context)
return ret
threshold_value()
class threshold_value_line(osv.osv):
_name = 'threshold.value.line'
_description = 'Threshold Value Line'
_rec_name = 'product_id'
def copy_data(self, cr, uid, ids, defaults={}, context=None):
res = super(threshold_value_line, self).copy_data(cr, uid, ids, defaults, context=context)
if isinstance(res, dict):
if 'threshold_value_id' in res:
del res['threshold_value_id']
if 'threshold_value_id2' in res:
del res['threshold_value_id2']
return res
def create(self, cr, uid, vals, context=None):
'''
Add the second link to the threshold value rule
'''
if 'threshold_value_id' in vals:
vals.update({'threshold_value_id2': vals['threshold_value_id']})
elif 'threshold_value_id2' in vals:
vals.update({'threshold_value_id': vals['threshold_value_id2']})
return super(threshold_value_line, self).create(cr, uid, vals, context=context)
def write(self, cr, uid, ids, vals, context=None):
'''
Add the second link to the threshold value rule
'''
if 'threshold_value_id' in vals:
vals.update({'threshold_value_id2': vals['threshold_value_id']})
elif 'threshold_value_id2' in vals:
vals.update({'threshold_value_id': vals['threshold_value_id2']})
context.update({'fake_threshold_value': vals.get('fake_threshold_value', False)})
vals.update({'fake_threshold_value': 0.00})
return super(threshold_value_line, self).write(cr, uid, ids, vals, context=context)
def _get_values(self, cr, uid, ids, field_name, arg, context=None):
'''
Compute and return the threshold value and qty to order
'''
res = {}
if context is None:
context = {}
for line in self.browse(cr, uid, ids, context=context):
if context.get('fake_threshold_value', False):
res[line.id] = context.get('fake_threshold_value', 0.00)
continue
res[line.id] = 0.00
rule = line.threshold_value_id
context.update({'location_id': rule.location_id.id, 'compute_child': True})
product = self.pool.get('product.product').browse(cr, uid, line.product_id.id, context=context)
result = self._get_threshold_value(cr, uid, line.id, product, rule.compute_method, rule.consumption_method,
rule.consumption_period_from, rule.consumption_period_to, rule.frequency,
rule.safety_month, rule.lead_time, rule.supplier_lt, line.product_uom_id.id, context)
res[line.id] = result.get(field_name, 0.00)
return res
def _get_threshold(self, cr, uid, ids, context={}):
res = {}
for t in self.pool.get('threshold.value').browse(cr, uid, ids, context=context):
for l in t.line_ids:
res[l.id] = True
return res.keys()
_columns = {
'product_id': fields.many2one('product.product', string='Product', required=True),
'product_uom_id': fields.many2one('product.uom', string='Product UoM', required=True),
'product_qty': fields.function(_get_values, method=True, type='float', string='Quantity to order'),
'fake_threshold_value': fields.float(digits=(16,2), string='Threshold value'),
'threshold_value': fields.function(_get_values, method=True, type='float', string='Threshold value',
store={'threshold.value.line': (lambda self, cr, uid, ids, c=None: ids, ['product_id'],20),
'threshold.value': (_get_threshold, ['compute_method',
'consumption_method',
'consumption_period_from',
'consumption_period_to',
'frequency',
'safety_month',
'lead_time',
'supplier_lt'], 10)}),
'fixed_product_qty': fields.float(digits=(16,2), string='Quantity to order'),
'fixed_threshold_value': fields.float(digits=(16,2), string='Threshold value'),
'threshold_value_id': fields.many2one('threshold.value', string='Threshold', ondelete='cascade', required=True),
'threshold_value_id2': fields.many2one('threshold.value', string='Threshold', ondelete='cascade', required=True)
}
def _check_uniqueness(self, cr, uid, ids, context=None):
'''
Check if the product is not already in the current rule
'''
for line in self.browse(cr, uid, ids, context=context):
lines = self.search(cr, uid, [('id', '!=', line.id),
('product_id', '=', line.product_id.id),
'|',
('threshold_value_id2', '=', line.threshold_value_id2.id),
('threshold_value_id', '=', line.threshold_value_id.id)], context=context)
if lines:
return False
return True
_constraints = [
(_check_uniqueness, 'You cannot have two times the same product on the same threshold value rule', ['product_id'])
]
def _get_threshold_value(self, cr, uid, line_id, product, compute_method, consumption_method,
consumption_period_from, consumption_period_to, frequency,
safety_month, lead_time, supplier_lt, uom_id, context=None):
'''
Return the threshold value and ordered qty of a product line
'''
if not context:
context = {}
if line_id and isinstance(line_id, list):
line_id = line_id[0]
cons = 0.00
threshold_value = 0.00
qty_to_order = 0.00
if compute_method == 'computed':
# Get the product available before change the context (from_date and to_date in context)
product_available = product.qty_available
# Change the context to compute consumption
c = context.copy()
c.update({'from_date': consumption_period_from, 'to_date': consumption_period_to})
product = self.pool.get('product.product').browse(cr, uid, product.id, context=c)
cons = consumption_method == 'fmc' and product.reviewed_consumption or product.product_amc
# Set lead time according to choices in threshold rule (supplier or manual lead time)
lt = supplier_lt and float(product.seller_delay)/30.0 or lead_time
# Compute the threshold value
threshold_value = cons * (lt + safety_month)
threshold_value = self.pool.get('product.uom')._compute_qty(cr, uid, product.uom_id.id, threshold_value, product.uom_id.id)
# Compute the quantity to re-order
qty_to_order = cons * (frequency + lt + safety_month)\
- product_available - product.incoming_qty + product.outgoing_qty
qty_to_order = self.pool.get('product.uom')._compute_qty(cr, uid, uom_id or product.uom_id.id, \
qty_to_order, product.uom_id.id)
qty_to_order = qty_to_order > 0.00 and qty_to_order or 0.00
elif line_id:
line = self.browse(cr, uid, line_id, context=context)
threshold_value = line.fixed_threshold_value
qty_to_order = line.fixed_product_qty
return {'threshold_value': threshold_value, 'product_qty': qty_to_order}
def onchange_product_id(self, cr, uid, ids, product_id, compute_method=False, consumption_method=False,
consumption_period_from=False, consumption_period_to=False, frequency=False,
safety_month=False, lead_time=False, supplier_lt=False, fixed_tv=0.00,
fixed_qty=0.00, uom_id=False, field='product_id', context=None):
""" Finds UoM for changed product.
@param product_id: Changed id of product.
@return: Dictionary of values.
"""
if not context:
context = {}
res = {'value': {'product_uom_id': False,
'fake_threshold_value': 0.00,
'threshold_value': 0.00}}
if product_id:
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
if field == 'product_id':
res['value'].update({'product_uom_id': prod.uom_id.id})
elif uom_id:
res['value'].update({'product_uom_id': uom_id})
if compute_method:
tv = self._get_threshold_value(cr, uid, ids, prod, compute_method, consumption_method,
consumption_period_from, consumption_period_to, frequency,
safety_month, lead_time, supplier_lt, uom_id or prod.uom_id.id, context=context)['threshold_value']
res['value'].update({'fake_threshold_value': tv, 'threshold_value': tv})
if prod.uom_id.id:
res = self.pool.get('product.uom')._change_round_up_qty(cr, uid, uom_id or prod.uom_id.id, tv, ['fixed_threshold_value', 'fixed_product_qty', 'threshold_value', 'fake_threshold_value'], result=res)
if prod.uom_id.id and fixed_tv:
res = self.pool.get('product.uom')._change_round_up_qty(cr, uid, uom_id or prod.uom_id.id, fixed_tv, ['fixed_threshold_value'], result=res)
if prod.uom_id.id and fixed_qty:
res = self.pool.get('product.uom')._change_round_up_qty(cr, uid, uom_id or prod.uom_id.id, fixed_tv, ['fixed_product_qty'], result=res)
return res
def onchange_uom_qty(self, cr, uid, ids, uom_id, tv_qty, product_qty):
'''
Check round of qty according to UoM
'''
res = {}
uom_obj = self.pool.get('product.uom')
if tv_qty:
res = uom_obj._change_round_up_qty(cr, uid, uom_id, tv_qty, 'fixed_threshold_value', result=res)
if product_qty:
res = uom_obj._change_round_up_qty(cr, uid, uom_id, product_qty, 'fixed_product_qty', result=res)
return res
threshold_value_line()
|