[CLEAN] Set Withespaces to PEP8 format
[odoo/odoo.git] / addons / hr / hr_department.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 from osv import fields, osv
23 import tools
24
25 class hr_department(osv.osv):
26
27     def name_get(self, cr, uid, ids, context=None):
28         if not len(ids):
29             return []
30         reads = self.read(cr, uid, ids, ['name', 'parent_id'], context)
31         res = []
32         for record in reads:
33             name = record['name']
34             if record['parent_id']:
35                 name = record['parent_id'][1] + ' / ' + name
36             res.append((record['id'], name))
37         return res
38
39     def _dept_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context):
40         res = self.name_get(cr, uid, ids, context)
41         return dict(res)
42
43     _name = "hr.department"
44     _columns = {
45         'name': fields.char('Department Name', size=64, required=True),
46         'complete_name': fields.function(_dept_name_get_fnc, method=True, type="char", string='Name'),
47         'company_id': fields.many2one('res.company', 'Company', select=True, required=True),
48         'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
49         'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
50         'note': fields.text('Note'),
51         'manager_id': fields.many2one('res.users', 'Manager', required=True),
52         'member_ids': fields.many2many('res.users', 'hr_department_user_rel', 'department_id', 'user_id', 'Members'),
53     }
54     def _get_members(self, cr, uid, context={}):
55         mids = self.search(cr, uid, [('manager_id', '=', uid)])
56         result = {uid:1}
57         for m in self.browse(cr, uid, mids, context):
58             for user in m.member_ids:
59                 result[user.id] = 1
60         return result.keys()
61     def _check_recursion(self, cr, uid, ids):
62         level = 100
63         while len(ids):
64             cr.execute('select distinct parent_id from hr_department where id =ANY(%s)', (ids,))
65             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
66             if not level:
67                 return False
68             level -= 1
69         return True
70
71     _constraints = [
72         (_check_recursion, 'Error! You can not create recursive departments.', ['parent_id'])
73     ]
74
75 hr_department()
76
77
78 class ir_action_window(osv.osv):
79     _inherit = 'ir.actions.act_window'
80
81     def read(self, cr, uid, ids, fields=None, context=None,
82             load='_classic_read'):
83         select = ids
84         if isinstance(ids, (int, long)):
85             select = [ids]
86         res = super(ir_action_window, self).read(cr, uid, select, fields=fields,
87                 context=context, load=load)
88         for r in res:
89             mystring = 'department_users_get()'
90             if mystring in (r.get('domain', '[]') or ''):
91                 r['domain'] = r['domain'].replace(mystring, str(
92                     self.pool.get('hr.department')._get_members(cr, uid)))
93         if isinstance(ids, (int, long)):
94             if res:
95                 return res[0]
96             else:
97                 return False
98         return res
99
100 ir_action_window()
101
102 class res_users(osv.osv):
103     _inherit = 'res.users'
104     _description = 'res.users'
105
106     def _parent_compute(self, cr, uid, ids, name, args, context={}):
107         result = {}
108         obj_dept = self.pool.get('hr.department')
109         for user_id in ids:
110             ids_dept = obj_dept.search(cr, uid, [('member_ids', 'in', [user_id])])
111             parent_ids = []
112             if ids_dept:
113                 data_dept = obj_dept.read(cr, uid, ids_dept, ['manager_id'])
114                 parent_ids = map(lambda x: x['manager_id'][0], data_dept)
115             result[user_id] = parent_ids
116         return result
117
118     def _parent_search(self, cr, uid, obj, name, args, context):
119         parent = []
120         for arg in args:
121             if arg[0] == 'parent_id':
122                 parent = arg[2]
123         child_ids = self._child_compute(cr, uid, parent, name, args, {})
124         if not child_ids:
125             return [('id', 'in', [0])]
126         return [('id', 'in', child_ids.get(uid, []))]
127
128     def _child_compute(self, cr, uid, ids, name, args, context={}):
129         obj_dept = self.pool.get('hr.department')
130         obj_user = self.pool.get('res.users')
131         result = {}
132         for manager_id in ids:
133             child_ids = []
134             mgnt_dept_ids = obj_dept.search(cr, uid, [('manager_id', '=', manager_id)])
135             ids_dept = obj_dept.search(cr, uid, [('id', 'child_of', mgnt_dept_ids)])
136             if ids_dept:
137                 data_dept = obj_dept.read(cr, uid, ids_dept, ['member_ids'])
138                 childs = map(lambda x: x['member_ids'], data_dept)
139                 childs = tools.flatten(childs)
140                 childs = obj_user.search(cr, uid, [('id', 'in', childs), ('active', '=', True)])
141                 if manager_id in childs:
142                     childs.remove(manager_id)
143
144                 child_ids.extend(tools.flatten(childs))
145                 set = {}
146                 map(set.__setitem__, child_ids, [])
147                 child_ids = set.keys()
148             else:
149                child_ids = []
150             result[manager_id] = child_ids
151         return result
152
153     def _child_search(self, cr, uid, obj, name, args, context):
154         parent = []
155         for arg in args:
156             if arg[0] == 'child_ids':
157                 parent = arg[2]
158         child_ids = self._child_compute(cr, uid, parent, name, args, {})
159         if not child_ids:
160             return [('id', 'in', [0])]
161         return [('id', 'in', child_ids.get(uid, []))]
162
163     _columns = {
164         'parent_id': fields.function(_parent_compute, relation='res.users', fnct_search=_parent_search, method=True, string="Managers", type='many2many'),
165         'child_ids': fields.function(_child_compute, relation='res.users', fnct_search=_child_search, method=True, string="Subordinates", type='many2many'),
166         'context_department_id': fields.many2one('hr.department', 'Departments'),
167     }
168 res_users()
169 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: