# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _


class ResCompany(models.Model):
    _inherit = 'res.company'

    resource_calendar_ids = fields.One2many(
        'resource.calendar', 'company_id', 'Working Hours')
    resource_calendar_id = fields.Many2one(
        'resource.calendar', 'Default Working Hours', ondelete='restrict')

    @api.model
    def _init_data_resource_calendar(self):
        for company in self.search([('resource_calendar_id', '=', False)]):
            company.resource_calendar_id = self.env['resource.calendar'].create({'name': _('Standard 40 hours/week')}).id

    @api.model
    def create(self, values):
        if not values.get('resource_calendar_id'):
            values['resource_calendar_id'] = self.env['resource.calendar'].sudo().create({'name': _('Standard 40 hours/week')}).id
        company = super(ResCompany, self).create(values)
        # calendar created from form view: no company_id set because record was still not created
        if not company.resource_calendar_id.company_id:
            company.resource_calendar_id.company_id = company.id
        return company

@@ -493,23 +493,8 @@
     
-for company in 
 self
@@ -540,16 +540,114 @@
 False)%5D)
+._create_resource_calendar()%0A%0A    def _create_resource_calendar(self):%0A        for company in self
 :%0A      
@@ -713,32 +713,49 @@
 endar'%5D.create(%7B
+%0A                
 'name': _('Stand
@@ -769,24 +769,79 @@
 hours/week')
+,%0A                'company_id': company.id%0A            
 %7D).id%0A%0A    @
@@ -892,237 +892,155 @@
     
-if not values.get('resource_calendar_id'):%0A            values%5B'resource_calendar_id'%5D = self.env%5B'resource.calendar'%5D.sudo().create(%7B'name': _('Standard 40 hours/week')%7D).id%0A        company = super(ResCompany, self
+company = super(ResCompany, self).create(values)%0A        if not company.resource_calendar_id:%0A            company.sudo(
 ).
+_
 create
-(values
+_resource_calendar(
 )%0A  

Set company_id on a resource.calendar on company creation