New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIX] hr_holidays: correctly calculate leave duration #161856
base: 16.0
Are you sure you want to change the base?
[FIX] hr_holidays: correctly calculate leave duration #161856
Conversation
0ecdae4
to
b462f94
Compare
Forward-porting to 'saas-16.4'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems to make sense, but I don't know if there is better ways to do it
seeing the code below does something similar:
odoo/addons/hr_holidays/models/hr_leave.py
Lines 922 to 934 in b4c8625
date_from = values.get('date_from') | |
date_to = values.get('date_to') | |
employee = employees.filtered(lambda emp: emp.id == employee_id) | |
attendance_from, attendance_to = self._get_attendances(employee, date_from.date(), date_to.date()) | |
hour_from = float_to_time(attendance_from.hour_from) | |
hour_to = float_to_time(attendance_to.hour_to) | |
hour_from = hour_from.hour + hour_from.minute / 60 | |
hour_to = hour_to.hour + hour_to.minute / 60 | |
values['date_from'] = self._get_start_or_end_from_attendance(hour_from, date_from.date(), employee) | |
values['date_to'] = self._get_start_or_end_from_attendance(hour_to, date_to.date(), employee) | |
values['request_date_from'], values['request_date_to'] = values['date_from'].date(), values['date_to'].date() | |
values['number_of_days'] = employee_leave_date_duration[(date_from, date_to)][values['employee_id']]['days'] |
I guess it's necessary and there is no method that would make it shorted.
b462f94
to
a75f9fd
Compare
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the leave is for half a day/specific hours? Won't that create a whole day leave when creating the individual leaves?
a75f9fd
to
56896d6
Compare
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 🤔 Isn't there any way to just compute date_from and date_to based from the UX fields? |
56896d6
to
029f696
Compare
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. |
029f696
to
f0fd6ab
Compare
Hello @Bertrand2, date_from and date_to are now correct for leaves in half_days or in hours. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes! 😄
I still have some more comments but we're getting there 💪
f0fd6ab
to
d6ef25b
Compare
Thanks for all the suggestions 👍 Changes made. |
d6ef25b
to
a6dcb66
Compare
fixed 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one last precision over what I meant for the tests, otherwise LGTM
a6dcb66
to
e5fd8ee
Compare
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 ----- Use the whole day as a basis for leave duration calculation if there is more than one schedule for the employees. Also add more logic to handle half days and hours leaves which had incorrect start and end hours. opw-3703793
e5fd8ee
to
8bfd00e
Compare
Changed 🙂 Thank you for the help |
Steps
default one and make him for example start earlier.
No Limit.
company") on the day where the employee is starting earlier.
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.
odoo/addons/hr_holidays/models/hr_leave.py
Line 918 in 64cbe38
odoo/addons/hr_holidays/models/hr_leave.py
Line 744 in 64cbe38
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