# -*- coding: utf-8 -*- from . import models from . import wizard from odoo import api, SUPERUSER_ID import logging _logger = logging.getLogger(__name__) def _auto_share_accounts_post_init(cr, registry): """ Automatically mirror Bank & Cash accounts from the Parent Company (ID 2) to branch companies that don't have their own Chart of Accounts. Also auto-creates Bank journals in each branch for the mirrored accounts. Odoo 17 Strategy: Since account.account uses a single company_id (M2O), we CREATE new account records in each branch company with the same code/name as the parent's bank/cash accounts (instead of sharing via M2M in Odoo 19). """ env = api.Environment(cr, SUPERUSER_ID, {}) parent_company = env['res.company'].browse(2) if not parent_company.exists(): _logger.warning("Parent company (ID=2) not found, skipping auto-share.") return branch_companies = env['res.company'].search([('id', '!=', 2)]) # Find parent's bank/cash accounts parent_bank_cash_accounts = env['account.account'].search([ ('company_id', '=', parent_company.id), ('account_type', '=', 'asset_cash'), ]) if not parent_bank_cash_accounts: _logger.info("No bank/cash accounts found in parent company, skipping.") return for branch in branch_companies: # Check if branch already has its own accounts existing_branch_accounts = env['account.account'].search([ ('company_id', '=', branch.id), ('account_type', '=', 'asset_cash'), ]) if existing_branch_accounts: _logger.info("Branch %s already has bank/cash accounts, skipping.", branch.name) continue # Clone parent's bank/cash accounts to this branch for parent_acc in parent_bank_cash_accounts: # Check if an account with the same code already exists in this branch existing = env['account.account'].search([ ('company_id', '=', branch.id), ('code', '=', parent_acc.code), ], limit=1) if not existing: env['account.account'].create({ 'name': parent_acc.name, 'code': parent_acc.code, 'account_type': parent_acc.account_type, 'company_id': branch.id, 'reconcile': parent_acc.reconcile, 'currency_id': parent_acc.currency_id.id if parent_acc.currency_id else False, 'note': "Auto-mirrored from parent company (%s)" % parent_company.name, }) _logger.info( "Created mirror account %s (%s) in branch %s", parent_acc.code, parent_acc.name, branch.name ) # Auto-create bank journals in branch for mirrored accounts parent_bank_journals = env['account.journal'].search([ ('company_id', '=', parent_company.id), ('type', 'in', ('bank', 'cash')), ]) for parent_journal in parent_bank_journals: existing_journal = env['account.journal'].search([ ('company_id', '=', branch.id), ('code', '=', parent_journal.code), ], limit=1) if not existing_journal: # Find the mirrored account in the branch branch_account = env['account.account'].search([ ('company_id', '=', branch.id), ('code', '=', parent_journal.default_account_id.code), ], limit=1) if branch_account: env['account.journal'].create({ 'name': parent_journal.name, 'code': parent_journal.code, 'type': parent_journal.type, 'company_id': branch.id, 'default_account_id': branch_account.id, }) _logger.info( "Created mirror journal %s (%s) in branch %s", parent_journal.code, parent_journal.name, branch.name ) def _cleanup_shared_accounts_uninstall(cr, registry): """ Remove the auto-mirrored accounts upon uninstallation. Only removes accounts tagged with the auto-mirror note and having no journal items. """ env = api.Environment(cr, SUPERUSER_ID, {}) mirrored_accounts = env['account.account'].search([ ('company_id', '!=', 2), ('note', 'ilike', 'Auto-mirrored from parent company'), ('account_type', '=', 'asset_cash'), ]) for acc in mirrored_accounts: # Only delete if no journal items reference this account has_items = env['account.move.line'].search([ ('account_id', '=', acc.id), ], limit=1) if not has_items: _logger.info("Removing mirrored account %s from company %s", acc.code, acc.company_id.name) acc.unlink() else: _logger.warning( "Cannot remove mirrored account %s from company %s: has journal items", acc.code, acc.company_id.name )