[IMP] account_move_last_payment retrieve only the date

This commit is contained in:
2026-10-29 12:07:30 +01:00
parent 42676fcdd0
commit d6b87de53c
5 changed files with 115 additions and 97 deletions

View File

@@ -1,3 +1,6 @@
# Copyright 2026 Elabore
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
@@ -5,80 +8,64 @@ class AccountMove(models.Model):
_inherit = "account.move"
last_payment_date = fields.Date(
compute="_compute_last_payment_info",
compute="_compute_last_payment_date",
store=True,
copy=False,
help="Date of the most recent payment reconciled with this "
"invoice, whatever the reconciliation path (explicit payment "
"or direct bank statement line reconciliation).",
)
last_payment_method = fields.Char(
compute="_compute_last_payment_info",
store=True,
copy=False,
help="Payment method of the most recent payment reconciled with this invoice.",
)
preferred_payment_method_line_id = fields.Many2one(string="Planned Payment Method")
@api.depends(
"line_ids.matched_debit_ids.debit_move_id.date",
"line_ids.matched_debit_ids.debit_move_id.move_id.origin_payment_id.payment_method_line_id.name",
"line_ids.matched_debit_ids.debit_move_id.move_id.origin_payment_id.date",
"line_ids.matched_debit_ids.debit_move_id.move_id.statement_line_id",
"line_ids.matched_credit_ids.credit_move_id.date",
"line_ids.matched_credit_ids.credit_move_id.move_id.origin_payment_id.payment_method_line_id.name",
"line_ids.matched_credit_ids.credit_move_id.move_id.origin_payment_id.date",
"line_ids.matched_credit_ids.credit_move_id.move_id.statement_line_id",
)
def _compute_last_payment_info(self):
"""Compute the last real payment date and method.
def _compute_last_payment_date(self):
"""Compute the date of the last real payment.
Only customer and vendor invoices carry the information; every
other move type gets both fields cleared. Candidates are the
other move type gets the field cleared. Candidates are the
reconciliation counterpart lines of the receivable/payable
lines (exchange difference moves excluded). The most recent
candidate wins; on equal dates the first candidate found wins.
date wins.
"""
for move in self:
move.last_payment_date = False
if move.move_type not in ("in_invoice", "out_invoice"):
move.last_payment_date = False
move.last_payment_method = False
continue
candidates = move._get_last_payment_candidates()
if not candidates:
move.last_payment_date = False
move.last_payment_method = False
else:
latest_date, latest_method = max(
candidates, key=lambda candidate: candidate[0]
)
move.last_payment_date = latest_date
move.last_payment_method = latest_method
dates = move._get_last_payment_dates()
if dates:
move.last_payment_date = max(dates)
def _get_last_payment_candidates(self):
"""Collect the candidate real payments for this invoice.
def _get_last_payment_dates(self):
"""Collect the dates of the real payments of this invoice.
A candidate is a ``(date, method)`` tuple built from each
reconciliation counterpart line:
A date is collected from each reconciliation counterpart line:
- counterpart move with ``origin_payment_id``: explicit
payment, dated with the payment date, method from the
payment method line;
payment, dated with the payment date;
- counterpart move with ``statement_line_id``: direct bank
statement reconciliation, dated with the counterpart move
date, method fixed to the bank transfer label;
date;
- any other manual reconciliation is ignored.
:return: list of ``(date, method)`` tuples, possibly empty.
:return: list of dates, possibly empty.
"""
self.ensure_one()
invoice_partials, exchange_diff_moves = self._get_reconciled_invoices_partials()
candidates = []
dates = []
for _partial, _amount, counterpart_line in invoice_partials:
if counterpart_line.move_id.id in exchange_diff_moves:
continue
payment = counterpart_line.move_id.origin_payment_id
if payment:
candidates.append((payment.date, payment.payment_method_line_id.name))
dates.append(payment.date)
elif counterpart_line.move_id.statement_line_id:
candidates.append(
(counterpart_line.move_id.date, self.env._("Bank Transfer"))
)
return candidates
dates.append(counterpart_line.move_id.date)
return dates