revert change of workflow
[odoo/odoo.git] / openerp / workflow / instance.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 import workitem
22
23 def create(cr, ident, wkf_id):
24     (uid,res_type,res_id) = ident
25     cr.execute('insert into wkf_instance (res_type,res_id,uid,wkf_id) values (%s,%s,%s,%s) RETURNING id', (res_type,res_id,uid,wkf_id))
26     id_new = cr.fetchone()[0]
27     cr.execute('select * from wkf_activity where flow_start=True and wkf_id=%s', (wkf_id,))
28     res = cr.dictfetchall()
29     stack = []
30     workitem.create(cr, res, id_new, ident, stack=stack)
31     update(cr, id_new, ident)
32     return id_new
33
34 def delete(cr, ident):
35     (uid,res_type,res_id) = ident
36     cr.execute('delete from wkf_instance where res_id=%s and res_type=%s', (res_id,res_type))
37
38 def validate(cr, inst_id, ident, signal, force_running=False):
39     cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,))
40     stack = []
41     for witem in cr.dictfetchall():
42         stack = []
43         workitem.process(cr, witem, ident, signal, force_running, stack=stack)
44         # An action is returned
45     _update_end(cr, inst_id, ident)
46     return stack and stack[0] or False
47
48 def update(cr, inst_id, ident):
49     cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,))
50     for witem in cr.dictfetchall():
51         stack = []
52         workitem.process(cr, witem, ident, stack=stack)
53     return _update_end(cr, inst_id, ident)
54
55 def _update_end(cr, inst_id, ident):
56     cr.execute('select wkf_id from wkf_instance where id=%s', (inst_id,))
57     wkf_id = cr.fetchone()[0]
58     cr.execute('select state,flow_stop from wkf_workitem w left join wkf_activity a on (a.id=w.act_id) where w.inst_id=%s', (inst_id,))
59     ok=True
60     for r in cr.fetchall():
61         if (r[0]<>'complete') or not r[1]:
62             ok=False
63             break
64     if ok:
65         cr.execute('select distinct a.name from wkf_activity a left join wkf_workitem w on (a.id=w.act_id) where w.inst_id=%s', (inst_id,))
66         act_names = cr.fetchall()
67         cr.execute("update wkf_instance set state='complete' where id=%s", (inst_id,))
68         cr.execute("update wkf_workitem set state='complete' where subflow_id=%s", (inst_id,))
69         cr.execute("select i.id,w.osv,i.res_id from wkf_instance i left join wkf w on (i.wkf_id=w.id) where i.id IN (select inst_id from wkf_workitem where subflow_id=%s)", (inst_id,))
70         for i in cr.fetchall():
71             for act_name in act_names:
72                 validate(cr, i[0], (ident[0],i[1],i[2]), 'subflow.'+act_name[0])
73     return ok
74
75
76 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
77