From 637dda0d2b02ba50deb2d0ac416f9afa20eb2f32 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Fri, 15 Dec 2017 11:39:19 +0100 Subject: [PATCH] [FIX] account: verify that account exists before automatic reconcile When using the fast_counterpart creation method to automatically reconcile bank statement lines, there was no verification that the account id was not set or that the statement was already linked to journal entries. It was causing a traceback in some conditions when the demo data were loaded because the fast_counterpart_creation was called without the verification, using an XML function. This commmit introduce the necessary verifcation. Thanks @qdp-odoo for the review and some docstrings/comments --- .../account/models/account_bank_statement.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/addons/account/models/account_bank_statement.py b/addons/account/models/account_bank_statement.py index 567c8504637..7de0fc49190 100644 --- a/addons/account/models/account_bank_statement.py +++ b/addons/account/models/account_bank_statement.py @@ -234,9 +234,10 @@ class AccountBankStatement(models.Model): for statement in statements: moves = self.env['account.move'] for st_line in statement.line_ids: - if st_line.account_id and not st_line.journal_entry_ids.ids: - st_line.fast_counterpart_creation() - elif not st_line.journal_entry_ids.ids and not statement.currency_id.is_zero(st_line.amount): + #upon bank statement confirmation, look if some lines have the account_id set. It would trigger a journal entry + #creation towards that account, with the wanted side-effect to skip that line in the bank reconciliation widget. + st_line.fast_counterpart_creation() + if not st_line.account_id and not st_line.journal_entry_ids.ids and not st_line.statement_id.currency_id.is_zero(st_line.amount): raise UserError(_('All the account entries lines must be processed in order to close the statement.')) for aml in st_line.journal_entry_ids: moves |= aml.move_id @@ -823,15 +824,20 @@ class AccountBankStatementLine(models.Model): st_line.with_context(ctx).process_reconciliation(datum.get('counterpart_aml_dicts', []), payment_aml_rec, datum.get('new_aml_dicts', [])) def fast_counterpart_creation(self): + """This function is called when confirming a bank statement and will allow to automatically process lines without + going in the bank reconciliation widget. By setting an account_id on bank statement lines, it will create a journal + entry using that account to counterpart the bank account + """ for st_line in self: # Technical functionality to automatically reconcile by creating a new move line - vals = { - 'name': st_line.name, - 'debit': st_line.amount < 0 and -st_line.amount or 0.0, - 'credit': st_line.amount > 0 and st_line.amount or 0.0, - 'account_id': st_line.account_id.id, - } - st_line.process_reconciliation(new_aml_dicts=[vals]) + if st_line.account_id and not st_line.journal_entry_ids.ids: + vals = { + 'name': st_line.name, + 'debit': st_line.amount < 0 and -st_line.amount or 0.0, + 'credit': st_line.amount > 0 and st_line.amount or 0.0, + 'account_id': st_line.account_id.id, + } + st_line.process_reconciliation(new_aml_dicts=[vals]) def _get_communication(self, payment_method_id): return self.name or ''