hr_plan_wizard.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. from odoo.exceptions import ValidationError
  5. class HrPlanWizard(models.TransientModel):
  6. _name = 'hr.plan.wizard'
  7. _description = 'Plan Wizard'
  8. def _default_plan_id(self):
  9. # We know that all employees belong to the same company
  10. employees = self.env['hr.employee'].browse(self.env.context.get('active_ids') if self.env.context.get('active_ids') else [])
  11. if not employees:
  12. return None
  13. if len(employees.department_id) > 1:
  14. return self.env['hr.plan'].search([
  15. ('company_id', '=', employees[0].company_id.id),
  16. ('department_id', '=', False)
  17. ], limit=1)
  18. else:
  19. return self.env['hr.plan'].search([
  20. ('company_id', '=', employees[0].company_id.id),
  21. '|',
  22. ('department_id', '=', employees[0].department_id.id),
  23. ('department_id', '=', False)
  24. ], limit=1)
  25. plan_id = fields.Many2one('hr.plan', default=lambda self: self._default_plan_id(),
  26. domain="[('company_id', 'in', [False, company_id]), '|', ('department_id', '=', department_id), ('department_id', '=', False)]")
  27. department_id = fields.Many2one('hr.department', compute='_compute_department_id')
  28. employee_ids = fields.Many2many(
  29. 'hr.employee', 'hr_employee_hr_plan_wizard_rel', 'employee_id', 'plan_wizard_id', string='Employee', required=True,
  30. default=lambda self: self.env.context.get('active_ids', []),
  31. )
  32. company_id = fields.Many2one('res.company', 'Company', compute='_compute_company_id', required=True)
  33. warning = fields.Html(compute='_compute_warning')
  34. @api.depends('employee_ids')
  35. def _compute_department_id(self):
  36. for wizard in self:
  37. all_departments = wizard.employee_ids.department_id
  38. wizard.department_id = False if len(all_departments) > 1 else all_departments
  39. @api.constrains('employee_ids')
  40. def _check_employee_companies(self):
  41. for wizard in self:
  42. if len(wizard.employee_ids.mapped('company_id')) > 1:
  43. raise ValidationError(_('The employees should belong to the same company.'))
  44. @api.depends('employee_ids')
  45. def _compute_company_id(self):
  46. for wizard in self:
  47. wizard.company_id = wizard.employee_ids and wizard.employee_ids[0].company_id or self.env.company
  48. def _get_warnings(self):
  49. self.ensure_one()
  50. warnings = set()
  51. for employee in self.employee_ids:
  52. for activity_type in self.plan_id.plan_activity_type_ids:
  53. warning = activity_type.get_responsible_id(employee)['warning']
  54. if warning:
  55. warnings.add(warning)
  56. return warnings
  57. @api.depends('employee_ids', 'plan_id')
  58. def _compute_warning(self):
  59. for wizard in self:
  60. warnings = wizard._get_warnings()
  61. if warnings:
  62. warning_display = _('The plan %s cannot be launched: <br><ul>', wizard.plan_id.name)
  63. for warning in warnings:
  64. warning_display += '<li>%s</li>' % warning
  65. warning_display += '</ul>'
  66. else:
  67. warning_display = False
  68. wizard.warning = warning_display
  69. def _get_activities_to_schedule(self):
  70. return self.plan_id.plan_activity_type_ids
  71. def action_launch(self):
  72. self.ensure_one()
  73. for employee in self.employee_ids:
  74. body = _('The plan %s has been started', self.plan_id.name)
  75. activities = set()
  76. for activity_type in self._get_activities_to_schedule():
  77. responsible = activity_type.get_responsible_id(employee)['responsible']
  78. if self.env['hr.employee'].with_user(responsible).check_access_rights('read', raise_exception=False):
  79. date_deadline = self.env['mail.activity']._calculate_date_deadline(activity_type.activity_type_id)
  80. employee.activity_schedule(
  81. activity_type_id=activity_type.activity_type_id.id,
  82. summary=activity_type.summary,
  83. note=activity_type.note,
  84. user_id=responsible.id,
  85. date_deadline=date_deadline
  86. )
  87. activity = _('%(activity)s, assigned to %(name)s, due on the %(deadline)s', activity=activity_type.summary, name=responsible.name, deadline=date_deadline)
  88. activities.add(activity)
  89. if activities:
  90. body += '<ul>'
  91. for activity in activities:
  92. body += '<li>%s</li>' % activity
  93. body += '</ul>'
  94. employee.message_post(body=body)
  95. if len(self.employee_ids) == 1:
  96. return {
  97. 'type': 'ir.actions.act_window',
  98. 'res_model': 'hr.employee',
  99. 'res_id': self.employee_ids.id,
  100. 'name': self.employee_ids.display_name,
  101. 'view_mode': 'form',
  102. 'views': [(False, "form")],
  103. }
  104. return {
  105. 'type': 'ir.actions.act_window',
  106. 'res_model': 'hr.employee',
  107. 'name': _('Launch Plans'),
  108. 'view_mode': 'tree,form',
  109. 'target': 'current',
  110. 'domain': [('id', 'in', self.employee_ids.ids)],
  111. }