account_analytic.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 UserError
  5. class AccountAnalyticLine(models.Model):
  6. _inherit = 'account.analytic.line'
  7. holiday_id = fields.Many2one("hr.leave", string='Leave Request', copy=False)
  8. global_leave_id = fields.Many2one("resource.calendar.leaves", string="Global Time Off", ondelete='cascade')
  9. task_id = fields.Many2one(domain="[('project_id.allow_timesheets', '=', True),"
  10. "('project_id', '=?', project_id), ('is_timeoff_task', '=', False)]")
  11. @api.ondelete(at_uninstall=False)
  12. def _unlink_except_linked_leave(self):
  13. if any(line.holiday_id for line in self):
  14. raise UserError(_('You cannot delete timesheets that are linked to time off requests. Please cancel your time off request from the Time Off application instead.'))
  15. @api.model_create_multi
  16. def create(self, vals_list):
  17. if not self.env.su:
  18. task_ids = [vals['task_id'] for vals in vals_list if vals.get('task_id')]
  19. has_timeoff_task = self.env['project.task'].search_count([('id', 'in', task_ids), ('is_timeoff_task', '=', True)], limit=1) > 0
  20. if has_timeoff_task:
  21. raise UserError(_('You cannot create timesheets for a task that is linked to a time off type. Please use the Time Off application to request new time off instead.'))
  22. return super().create(vals_list)
  23. def _check_can_update_timesheet(self):
  24. return self.env.su or not self.filtered('holiday_id')
  25. def write(self, vals):
  26. if not self._check_can_update_timesheet():
  27. raise UserError(_('You cannot modify timesheets that are linked to time off requests. Please use the Time Off application to modify your time off requests instead.'))
  28. return super().write(vals)