odoo_utility_scripts/migrate_to_branch.py

128 lines
4.3 KiB
Python

import sys
def migrate_to_branch(env):
source_name = 'PT Kipas Lima Delapan'
target_name = 'Kedai Kipas 58 Rungkut'
source_company = env['res.company'].search([('name', '=', source_name)], limit=1)
target_company = env['res.company'].search([('name', '=', 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 Company: {source_company.name} (ID: {source_company.id})")
print(f"Target Company: {target_company.name} (ID: {target_company.id})")
# List of safe transaction and config tables to update.
# We avoid global/shared configs like account_account, account_tax, product_template, res_partner, res_users
# as these should either remain with the parent company (in Odoo 17 branch setups) or be global.
tables_to_update = [
# POS Config & Transactions
'pos_config',
'pos_session',
'pos_order',
'pos_order_line',
'pos_payment',
'pos_payment_method',
# Accounting / Assets
'account_asset',
# Shop Floor / Inventory
'stock_warehouse',
'stock_location',
'stock_picking_type',
'stock_picking',
'stock_move',
'stock_move_line',
'stock_quant',
'stock_valuation_layer',
'stock_scrap',
'stock_inventory',
'stock_rule',
'stock_route',
'stock_putaway_rule',
# Shop Floor / Manufacturing
'mrp_production',
'mrp_workorder',
'mrp_workcenter',
'mrp_routing_workcenter',
'mrp_bom',
'mrp_bom_line',
'mrp_unbuild',
'mrp_consumption_warning',
# Journals and Accounting Transactions
'account_journal',
'account_move',
'account_move_line',
'account_payment',
'account_bank_statement',
'account_bank_statement_line',
'account_partial_reconcile',
'account_payment_term',
# Sales and Purchases
'sale_order',
'sale_order_line',
'purchase_order',
'purchase_order_line',
'purchase_requisition',
'purchase_requisition_line',
# Employees and HR (Optional, remove if employees shouldn't be moved)
'hr_employee',
'hr_contract',
'hr_attendance',
'hr_payslip',
'hr_expense',
'hr_expense_sheet',
# Products / Pricing
'product_pricelist',
# Properties / Sequences
'ir_sequence',
]
# We'll use raw SQL because Odoo ORM will block updates to company_id on posted entries and confirmed orders.
total_updated = 0
for table in tables_to_update:
# Check if table exists and has company_id
env.cr.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name=%s AND column_name='company_id'
""", (table,))
if env.cr.fetchone():
env.cr.execute(f"""
UPDATE {table}
SET company_id = %s
WHERE company_id = %s
""", (target_company.id, source_company.id))
rowcount = env.cr.rowcount
if rowcount > 0:
print(f"Updated {rowcount} records in table '{table}'")
total_updated += rowcount
# Note on ir_property: it stores things like default receivable/payable accounts
# Updating them blindly might break the parent company. If the parent
# needs to remain somewhat functional, we probably shouldn't migrate all ir_properties.
# But if it's strictly a "move everything to the new shop", we could.
# For safety, I've left ir_property out and let standard branch inheritance handle accounts.
print(f"Committing changes... (Total {total_updated} rows updated)")
env.cr.commit()
env.invalidate_all()
print("Migration complete!")
if __name__ == '__main__':
try:
migrate_to_branch(env)
except NameError:
print("Please run this script using Odoo shell:")
print("Example: ./.venv/bin/python ./odoo/odoo-bin shell -d <DB_NAME> -c odoo.conf < scripts/migrate_to_branch.py")