From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/hr_org_chart/controllers/__init__.py | 2 + addons/hr_org_chart/controllers/hr_org_chart.py | 100 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 addons/hr_org_chart/controllers/__init__.py create mode 100644 addons/hr_org_chart/controllers/hr_org_chart.py (limited to 'addons/hr_org_chart/controllers') diff --git a/addons/hr_org_chart/controllers/__init__.py b/addons/hr_org_chart/controllers/__init__.py new file mode 100644 index 00000000..3177ae13 --- /dev/null +++ b/addons/hr_org_chart/controllers/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -* +from . import hr_org_chart diff --git a/addons/hr_org_chart/controllers/hr_org_chart.py b/addons/hr_org_chart/controllers/hr_org_chart.py new file mode 100644 index 00000000..f9160767 --- /dev/null +++ b/addons/hr_org_chart/controllers/hr_org_chart.py @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import http +from odoo.exceptions import AccessError +from odoo.http import request + + +class HrOrgChartController(http.Controller): + _managers_level = 5 # FP request + + def _check_employee(self, employee_id, **kw): + if not employee_id: # to check + return None + employee_id = int(employee_id) + + if 'allowed_company_ids' in request.env.context: + cids = request.env.context['allowed_company_ids'] + else: + cids = [request.env.company.id] + + Employee = request.env['hr.employee.public'].with_context(allowed_company_ids=cids) + # check and raise + if not Employee.check_access_rights('read', raise_exception=False): + return None + try: + Employee.browse(employee_id).check_access_rule('read') + except AccessError: + return None + else: + return Employee.browse(employee_id) + + def _prepare_employee_data(self, employee): + job = employee.sudo().job_id + return dict( + id=employee.id, + name=employee.name, + link='/mail/view?model=%s&res_id=%s' % ('hr.employee.public', employee.id,), + job_id=job.id, + job_name=job.name or '', + job_title=employee.job_title or '', + direct_sub_count=len(employee.child_ids), + indirect_sub_count=employee.child_all_count, + ) + + @http.route('/hr/get_redirect_model', type='json', auth='user') + def get_redirect_model(self): + if request.env['hr.employee'].check_access_rights('read', raise_exception=False): + return 'hr.employee' + return 'hr.employee.public' + + @http.route('/hr/get_org_chart', type='json', auth='user') + def get_org_chart(self, employee_id, **kw): + + employee = self._check_employee(employee_id, **kw) + if not employee: # to check + return { + 'managers': [], + 'children': [], + } + + # compute employee data for org chart + ancestors, current = request.env['hr.employee.public'].sudo(), employee.sudo() + while current.parent_id and len(ancestors) < self._managers_level+1: + ancestors += current.parent_id + current = current.parent_id + + values = dict( + self=self._prepare_employee_data(employee), + managers=[ + self._prepare_employee_data(ancestor) + for idx, ancestor in enumerate(ancestors) + if idx < self._managers_level + ], + managers_more=len(ancestors) > self._managers_level, + children=[self._prepare_employee_data(child) for child in employee.child_ids], + ) + values['managers'].reverse() + return values + + @http.route('/hr/get_subordinates', type='json', auth='user') + def get_subordinates(self, employee_id, subordinates_type=None, **kw): + """ + Get employee subordinates. + Possible values for 'subordinates_type': + - 'indirect' + - 'direct' + """ + employee = self._check_employee(employee_id, **kw) + if not employee: # to check + return {} + + if subordinates_type == 'direct': + res = employee.child_ids.ids + elif subordinates_type == 'indirect': + res = (employee.subordinate_ids - employee.child_ids).ids + else: + res = employee.subordinate_ids.ids + + return res -- cgit v1.2.3