[IMP] Upgrade the translations
[odoo/odoo.git] / addons / hr / hr_department.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields,osv
24 import tools
25
26 class hr_department(osv.osv):
27     _name = "hr.department"
28     _columns = {
29         'name': fields.char('Department Name', size=64, required=True),
30         'company_id': fields.many2one('res.company', 'Company', select=True, required=True),
31         'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
32         'child_ids': fields.one2many('hr.department', 'parent_id', 'Childs Departments'),
33         'note': fields.text('Note'),
34         'manager_id': fields.many2one('res.users', 'Manager', required=True),
35         'member_ids': fields.many2many('res.users', 'hr_department_user_rel', 'department_id', 'user_id', 'Members'),
36     }
37     def _get_members(self,cr, uid, context={}):
38         mids = self.search(cr, uid, [('manager_id','=',uid)])
39         result = {uid:1}
40         for m in self.browse(cr, uid, mids, context):
41             for user in m.member_ids:
42                 result[user.id] = 1
43         return result.keys()
44     def _check_recursion(self, cr, uid, ids):
45         level = 100
46         while len(ids):
47             cr.execute('select distinct parent_id from hr_department where id in ('+','.join(map(str,ids))+')')
48             ids = filter(None, map(lambda x:x[0], cr.fetchall()))
49             if not level:
50                 return False
51             level -= 1
52         return True
53     _constraints = [
54         (_check_recursion, 'Error! You can not create recursive departments.', ['parent_id'])
55     ]
56
57 hr_department()
58
59
60 class ir_action_window(osv.osv):
61     _inherit = 'ir.actions.act_window'
62
63     def read(self, cr, uid, ids, fields=None, context=None,
64             load='_classic_read'):
65         select = ids
66         if isinstance(ids, (int, long)):
67             select = [ids]
68         res = super(ir_action_window, self).read(cr, uid, select, fields=fields,
69                 context=context, load=load)
70         for r in res:
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)):
76             if res:
77                 return res[0]
78             else:
79                 return False
80         return res
81
82 ir_action_window()
83
84
85
86 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
87