[FIX] hr_holidays: correctly calculate leave duration
Steps
- Time off installed
- Put an employee on a different schedule than the company's default one and make him for example start earlier.
- Create a new Time Off type, set "Requires allocation" to No Limit.
- Approvals > Time off > New and create a time off (mode "by company") on the day where the employee is starting earlier.
- Approve it.
- Approvals > Time off : the leave's duration for the employee on a different schedule is incorrect (slightly less than 1 day).
Issue
When creating a leave on a company level, a leave will first be created with start and end hours based on the company's default schedule. When approving the leave, a leave is then created for each affected employee. The date_from and date_to received by create are based on the company's default schedule. This is not generally problematic as it is taken into account. For example, the date_from and date_to of the employee's leave are correct even if he is on a different schedule.
However, when we compute the number of days, it is not accounted for. https://github.com/odoo/odoo/blob/64cbe389e698398eee93ebde9c61b2ee79756380/addons/hr_holidays/models/hr_leave.py#L918 https://github.com/odoo/odoo/blob/64cbe389e698398eee93ebde9c61b2ee79756380/addons/hr_holidays/models/hr_leave.py#L744 As a result, the hours worked by an employee on a different schedule which are "outside" of the company's default schedule don't count towards the calculation. https://github.com/odoo/odoo/blob/3eec4d6a69ff76cbf750b960dc8db2eb5e0a45f9/addons/resource/models/resource_mixin.py#L94-101
Note: the issue is the same for leaves given to a department or an employee tag since they are created the same way.
Fix
When creating a leave for employees, if the leave is not for half days or hours, give the whole day as a leave to account for different schedules. Note that the actual date_from and date_to of each employee's leave are still based on their actual attendance, but if an employee has hours outside of the company's schedule, they will count towards the leave's duration.
opw-3703793
@fw-bot up to saas-16.4 https://github.com/odoo/odoo/commit/6074d86b2076117978136259d70d043ef3dba1ee refactored the leave duration in 17.0 and fixed this issue.
Forward-porting to 'saas-16.4'.
It seems to me we could also calculate the number of days of a leave by passing to _get_number_of_days_batch the start of the first day and the end of the last day, but not too sure about that.
I've modified the PR. I think conceptually it makes sense to consider the whole day as a leave if we have different schedules, let me know what you think.
This leaves the initial issue for half-days :thinking: Isn't there any way to just compute date_from and date_to based from the UX fields?
It should now work for half days, the problem is that _get_number_of_days_batch doesn't always return exactly 0.5 for half days.
This doesn't work for half_days. The leave will indeed have a 0.5 days duration, but the date_from and date_to will cover the whole day.
Hello @Bertrand2, date_from and date_to are now correct for leaves in half_days or in hours.
Thanks for all the suggestions :+1: Changes made.
fixed :+1:
Changed :slightly_smiling_face: Thank you for the help
robodoo r+
@taqu-odoo @Bertrand2 this pull request has forward-port PRs awaiting action (not merged or closed):
- odoo/odoo#165947
