diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/hr_holidays/models/hr_department.py | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/hr_holidays/models/hr_department.py')
| -rw-r--r-- | addons/hr_holidays/models/hr_department.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/addons/hr_holidays/models/hr_department.py b/addons/hr_holidays/models/hr_department.py new file mode 100644 index 00000000..d076113e --- /dev/null +++ b/addons/hr_holidays/models/hr_department.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import datetime +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models + + +class Department(models.Model): + + _inherit = 'hr.department' + + absence_of_today = fields.Integer( + compute='_compute_leave_count', string='Absence by Today') + leave_to_approve_count = fields.Integer( + compute='_compute_leave_count', string='Time Off to Approve') + allocation_to_approve_count = fields.Integer( + compute='_compute_leave_count', string='Allocation to Approve') + total_employee = fields.Integer( + compute='_compute_total_employee', string='Total Employee') + + def _compute_leave_count(self): + Requests = self.env['hr.leave'] + Allocations = self.env['hr.leave.allocation'] + today_date = datetime.datetime.utcnow().date() + today_start = fields.Datetime.to_string(today_date) # get the midnight of the current utc day + today_end = fields.Datetime.to_string(today_date + relativedelta(hours=23, minutes=59, seconds=59)) + + leave_data = Requests.read_group( + [('department_id', 'in', self.ids), + ('state', '=', 'confirm')], + ['department_id'], ['department_id']) + allocation_data = Allocations.read_group( + [('department_id', 'in', self.ids), + ('state', '=', 'confirm')], + ['department_id'], ['department_id']) + absence_data = Requests.read_group( + [('department_id', 'in', self.ids), ('state', 'not in', ['cancel', 'refuse']), + ('date_from', '<=', today_end), ('date_to', '>=', today_start)], + ['department_id'], ['department_id']) + + res_leave = dict((data['department_id'][0], data['department_id_count']) for data in leave_data) + res_allocation = dict((data['department_id'][0], data['department_id_count']) for data in allocation_data) + res_absence = dict((data['department_id'][0], data['department_id_count']) for data in absence_data) + + for department in self: + department.leave_to_approve_count = res_leave.get(department.id, 0) + department.allocation_to_approve_count = res_allocation.get(department.id, 0) + department.absence_of_today = res_absence.get(department.id, 0) + + def _compute_total_employee(self): + emp_data = self.env['hr.employee'].read_group([('department_id', 'in', self.ids)], ['department_id'], ['department_id']) + result = dict((data['department_id'][0], data['department_id_count']) for data in emp_data) + for department in self: + department.total_employee = result.get(department.id, 0) |
