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 openerp.osv import fields, osv
23 from openerp import tools
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):
41 res = self.name_get(cr, uid, ids, context=context)
44 _name = "hr.department"
46 'name': fields.char('Department Name', size=64, required=True),
47 'complete_name': fields.function(_dept_name_get_fnc, type="char", string='Name'),
48 'company_id': fields.many2one('res.company', 'Company', select=True, required=False),
49 'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
50 'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
51 'note': fields.text('Note'),
55 'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.department', context=c),
58 def _get_members(self, cr, uid, context=None):
59 mids = self.search(cr, uid, [('manager_id', '=', uid)], context=context)
61 for m in self.browse(cr, uid, mids, context=context):
62 for user in m.member_ids:
66 def _check_recursion(self, cr, uid, ids, context=None):
71 cr.execute('select distinct parent_id from hr_department where id IN %s',(tuple(ids),))
72 ids = filter(None, map(lambda x:x[0], cr.fetchall()))
79 (_check_recursion, 'Error! You cannot create recursive departments.', ['parent_id'])
83 class ir_action_window(osv.osv):
84 _inherit = 'ir.actions.act_window'
86 def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
89 obj_dept = self.pool.get('hr.department')
91 if isinstance(ids, (int, long)):
93 res = super(ir_action_window, self).read(cr, uid, select, fields=fields, context=context, load=load)
95 mystring = 'department_users_get()'
96 if mystring in (r.get('domain', '[]') or ''):
97 r['domain'] = r['domain'].replace(mystring, str(obj_dept._get_members(cr, uid)))
98 if isinstance(ids, (int, long)):
107 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: