import sys def clone_journals(env): source_name = 'Kedai Kipas 58 Rungkut' target_name = 'PT Kipas Lima Delapan' source_company = env['res.company'].search([('name', 'ilike', source_name)], limit=1) target_company = env['res.company'].search([('name', 'ilike', target_name)], limit=1) if not source_company or not target_company: print(f"Could not find one or both companies: '{source_name}', '{target_name}'") return print(f"Source: {source_company.name} (ID: {source_company.id})") print(f"Target: {target_company.name} (ID: {target_company.id})") # Fetch all journals from the source company journals = env['account.journal'].with_context(active_test=False).search([ ('company_id', '=', source_company.id) ]) print(f"Found {len(journals)} journals in {source_company.name} to clone.") count = 0 for journal in journals: # Check if a journal with the same code already exists in target existing = env['account.journal'].with_context(active_test=False).search([ ('code', '=', journal.code), ('company_id', '=', target_company.id) ], limit=1) if existing: print(f" -> '{journal.name}' (Code: {journal.code}) already exists in {target_company.name}. Skipping.") continue try: with env.cr.savepoint(): # Explicitly pass the shared accounts so Odoo doesn't try to auto-create new ones copy_defaults = { 'company_id': target_company.id, 'default_account_id': journal.default_account_id.id if journal.default_account_id else False, 'suspense_account_id': journal.suspense_account_id.id if journal.suspense_account_id else False, 'profit_account_id': journal.profit_account_id.id if journal.profit_account_id else False, 'loss_account_id': journal.loss_account_id.id if journal.loss_account_id else False, } # Clone the journal new_journal = journal.copy(copy_defaults) # Odoo's copy might override name/code or append "(copy)" # Write to ensure they are identical to source new_journal.write({ 'name': journal.name, 'code': journal.code, }) print(f" -> Cloned '{journal.name}' [{journal.code}] (New ID: {new_journal.id})") count += 1 except Exception as e: print(f" -> Failed to clone '{journal.name}' [{journal.code}]: {e}") print(f"Committing changes... (Cloned {count} journals)") env.cr.commit() print("Cloning complete!") if __name__ == '__main__': try: clone_journals(env) except NameError: print("Please run this script using Odoo shell:") print("Example: ./.venv/bin/python ./odoo/odoo-bin shell -d -c odoo.conf < scripts/clone_journals_rungkut_to_pusat.py")