1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields,osv
25 class hr_department(osv.osv):
26 _name = "hr.department"
28 'name': fields.char('Department Name', size=64, required=True),
29 'company_id': fields.many2one('res.company', 'Company', select=True, required=True),
30 'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
31 'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
32 'note': fields.text('Note'),
33 'manager_id': fields.many2one('res.users', 'Manager', required=True),
34 'member_ids': fields.many2many('res.users', 'hr_department_user_rel', 'department_id', 'user_id', 'Members'),
36 def _get_members(self,cr, uid, context={}):
37 mids = self.search(cr, uid, [('manager_id','=',uid)])
39 for m in self.browse(cr, uid, mids, context):
40 for user in m.member_ids:
43 def _check_recursion(self, cr, uid, ids):
46 cr.execute('select distinct parent_id from hr_department where id =ANY(%s)',(ids,))
47 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
54 (_check_recursion, 'Error! You can not create recursive departments.', ['parent_id'])
60 class ir_action_window(osv.osv):
61 _inherit = 'ir.actions.act_window'
63 def read(self, cr, uid, ids, fields=None, context=None,
64 load='_classic_read'):
66 if isinstance(ids, (int, long)):
68 res = super(ir_action_window, self).read(cr, uid, select, fields=fields,
69 context=context, load=load)
71 mystring = 'department_users_get()'
72 if mystring in (r.get('domain', '[]') or ''):
73 r['domain'] = r['domain'].replace(mystring, str(
74 self.pool.get('hr.department')._get_members(cr, uid)))
75 if isinstance(ids, (int, long)):
84 class res_users(osv.osv):
85 _inherit = 'res.users'
86 _description = 'res.users'
88 def _parent_compute(self, cr, uid, ids, name, args, context={}):
90 obj_dept = self.pool.get('hr.department')
92 ids_dept = obj_dept.search(cr, uid, [('member_ids', 'in', [user_id])])
95 data_dept = obj_dept.read(cr, uid, ids_dept, ['manager_id'])
96 parent_ids = map(lambda x: x['manager_id'][0], data_dept)
97 result[user_id] = parent_ids
100 def _parent_search(self, cr, uid, obj, name, args, context):
103 if arg[0] == 'parent_id':
105 child_ids = self._child_compute(cr, uid, parent,name, args, {})
107 return [('id', 'in', [0])]
108 return [('id', 'in', child_ids.get(uid,[]))]
110 def _child_compute(self, cr, uid, ids, name, args, context={}):
111 obj_dept = self.pool.get('hr.department')
112 obj_user = self.pool.get('res.users')
114 for manager_id in ids:
116 mgnt_dept_ids = obj_dept.search(cr, uid, [('manager_id', '=', manager_id)])
117 ids_dept = obj_dept.search(cr, uid, [('id', 'child_of', mgnt_dept_ids)])
119 data_dept = obj_dept.read(cr, uid, ids_dept, ['member_ids'])
120 childs = map(lambda x: x['member_ids'], data_dept)
121 childs = tools.flatten(childs)
122 childs = obj_user.search(cr, uid, [('id','in',childs),('active','=',True)])
123 if manager_id in childs:
124 childs.remove(manager_id)
126 child_ids.extend(tools.flatten(childs))
128 map(set.__setitem__, child_ids, [])
129 child_ids = set.keys()
132 result[manager_id] = child_ids
135 def _child_search(self, cr, uid, obj, name, args, context):
138 if arg[0] == 'child_ids':
140 child_ids = self._child_compute(cr, uid, parent,name, args, {})
142 return [('id', 'in', [0])]
143 return [('id', 'in', child_ids.get(uid,[]))]
146 'parent_id': fields.function(_parent_compute, relation='res.users',fnct_search=_parent_search, method=True, string="Managers", type='many2many'),
147 'child_ids': fields.function(_child_compute, relation='res.users', fnct_search=_child_search,method=True, string="Subordinates", type='many2many'),
151 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: