project_task.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, _
  4. class Task(models.Model):
  5. _inherit = 'project.task'
  6. leave_types_count = fields.Integer(compute='_compute_leave_types_count')
  7. is_timeoff_task = fields.Boolean("Is Time off Task", compute="_compute_is_timeoff_task", search="_search_is_timeoff_task")
  8. def _compute_leave_types_count(self):
  9. time_off_type_read_group = self.env['hr.leave.type']._read_group(
  10. [('timesheet_task_id', 'in', self.ids)],
  11. ['timesheet_task_id'],
  12. ['timesheet_task_id'],
  13. )
  14. time_off_type_count_per_task = {res['timesheet_task_id'][0]: res['timesheet_task_id_count'] for res in time_off_type_read_group}
  15. for task in self:
  16. task.leave_types_count = time_off_type_count_per_task.get(task.id, 0)
  17. def _compute_is_timeoff_task(self):
  18. timeoff_tasks = self.filtered(lambda task: task.leave_types_count or task.company_id.leave_timesheet_task_id == task)
  19. timeoff_tasks.is_timeoff_task = True
  20. (self - timeoff_tasks).is_timeoff_task = False
  21. def _search_is_timeoff_task(self, operator, value):
  22. if operator not in ['=', '!='] or not isinstance(value, bool):
  23. raise NotImplementedError(_('Operation not supported'))
  24. leave_type_read_group = self.env['hr.leave.type']._read_group(
  25. [('timesheet_task_id', '!=', False)],
  26. ['timesheet_task_ids:array_agg(timesheet_task_id)'],
  27. [],
  28. )
  29. timeoff_task_ids = leave_type_read_group[0]['timesheet_task_ids'] if leave_type_read_group[0]['timesheet_task_ids'] else []
  30. if self.env.company.leave_timesheet_task_id:
  31. timeoff_task_ids.append(self.env.company.leave_timesheet_task_id.id)
  32. if operator == '!=':
  33. value = not value
  34. return [('id', 'in' if value else 'not in', timeoff_task_ids)]