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 def name_get(self, cr, uid, ids, context=None):
31 reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
35 if record['parent_id']:
36 name = record['parent_id'][1]+' / '+name
37 res.append((record['id'], name))
40 def _dept_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
43 res = self.name_get(cr, uid, ids, context=context)
46 _name = "hr.department"
48 'name': fields.char('Department Name', size=64, required=True),
49 'complete_name': fields.function(_dept_name_get_fnc, method=True, type="char", string='Name'),
50 'company_id': fields.many2one('res.company', 'Company', select=True, required=False),
51 'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
52 'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
53 'note': fields.text('Note'),
57 'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.department', context=c),
60 def _get_members(self, cr, uid, context=None):
63 mids = self.search(cr, uid, [('manager_id', '=', uid)], context=context)
65 for m in self.browse(cr, uid, mids, context=context):
66 for user in m.member_ids:
70 def _check_recursion(self, cr, uid, ids, context=None):
75 cr.execute('select distinct parent_id from hr_department where id IN %s',(tuple(ids),))
76 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
83 (_check_recursion, 'Error! You can not create recursive departments.', ['parent_id'])
88 class ir_action_window(osv.osv):
89 _inherit = 'ir.actions.act_window'
91 def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
94 obj_dept = self.pool.get('hr.department')
96 if isinstance(ids, (int, long)):
98 res = super(ir_action_window, self).read(cr, uid, select, fields=fields, context=context, load=load)
100 mystring = 'department_users_get()'
101 if mystring in (r.get('domain', '[]') or ''):
102 r['domain'] = r['domain'].replace(mystring, str(obj_dept._get_members(cr, uid)))
103 if isinstance(ids, (int, long)):
112 class res_users(osv.osv):
113 _inherit = 'res.users'
114 _description = 'User'
117 'context_department_id': fields.many2one('hr.department', 'Departments'),
122 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: