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