|
| 1 | +from odoo import api, fields, models |
| 2 | + |
| 3 | + |
| 4 | +class HrEmployee(models.Model): |
| 5 | + _inherit = "hr.employee" |
| 6 | + |
| 7 | + @api.model_create_multi |
| 8 | + def create(self, vals_list): |
| 9 | + employees = super().create(vals_list) |
| 10 | + for employee in employees: |
| 11 | + employee._assign_accrual_plans_by_tags() |
| 12 | + return employees |
| 13 | + |
| 14 | + def write(self, vals): |
| 15 | + res = super().write(vals) |
| 16 | + if "category_ids" in vals: |
| 17 | + for employee in self: |
| 18 | + employee._assign_accrual_plans_by_tags() |
| 19 | + return res |
| 20 | + |
| 21 | + def _assign_accrual_plans_by_tags(self): |
| 22 | + self.ensure_one() |
| 23 | + employee_tag_names = self.category_ids.mapped("name") |
| 24 | + accrual_allocations = self.env["hr.leave.allocation"].search( |
| 25 | + [ |
| 26 | + ("employee_id", "=", False), |
| 27 | + ("holiday_type", "=", "category"), |
| 28 | + ("accrual_plan_id", "!=", False), |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
| 32 | + for allocation in accrual_allocations: |
| 33 | + existing_assignment = self.env["hr.leave.allocation"].search( |
| 34 | + [ |
| 35 | + ("employee_id", "=", self.id), |
| 36 | + ("accrual_plan_id", "=", allocation.accrual_plan_id.id), |
| 37 | + ("date_to", "=", False), |
| 38 | + ], |
| 39 | + limit=1, |
| 40 | + ) |
| 41 | + if ( |
| 42 | + allocation.category_id.name in employee_tag_names |
| 43 | + and not existing_assignment |
| 44 | + ): |
| 45 | + new_allocation = { |
| 46 | + "name": allocation.name, |
| 47 | + "holiday_type": "employee", |
| 48 | + "holiday_status_id": allocation.holiday_status_id.id, |
| 49 | + "notes": allocation.notes, |
| 50 | + "number_of_days": allocation.number_of_days, |
| 51 | + "parent_id": allocation.id, |
| 52 | + "employee_id": self.id, |
| 53 | + "employee_ids": [(6, 0, [self.id])], |
| 54 | + "state": "confirm", |
| 55 | + "allocation_type": allocation.allocation_type, |
| 56 | + "date_from": fields.Date.context_today(self), |
| 57 | + "accrual_plan_id": allocation.accrual_plan_id.id, |
| 58 | + } |
| 59 | + childs = allocation.with_context( |
| 60 | + mail_notify_force_send=False, mail_activity_automation_skip=True |
| 61 | + ).create(new_allocation) |
| 62 | + childs.action_validate() |
| 63 | + elif ( |
| 64 | + allocation.category_id.name not in employee_tag_names |
| 65 | + and existing_assignment |
| 66 | + ): |
| 67 | + existing_assignment.date_to = fields.Date.context_today(self) |
0 commit comments