0a8be26995b280159523255b041129bd10e0d43a
[odoo/odoo.git] / addons / process / process.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2005-TODAY TINY SPRL. (http://tiny.be) All Rights Reserved.
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 import netsvc
30 import pooler, tools
31
32 from osv import fields, osv
33
34 class process_process(osv.osv):
35     _name = "process.process"
36     _description = "Process"
37     _columns = {
38         'name': fields.char('Name', size=30,required=True),
39         'active': fields.boolean('Active'),
40         'note': fields.text('Notes'),
41         'node_ids': fields.one2many('process.node', 'process_id', 'Nodes')
42     }
43     _defaults = {
44         'active' : lambda *a: True,
45     }
46
47     def graph_get(self, cr, uid, id, res_model, res_id, scale, context):
48         pool = pooler.get_pool(cr.dbname)
49         process = pool.get('process.process').browse(cr, uid, [id])[0]
50         current_object = pool.get(res_model).browse(cr, uid, [res_id])[0]
51         nodes = {}
52         start = []
53         transitions = {}
54         for node in process.node_ids:
55
56             data = {}
57
58             data['name'] = node.name
59             data['menu'] = (node.menu_id or None) and node.menu_id.name
60             data['model'] = (node.model_id or None) and node.model_id.model
61             data['kind'] = node.kind
62             data['notes'] = node.note
63             data['active'] = 0
64
65             if node.kind == "state" and node.model_id and node.model_id.model == res_model:
66                 states = node.model_states
67                 states = (states or []) and states.split(',')
68                 data['active'] = (states and current_object.state in states) or not states
69
70             elif node.kind == "router":
71                 #TODO:
72                 pass
73
74             elif node.kind == "subflow":
75                 #TODO: subflow
76                 pass
77
78             nodes[node.id] = data
79             if node.flow_start:
80                 start.append(node.id)
81
82             for tr in node.transition_out:
83                 data = {}
84                 data['name'] = tr.name
85                 data['source'] = tr.source_node_id.id
86                 data['target'] = tr.target_node_id.id
87                 data['notes'] = tr.note
88                 data['buttons'] = buttons = []
89                 for b in tr.action_ids:
90                     button = {}
91                     button['name'] = b.name
92                     button['state'] = b.state
93                     button['action'] = b.action
94                     buttons.append(button)
95                 data['roles'] = roles = []
96                 for r in tr.transition_ids:
97                     if r.role_id:
98                         role = {}
99                         role['name'] = r.role_id.name
100                         roles.append(role)
101                 transitions[tr.id] = data
102
103         g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
104         g.process(start)
105         #g.scale(100, 100, 180, 120)
106         g.scale(*scale)
107         graph = g.result_get()
108         miny = -1
109
110         for k,v in nodes.items():
111             x = graph[k]['y']
112             y = graph[k]['x']
113             if miny == -1:
114                 miny = y
115             miny = min(y, miny)
116             v['x'] = x
117             v['y'] = y
118
119         for k, v in nodes.items():
120             y = v['y']
121             v['y'] = min(y - miny + 10, y)
122         return dict(nodes=nodes, transitions=transitions)
123
124 process_process()
125
126 class process_node(osv.osv):
127     _name = 'process.node'
128     _description ='Process Nodes'
129     _columns = {
130         'name': fields.char('Name', size=30,required=True),
131         'process_id': fields.many2one('process.process', 'Process', required=True),
132         'kind': fields.selection([('state','State'),('router','Router'),('subflow','Subflow')],'Kind of Node', required=True),
133         'menu_id': fields.many2one('ir.ui.menu', 'Related Menu'),
134         'note': fields.text('Notes'),
135         'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
136         'model_states': fields.char('States Expression', size=128),
137         'flow_start': fields.boolean('Starting Flow'),
138         'transition_in': fields.one2many('process.transition', 'target_node_id', 'Starting Transitions'),
139         'transition_out': fields.one2many('process.transition', 'source_node_id', 'Ending Transitions'),
140     }
141     _defaults = {
142         'kind': lambda *args: 'state',
143         'model_states': lambda *args: False,
144         'flow_start': lambda *args: False,
145     }
146 process_node()
147
148 class process_transition(osv.osv):
149     _name = 'process.transition'
150     _description ='Process Transitions'
151     _columns = {
152         'name': fields.char('Name', size=32, required=True),
153         'source_node_id': fields.many2one('process.node', 'Source Node', required=True, ondelete='cascade'),
154         'target_node_id': fields.many2one('process.node', 'Target Node', required=True, ondelete='cascade'),        
155         'action_ids': fields.one2many('process.transition.action', 'transition_id', 'Buttons'),
156         'transition_ids': fields.many2many('workflow.transition', 'process_transition_ids', 'ptr_id', 'wtr_id', 'Workflow Transitions'),
157         'note': fields.text('Description'),
158     }
159 process_transition()
160
161 class process_transition_action(osv.osv):
162     _name = 'process.transition.action'
163     _description ='Process Transitions Actions'
164     _columns = {
165         'name': fields.char('Name', size=32, required=True),
166         'state': fields.selection([('dummy','Dummy'),
167                                    ('method','Object Method'),
168                                    ('workflow','Workflow Trigger'),
169                                    ('action','Action')], 'Type', required=True),
170         'action': fields.char('Action ID', size=64, states={
171             'dummy':[('readonly',1)],
172             'method':[('required',1)],
173             'workflow':[('required',1)],
174             'action':[('required',1)],
175         },),
176         'transition_id': fields.many2one('process.transition', 'Transition', required=True, ondelete='cascade')
177     }
178     _defaults = {
179         'state': lambda *args: 'dummy',
180     }
181 process_transition_action()
182