
from odoo import api, tools, SUPERUSER_ID


def post_init_hook(cr, registry):
    env = api.Environment(cr, SUPERUSER_ID, {})
    coa_generic_tmpl = env.ref(
        'l10n_br_coa_generic.l10n_br_coa_generic_template')
    if env['ir.module.module'].search_count([
        ('name', '=', 'l10n_br_account'),
        ('state', '=', 'installed'),
    ]):
        from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
        # Relate fiscal taxes to account taxes.
        load_fiscal_taxes(env, coa_generic_tmpl)

    # Load COA to Demo Company
    if not tools.config.get('without_demo'):
        env.user.company_id = env.ref(
            'l10n_br_fiscal.empresa_lucro_presumido')
        coa_generic_tmpl.try_loading_for_current_company()


from odoo import api, tools, SUPERUSER_ID


def post_init_hook(cr, registry):
    env = api.Environment(cr, SUPERUSER_ID, {})
    coa_generic_tmpl = env.ref(
        'l10n_br_coa_generic.l10n_br_coa_generic_template')
    if env['ir.module.module'].search_count([
        ('name', '=', 'l10n_br_account'),
        ('state', '=', 'installed'),
    ]):
        from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
        # Relate fiscal taxes to account taxes.
        load_fiscal_taxes(env, coa_generic_tmpl)

    # Load COA to Demo Company
    if not tools.config.get('without_demo'):
        user_admin = env.ref('base.user_admin')
        user_admin.company_id = env.ref(
            'l10n_br_base.empresa_lucro_presumido')
        coa_generic_tmpl.sudo(
            user=user_admin.id).try_loading_for_current_company()
        user_admin.company_id = env.ref('base.main_company')

Use admin user to create COA
[FIX] l10n_br_coa_generic: Use admin user to create COA

  
  from odoo import api, tools, SUPERUSER_ID
  
  
  def post_init_hook(cr, registry):
      env = api.Environment(cr, SUPERUSER_ID, {})
      coa_generic_tmpl = env.ref(
          'l10n_br_coa_generic.l10n_br_coa_generic_template')
      if env['ir.module.module'].search_count([
          ('name', '=', 'l10n_br_account'),
          ('state', '=', 'installed'),
      ]):
          from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
          # Relate fiscal taxes to account taxes.
          load_fiscal_taxes(env, coa_generic_tmpl)
  
      # Load COA to Demo Company
      if not tools.config.get('without_demo'):
+         user_admin = env.ref('base.user_admin')
-         env.user.company_id = env.ref(
+         user_admin.company_id = env.ref(
-             'l10n_br_fiscal.empresa_lucro_presumido')
+             'l10n_br_base.empresa_lucro_presumido')
-         coa_generic_tmpl.try_loading_for_current_company()
+         coa_generic_tmpl.sudo(
+             user=user_admin.id).try_loading_for_current_company()
+         user_admin.company_id = env.ref('base.main_company')
  
Use admin user to create COA
## Code Before:

from odoo import api, tools, SUPERUSER_ID


def post_init_hook(cr, registry):
    env = api.Environment(cr, SUPERUSER_ID, {})
    coa_generic_tmpl = env.ref(
        'l10n_br_coa_generic.l10n_br_coa_generic_template')
    if env['ir.module.module'].search_count([
        ('name', '=', 'l10n_br_account'),
        ('state', '=', 'installed'),
    ]):
        from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
        # Relate fiscal taxes to account taxes.
        load_fiscal_taxes(env, coa_generic_tmpl)

    # Load COA to Demo Company
    if not tools.config.get('without_demo'):
        env.user.company_id = env.ref(
            'l10n_br_fiscal.empresa_lucro_presumido')
        coa_generic_tmpl.try_loading_for_current_company()

## Instruction:
Use admin user to create COA
## Code After:

from odoo import api, tools, SUPERUSER_ID


def post_init_hook(cr, registry):
    env = api.Environment(cr, SUPERUSER_ID, {})
    coa_generic_tmpl = env.ref(
        'l10n_br_coa_generic.l10n_br_coa_generic_template')
    if env['ir.module.module'].search_count([
        ('name', '=', 'l10n_br_account'),
        ('state', '=', 'installed'),
    ]):
        from odoo.addons.l10n_br_account.hooks import load_fiscal_taxes
        # Relate fiscal taxes to account taxes.
        load_fiscal_taxes(env, coa_generic_tmpl)

    # Load COA to Demo Company
    if not tools.config.get('without_demo'):
        user_admin = env.ref('base.user_admin')
        user_admin.company_id = env.ref(
            'l10n_br_base.empresa_lucro_presumido')
        coa_generic_tmpl.sudo(
            user=user_admin.id).try_loading_for_current_company()
        user_admin.company_id = env.ref('base.main_company')
