mirror of
https://github.com/bringout/oca-ocb-hr.git
synced 2026-04-26 20:52:06 +02:00
19.0 vanilla
This commit is contained in:
parent
e1d89e11e3
commit
a1f02d8cc7
225 changed files with 2335 additions and 775 deletions
|
|
@ -203,8 +203,8 @@ class HrLeaveAllocation(models.Model):
|
|||
|
||||
@api.depends('employee_id', 'holiday_status_id')
|
||||
def _compute_leaves(self):
|
||||
date_from = fields.Date.from_string(self.env.context['default_date_from']) if 'default_date_from' in self.env.context else fields.Date.today()
|
||||
employee_days_per_allocation = self.employee_id._get_consumed_leaves(self.holiday_status_id, date_from)[0]
|
||||
date_from = fields.Date.today()
|
||||
employee_days_per_allocation = self.employee_id._get_consumed_leaves(self.holiday_status_id, date_from, ignore_future=True)[0]
|
||||
for allocation in self:
|
||||
origin = allocation._origin
|
||||
virtual_leave = employee_days_per_allocation[origin.employee_id][origin.holiday_status_id][origin]
|
||||
|
|
@ -436,6 +436,16 @@ class HrLeaveAllocation(models.Model):
|
|||
The goal of this method is to retroactively apply accrual plan levels and progress from nextcall to date_to or today.
|
||||
If force_period is set, the accrual will run until date_to in a prorated way (used for end of year accrual actions).
|
||||
"""
|
||||
def _get_leaves_taken(allocation):
|
||||
precomputed_allocations = allocation
|
||||
if context_precomputed := self.env.context.get('precomputed_allocations'):
|
||||
precomputed_allocations |= context_precomputed
|
||||
# By setting `precomputed_allocations`, avoid infinite loop (otherwise _get_consumed_leaves -> _get_future_leaves_on -> _process_accrual_plans -> ...)
|
||||
employee_days_per_allocation = allocation.employee_id.with_context(precomputed_allocations=precomputed_allocations)._get_consumed_leaves(
|
||||
allocation.holiday_status_id, allocation.nextcall, ignore_future=True)[0]
|
||||
origin = allocation._origin
|
||||
leaves_taken = employee_days_per_allocation[origin.employee_id][origin.holiday_status_id][origin]['leaves_taken']
|
||||
return leaves_taken
|
||||
|
||||
date_to = date_to or fields.Date.today()
|
||||
already_accrued = {allocation.id: allocation.already_accrued or (allocation.number_of_days != 0 and allocation.accrual_plan_id.accrued_gain_time == 'start') for allocation in self}
|
||||
|
|
@ -452,10 +462,6 @@ class HrLeaveAllocation(models.Model):
|
|||
# even if the value doesn't change. This is the best performance atm.
|
||||
first_level = level_ids[0]
|
||||
first_level_start_date = allocation.date_from + get_timedelta(first_level.start_count, first_level.start_type)
|
||||
if allocation.holiday_status_id.request_unit in ["day", "half_day"]:
|
||||
leaves_taken = allocation.leaves_taken
|
||||
else:
|
||||
leaves_taken = allocation.leaves_taken / allocation.employee_id._get_hours_per_day(allocation.date_from)
|
||||
allocation.already_accrued = already_accrued[allocation.id]
|
||||
# first time the plan is run, initialize nextcall and take carryover / level transition into account
|
||||
if not allocation.nextcall:
|
||||
|
|
@ -480,6 +486,10 @@ class HrLeaveAllocation(models.Model):
|
|||
# get current level and normal period boundaries, then set nextcall, adjusted for level transition and carryover
|
||||
# add days, trimmed if there is a maximum_leave
|
||||
while allocation.nextcall <= date_to:
|
||||
if allocation.holiday_status_id.request_unit in ["day", "half_day"]:
|
||||
leaves_taken = _get_leaves_taken(allocation)
|
||||
else:
|
||||
leaves_taken = _get_leaves_taken(allocation) / allocation.employee_id._get_hours_per_day(allocation.nextcall or allocation.date_from)
|
||||
(current_level, current_level_idx) = allocation._get_current_accrual_plan_level_id(allocation.nextcall)
|
||||
if not current_level:
|
||||
break
|
||||
|
|
@ -487,7 +497,7 @@ class HrLeaveAllocation(models.Model):
|
|||
if current_level.added_value_type == "day":
|
||||
current_level_maximum_leave = current_level.maximum_leave
|
||||
else:
|
||||
current_level_maximum_leave = current_level.maximum_leave / allocation.employee_id._get_hours_per_day(allocation.date_from)
|
||||
current_level_maximum_leave = current_level.maximum_leave / allocation.employee_id._get_hours_per_day(allocation.nextcall or allocation.date_from)
|
||||
nextcall = current_level._get_next_date(allocation.nextcall)
|
||||
# Since _get_previous_date returns the given date if it corresponds to a call date
|
||||
# this will always return lastcall except possibly on the first call
|
||||
|
|
@ -530,7 +540,7 @@ class HrLeaveAllocation(models.Model):
|
|||
# allocation.expiring_carryover_days - allocation.leaves_taken or 0 if all the expiring days were used
|
||||
# to take time off.
|
||||
# This ensures that only the days that weren't used to take time off will expire.
|
||||
expiring_days = max(0, allocation.expiring_carryover_days - allocation.leaves_taken)
|
||||
expiring_days = max(0, allocation.expiring_carryover_days - leaves_taken)
|
||||
allocation.number_of_days = max(0, allocation.number_of_days - expiring_days)
|
||||
allocation.expiring_carryover_days = 0
|
||||
|
||||
|
|
@ -571,6 +581,7 @@ class HrLeaveAllocation(models.Model):
|
|||
if allocation.accrual_plan_id.accrued_gain_time == 'start' and allocation.last_executed_carryover_date:
|
||||
last_carryover_date = allocation.last_executed_carryover_date
|
||||
carryover_level, carryover_level_idx = allocation._get_current_accrual_plan_level_id(last_carryover_date)
|
||||
carryover_period_start = carryover_level._get_previous_date(last_carryover_date)
|
||||
carryover_period_end = carryover_level._get_next_date(last_carryover_date)
|
||||
# Adjust carryover_period_end based on level_transition.
|
||||
if carryover_level_idx < (len(level_ids) - 1) and allocation.accrual_plan_id.transition_mode == 'immediately':
|
||||
|
|
@ -586,7 +597,8 @@ class HrLeaveAllocation(models.Model):
|
|||
# That is why (allocation.nextcall == period_end) is used instead of (is_accrual_date)
|
||||
accrued = not allocation.already_accrued and allocation.nextcall == period_end
|
||||
# If the days were accrued on the carryover period, then apply the carryover policy
|
||||
if accrued and last_carryover_date <= allocation.nextcall <= carryover_period_end:
|
||||
# If allocation.actual_lastcall == carryover_period_start, it means this loop has already been run once (skip to avoid applying the carryover twice)
|
||||
if accrued and last_carryover_date <= allocation.nextcall <= carryover_period_end and allocation.actual_lastcall != carryover_period_start:
|
||||
if carryover_level.action_with_unused_accruals == 'lost' or carryover_level.carryover_options == 'limited':
|
||||
allocation.last_executed_carryover_date = carryover_date
|
||||
allocated_days_left = allocation.number_of_days - leaves_taken
|
||||
|
|
@ -622,7 +634,8 @@ class HrLeaveAllocation(models.Model):
|
|||
current_level_maximum_leave = current_level.maximum_leave / allocation.employee_id._get_hours_per_day(allocation.date_from)
|
||||
if allocation.actual_lastcall in {period_start, allocation.date_from} | set(level_start.keys())\
|
||||
or (allocation.actual_lastcall - get_timedelta(current_level.accrual_validity_count, current_level.accrual_validity_type)
|
||||
in {period_start, allocation.date_from} | set(level_start.keys())):
|
||||
in {period_start, allocation.date_from} | set(level_start.keys())):
|
||||
leaves_taken = _get_leaves_taken(allocation)
|
||||
allocation._add_days_to_allocation(current_level, current_level_maximum_leave, leaves_taken, period_start, allocation.nextcall)
|
||||
allocation.already_accrued = True
|
||||
|
||||
|
|
@ -655,8 +668,8 @@ class HrLeaveAllocation(models.Model):
|
|||
and (not self.nextcall or self.nextcall <= accrual_date)):
|
||||
return 0
|
||||
|
||||
fake_allocation = self.env['hr.leave.allocation'].with_context(default_date_from=accrual_date).new(origin=self)
|
||||
fake_allocation.sudo().with_context(default_date_from=accrual_date)._process_accrual_plans(accrual_date, log=False)
|
||||
fake_allocation = self.env['hr.leave.allocation'].new(origin=self)
|
||||
fake_allocation.sudo()._process_accrual_plans(accrual_date, log=False)
|
||||
if self.holiday_status_id.request_unit in ['hour']:
|
||||
res = float_round(fake_allocation.number_of_hours_display - self.number_of_hours_display, precision_digits=2)
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue