hr_leave_accrual_plan.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. class AccrualPlan(models.Model):
  5. _name = "hr.leave.accrual.plan"
  6. _description = "Accrual Plan"
  7. name = fields.Char('Name', required=True)
  8. time_off_type_id = fields.Many2one('hr.leave.type', string="Time Off Type",
  9. help="""Specify if this accrual plan can only be used with this Time Off Type.
  10. Leave empty if this accrual plan can be used with any Time Off Type.""")
  11. employees_count = fields.Integer("Employees", compute='_compute_employee_count')
  12. level_ids = fields.One2many('hr.leave.accrual.level', 'accrual_plan_id', copy=True)
  13. allocation_ids = fields.One2many('hr.leave.allocation', 'accrual_plan_id')
  14. transition_mode = fields.Selection([
  15. ('immediately', 'Immediately'),
  16. ('end_of_accrual', "After this accrual's period")],
  17. string="Level Transition", default="immediately", required=True,
  18. help="""Immediately: When the date corresponds to the new level, your accrual is automatically computed, granted and you switch to new level
  19. After this accrual's period: When the accrual is complete (a week, a month), and granted, you switch to next level if allocation date corresponds""")
  20. show_transition_mode = fields.Boolean(compute='_compute_show_transition_mode')
  21. @api.depends('level_ids')
  22. def _compute_show_transition_mode(self):
  23. for plan in self:
  24. plan.show_transition_mode = len(plan.level_ids) > 1
  25. level_count = fields.Integer('Levels', compute='_compute_level_count')
  26. @api.depends('level_ids')
  27. def _compute_level_count(self):
  28. level_read_group = self.env['hr.leave.accrual.level']._read_group(
  29. [('accrual_plan_id', 'in', self.ids)],
  30. fields=['accrual_plan_id'],
  31. groupby=['accrual_plan_id'],
  32. )
  33. mapped_count = {group['accrual_plan_id'][0]: group['accrual_plan_id_count'] for group in level_read_group}
  34. for plan in self:
  35. plan.level_count = mapped_count.get(plan.id, 0)
  36. @api.depends('allocation_ids')
  37. def _compute_employee_count(self):
  38. allocations_read_group = self.env['hr.leave.allocation']._read_group(
  39. [('accrual_plan_id', 'in', self.ids)],
  40. ['accrual_plan_id', 'employee_count:count_distinct(employee_id)'],
  41. ['accrual_plan_id'],
  42. )
  43. allocations_dict = {res['accrual_plan_id'][0]: res['employee_count'] for res in allocations_read_group}
  44. for plan in self:
  45. plan.employees_count = allocations_dict.get(plan.id, 0)
  46. def action_open_accrual_plan_employees(self):
  47. self.ensure_one()
  48. return {
  49. 'name': _("Accrual Plan's Employees"),
  50. 'type': 'ir.actions.act_window',
  51. 'view_mode': 'kanban,tree,form',
  52. 'res_model': 'hr.employee',
  53. 'domain': [('id', 'in', self.allocation_ids.employee_id.ids)],
  54. }
  55. @api.returns('self', lambda value: value.id)
  56. def copy(self, default=None):
  57. default = dict(default or {},
  58. name=_("%s (copy)", self.name))
  59. return super().copy(default=default)