diff --git a/tryton/modules/account/account.py b/tryton/modules/account/account.py index 05730d5b6e..c418aa0227 100644 --- a/tryton/modules/account/account.py +++ b/tryton/modules/account/account.py @@ -434,8 +434,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 = ( @@ -3309,6 +3312,8 @@ class UpdateChart(Wizard): account.type.template.create_type( company.id, template2type=template2type) + # Update again to set new parent + account.type.update_type(template2type=template2type) # Update accounts template2account = {} @@ -3333,6 +3338,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 = {} @@ -3344,6 +3354,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 = {} @@ -3360,7 +3374,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/tryton/modules/account/tax.py b/tryton/modules/account/tax.py index 5c13e8e53e..4528d58aa0 100644 --- a/tryton/modules/account/tax.py +++ b/tryton/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/tryton/modules/account/tests/test_module.py b/tryton/modules/account/tests/test_module.py index 5ce6b9da79..56cd49fb5b 100644 --- a/tryton/modules/account/tests/test_module.py +++ b/tryton/modules/account/tests/test_module.py @@ -1861,5 +1861,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