~zaber/openobject-addons/timesheet-tz

« back to all changes in this revision

Viewing changes to resource/hr_department.py

[ADD]:added resource module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#    
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2009 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
    _name = "hr.department"
 
27
    _columns = {
 
28
        'name': fields.char('Department Name', size=64, required=True),
 
29
        'company_id': fields.many2one('res.company', 'Company', select=True, required=True),
 
30
        'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
 
31
        'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
 
32
        'note': fields.text('Note'),
 
33
        'manager_id': fields.many2one('res.users', 'Manager', required=True),
 
34
        'member_ids': fields.many2many('res.users', 'hr_department_user_rel', 'department_id', 'user_id', 'Members'),
 
35
    }
 
36
    def _get_members(self,cr, uid, context={}):
 
37
        mids = self.search(cr, uid, [('manager_id','=',uid)])
 
38
        result = {uid:1}
 
39
        for m in self.browse(cr, uid, mids, context):
 
40
            for user in m.member_ids:
 
41
                result[user.id] = 1
 
42
        return result.keys()
 
43
    def _check_recursion(self, cr, uid, ids):
 
44
        level = 100
 
45
        while len(ids):
 
46
            cr.execute('select distinct parent_id from hr_department where id in ('+','.join(map(str, ids))+')')
 
47
            ids = filter(None, map(lambda x:x[0], cr.fetchall()))
 
48
            if not level:
 
49
                return False
 
50
            level -= 1
 
51
        return True
 
52
 
 
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
class res_users(osv.osv):
 
85
    _inherit = 'res.users'
 
86
    _description = 'res.users'
 
87
 
 
88
    def _parent_compute(self, cr, uid, ids, name, args, context={}):
 
89
        result = {}
 
90
        obj_dept = self.pool.get('hr.department')
 
91
        for user_id in ids:
 
92
            ids_dept = obj_dept.search(cr, uid, [('member_ids', 'in', [user_id])])
 
93
            parent_ids = []
 
94
            if ids_dept:
 
95
                data_dept = obj_dept.read(cr, uid, ids_dept, ['manager_id'])
 
96
                parent_ids = map(lambda x: x['manager_id'][0], data_dept)
 
97
            result[user_id] = parent_ids
 
98
        return result
 
99
 
 
100
    def _parent_search(self, cr, uid, obj, name, args):
 
101
        parent = []
 
102
        for arg in args:
 
103
            if arg[0] == 'parent_id':
 
104
                parent = arg[2]
 
105
        child_ids = self._child_compute(cr, uid, parent,name, args, {})
 
106
        if not child_ids:
 
107
            return [('id', 'in', [0])]
 
108
        return [('id', 'in', child_ids.get(uid,[]))]
 
109
 
 
110
    def _child_compute(self, cr, uid, ids, name, args, context={}):
 
111
        obj_dept = self.pool.get('hr.department')
 
112
        obj_user = self.pool.get('res.users')
 
113
        result = {}
 
114
        for manager_id in ids:
 
115
            child_ids = []
 
116
            mgnt_dept_ids = obj_dept.search(cr, uid, [('manager_id', '=', manager_id)])
 
117
            ids_dept = obj_dept.search(cr, uid, [('id', 'child_of', mgnt_dept_ids)])
 
118
            if ids_dept:
 
119
                data_dept = obj_dept.read(cr, uid, ids_dept, ['member_ids'])
 
120
                childs = map(lambda x: x['member_ids'], data_dept)                
 
121
                childs = tools.flatten(childs)
 
122
                childs = obj_user.search(cr, uid, [('id','in',childs),('active','=',True)])                
 
123
                if manager_id in childs:
 
124
                    childs.remove(manager_id)
 
125
                
 
126
                child_ids.extend(tools.flatten(childs))
 
127
                set = {}
 
128
                map(set.__setitem__, child_ids, [])
 
129
                child_ids =  set.keys()
 
130
            else:
 
131
               child_ids = []
 
132
            result[manager_id] = child_ids
 
133
        return result
 
134
 
 
135
    def _child_search(self, cr, uid, obj, name, args):
 
136
        parent = []
 
137
        for arg in args:
 
138
            if arg[0] == 'child_ids':
 
139
                parent = arg[2]
 
140
        child_ids = self._child_compute(cr, uid, parent,name, args, {})
 
141
        if not child_ids:
 
142
            return [('id', 'in', [0])]
 
143
        return [('id', 'in', child_ids.get(uid,[]))]
 
144
 
 
145
    _columns = {
 
146
        'parent_id': fields.function(_parent_compute, relation='res.users',fnct_search=_parent_search, method=True, string="Managers", type='many2many'),
 
147
        'child_ids': fields.function(_child_compute, relation='res.users', fnct_search=_child_search,method=True, string="Subordinates", type='many2many'),
 
148
    }
 
149
 
 
150
 
 
151
res_users()
 
152
 
 
153
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'