Launchpad automatic translations update.
[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 openerp.osv import fields, osv
23 from openerp import tools
24
25 class hr_department(osv.osv):
26     def name_get(self, cr, uid, ids, context=None):
27         if context is None:
28             context = {}
29         if not ids:
30             return []
31         reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
32         res = []
33         for record in reads:
34             name = record['name']
35             if record['parent_id']:
36                 name = record['parent_id'][1]+' / '+name
37             res.append((record['id'], name))
38         return res
39
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)
42         return dict(res)
43
44     _name = "hr.department"
45     _columns = {
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'),
52     }
53
54     _defaults = {
55         'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.department', context=c),
56                 }
57
58     def _get_members(self, cr, uid, context=None):
59         mids = self.search(cr, uid, [('manager_id', '=', uid)], context=context)
60         result = {uid: 1}
61         for m in self.browse(cr, uid, mids, context=context):
62             for user in m.member_ids:
63                 result[user.id] = 1
64         return result.keys()
65
66     def _check_recursion(self, cr, uid, ids, context=None):
67         if context is None:
68             context = {}
69         level = 100
70         while len(ids):
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()))
73             if not level:
74                 return False
75             level -= 1
76         return True
77
78     _constraints = [
79         (_check_recursion, 'Error! You cannot create recursive departments.', ['parent_id'])
80     ]
81
82 hr_department()
83
84 class ir_action_window(osv.osv):
85     _inherit = 'ir.actions.act_window'
86
87     def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
88         if context is None:
89             context = {}
90         obj_dept = self.pool.get('hr.department')
91         select = ids
92         if isinstance(ids, (int, long)):
93             select = [ids]
94         res = super(ir_action_window, self).read(cr, uid, select, fields=fields, context=context, load=load)
95         for r in res:
96             mystring = 'department_users_get()'
97             if mystring in (r.get('domain', '[]') or ''):
98                 r['domain'] = r['domain'].replace(mystring, str(obj_dept._get_members(cr, uid)))
99         if isinstance(ids, (int, long)):
100             if res:
101                 return res[0]
102             else:
103                 return False
104         return res
105
106 ir_action_window()
107
108
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: