Debian Codegraph @

addons/hr_timesheet/models/res_company.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4 from odoo import api, fields, models, _
5 from odoo.exceptions import ValidationError
6
7
8 class ResCompany(models.Model):
9 _inherit = 'res.company'
10
11 @api.model
12 def _default_project_time_mode_id(self):
13 return self.env.ref('uom.product_uom_hour', raise_if_not_found=False)
14
15 @api.model
16 def _default_timesheet_encode_uom_id(self):
17 return self.env.ref('uom.product_uom_hour', raise_if_not_found=False)
18
19 project_time_mode_id = fields.Many2one('uom.uom', string='Project Time Unit',
20 default=_default_project_time_mode_id,
21 help="This will set the unit of measure used in projects and tasks.\n"
22 "If you use the timesheet linked to projects, don't "
23 "forget to setup the right unit of measure in your employees.")
24 timesheet_encode_uom_id = fields.Many2one('uom.uom', string="Timesheet Encoding Unit",
25 default=_default_timesheet_encode_uom_id)
26 internal_project_id = fields.Many2one(
27 "project.project", string="Internal Project",
28 domain=[("is_template", "=", False)],
29 help="Default project value for timesheet generated from time off type.",
30 )
31
32 @api.constrains('internal_project_id')
33 def _check_internal_project_id_company(self):
34 if self.filtered(lambda company: company.internal_project_id and company.internal_project_id.sudo().company_id != company):
35 raise ValidationError(_('The Internal Project of a company should be in that company.'))
36
37 @api.model_create_multi
38 def create(self, vals_list):
39 company = super().create(vals_list)
40 # use sudo as the user could have the right to create a company
41 # but not to create a project. On the other hand, when the company
42 # is created, it is not in the allowed_company_ids on the env
43 company.sudo()._create_internal_project_task()
44 return company
45
46 def _create_internal_project_task(self):
47 results = []
48 type_ids_ref = self.env.ref('hr_timesheet.internal_project_default_stage', raise_if_not_found=False)
49 type_ids = [(4, type_ids_ref.id)] if type_ids_ref else []
50 for company in self:
51 company = company.with_company(company)
52 results += [{
53 'name': _('Internal'),
54 'allow_timesheets': True,
55 'company_id': company.id,
56 'type_ids': type_ids,
57 'task_ids': [(0, 0, {
58 'name': name,
59 'company_id': company.id,
60 }) for name in [_('Training'), _('Meeting')]]
61 }]
62 project_ids = self.env['project.project'].create(results)
63 projects_by_company = {project.company_id.id: project for project in project_ids}
64 for company in self:
65 company.internal_project_id = projects_by_company.get(company.id, False)
66 return project_ids