-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock.py
578 lines (512 loc) · 23.9 KB
/
stock.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
##############################################################################
#
# Copyright (c) 2012 NaN Projectes de Programari Lliure, S.L.
# All Rights Reserved.
# http://www.NaN-tic.com
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from osv import osv, fields
from tools.translate import _
import netsvc
class stock_production_lot_qc_trigger_test(osv.osv):
'''
Model that defines the Quality Control Tests which a Production Lot must
to pass in certain situations defined by the Trigger Tag.
'''
_name = 'stock.production.lot.qc.trigger.test'
_description = 'Quality Control Test Triggers by Lot'
_rec_name = 'trigger_id'
_order = 'prodlot_id, sequence'
# stock.production.lot.qc.trigger.test
def name_get(self, cr, uid, ids, context=None):
res = []
for trigger in self.read(cr, uid, ids, ['trigger_id'], context):
res.append((trigger['id'], trigger['trigger_id'][1]))
return res
# stock.production.lot.qc.trigger.test
def _calc_test_state_data(self, cr, uid, ids, fieldnames, args,
context=None):
"""
Calcs the value of fields 'test_approved' and 'test_success'.
The 'test_approved' field will be true if its test is in 'success' or
'failed' final states, and the fields 'test_success' will be True only
if its test is in 'success' state.
"""
res = {}
for lot_trigger in self.browse(cr, uid, ids, context):
res[lot_trigger.id] = {}.fromkeys(fieldnames, False)
if (not lot_trigger.test_id or
lot_trigger.test_id.state not in ('success', 'failed')):
continue
if 'test_approved' in fieldnames:
res[lot_trigger.id]['test_approved'] = True
if ('test_success' in fieldnames and
lot_trigger.test_id.state == 'success'):
res[lot_trigger.id]['test_success'] = True
return res
_columns = {
'prodlot_id': fields.many2one('stock.production.lot', 'Lot',
required=True, ondelete="cascade"),
'sequence': fields.integer('Sequence', required=True),
'trigger_id': fields.many2one('qc.trigger', 'Trigger', required=True,
help="The Quality Control Trigger Tag which defines when must "
"to be created a Test (using the specified template) for a "
"Production Lot of this Product."),
'test_id': fields.many2one('qc.test', 'Test', required=True),
'test_approved': fields.function(_calc_test_state_data, method=True,
type='boolean', string="Test approved?", multi='test_state'),
'test_success': fields.function(_calc_test_state_data, method=True,
type='boolean', string="Test success?", multi='test_state'),
}
_defaults = {
'sequence': 0,
}
stock_production_lot_qc_trigger_test()
class stock_production_lot(osv.osv):
'''
Adds to the Lot an 'state' field and a workflow to manage its status
related to the Quality test that has passed.
It also adds one2many field to the model which defines the Quality Control
Tests that it must to pass.
'''
_inherit = 'stock.production.lot'
# stock.production.lot
def get_available_states(self, cr, uid, context=None):
"""
Returns the list of available states for Lot objects. It is defined as
a function to make easier to extend the list of states.
"""
return [
('draft', 'Draft'),
('pending_test', 'Waiting QC Test'),
('valid', 'Valid'),
('test_failed', 'QC Test Failed'),
('cancel', 'Cancelled'),
]
# stock.production.lot
def __get_available_states(self, cr, uid, context=None):
"""
Returns the list of available states for Lot objects.
It call's the function '_get_available_states' which could be
reimplemented to extend or modify the list of available states.
@see: _get_available_states
"""
return self.get_available_states(cr, uid, context)
# stock.production.lot
def _calc_test_trigger_data(self, cr, uid, ids, fieldnames, args,
context=None):
trigger_proxy = self.pool.get('stock.production.lot.qc.trigger.test')
res = {}
for lot_id in ids:
res[lot_id] = {}.fromkeys(fieldnames, [])
lot_trigger_ids = trigger_proxy.search(cr, uid, [
('prodlot_id', 'in', ids),
], context=context)
for lot_trigger in trigger_proxy.browse(cr, uid, lot_trigger_ids,
context):
lot_id = lot_trigger.prodlot_id.id
if 'qc_test_trigger_ro_ids' in fieldnames:
res[lot_id]['qc_test_trigger_ro_ids'].append(lot_trigger.id)
if 'qc_trigger_ids' in fieldnames:
res[lot_id]['qc_trigger_ids'].append(lot_trigger.trigger_id.id)
return res
_columns = {
# adds the 'readonly' and 'states' attributes
'product_id': fields.many2one('product.product', 'Product',
required=True, readonly=True, states={
'draft': [('readonly', False), ],
}),
'state': fields.selection(__get_available_states, 'State',
required=True, readonly=True),
'active': fields.boolean('Active'),
'qc_test_trigger_ids': fields.one2many(
'stock.production.lot.qc.trigger.test', 'prodlot_id',
'QC Tests', help="Defines the Quality Control Tests that this "
"Production Lot must to pass in certain situations defined by "
"the Trigger Tag."),
# Read Only version of previous field. Better solutions are wellcome
'qc_test_trigger_ro_ids': fields.function(_calc_test_trigger_data,
method=True, type='one2many', string='QC Tests',
relation='stock.production.lot.qc.trigger.test',
multi='trigges'),
'qc_trigger_ids': fields.function(_calc_test_trigger_data, method=True,
type='many2many', string='QC Triggers', relation='qc.trigger',
multi='trigges'),
'current_qc_test_trigger_id': fields.many2one(
'stock.production.lot.qc.trigger.test',
'Current QC Test Trigger',
domain="[('id','in',[x.id for x in qc_test_trigger_ids])]",
readonly=True, states = {
'draft': [('readonly', False), ],
'valid': [('readonly', False), ],
}),
'current_qc_test_id': fields.related('current_qc_test_trigger_id',
'test_id', type='many2one', relation='qc.test',
string="Current QC Test", readonly=True),
}
_defaults = {
'state': 'draft',
'active': True,
}
# stock.production.lot
def onchange_product_id(self, cr, uid, ids, product_id, context):
"""
Void function. Defined to could be extended in dependan modules
"""
return {}
# stock.production.lot
def _calc_qc_test_vals(self, cr, uid, prodlot, context):
"""
Prepare data to create a qc.test instance related to a Production Lot
@param prodlot: Browse object of stock.production.lot
@return: Dictionary of stock.production.lot values
"""
reference = 'stock.production.lot,%d' % prodlot.id
return {
'object_id': reference,
}
# stock.production.lot
def _calc_qc_test_trigger_ids_vals(self, cr, uid, prodlot, trigger_id,
context):
"""
Prepare data to create and write in the field 'qc_test_trigger_ids' of
Production Lot the QC Test Triggers corresponding to the supplied
trigger.
@param prodlot: Browse object of stock.production.lot instance
@param trigger_id: ID of qc.trigger instance
@return: Dictionary of stock.production.lot values
"""
qc_test_proxy = self.pool.get('qc.test')
if trigger_id in [x.id for x in prodlot.qc_trigger_ids]:
netsvc.Logger().notifyChannel(self._name, netsvc.LOG_DEBUG,
"Lot id:%d already has QC Test Trigger for Trigger %d" % (
prodlot.id, trigger_id))
return False
if trigger_id not in [x.id for x in prodlot.product_id.qc_trigger_ids]:
netsvc.Logger().notifyChannel(self._name, netsvc.LOG_DEBUG,
"Product id:%d of Lot id:%d doesn't have QC Template "
"Trigger for Trigger %d" % (prodlot.product_id.id,
prodlot.id, trigger_id))
return False
test_trigger_vals = []
for template_trigger in prodlot.product_id.qc_template_trigger_ids:
if template_trigger.trigger_id.id != trigger_id:
continue
test_vals = self._calc_qc_test_vals(cr, uid, prodlot, context)
test_id = qc_test_proxy.create(cr, uid, test_vals, context)
qc_test_proxy.set_test_template(cr, uid, [test_id],
template_trigger.template_id.id, context=context)
test_trigger_vals.append((0, 0, {
'sequence': template_trigger.sequence,
'trigger_id': template_trigger.trigger_id.id,
'test_id': test_id,
}))
if not test_trigger_vals:
return False
return {
'qc_test_trigger_ids': test_trigger_vals,
}
# stock.production.lot
def create_qc_test_triggers(self, cr, uid, prodlot, trigger_id,
set_next_test, context):
"""
Create and write into Production Lot the Trigger Test corresponding to
supplied Trigger, getting templates configuration and QC templates from
the Product of Lot.
@param prodlot: Browse object of stock.production.lot instance
@param trigger_id: ID of qc.trigger instance
@param set_next_test: Sets the first Test of Trigger as
current_qc_test_id and move Lot workflow to Pending Test
@return: Boolean value depending of triggers has been created
@raise except_osv: If set_next_test is True and Lot is not in 'Draft'
or 'Valid' state
"""
trigger_proxy = self.pool.get('stock.production.lot.qc.trigger.test')
wf_service = netsvc.LocalService("workflow")
if set_next_test and prodlot.state not in ('draft', 'valid'):
raise osv.except_osv(
_("Error setting next test!"),
_("It is trying to set a new next test for Production Lot "
"'%(lot_name)s' (id: %(lot_id)s) but it is in state "
"'%(lot_state)s' when it is expected 'Draft' or 'Valid' "
"states to change the current test.") % {
'lot_name': prodlot.name,
'lot_id': prodlot.id,
'lot_state': prodlot.state,
})
test_trigger_vals = self._calc_qc_test_trigger_ids_vals(cr, uid,
prodlot, trigger_id, context)
if test_trigger_vals:
self.write(cr, uid, [prodlot.id], test_trigger_vals, context)
res = True
else:
res = False
if not set_next_test:
return res
trigger_test_ids = trigger_proxy.search(cr, uid, [
('prodlot_id', '=', prodlot.id),
('trigger_id', '=', trigger_id),
], order='sequence asc', limit=1, context=context)
if trigger_test_ids:
self.write(cr, uid, [prodlot.id], {
'current_qc_test_trigger_id': trigger_test_ids[0],
}, context)
# If 'set_next_test' is true it move the workflow independly if the Lot
# has any test (in this case it will be moved to 'Valid' state
if prodlot.state == 'draft':
wf_service.trg_validate(uid, 'stock.production.lot', prodlot.id,
'confirm', cr)
else:
wf_service.trg_validate(uid, 'stock.production.lot', prodlot.id,
'next_test', cr)
return res
# stock.production.lot
def action_workflow_draft(self, cr, uid, ids, context=None):
"""
Sets the State of Lot to 'Draft' and deactivate it
"""
self.write(cr, uid, ids, {
'state': 'draft',
'active': False,
}, context)
return True
# stock.production.lot
def _get_next_test_trigger_id(self, cr, uid, prodlot, context):
"""
Calculates the Lot Trigger Test Id for the next Test to pass.
If there isn't a current trigger test specified, it returns False.
If the current Trigger Test is not 'Success', it returns this.
In other case, search the next trigger test (by Sequence) with the same
Trigger and return its Id.
@param prodlot: Browse object of stock.production.lot
@return: ID of stock.production.lot.qc.trigger.test instance or False
"""
trigger_proxy = self.pool.get('stock.production.lot.qc.trigger.test')
if not prodlot.current_qc_test_trigger_id:
return False
# if current test is not 'Success' => it remains as Current test
if not prodlot.current_qc_test_trigger_id.test_success:
return prodlot.current_qc_test_trigger_id.id
# if current test is 'Success' => search next test for the same trigger
current_trigger = prodlot.current_qc_test_trigger_id
next_trigger_ids = trigger_proxy.search(cr, uid, [
('prodlot_id', '=', prodlot.id),
('id', '!=', current_trigger.id),
('sequence', '>=', current_trigger.sequence),
('trigger_id', '=', current_trigger.trigger_id.id),
], limit=1, context=context)
return next_trigger_ids and next_trigger_ids[0] or False
# stock.production.lot
def action_workflow_next_test(self, cr, uid, ids, context=None):
"""
Find the Next Test to pass and write it to 'current_qc_test_trigger_id'
field.
It will be empty if there isn't any current test or there aren't any
next test for current Trigger.
It will maintain the current test trigger if its test is not 'Success'
@see: _get_next_test_trigger_id
@param ids: List of IDs of stock.production.lot
"""
for prodlot in self.browse(cr, uid, ids, context):
next_trigger_id = self._get_next_test_trigger_id(cr, uid, prodlot,
context)
self.write(cr, uid, [prodlot.id], {
'current_qc_test_trigger_id': next_trigger_id,
}, context)
return True
# stock.production.lot
def test_valid(self, cr, uid, ids, context=None):
"""
Checks that Production Lot doesn't has Current Test or it is in
'Success' state.
"""
for prodlot in self.browse(cr, uid, ids, context):
if (prodlot.current_qc_test_id and
prodlot.current_qc_test_id.state != 'success'):
return False
return True
# stock.production.lot
def action_workflow_valid(self, cr, uid, ids, context=None):
"""
Sets the State of Lot to 'Valid' and activate the Lot.
"""
self.write(cr, uid, ids, {
'current_qc_test_trigger_id': False,
'state': 'valid',
'active': True,
}, context)
return True
# stock.production.lot
def test_pending_test(self, cr, uid, ids, context=None):
"""
Checks that Production Lot has a Current Test and it is not Success
"""
for prodlot in self.browse(cr, uid, ids, context):
if (not prodlot.current_qc_test_id or
prodlot.current_qc_test_trigger_id.test_success):
return False
return True
# stock.production.lot
def action_workflow_pending_test(self, cr, uid, ids, context=None):
"""
Sets the State of Lot to 'Pending Test' and deactivate it, and returns
the ID of current QC Test.
"""
assert len(ids) == 1, "Unexpected number of Lot IDs in function of " \
"'Pending Test' workflow step."
lot = self.browse(cr, uid, ids[0], context)
assert lot.current_qc_test_id, "The 'Current Test' field is " \
"required for the Lots which reaches the 'Pending Test' " \
"workflow step, but the Lot %d doesn't have." % lot.id
self.write(cr, uid, ids, {
'state': 'pending_test',
'active': False,
}, context)
return lot.current_qc_test_id.id
# stock.production.lot
def action_workflow_test_failed(self, cr, uid, ids, context=None):
"""
Sets the State of Lot to 'Test Failed', deactivate it and returns the
ID of current Test
"""
assert len(ids) == 1, "Unexpected number of Lot IDs in function of " \
"'Pending Test' workflow step."
lot = self.browse(cr, uid, ids[0], context)
assert lot.current_qc_test_id, "The 'Current Test' field is " \
"required for the Lots which reaches the 'Test Failed' " \
"workflow step, but the Lot %d doesn't have." % lot.id
self.write(cr, uid, ids, {
'state': 'test_failed',
'active': False,
}, context)
return lot.current_qc_test_id.id
def test_not_qc_test_subflow(self, cr, uid, ids, context=None):
"""
To be able to go out of 'subflow' activity without subflow signal,
the workitem must to be disassociated of any subflow.
It could be done with 'force_cancel' function which after raise
'cancel' signal
"""
workitem_proxy = self.pool.get('workflow.workitem')
workflow_ids = self.pool.get('workflow').search(cr, uid, [
('osv', '=', 'stock.production.lot'),
('name', '=', 'stock.production.lot.basic'),
], context=context)
assert len(workflow_ids) == 1, "Unexpected number of workflows for " \
"'stock.production.lot' with name " \
"'stock.production.lot.basic'. Expected 1 and found %d" \
% len(workflow_ids)
instance_ids = self.pool.get('workflow.instance').search(cr, uid, [
('wkf_id', '=', workflow_ids[0]),
('res_type', '=', 'stock.production.lot'),
('res_id', 'in', ids),
('state', '=', 'active'),
], context)
if not instance_ids:
netsvc.Logger().notifyChannel(self._name, netsvc.LOG_WARNING,
"Unexpected not found any active Workflow Instance for "
"Production Lots with ids '%s' in "
"test_not_qc_test_subflow function." % str(ids))
return True
workitem_ids = workitem_proxy.search(cr, uid, [
('inst_id', 'in', instance_ids),
('state', '=', 'running'),
('subflow_id', '!=', False),
], context=context)
if not workitem_ids:
return True
lot_w_subflow = [str(w.inst_id.res_id)
for w in workitem_proxy.browse(cr, uid, workitem_ids, context)]
raise osv.except_osv(
_("Error Cancelling Lot with subflow!"),
_("You are trying to cancel the Production Lots with IDs "
"'%(lot_ids)s' which are associated to QC Test workflow, "
"and it is not allowed.\n"
"Please, use the 'Force Cancel' button or function to "
"cancel a Lot in 'Pending Test' or 'Test Failed' state.") % {
'lot_ids': ", ".join(lot_w_subflow),
})
return False
# stock.production.lot
def action_workflow_cancel(self, cr, uid, ids, context=None):
"""
Sets the State of Lot to 'Cancel', deactivate it and leave empty the
'Current QC Test Trigger' field.
"""
self.write(cr, uid, ids, {
'current_qc_test_trigger_id': False,
'state': 'cancel',
'active': False,
}, context)
return True
# stock.production.lot
def action_force_cancel(self, cr, uid, ids, context=None):
"""
Find Workitems (workflow) for these Lots with subflow and disassociate
them and sets their state to 'complete', and raise a 'cancel' signal
for all supplied Lot's ID's.
"""
workitem_proxy = self.pool.get('workflow.workitem')
wf_service = netsvc.LocalService("workflow")
workflow_ids = self.pool.get('workflow').search(cr, uid, [
('osv', '=', 'stock.production.lot'),
('name', '=', 'stock.production.lot.basic'),
], context=context)
assert len(workflow_ids) == 1, "Unexpected number of workflows for " \
"'stock.production.lot' with name " \
"'stock.production.lot.basic'. Expected 1 and found %d" \
% len(workflow_ids)
instance_ids = self.pool.get('workflow.instance').search(cr, uid, [
('wkf_id', '=', workflow_ids[0]),
('res_type', '=', 'stock.production.lot'),
('res_id', 'in', ids),
('state', '=', 'active'),
], context=context)
if instance_ids:
workitem_ids = workitem_proxy.search(cr, uid, [
('inst_id', 'in', instance_ids),
('state', '=', 'running'),
('subflow_id', '!=', False),
], context=context)
if workitem_ids:
workitem_proxy.write(cr, uid, workitem_ids, {
'subflow_id': False,
'state': 'complete',
}, context)
for lot_id in ids:
wf_service.trg_validate(uid, 'stock.production.lot', lot_id,
'cancel', cr)
return True
# stock.production.lot
def copy(self, cr, uid, orig_id, default=None, context=None):
"""
It only duplicate the trigger tests if the context has a True value for
'duplicate_trigger_test' key
"""
if not context or not context.get('duplicate_trigger_test'):
default['qc_test_trigger_ids'] = False
return super(stock_production_lot, self).copy(cr, uid, orig_id,
default, context)
stock_production_lot()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: