Skip to content

Commit

Permalink
[REF] Allocation plan
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Felipe Miléo <[email protected]>
  • Loading branch information
mileo committed Nov 29, 2024
1 parent 45be6a4 commit 488efc3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 54 deletions.
102 changes: 48 additions & 54 deletions hr_holidays_allocation_plan/models/hr_leave_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ class HrLeaveAllocationPlan(models.Model):
default=False,
)

validity_period = fields.Integer(
string="Validity Period (years)",
default=1,
help="Defines how long the allocated leave is valid from the allocation date.",
)

def action_recompute_plan(self):
for record in self:

Expand Down Expand Up @@ -290,19 +296,6 @@ def action_run_update(self):

for employee in record.plan_employee_ids:

employee_allocation = record.allocation_ids.filtered(
lambda r: r.employee_id == employee
)
if employee_allocation:
if record.allocation_type != "recurrent":
continue

allocations_to_renew = employee_allocation.filtered(
lambda r: r.date_to < fields.Date.today()
)
if not allocations_to_renew:
continue

running_contracts = (
self.env["hr.contract"]
.search(
Expand All @@ -318,47 +311,48 @@ def action_run_update(self):
continue

oldest_running_contract = min(running_contracts)
current_date = (
oldest_running_contract # Start allocation from contract start date
)

if record.allocation_type == "recurrent":
if (
fields.Date.today() - relativedelta(years=1)
< oldest_running_contract
and not record.immediate_allocation
):
continue

allocation_type = record.allocation_type
date_to = record.date_to

number_of_days = record.number_of_days
if allocation_type == "accrual":
number_of_days = 0

date_from = max([record.date_from, oldest_running_contract])
if allocation_type == "recurrent":
allocation_type = "regular"
date_from = oldest_running_contract.replace(
year=fields.Date.today().year
)
date_to = date_from + relativedelta(
years=record.recurring_renewal_frequency
while current_date < fields.Date.today():
date_from = current_date
date_to = date_from + relativedelta(years=record.validity_period)

allocation = self.env["hr.leave.allocation"].create(
{
"name": record.name,
"holiday_type": "employee",
"holiday_status_id": record.holiday_status_id.id,
"notes": record.notes,
"number_of_days": record.number_of_days,
"employee_id": employee.id,
"employee_ids": [(6, 0, [employee.id])],
"state": "confirm",
"allocation_type": "regular",
"date_from": date_from,
"date_to": date_to,
"accrual_plan_id": record.accrual_plan_id.id,
"allocation_plan_id": record.id,
}
)
allocation.action_validate()

allocation = self.env["hr.leave.allocation"].create(
{
"name": record.name,
"holiday_type": "employee",
"holiday_status_id": record.holiday_status_id.id,
"notes": record.notes,
"number_of_days": number_of_days,
"employee_id": employee.id,
"employee_ids": [(6, 0, [employee.id])],
"state": "confirm",
"allocation_type": allocation_type,
"date_from": date_from,
"date_to": date_to,
"accrual_plan_id": record.accrual_plan_id.id,
"allocation_plan_id": record.id,
}
)
allocation.action_validate()
# Increment to the next year after the first allocation
current_date += relativedelta(years=1)

def action_cancel(self):
for record in self:
record.state = "cancel"

def action_allocation_refuse(self):
for record in self:
record.allocation_ids.action_refuse()

def action_allocation_draft(self):
for record in self:
record.allocation_ids.action_draft()

def action_allocation_confirm(self):
for record in self:
record.allocation_ids.action_confirm()
17 changes: 17 additions & 0 deletions hr_holidays_allocation_plan/views/hr_leave_allocation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
name="action_run_update"
type="object"
/>
<button string="Cancel" name="action_cancel" type="object" />
<field
name="state"
widget="statusbar"
Expand Down Expand Up @@ -120,13 +121,29 @@
<field name="recurring_renewal_frequency" />
<field name="number_of_days" />
<field name="immediate_allocation" />
<field name="validity_period" />
</group>
</group>
</page>
<page name="employees" string="Employess">
<field name="plan_employee_ids" nolabel="1" />
</page>
<page name="allocations" string="Allocations">
<button
string="Allocation Confirm"
name="action_allocation_confirm"
type="object"
/>
<button
string="Allocation Refuse"
name="action_allocation_refuse"
type="object"
/>
<button
string="Allocation Draft"
name="action_allocation_draft"
type="object"
/>
<field name="allocation_ids" nolabel="1">
<tree>
<field name="create_date" />
Expand Down

0 comments on commit 488efc3

Please sign in to comment.