res_company.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Company(models.Model):
  5. _inherit = 'res.company'
  6. leave_timesheet_task_id = fields.Many2one(
  7. 'project.task', string="Time Off Task",
  8. domain="[('project_id', '=', internal_project_id)]")
  9. def init(self):
  10. type_ids = [(4, self.env.ref('hr_timesheet.internal_project_default_stage').id)]
  11. companies = self.search(['|', ('internal_project_id', '=', False), ('leave_timesheet_task_id', '=', False)])
  12. internal_projects_by_company_dict = None
  13. Project = self.env['project.project']
  14. for company in companies:
  15. company = company.with_company(company)
  16. if not company.internal_project_id:
  17. if not internal_projects_by_company_dict:
  18. internal_projects_by_company_read = Project.search_read([
  19. ('name', '=', _('Internal')),
  20. ('allow_timesheets', '=', True),
  21. ('company_id', 'in', companies.ids),
  22. ], ['company_id', 'id'])
  23. internal_projects_by_company_dict = {res['company_id'][0]: res['id'] for res in internal_projects_by_company_read}
  24. project_id = internal_projects_by_company_dict.get(company.id, False)
  25. if not project_id:
  26. project_id = Project.create({
  27. 'name': _('Internal'),
  28. 'allow_timesheets': True,
  29. 'company_id': company.id,
  30. 'type_ids': type_ids,
  31. }).id
  32. company.write({'internal_project_id': project_id})
  33. if not company.leave_timesheet_task_id:
  34. task = company.env['project.task'].create({
  35. 'name': _('Time Off'),
  36. 'project_id': company.internal_project_id.id,
  37. 'active': True,
  38. 'company_id': company.id,
  39. })
  40. company.write({
  41. 'leave_timesheet_task_id': task.id,
  42. })
  43. def _create_internal_project_task(self):
  44. projects = super()._create_internal_project_task()
  45. for project in projects:
  46. company = project.company_id
  47. company = company.with_company(company)
  48. if not company.leave_timesheet_task_id:
  49. task = company.env['project.task'].sudo().create({
  50. 'name': _('Time Off'),
  51. 'project_id': company.internal_project_id.id,
  52. 'active': True,
  53. 'company_id': company.id,
  54. })
  55. company.write({
  56. 'leave_timesheet_task_id': task.id,
  57. })
  58. return projects