1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo import api, fields, models, _
5
6
7class ResCompany(models.Model):
8    _inherit = 'res.company'
9
10    resource_calendar_ids = fields.One2many(
11        'resource.calendar', 'company_id', 'Working Hours')
12    resource_calendar_id = fields.Many2one(
13        'resource.calendar', 'Default Working Hours', ondelete='restrict')
14
15    @api.model
16    def _init_data_resource_calendar(self):
17        self.search([('resource_calendar_id', '=', False)])._create_resource_calendar()
18
19    def _create_resource_calendar(self):
20        for company in self:
21            company.resource_calendar_id = self.env['resource.calendar'].create({
22                'name': _('Standard 40 hours/week'),
23                'company_id': company.id
24            }).id
25
26    @api.model
27    def create(self, values):
28        company = super(ResCompany, self).create(values)
29        if not company.resource_calendar_id:
30            company.sudo()._create_resource_calendar()
31        # calendar created from form view: no company_id set because record was still not created
32        if not company.resource_calendar_id.company_id:
33            company.resource_calendar_id.company_id = company.id
34        return company
35