Creating records and salary rules dynamically for each company existing, and linking them to the accounting plan

239 views Asked by At

In Odoo 13 we have encountered a multi company problem, where there need's to be the same set of salary rules and salary structures (belonging to the OCA payroll module) for each company.

We don't want to statically define the records for each company:

what we want is to create is an Odoo function that automatically creates the records for each company and links to the proper accounts in the accounting plan for the proper company:

@api.model
def create_rule_for_every_company(self):
    """
    function that'll create the rule for each company, in the group_system
    """
    

(function is called on installing the module from the XML data file)

But then quickly realized that you need to do the same for the salary structure and such, and so I'm wondering if there's an easier way to do this in the Odoo logic that I don't know of.

The purpose of doing this is to have every rule linked to the proper : account.account for the company.

1

There are 1 answers

0
Houssem Chouia On BEST ANSWER

Basically, I came up with this:

  @api.model
        def create_rule_for_every_company(self):
            """
            function that'll create the rule for each company, in the group_system
            """
            list_companies = self.env['res.company'].sudo().search([])
            list_rules = self.env['hr.salary.rule'].sudo().search([('company_id', '=', 0)])
            for rule in list_rules:
              if rule.company_id is False:
                for company in list_companies:
                    if rule.child_ids:
                        for child in rule.child_ids:
                            child_copy = child.copy()
                            child_copy.sudo().write({'company_id': company.id, 'parent_rule_id': rule.id, 'rubric_id': child.rubric_id})
                        rule_copy = rule.sudo().copy()
                        rule_copy.sudo().write({'company_id': company.id, 'rubric_id': rule.rubric_id})
                    else:
                        rule_copy = rule.sudo().copy()
                        rule_copy.sudo().write({'company_id': company.id, 'rubric_id': rule.rubric_id})
              for rule in list_rules:
                  if rule.company_id is False:
                     self.sudo().unlink(rule.id)

Still looking for more optimal solutions.

This solution feels unstable.