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_holidays/report/__init__.py | 6 + .../hr_holidays/report/holidays_summary_report.py | 124 +++++++++++++++++++++ addons/hr_holidays/report/hr_holidays_reports.xml | 28 +++++ .../hr_holidays/report/hr_holidays_templates.xml | 82 ++++++++++++++ addons/hr_holidays/report/hr_leave_report.py | 111 ++++++++++++++++++ .../hr_holidays/report/hr_leave_report_calendar.py | 68 +++++++++++ .../report/hr_leave_report_calendar.xml | 40 +++++++ addons/hr_holidays/report/hr_leave_reports.xml | 118 ++++++++++++++++++++ 8 files changed, 577 insertions(+) create mode 100644 addons/hr_holidays/report/__init__.py create mode 100644 addons/hr_holidays/report/holidays_summary_report.py create mode 100644 addons/hr_holidays/report/hr_holidays_reports.xml create mode 100644 addons/hr_holidays/report/hr_holidays_templates.xml create mode 100644 addons/hr_holidays/report/hr_leave_report.py create mode 100644 addons/hr_holidays/report/hr_leave_report_calendar.py create mode 100644 addons/hr_holidays/report/hr_leave_report_calendar.xml create mode 100644 addons/hr_holidays/report/hr_leave_reports.xml (limited to 'addons/hr_holidays/report') diff --git a/addons/hr_holidays/report/__init__.py b/addons/hr_holidays/report/__init__.py new file mode 100644 index 00000000..c59b1e68 --- /dev/null +++ b/addons/hr_holidays/report/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import holidays_summary_report +from . import hr_leave_report +from . import hr_leave_report_calendar diff --git a/addons/hr_holidays/report/holidays_summary_report.py b/addons/hr_holidays/report/holidays_summary_report.py new file mode 100644 index 00000000..74d41d19 --- /dev/null +++ b/addons/hr_holidays/report/holidays_summary_report.py @@ -0,0 +1,124 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import calendar + +from datetime import timedelta +from dateutil.relativedelta import relativedelta +from odoo import api, fields, models, _ +from odoo.exceptions import UserError + + +class HrHolidaySummaryReport(models.AbstractModel): + _name = 'report.hr_holidays.report_holidayssummary' + _description = 'Holidays Summary Report' + + def _get_header_info(self, start_date, holiday_type): + st_date = fields.Date.from_string(start_date) + return { + 'start_date': fields.Date.to_string(st_date), + 'end_date': fields.Date.to_string(st_date + relativedelta(days=59)), + 'holiday_type': 'Confirmed and Approved' if holiday_type == 'both' else holiday_type + } + + def _date_is_day_off(self, date): + return date.weekday() in (calendar.SATURDAY, calendar.SUNDAY,) + + def _get_day(self, start_date): + res = [] + start_date = fields.Date.from_string(start_date) + for x in range(0, 60): + color = '#ababab' if self._date_is_day_off(start_date) else '' + res.append({'day_str': start_date.strftime('%a'), 'day': start_date.day , 'color': color}) + start_date = start_date + relativedelta(days=1) + return res + + def _get_months(self, start_date): + # it works for geting month name between two dates. + res = [] + start_date = fields.Date.from_string(start_date) + end_date = start_date + relativedelta(days=59) + while start_date <= end_date: + last_date = start_date + relativedelta(day=1, months=+1, days=-1) + if last_date > end_date: + last_date = end_date + month_days = (last_date - start_date).days + 1 + res.append({'month_name': start_date.strftime('%B'), 'days': month_days}) + start_date += relativedelta(day=1, months=+1) + return res + + def _get_leaves_summary(self, start_date, empid, holiday_type): + res = [] + count = 0 + start_date = fields.Date.from_string(start_date) + end_date = start_date + relativedelta(days=59) + for index in range(0, 60): + current = start_date + timedelta(index) + res.append({'day': current.day, 'color': ''}) + if self._date_is_day_off(current) : + res[index]['color'] = '#ababab' + # count and get leave summary details. + holiday_type = ['confirm','validate'] if holiday_type == 'both' else ['confirm'] if holiday_type == 'Confirmed' else ['validate'] + holidays = self.env['hr.leave'].search([ + ('employee_id', '=', empid), ('state', 'in', holiday_type), + ('date_from', '<=', str(end_date)), + ('date_to', '>=', str(start_date)) + ]) + for holiday in holidays: + # Convert date to user timezone, otherwise the report will not be consistent with the + # value displayed in the interface. + date_from = fields.Datetime.from_string(holiday.date_from) + date_from = fields.Datetime.context_timestamp(holiday, date_from).date() + date_to = fields.Datetime.from_string(holiday.date_to) + date_to = fields.Datetime.context_timestamp(holiday, date_to).date() + for index in range(0, ((date_to - date_from).days + 1)): + if date_from >= start_date and date_from <= end_date: + res[(date_from-start_date).days]['color'] = holiday.holiday_status_id.color_name + date_from += timedelta(1) + count += holiday.number_of_days + employee = self.env['hr.employee'].browse(empid) + return {'emp': employee.name, 'display': res, 'sum': count} + + def _get_data_from_report(self, data): + res = [] + Employee = self.env['hr.employee'] + if 'depts' in data: + for department in self.env['hr.department'].browse(data['depts']): + res.append({ + 'dept': department.name, + 'data': [ + self._get_leaves_summary(data['date_from'], emp.id, data['holiday_type']) + for emp in Employee.search([('department_id', '=', department.id)]) + ], + 'color': self._get_day(data['date_from']), + }) + elif 'emp' in data: + res.append({'data': [ + self._get_leaves_summary(data['date_from'], emp.id, data['holiday_type']) + for emp in Employee.browse(data['emp']) + ]}) + return res + + def _get_holidays_status(self): + res = [] + for holiday in self.env['hr.leave.type'].search([]): + res.append({'color': holiday.color_name, 'name': holiday.name}) + return res + + @api.model + def _get_report_values(self, docids, data=None): + if not data.get('form'): + raise UserError(_("Form content is missing, this report cannot be printed.")) + + holidays_report = self.env['ir.actions.report']._get_report_from_name('hr_holidays.report_holidayssummary') + holidays = self.env['hr.leave'].browse(self.ids) + return { + 'doc_ids': self.ids, + 'doc_model': holidays_report.model, + 'docs': holidays, + 'get_header_info': self._get_header_info(data['form']['date_from'], data['form']['holiday_type']), + 'get_day': self._get_day(data['form']['date_from']), + 'get_months': self._get_months(data['form']['date_from']), + 'get_data_from_report': self._get_data_from_report(data['form']), + 'get_holidays_status': self._get_holidays_status(), + } diff --git a/addons/hr_holidays/report/hr_holidays_reports.xml b/addons/hr_holidays/report/hr_holidays_reports.xml new file mode 100644 index 00000000..fbec8363 --- /dev/null +++ b/addons/hr_holidays/report/hr_holidays_reports.xml @@ -0,0 +1,28 @@ + + + + + Time Off Summary + hr.holidays.summary.dept + qweb-pdf + hr_holidays.report_holidayssummary + hr_holidays.report_holidayssummary + + + + + + + + Time Off Summary + hr.leave.allocation + qweb-pdf + hr_holidays.report_holidayssummary + hr_holidays.report_holidayssummary + + + + + + + diff --git a/addons/hr_holidays/report/hr_holidays_templates.xml b/addons/hr_holidays/report/hr_holidays_templates.xml new file mode 100644 index 00000000..dc2d46b8 --- /dev/null +++ b/addons/hr_holidays/report/hr_holidays_templates.xml @@ -0,0 +1,82 @@ + + + + + + diff --git a/addons/hr_holidays/report/hr_leave_report.py b/addons/hr_holidays/report/hr_leave_report.py new file mode 100644 index 00000000..3ea1f0e7 --- /dev/null +++ b/addons/hr_holidays/report/hr_leave_report.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models, tools, exceptions, _ +from odoo.osv import expression + + +class LeaveReport(models.Model): + _name = "hr.leave.report" + _description = 'Time Off Summary / Report' + _auto = False + _order = "date_from DESC, employee_id" + + employee_id = fields.Many2one('hr.employee', string="Employee", readonly=True) + name = fields.Char('Description', readonly=True) + number_of_days = fields.Float('Number of Days', readonly=True) + leave_type = fields.Selection([ + ('allocation', 'Allocation'), + ('request', 'Time Off') + ], string='Request Type', readonly=True) + department_id = fields.Many2one('hr.department', string='Department', readonly=True) + category_id = fields.Many2one('hr.employee.category', string='Employee Tag', readonly=True) + holiday_status_id = fields.Many2one("hr.leave.type", string="Leave Type", readonly=True) + state = fields.Selection([ + ('draft', 'To Submit'), + ('cancel', 'Cancelled'), + ('confirm', 'To Approve'), + ('refuse', 'Refused'), + ('validate1', 'Second Approval'), + ('validate', 'Approved') + ], string='Status', readonly=True) + holiday_type = fields.Selection([ + ('employee', 'By Employee'), + ('category', 'By Employee Tag') + ], string='Allocation Mode', readonly=True) + date_from = fields.Datetime('Start Date', readonly=True) + date_to = fields.Datetime('End Date', readonly=True) + payslip_status = fields.Boolean('Reported in last payslips', readonly=True) + + def init(self): + tools.drop_view_if_exists(self._cr, 'hr_leave_report') + + self._cr.execute(""" + CREATE or REPLACE view hr_leave_report as ( + SELECT row_number() over(ORDER BY leaves.employee_id) as id, + leaves.employee_id as employee_id, leaves.name as name, + leaves.number_of_days as number_of_days, leaves.leave_type as leave_type, + leaves.category_id as category_id, leaves.department_id as department_id, + leaves.holiday_status_id as holiday_status_id, leaves.state as state, + leaves.holiday_type as holiday_type, leaves.date_from as date_from, + leaves.date_to as date_to, leaves.payslip_status as payslip_status + from (select + allocation.employee_id as employee_id, + allocation.private_name as name, + allocation.number_of_days as number_of_days, + allocation.category_id as category_id, + allocation.department_id as department_id, + allocation.holiday_status_id as holiday_status_id, + allocation.state as state, + allocation.holiday_type, + null as date_from, + null as date_to, + FALSE as payslip_status, + 'allocation' as leave_type + from hr_leave_allocation as allocation + union all select + request.employee_id as employee_id, + request.private_name as name, + (request.number_of_days * -1) as number_of_days, + request.category_id as category_id, + request.department_id as department_id, + request.holiday_status_id as holiday_status_id, + request.state as state, + request.holiday_type, + request.date_from as date_from, + request.date_to as date_to, + request.payslip_status as payslip_status, + 'request' as leave_type + from hr_leave as request) leaves + ); + """) + + @api.model + def action_time_off_analysis(self): + domain = [('holiday_type', '=', 'employee')] + + if self.env.context.get('active_ids'): + domain = expression.AND([ + domain, + [('employee_id', 'in', self.env.context.get('active_ids', []))] + ]) + + return { + 'name': _('Time Off Analysis'), + 'type': 'ir.actions.act_window', + 'res_model': 'hr.leave.report', + 'view_mode': 'tree,pivot,form', + 'search_view_id': self.env.ref('hr_holidays.view_hr_holidays_filter_report').id, + 'domain': domain, + 'context': { + 'search_default_group_type': True, + 'search_default_year': True, + 'search_default_validated': True, + } + } + + @api.model + def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True): + if not self.user_has_groups('hr_holidays.group_hr_holidays_user') and 'name' in groupby: + raise exceptions.UserError(_('Such grouping is not allowed.')) + return super(LeaveReport, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy) diff --git a/addons/hr_holidays/report/hr_leave_report_calendar.py b/addons/hr_holidays/report/hr_leave_report_calendar.py new file mode 100644 index 00000000..0a5b45db --- /dev/null +++ b/addons/hr_holidays/report/hr_leave_report_calendar.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, fields, models, tools, SUPERUSER_ID + +from odoo.addons.base.models.res_partner import _tz_get + + +class LeaveReportCalendar(models.Model): + _name = "hr.leave.report.calendar" + _description = 'Time Off Calendar' + _auto = False + _order = "start_datetime DESC, employee_id" + + name = fields.Char(string='Name', readonly=True) + start_datetime = fields.Datetime(string='From', readonly=True) + stop_datetime = fields.Datetime(string='To', readonly=True) + tz = fields.Selection(_tz_get, string="Timezone", readonly=True) + duration = fields.Float(string='Duration', readonly=True) + employee_id = fields.Many2one('hr.employee', readonly=True) + company_id = fields.Many2one('res.company', readonly=True) + state = fields.Selection([ + ('draft', 'To Submit'), + ('cancel', 'Cancelled'), # YTI This state seems to be unused. To remove + ('confirm', 'To Approve'), + ('refuse', 'Refused'), + ('validate1', 'Second Approval'), + ('validate', 'Approved') + ], readonly=True) + + def init(self): + tools.drop_view_if_exists(self._cr, 'hr_leave_report_calendar') + self._cr.execute("""CREATE OR REPLACE VIEW hr_leave_report_calendar AS + (SELECT + row_number() OVER() AS id, + CONCAT(em.name, ': ', hl.duration_display) AS name, + hl.date_from AS start_datetime, + hl.date_to AS stop_datetime, + hl.employee_id AS employee_id, + hl.state AS state, + em.company_id AS company_id, + CASE + WHEN hl.holiday_type = 'employee' THEN rr.tz + ELSE %s + END AS tz + FROM hr_leave hl + LEFT JOIN hr_employee em + ON em.id = hl.employee_id + LEFT JOIN resource_resource rr + ON rr.id = em.resource_id + WHERE + hl.state IN ('confirm', 'validate', 'validate1') + ORDER BY id); + """, [self.env.company.resource_calendar_id.tz or self.env.user.tz or 'UTC']) + + def _read(self, fields): + res = super()._read(fields) + if self.env.context.get('hide_employee_name') and 'employee_id' in self.env.context.get('group_by', []): + name_field = self._fields['name'] + for record in self.with_user(SUPERUSER_ID): + self.env.cache.set(record, name_field, record.name.split(':')[-1].strip()) + return res + + @api.model + def get_unusual_days(self, date_from, date_to=None): + # Checking the calendar directly allows to not grey out the leaves taken + # by the employee + return self.env['hr.leave'].get_unusual_days(date_from, date_to=date_to) diff --git a/addons/hr_holidays/report/hr_leave_report_calendar.xml b/addons/hr_holidays/report/hr_leave_report_calendar.xml new file mode 100644 index 00000000..c7f8a78e --- /dev/null +++ b/addons/hr_holidays/report/hr_leave_report_calendar.xml @@ -0,0 +1,40 @@ + + + + All Time Off + hr.leave.report.calendar + calendar + + + + [('employee_id.active','=',True)] + {'hide_employee_name': 1} + + + + hr.leave.report.calendar.view + hr.leave.report.calendar + + + + + + + + + + hr.leave.report.calendar.view.form + hr.leave.report.calendar + +
+ + + + + + +
+
+
+
diff --git a/addons/hr_holidays/report/hr_leave_reports.xml b/addons/hr_holidays/report/hr_leave_reports.xml new file mode 100644 index 00000000..efe3f81b --- /dev/null +++ b/addons/hr_holidays/report/hr_leave_reports.xml @@ -0,0 +1,118 @@ + + + + + hr.holidays.filter + hr.leave.report + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + report.hr.holidays.report.leave_all.tree + hr.leave.report + + + + + + + + + + + + + + + + report.hr.holidays.report.leave_all.kanban + hr.leave.report + + + + + + + + + +
+
+ + + + +
+ + + + +
+
+ +
+
+
+
+
+
+ + + + + + + + + +
from
to
+
+
+ days +
+
+
+
+
+
+
+
+ + + Time off Analysis + + + code + + + action = model.action_time_off_analysis() + + + +
-- cgit v1.2.3