From 6d420a33245aebc1365d46be4abbe80700e8c7be Mon Sep 17 00:00:00 2001 From: "Habib (ayh)" Date: Wed, 3 May 2023 09:50:12 +0000 Subject: [PATCH] [IMP] account: fix transaction demo data MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bank statement & transaction demo data has problems: 1. the statement doesn't have a name 2. the creation of the statement lines are postponed causing the opening/closing balances to be invalid (as the order of the statement lines is important) This is due to the _load_data method in account, which defers the creation of records to ensure that related records are created before they are referenced in relational fields. This commit adds a condition to avoid deferring fields that have a list of ORM create commands, and fixes the statement name and balances. closes odoo/odoo#120975 X-original-commit: 1d219de36944685153a77aeeb72920b06728383e Signed-off-by: William André (wan) Signed-off-by: Ayob Habib (ayh) --- addons/account/demo/account_demo.py | 11 +++-------- addons/account/models/chart_template.py | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/addons/account/demo/account_demo.py b/addons/account/demo/account_demo.py index 4d69836dc43..4e9facb2635 100644 --- a/addons/account/demo/account_demo.py +++ b/addons/account/demo/account_demo.py @@ -139,20 +139,15 @@ class AccountChartTemplate(models.AbstractModel): ) return { 'demo_bank_statement_1': { - 'journal_id': self.env['account.journal'].search([ - ('type', '=', 'bank'), - ('company_id', '=', cid), - ], limit=1).id, - 'date': time.strftime('%Y')+'-01-01', - 'balance_end_real': 9944.87, - 'balance_start': 5103.0, + 'name': f'{bnk_journal.name} - {time.strftime("%Y")}-01-01/1', + 'balance_end_real': 6378.0, + 'balance_start': 0.0, 'line_ids': [ Command.create({ 'journal_id': bnk_journal.id, 'payment_ref': 'Initial balance', 'amount': 5103.0, 'date': time.strftime('%Y-01-01'), - 'partner_id': 'base.res_partner_12', }), Command.create({ 'journal_id': bnk_journal.id, diff --git a/addons/account/models/chart_template.py b/addons/account/models/chart_template.py index b642e18c3f5..e3165b07cea 100644 --- a/addons/account/models/chart_template.py +++ b/addons/account/models/chart_template.py @@ -393,15 +393,27 @@ class AccountChartTemplate(models.AbstractModel): to_delay = defaultdict(dict) for xml_id, vals in data.items(): to_be_removed = [] - for field_name in vals: + for field_name, field_val in vals.items(): field = self.env[model]._fields.get(field_name, None) - if (field and - field.relational and - field.comodel_name not in created_models and - (field.comodel_name in dict(all_data) or field.comodel_name == model) + if ( + field + and field.relational + and field_val + and ( # allow create commands but delay all other related fields + not isinstance(field_val, (list, tuple)) + or ( + isinstance(field_val[0], (list, tuple)) + and {command for command, *dummy in field_val} != {Command.CREATE} + ) + ) + and field.comodel_name not in created_models + and ( + field.comodel_name in dict(all_data) + or field.comodel_name == model + ) ): to_be_removed.append(field_name) - to_delay[xml_id][field_name] = vals.get(field_name) + to_delay[xml_id][field_name] = field_val for field_name in to_be_removed: del vals[field_name] if any(to_delay.values()):