2009-10-14 11:15:34 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2007-06-19 17:01:25 +00:00
|
|
|
##############################################################################
|
2009-11-26 12:10:51 +00:00
|
|
|
#
|
2009-03-18 10:40:38 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2010-01-12 09:18:39 +00:00
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 11:15:34 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2007-06-19 17:01:25 +00:00
|
|
|
#
|
2008-11-03 19:18:56 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 11:15:34 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2007-06-19 17:01:25 +00:00
|
|
|
#
|
2009-10-14 11:15:34 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2009-11-26 12:10:51 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-06-19 17:01:25 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2010-08-13 05:28:18 +00:00
|
|
|
from osv import fields, osv
|
2007-06-19 17:01:25 +00:00
|
|
|
import tools
|
|
|
|
|
|
|
|
class hr_department(osv.osv):
|
2010-02-17 05:25:53 +00:00
|
|
|
def name_get(self, cr, uid, ids, context=None):
|
2010-07-03 10:21:52 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-10-27 12:49:59 +00:00
|
|
|
if not ids:
|
2010-02-17 05:25:53 +00:00
|
|
|
return []
|
2010-07-26 06:15:27 +00:00
|
|
|
reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
|
2010-02-17 05:25:53 +00:00
|
|
|
res = []
|
|
|
|
for record in reads:
|
|
|
|
name = record['name']
|
|
|
|
if record['parent_id']:
|
|
|
|
name = record['parent_id'][1]+' / '+name
|
|
|
|
res.append((record['id'], name))
|
|
|
|
return res
|
|
|
|
|
2010-07-03 10:21:52 +00:00
|
|
|
def _dept_name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
|
2010-07-07 07:04:42 +00:00
|
|
|
res = self.name_get(cr, uid, ids, context=context)
|
2010-02-17 05:25:53 +00:00
|
|
|
return dict(res)
|
|
|
|
|
2008-07-22 15:11:28 +00:00
|
|
|
_name = "hr.department"
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Department Name', size=64, required=True),
|
2011-07-01 23:41:24 +00:00
|
|
|
'complete_name': fields.function(_dept_name_get_fnc, type="char", string='Name'),
|
2010-03-22 05:12:40 +00:00
|
|
|
'company_id': fields.many2one('res.company', 'Company', select=True, required=False),
|
2008-07-22 15:11:28 +00:00
|
|
|
'parent_id': fields.many2one('hr.department', 'Parent Department', select=True),
|
2009-01-27 11:15:46 +00:00
|
|
|
'child_ids': fields.one2many('hr.department', 'parent_id', 'Child Departments'),
|
2008-07-22 15:11:28 +00:00
|
|
|
'note': fields.text('Note'),
|
|
|
|
}
|
2010-07-07 07:04:42 +00:00
|
|
|
|
|
|
|
_defaults = {
|
2010-08-12 04:17:18 +00:00
|
|
|
'company_id': lambda self, cr, uid, c: self.pool.get('res.company')._company_default_get(cr, uid, 'hr.department', context=c),
|
2010-07-07 07:04:42 +00:00
|
|
|
}
|
2010-07-03 10:21:52 +00:00
|
|
|
|
2010-08-12 04:17:18 +00:00
|
|
|
def _get_members(self, cr, uid, context=None):
|
2010-07-26 06:15:27 +00:00
|
|
|
mids = self.search(cr, uid, [('manager_id', '=', uid)], context=context)
|
2010-07-07 07:04:42 +00:00
|
|
|
result = {uid: 1}
|
|
|
|
for m in self.browse(cr, uid, mids, context=context):
|
2008-07-22 15:11:28 +00:00
|
|
|
for user in m.member_ids:
|
|
|
|
result[user.id] = 1
|
|
|
|
return result.keys()
|
2010-07-03 10:21:52 +00:00
|
|
|
|
2010-07-07 07:04:42 +00:00
|
|
|
def _check_recursion(self, cr, uid, ids, context=None):
|
2010-07-26 06:15:27 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2008-07-22 15:11:28 +00:00
|
|
|
level = 100
|
|
|
|
while len(ids):
|
2010-06-10 13:34:19 +00:00
|
|
|
cr.execute('select distinct parent_id from hr_department where id IN %s',(tuple(ids),))
|
2008-07-22 15:11:28 +00:00
|
|
|
ids = filter(None, map(lambda x:x[0], cr.fetchall()))
|
|
|
|
if not level:
|
|
|
|
return False
|
|
|
|
level -= 1
|
|
|
|
return True
|
2009-03-18 10:40:38 +00:00
|
|
|
|
2008-07-22 15:11:28 +00:00
|
|
|
_constraints = [
|
|
|
|
(_check_recursion, 'Error! You can not create recursive departments.', ['parent_id'])
|
|
|
|
]
|
2007-06-19 17:01:25 +00:00
|
|
|
|
|
|
|
hr_department()
|
|
|
|
|
2007-06-28 13:44:47 +00:00
|
|
|
class ir_action_window(osv.osv):
|
2008-07-22 15:11:28 +00:00
|
|
|
_inherit = 'ir.actions.act_window'
|
2007-09-26 05:20:18 +00:00
|
|
|
|
2010-07-03 10:21:52 +00:00
|
|
|
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
2010-07-26 06:15:27 +00:00
|
|
|
obj_dept = self.pool.get('hr.department')
|
2008-07-22 15:11:28 +00:00
|
|
|
select = ids
|
|
|
|
if isinstance(ids, (int, long)):
|
|
|
|
select = [ids]
|
2010-07-03 10:21:52 +00:00
|
|
|
res = super(ir_action_window, self).read(cr, uid, select, fields=fields, context=context, load=load)
|
2008-07-22 15:11:28 +00:00
|
|
|
for r in res:
|
|
|
|
mystring = 'department_users_get()'
|
|
|
|
if mystring in (r.get('domain', '[]') or ''):
|
2010-07-26 06:15:27 +00:00
|
|
|
r['domain'] = r['domain'].replace(mystring, str(obj_dept._get_members(cr, uid)))
|
2008-07-22 15:11:28 +00:00
|
|
|
if isinstance(ids, (int, long)):
|
2008-12-08 17:08:40 +00:00
|
|
|
if res:
|
|
|
|
return res[0]
|
|
|
|
else:
|
|
|
|
return False
|
2008-07-22 15:11:28 +00:00
|
|
|
return res
|
2007-09-26 05:20:18 +00:00
|
|
|
|
2007-06-28 13:44:47 +00:00
|
|
|
ir_action_window()
|
|
|
|
|
2009-03-18 10:40:38 +00:00
|
|
|
class res_users(osv.osv):
|
|
|
|
_inherit = 'res.users'
|
2010-05-19 18:32:32 +00:00
|
|
|
_description = 'User'
|
2009-03-18 10:40:38 +00:00
|
|
|
|
|
|
|
_columns = {
|
2010-02-19 11:19:13 +00:00
|
|
|
'context_department_id': fields.many2one('hr.department', 'Departments'),
|
2009-09-24 10:46:21 +00:00
|
|
|
}
|
2010-07-03 10:21:52 +00:00
|
|
|
|
2009-03-18 10:40:38 +00:00
|
|
|
res_users()
|
2010-07-03 10:21:52 +00:00
|
|
|
|
2010-09-23 07:52:53 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|