diff --git a/account_reconciliation_widget/models/account_bank_statement.py b/account_reconciliation_widget/models/account_bank_statement.py index 44130980..5ff9ae02 100644 --- a/account_reconciliation_widget/models/account_bank_statement.py +++ b/account_reconciliation_widget/models/account_bank_statement.py @@ -97,10 +97,6 @@ class AccountBankStatementLine(models.Model): new_aml_dicts = new_aml_dicts or [] aml_obj = self.env["account.move.line"] - - company_currency = self.journal_id.company_id.currency_id - statement_currency = self.journal_id.currency_id or company_currency - counterpart_moves = self.env["account.move"] # Check and prepare received data @@ -129,34 +125,19 @@ class AccountBankStatementLine(models.Model): and user_type_id not in account_types ): account_types |= user_type_id - # Fully reconciled moves are just linked to the bank statement - total = self.amount - currency = self.currency_id or statement_currency + # Fully reconciled moves are just linked to the bank statement (blue lines), + # but the generated move on statement post should be removed and link the + # payment one for not having double entry + # TODO: To mix already done payments with new ones. Not sure if possible. for aml_rec in payment_aml_rec: - balance = ( - aml_rec.amount_currency if aml_rec.currency_id else aml_rec.balance - ) - aml_currency = aml_rec.currency_id or aml_rec.company_currency_id - total -= aml_currency._convert( - balance, currency, aml_rec.company_id, aml_rec.date - ) aml_rec.with_context(check_move_validity=False).write( {"statement_line_id": self.id} ) - counterpart_moves = counterpart_moves | aml_rec.move_id - # Update - if aml_rec.payment_id and aml_rec.move_id.state == "draft": - # In case the journal is set to only post payments when - # performing bank reconciliation, we modify its date and post - # it. - aml_rec.move_id.date = self.date - aml_rec.payment_id.payment_date = self.date - aml_rec.move_id.action_post() - # We check the paid status of the invoices reconciled with this - # payment - for invoice in aml_rec.payment_id.reconciled_invoice_ids: - self._check_invoice_state(invoice) - + old_move = self.move_id.with_context(force_delete=True) + self.move_id = aml_rec.move_id.id + old_move.button_draft() + old_move.unlink() + counterpart_moves |= aml_rec.move_id # Create move line(s). Either matching an existing journal entry # (eg. invoice), in which case we reconcile the existing and the # new move lines together, or being a write-off. @@ -325,3 +306,39 @@ class AccountBankStatementLine(models.Model): def _check_invoice_state(self, invoice): if invoice.is_invoice(include_receipts=True): invoice._compute_amount() + + def button_undo_reconciliation(self): + """Handle the case when the reconciliation was done against a direct payment + with the bank account as counterpart. This may be the case for payments made + in previous versions of Odoo. + """ + handled = self.env[self._name] + for record in self: + if record.move_id.payment_id: + # The reconciliation was done against a blue line (existing move) + # We remove the link on the current existing move, preserving it, + # and recreate a new move as if the statement line was new + record.move_id.line_ids.statement_line_id = False + statement = record.statement_id + journal = statement.journal_id + line_vals_list = record._prepare_move_line_default_vals() + new_move = self.env["account.move"].create( + { + "move_type": "entry", + "statement_line_id": record.id, + "ref": statement.name, + "date": record.date, + "journal_id": journal.id, + "partner_id": record.partner_id.id, + "currency_id": ( + journal.currency_id or journal.company_id.currency_id + ).id, + "line_ids": [(0, 0, line_vals) for line_vals in line_vals_list], + } + ) + new_move.action_post() + record.move_id = new_move.id + handled += record + return super( + AccountBankStatementLine, self - handled + ).button_undo_reconciliation() diff --git a/account_reconciliation_widget/static/src/js/reconciliation/reconciliation_model.js b/account_reconciliation_widget/static/src/js/reconciliation/reconciliation_model.js index 75f67935..fdb668e0 100644 --- a/account_reconciliation_widget/static/src/js/reconciliation/reconciliation_model.js +++ b/account_reconciliation_widget/static/src/js/reconciliation/reconciliation_model.js @@ -49,7 +49,7 @@ odoo.define("account.ReconciliationModel", function (require) { * label: string * amount: number - real amount * amount_str: string - formated amount - * [is_liquidity_line]: boolean + * [already_paid]: boolean * [partner_id]: integer * [partner_name]: string * [account_code]: string @@ -969,13 +969,13 @@ odoo.define("account.ReconciliationModel", function (require) { partner_id: line.st_line.partner_id, counterpart_aml_dicts: _.map( _.filter(props, function (prop) { - return !isNaN(prop.id) && !prop.is_liquidity_line; + return !isNaN(prop.id) && !prop.already_paid; }), self._formatToProcessReconciliation.bind(self, line) ), payment_aml_ids: _.pluck( _.filter(props, function (prop) { - return !isNaN(prop.id) && prop.is_liquidity_line; + return !isNaN(prop.id) && prop.already_paid; }), "id" ), @@ -1143,7 +1143,7 @@ odoo.define("account.ReconciliationModel", function (require) { } return; } - if (!prop.is_liquidity_line && parseInt(prop.id)) { + if (!prop.already_paid && parseInt(prop.id)) { prop.is_move_line = true; } reconciliation_proposition.push(prop);