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
This commit is contained in:
Cédric Krier committed 2023-11-16 16:32:43 +01:00
1 parent 3af76aa548
commit c6335a368c
3 files changed
+79 -6

No files matched your search

+17 -3
View File
@@ -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
+3 -3
View File
@@ -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 = (
+59
View File
@@ -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