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):
27 def name_get(self, cr, uid, ids, context=None):
30 reads = self.read(cr, uid, ids, ['name','parent_id'], context)
34 if record['parent_id']:
35 name = record['parent_id'][1]+' / '+name
36 res.append((record['id'], name))
39 def _dept_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context):
40 res = self.name_get(cr, uid, ids, context)
43 _name = "hr.department"
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'),
54 def _get_members(self,cr, uid, context={}):
55 mids = self.search(cr, uid, [('manager_id','=',uid)])
57 for m in self.browse(cr, uid, mids, context):
58 for user in m.member_ids:
61 def _check_recursion(self, cr, uid, 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()))
72 (_check_recursion, 'Error! You can not create recursive departments.', ['parent_id'])
78 class ir_action_window(osv.osv):
79 _inherit = 'ir.actions.act_window'
81 def read(self, cr, uid, ids, fields=None, context=None,
82 load='_classic_read'):
84 if isinstance(ids, (int, long)):
86 res = super(ir_action_window, self).read(cr, uid, select, fields=fields,
87 context=context, load=load)
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)):
102 class res_users(osv.osv):
103 _inherit = 'res.users'
104 _description = 'res.users'
106 def _parent_compute(self, cr, uid, ids, name, args, context={}):
108 obj_dept = self.pool.get('hr.department')
110 ids_dept = obj_dept.search(cr, uid, [('member_ids', 'in', [user_id])])
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
118 def _parent_search(self, cr, uid, obj, name, args, context):
121 if arg[0] == 'parent_id':
123 child_ids = self._child_compute(cr, uid, parent,name, args, {})
125 return [('id', 'in', [0])]
126 return [('id', 'in', child_ids.get(uid,[]))]
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')
132 for manager_id in 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)])
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)
144 child_ids.extend(tools.flatten(childs))
146 map(set.__setitem__, child_ids, [])
147 child_ids = set.keys()
150 result[manager_id] = child_ids
153 def _child_search(self, cr, uid, obj, name, args, context):
156 if arg[0] == 'child_ids':
158 child_ids = self._child_compute(cr, uid, parent,name, args, {})
160 return [('id', 'in', [0])]
161 return [('id', 'in', child_ids.get(uid,[]))]
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'),
169 class department(osv.osv):
170 _inherit = 'res.users'
171 _description = "Users"
173 'context_department_id': fields.many2one('hr.department', 'Departments'),
176 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: