odoo_utility_scripts/clone_cash_rounding_pricelist.py

113 lines
5.2 KiB
Python

import sys
def clone_rounding_and_pricelist(env):
source_name = 'Kedai Kipas 58 Rungkut'
target_name = 'Kedai Kipas 58 Tenggilis'
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})")
# Let's clone Cash Rounding. If it's owned by parent, we don't necessarily need to clone the model,
# but let's check what roundings Rungkut has. Rungkut might share the parent's rounding.
# The user asked to "clone cash rounding setup". This probably means the POS config settings!
print("\n--- Applying Cash Rounding Setup from Rungkut to Tenggilis POS ---")
source_configs = env['pos.config'].with_context(active_test=False).search([
('company_id', '=', source_company.id),
('cash_rounding', '=', True) # Only grab the one that actually has it configured!
], limit=1)
target_configs = env['pos.config'].with_context(active_test=False).search([
('company_id', '=', target_company.id)
])
if source_configs and target_configs:
template_config = source_configs[0]
for t_config in target_configs:
t_config.write({
'cash_rounding': template_config.cash_rounding,
'rounding_method': template_config.rounding_method.id if template_config.rounding_method else False,
'only_round_cash_method': template_config.only_round_cash_method,
})
print(f" -> Applied Cash Rounding setup to POS '{t_config.name}'")
else:
print(" -> Could not find POS Configs in one or both companies, skipping pos.config rounding setup.")
# 2. Clone Pricelists
print("\n--- Cloning Pricelists ---")
pricelists = env['product.pricelist'].with_context(active_test=False).search([
('company_id', '=', source_company.id)
])
count_pricelist = 0
for pricelist in pricelists:
existing = env['product.pricelist'].with_context(active_test=False).search([
('name', '=', pricelist.name),
('company_id', '=', target_company.id)
], limit=1)
if existing:
print(f" -> '{pricelist.name}' already exists in {target_company.name}. Skipping.")
continue
try:
with env.cr.savepoint():
new_pricelist = pricelist.copy({
'company_id': target_company.id,
# We clear website_id to fix the website company restriction error
'website_id': False,
})
# Ensure name matches exactly to avoid "(copy)"
new_pricelist.write({'name': pricelist.name})
print(f" -> Cloned Pricelist '{pricelist.name}' (New ID: {new_pricelist.id})")
count_pricelist += 1
except Exception as e:
print(f" -> Failed to clone Pricelist '{pricelist.name}': {e}")
# Do Tenggilis POS configs need the cloned pricelists?
# If the Rungkut POS had a pricelist set, we should set the equivalent in Tenggilis
if source_configs and target_configs:
print("\n--- Updating POS Pricelist configuration ---")
for s_config, t_config in zip(source_configs, target_configs):
if s_config.pricelist_id:
# Find the Tenggilis equivalent of this pricelist
matching_pl = env['product.pricelist'].with_context(active_test=False).search([
('name', '=', s_config.pricelist_id.name),
('company_id', '=', target_company.id)
], limit=1)
if matching_pl:
t_config.write({'pricelist_id': matching_pl.id})
print(f" -> Set Pricelist '{matching_pl.name}' on POS '{t_config.name}'")
# Also handle available pricelists
if s_config.available_pricelist_ids:
mapped_ids = []
for pl in s_config.available_pricelist_ids:
match = env['product.pricelist'].with_context(active_test=False).search([
('name', '=', pl.name),
('company_id', '=', target_company.id)
], limit=1)
if match:
mapped_ids.append(match.id)
if mapped_ids:
t_config.write({'available_pricelist_ids': [(6, 0, mapped_ids)]})
print(f" -> Set {len(mapped_ids)} Available Pricelists on POS '{t_config.name}'")
print(f"\nCommitting changes... (Cloned setup, {count_pricelist} pricelists)")
env.cr.commit()
print("Done!")
if __name__ == '__main__':
try:
clone_rounding_and_pricelist(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/clone_cash_rounding_pricelist.py")