[FIX] hr_holidays: manage refused leave in the report

Steps to reproduce:
-------------------
- add an allocation for an employee;
- add a leave with this allocation;
- validate the leave;
- refuse the leave;
- in action menu, click on "Time off Analysis by Employee and Time Off Type".

Issue:
------
The report does not take into account refused allocations and leaves.

Solution:
---------
Add a condition to take a number of days equal to zero
if the allocation or leave is in a refused state.

Handled the case where no record is found for leave
(with `COALESCE` because `value - NULL = NULL`).

opw-3503617

closes odoo/odoo#136217

X-original-commit: 4c73d4a7d5cec388177e2703ac9b0a42de8c12c8
Signed-off-by: Sofie Gvaladze (sgv) <sgv@odoo.com>
Signed-off-by: Thomas Lefebvre (thle) <thle@odoo.com>
This commit is contained in:
Thomas Lefebvre (thle)
2023-09-22 04:10:06 +00:00
parent d5c62fc80f
commit b365ea4e1f
@@ -53,7 +53,7 @@ class LeaveReport(models.Model):
employee.active as active_employee,
CASE
WHEN allocation.id = min_allocation_id.min_id
THEN aggregate_allocation.number_of_days - aggregate_leave.number_of_days
THEN aggregate_allocation.number_of_days - COALESCE(aggregate_leave.number_of_days, 0)
ELSE 0
END as number_of_days,
allocation.department_id as department_id,
@@ -72,16 +72,21 @@ class LeaveReport(models.Model):
FROM hr_leave_allocation GROUP BY employee_id, holiday_status_id) min_allocation_id
on (allocation.employee_id=min_allocation_id.employee_id and allocation.holiday_status_id=min_allocation_id.holiday_status_id)
/* Obtain the sum of allocations */
/* Obtain the sum of allocations (validated) */
LEFT JOIN
(SELECT employee_id, holiday_status_id, sum(number_of_days) as number_of_days
FROM hr_leave_allocation GROUP BY employee_id, holiday_status_id) aggregate_allocation
(SELECT employee_id, holiday_status_id,
sum(CASE WHEN state = 'validate' and active = True THEN number_of_days ELSE 0 END) as number_of_days
FROM hr_leave_allocation
GROUP BY employee_id, holiday_status_id) aggregate_allocation
on (allocation.employee_id=aggregate_allocation.employee_id and allocation.holiday_status_id=aggregate_allocation.holiday_status_id)
/* Obtain the sum of requested leaves (validated and confirmed) */
/* Obtain the sum of requested leaves (validated) */
LEFT JOIN
(SELECT employee_id, holiday_status_id, sum(number_of_days) as number_of_days
FROM hr_leave GROUP BY employee_id, holiday_status_id) aggregate_leave
(SELECT employee_id, holiday_status_id,
sum(CASE WHEN state IN ('validate', 'validate1') THEN number_of_days ELSE 0 END) as number_of_days
FROM hr_leave
GROUP BY employee_id, holiday_status_id) aggregate_leave
on (allocation.employee_id=aggregate_leave.employee_id and allocation.holiday_status_id = aggregate_leave.holiday_status_id)
UNION ALL SELECT
@@ -99,7 +104,8 @@ class LeaveReport(models.Model):
END as holiday_status,
request.employee_company_id as company_id
FROM hr_leave as request
INNER JOIN hr_employee as employee ON (request.employee_id = employee.id)) leaves
INNER JOIN hr_employee as employee ON (request.employee_id = employee.id)
WHERE request.state IN ('confirm', 'validate', 'validate1')) leaves
);
""")