[MERGE] merge with lp:openobject-server
[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
22 import wkf_logs
23 import workitem
24
25 import openerp.netsvc as netsvc
26 import openerp.pooler as pooler
27
28 def create(cr, ident, wkf_id):
29     (uid,res_type,res_id) = ident
30     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))
31     id_new = cr.fetchone()[0]
32     cr.execute('select * from wkf_activity where flow_start=True and wkf_id=%s', (wkf_id,))
33     res = cr.dictfetchall()
34     stack = []
35     workitem.create(cr, res, id_new, ident, stack=stack)
36     update(cr, id_new, ident)
37     return id_new
38
39 def delete(cr, ident):
40     (uid,res_type,res_id) = ident
41     cr.execute('delete from wkf_instance where res_id=%s and res_type=%s', (res_id,res_type))
42
43 def validate(cr, inst_id, ident, signal, force_running=False):
44     cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,))
45     stack = []
46     for witem in cr.dictfetchall():
47         stack = []
48         workitem.process(cr, witem, ident, signal, force_running, stack=stack)
49         # An action is returned
50     _update_end(cr, inst_id, ident)
51     return stack and stack[0] or False
52
53 def update(cr, inst_id, ident):
54     cr.execute("select * from wkf_workitem where inst_id=%s", (inst_id,))
55     for witem in cr.dictfetchall():
56         stack = []
57         workitem.process(cr, witem, ident, stack=stack)
58     return _update_end(cr, inst_id, ident)
59
60 def _update_end(cr, inst_id, ident):
61     cr.execute('select wkf_id from wkf_instance where id=%s', (inst_id,))
62     wkf_id = cr.fetchone()[0]
63     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,))
64     ok=True
65     for r in cr.fetchall():
66         if (r[0]<>'complete') or not r[1]:
67             ok=False
68             break
69     if ok:
70         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,))
71         act_names = cr.fetchall()
72         cr.execute("update wkf_instance set state='complete' where id=%s", (inst_id,))
73         cr.execute("update wkf_workitem set state='complete' where subflow_id=%s", (inst_id,))
74         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,))
75         for i in cr.fetchall():
76             for act_name in act_names:
77                 validate(cr, i[0], (ident[0],i[1],i[2]), 'subflow.'+act_name[0])
78     return ok
79
80
81 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
82