feat: implement post-init and uninstall hooks to automatically sync and manage shared bank/cash accounts across company branches
This commit is contained in:
parent
1f60af1e81
commit
6a8fcab936
43
__init__.py
43
__init__.py
@ -1 +1,44 @@
|
||||
from . import models
|
||||
from odoo import api, SUPERUSER_ID
|
||||
|
||||
def _auto_share_accounts_post_init(env):
|
||||
"""
|
||||
Automatically link accounts from the Parent Company (ID 2 - OT) to any Branch Company
|
||||
that does not have its own Chart of Accounts.
|
||||
"""
|
||||
# Link Parent Company (ID 2) accounts to other companies if they don't have their own COA
|
||||
env.cr.execute("""
|
||||
INSERT INTO account_account_res_company_rel (account_id, company_id)
|
||||
SELECT a.id, c.id
|
||||
FROM account_account a
|
||||
CROSS JOIN res_company c
|
||||
WHERE a.company_id = 2
|
||||
AND c.id != 2
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM account_account_res_company_rel r
|
||||
WHERE r.account_id = a.id AND r.company_id = c.id
|
||||
)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM account_account a2
|
||||
WHERE a2.company_id = c.id
|
||||
)
|
||||
ON CONFLICT DO NOTHING
|
||||
""")
|
||||
|
||||
# Set a dummy chart template for branches to satisfy Odoo 19 POS validation
|
||||
env.cr.execute("""
|
||||
UPDATE res_company
|
||||
SET chart_template = 'id'
|
||||
WHERE id != 2 AND chart_template IS NULL
|
||||
""")
|
||||
|
||||
def _cleanup_shared_accounts_uninstall(env):
|
||||
"""
|
||||
Remove the shared account relations upon uninstallation to prevent
|
||||
UserErrors when Odoo's standard company consistency checks are restored.
|
||||
"""
|
||||
env.cr.execute("""
|
||||
DELETE FROM account_account_res_company_rel
|
||||
WHERE company_id != 2
|
||||
AND account_id IN (SELECT id FROM account_account WHERE company_id = 2)
|
||||
""")
|
||||
|
||||
@ -15,4 +15,6 @@ This module removes the standard restriction that prevents Chart of Accounts (CO
|
||||
'installable': True,
|
||||
'application': False,
|
||||
'license': 'LGPL-3',
|
||||
'post_init_hook': '_auto_share_accounts_post_init',
|
||||
'uninstall_hook': '_cleanup_shared_accounts_uninstall',
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user