From f309572ba0a336e70f6d902dea0bd34130dc7cee Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Fri, 9 Jun 2023 14:12:34 +0200 Subject: [PATCH] account_check_deposit: backport improvements from v16 Backport code from account_check_deposit v16: - reduce number of account.move.line in the move of the check deposit: 1 line in the check account instead of 1 line per check - backport the code of the method backtodraft() (state done -> draft) --- .../models/account_check_deposit.py | 126 +++++++++--------- 1 file changed, 61 insertions(+), 65 deletions(-) diff --git a/account_check_deposit/models/account_check_deposit.py b/account_check_deposit/models/account_check_deposit.py index 53a5b2411..61d380589 100644 --- a/account_check_deposit/models/account_check_deposit.py +++ b/account_check_deposit/models/account_check_deposit.py @@ -206,15 +206,21 @@ class AccountCheckDeposit(models.Model): for deposit in self: if deposit.move_id: move = deposit.move_id - # It will raise here if journal_id.update_posted = False + counterpart_move_line = move.line_ids.filtered( + lambda x: x.account_id.id != deposit.journal_default_account_id.id + ) + if counterpart_move_line.reconciled: + raise UserError( + _("Deposit '%s' has already been credited on the bank account.") + % deposit.display_name + ) + move.line_ids.filtered( + lambda x: x.account_id.id == deposit.journal_default_account_id.id + ).remove_move_reconcile() if move.state == "posted": - move.button_draft() - for line in deposit.check_payment_ids: - if line.reconciled: - line.remove_move_reconcile() - move.unlink() + move.button_cancel() + move.with_context(force_delete=True).unlink() deposit.write({"state": "draft"}) - return True @api.model def create(self, vals): @@ -226,76 +232,66 @@ class AccountCheckDeposit(models.Model): ) return super().create(vals) - def _prepare_account_move_vals(self): + def _prepare_move_vals(self): self.ensure_one() - move_vals = { + total_debit = 0.0 + total_amount_currency = 0.0 + for line in self.check_payment_ids: + total_debit += line.debit + total_amount_currency += line.amount_currency + + total_debit = self.company_id.currency_id.round(total_debit) + total_amount_currency = self.currency_id.round(total_amount_currency) + + counterpart_account_id = self.bank_journal_id.payment_debit_account_id.id + if not counterpart_account_id: + raise UserError( + _("Missing 'Outstanding Receipts Account' on the journal '%s'.") + % self.bank_journal_id.display_name + ) + + vals = { "journal_id": self.journal_id.id, "date": self.deposit_date, "ref": _("Check Deposit %s") % self.name, "company_id": self.company_id.id, + "line_ids": [ + ( + 0, + 0, + { + "account_id": self.journal_default_account_id.id, + "partner_id": False, + "credit": total_debit, + "currency_id": self.currency_id.id, + "amount_currency": total_amount_currency * -1, + }, + ), + ( + 0, + 0, + { + "account_id": counterpart_account_id, + "partner_id": False, + "debit": total_debit, + "currency_id": self.currency_id.id, + "amount_currency": total_amount_currency, + }, + ), + ], } - return move_vals - - @api.model - def _prepare_move_line_vals(self, line): - assert line.debit > 0, "Debit must have a value" - return { - "name": line.ref and _("Check Ref. %s") % line.ref or False, - "credit": line.debit, - "debit": 0.0, - "account_id": line.account_id.id, - "partner_id": line.partner_id.id, - "currency_id": line.currency_id.id or False, - "amount_currency": line.amount_currency * -1, - } - - def _prepare_counterpart_move_lines_vals(self, total_debit, total_amount_currency): - self.ensure_one() - if not self.bank_journal_id.payment_debit_account_id: - raise UserError( - _("Missing 'Outstanding Receipts Account' on the bank journal '%s'.") - % self.bank_journal_id.display_name - ) - return { - "debit": total_debit, - "credit": 0.0, - "account_id": self.bank_journal_id.payment_debit_account_id.id, - "partner_id": False, - "currency_id": self.currency_id.id or False, - "amount_currency": total_amount_currency, - } + return vals def validate_deposit(self): am_obj = self.env["account.move"] - move_line_obj = self.env["account.move.line"] for deposit in self: - move_vals = deposit._prepare_account_move_vals() - move = am_obj.create(move_vals) - total_debit = 0.0 - total_amount_currency = 0.0 - to_reconcile_lines = [] - for line in deposit.check_payment_ids: - total_debit += line.debit - total_amount_currency += line.amount_currency - line_vals = self._prepare_move_line_vals(line) - line_vals["move_id"] = move.id - move_line = move_line_obj.with_context( - check_move_validity=False - ).create(line_vals) - to_reconcile_lines.append(line + move_line) - - # Create counter-part - counter_vals = deposit._prepare_counterpart_move_lines_vals( - total_debit, total_amount_currency - ) - counter_vals["move_id"] = move.id - move_line_obj.create(counter_vals) + move = am_obj.create(deposit._prepare_move_vals()) move.action_post() - + lines_to_rec = self.check_payment_ids + move.line_ids.filtered( + lambda x: x.account_id.id == self.journal_default_account_id.id + ) + lines_to_rec.reconcile() deposit.write({"state": "done", "move_id": move.id}) - for reconcile_lines in to_reconcile_lines: - reconcile_lines.reconcile() - return True @api.onchange("company_id") def onchange_company_id(self):