From c6335a368c02706250e4c44cb32c8d49c8028652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 16 Nov 2023 16:32:43 +0100 Subject: [PATCH] Update trees of chart of accounts with 2 passes When a new parent is added to a tree template like types, taxes or tax codes, the corresponding record of the new template parent is not yet created. So we need to use a default value for the parent and update again the tree after creating the new records. Closes #12755 --- modules/account/account.py | 20 ++++++++-- modules/account/tax.py | 6 +-- modules/account/tests/test_module.py | 59 ++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/modules/account/account.py b/modules/account/account.py index 11225ae174..591b95b1bd 100644 --- a/modules/account/account.py +++ b/modules/account/account.py @@ -427,8 +427,11 @@ class Type( if child.template: if not child.template_override: if child.template.parent: + # Fallback to current parent + # to keep under the same root parent = template2type.get( - child.template.parent.id) + child.template.parent.id, + child.parent) else: parent = None old_parent = ( @@ -3316,14 +3319,16 @@ class UpdateChart(Wizard): account = Account(self.start.account.id) company = account.company - # Update account types template2type = {} + # Update account types account.type.update_type(template2type=template2type) # Create missing account types if account.type.template: account.type.template.create_type( company.id, template2type=template2type) + # Update again to set new parent + account.type.update_type(template2type=template2type) # Update accounts template2account = {} @@ -3348,6 +3353,11 @@ class UpdateChart(Wizard): account.template.id, account.company.id, template2account=template2account, template2tax=template2tax) + # Update again to set new parent + Tax.update_tax( + company.id, + template2account=template2account, + template2tax=template2tax) # Update tax codes template2tax_code = {} @@ -3359,6 +3369,10 @@ class UpdateChart(Wizard): TaxCodeTemplate.create_tax_code( account.template.id, company.id, template2tax_code=template2tax_code) + # Update again to set new parent + TaxCode.update_tax_code( + company.id, + template2tax_code=template2tax_code) # Update tax code lines template2tax_code_line = {} @@ -3375,7 +3389,7 @@ class UpdateChart(Wizard): template2tax_code=template2tax_code, template2tax_code_line=template2tax_code_line) - # Update taxes and replaced_by on accounts + # Update parent, taxes and replaced_by on accounts account.update_account2(template2account, template2tax) # Update tax rules diff --git a/modules/account/tax.py b/modules/account/tax.py index 5c13e8e53e..4528d58aa0 100644 --- a/modules/account/tax.py +++ b/modules/account/tax.py @@ -292,8 +292,8 @@ class TaxCode( if child.template: if not child.template_override: if child.template.parent: - parent = template2tax_code[ - child.template.parent.id] + parent = template2tax_code.get( + child.template.parent.id) else: parent = None old_parent = ( @@ -1172,7 +1172,7 @@ class Tax(sequence_ordered(), ModelSQL, ModelView, DeactivableMixin): if child.template: if not child.template_override: if child.template.parent: - parent = template2tax[child.template.parent.id] + parent = template2tax.get(child.template.parent.id) else: parent = None old_parent = ( diff --git a/modules/account/tests/test_module.py b/modules/account/tests/test_module.py index c152095150..1ddd5d7c6b 100644 --- a/modules/account/tests/test_module.py +++ b/modules/account/tests/test_module.py @@ -1929,5 +1929,64 @@ class AccountTestCase( Account.search([('name', '=', 'Main Cash')], count=True), 1) + @with_transaction() + def test_update_chart_new_parent(self): + "Test update chart of accounts with new parent" + pool = Pool() + ModelData = pool.get('ir.model.data') + TypeTemplate = pool.get('account.account.type.template') + Type = pool.get('account.account.type') + AccountTemplate = pool.get('account.account.template') + Account = pool.get('account.account') + UpdateChart = pool.get('account.update_chart', type='wizard') + + company = create_company() + with set_company(company): + create_chart(company, True) + + with Transaction().set_user(0): + root_type_template = TypeTemplate(ModelData.get_id( + 'account', 'account_type_template_minimal_en')) + + type_template, = TypeTemplate.search([ + ('parent', '!=', None), + ('parent', 'child_of', [root_type_template.id]), + ], limit=1) + new_type_template, = TypeTemplate.copy([type_template]) + type_template.parent = new_type_template + type_template.save() + + root_template = AccountTemplate(ModelData.get_id( + 'account', 'account_template_root_en')) + + account_template, = AccountTemplate.search([ + ('parent', '!=', None), + ('parent', 'child_of', [root_template.id]), + ], limit=1) + new_account_template, = AccountTemplate.copy([account_template]) + account_template.parent = new_account_template + account_template.save() + + with set_company(company): + account, = Account.search([('parent', '=', None)]) + session_id, _, _ = UpdateChart.create() + update_chart = UpdateChart(session_id) + update_chart.start.account = account + update_chart.transition_update() + + new_type, = Type.search( + [('template', '=', new_type_template.id)]) + type, = Type.search( + [('template', '=', type_template.id)]) + + self.assertEqual(type.parent, new_type) + + new_account, = Account.search( + [('template', '=', new_account_template.id)]) + account, = Account.search( + [('template', '=', account_template.id)]) + + self.assertEqual(account.parent, new_account) + del ModuleTestCase