odoo icon indicating copy to clipboard operation
odoo copied to clipboard

[FIX] hr_holidays: correctly calculate leave duration

Open taqu-odoo opened this issue 1 year ago • 11 comments

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

taqu-odoo avatar Apr 15 '24 10:04 taqu-odoo

Pull request status dashboard.

robodoo avatar Apr 15 '24 10:04 robodoo

@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.

taqu-odoo avatar Apr 19 '24 10:04 taqu-odoo

Forward-porting to 'saas-16.4'.

fw-bot avatar Apr 19 '24 10:04 fw-bot

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.

taqu-odoo avatar Apr 23 '24 13:04 taqu-odoo

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.

taqu-odoo avatar Apr 25 '24 13:04 taqu-odoo

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?

Bertrand2 avatar Apr 26 '24 11:04 Bertrand2

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.

taqu-odoo avatar Apr 29 '24 09:04 taqu-odoo

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.

Bertrand2 avatar Apr 30 '24 11:04 Bertrand2

Hello @Bertrand2, date_from and date_to are now correct for leaves in half_days or in hours.

taqu-odoo avatar May 06 '24 11:05 taqu-odoo

Thanks for all the suggestions :+1: Changes made.

taqu-odoo avatar May 10 '24 10:05 taqu-odoo

fixed :+1:

taqu-odoo avatar May 14 '24 11:05 taqu-odoo

Changed :slightly_smiling_face: Thank you for the help

taqu-odoo avatar May 16 '24 08:05 taqu-odoo

robodoo r+

Bertrand2 avatar May 17 '24 08:05 Bertrand2

@taqu-odoo @Bertrand2 this pull request has forward-port PRs awaiting action (not merged or closed):

  • odoo/odoo#165947

fw-bot avatar May 21 '24 05:05 fw-bot