72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
# Copyright 2026 Elabore
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
|
|
from odoo import api, fields, models
|
|
|
|
|
|
class AccountMove(models.Model):
|
|
_inherit = "account.move"
|
|
|
|
last_payment_date = fields.Date(
|
|
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).",
|
|
)
|
|
|
|
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.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.date",
|
|
"line_ids.matched_credit_ids.credit_move_id.move_id.statement_line_id",
|
|
)
|
|
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 the field cleared. Candidates are the
|
|
reconciliation counterpart lines of the receivable/payable
|
|
lines (exchange difference moves excluded). The most recent
|
|
date wins.
|
|
"""
|
|
for move in self:
|
|
move.last_payment_date = False
|
|
if move.move_type not in ("in_invoice", "out_invoice"):
|
|
continue
|
|
dates = move._get_last_payment_dates()
|
|
if dates:
|
|
move.last_payment_date = max(dates)
|
|
|
|
def _get_last_payment_dates(self):
|
|
"""Collect the dates of the real payments of this invoice.
|
|
|
|
A date is collected from each reconciliation counterpart line:
|
|
|
|
- counterpart move with ``origin_payment_id``: explicit
|
|
payment, dated with the payment date;
|
|
- counterpart move with ``statement_line_id``: direct bank
|
|
statement reconciliation, dated with the counterpart move
|
|
date;
|
|
- any other manual reconciliation is ignored.
|
|
|
|
:return: list of dates, possibly empty.
|
|
"""
|
|
self.ensure_one()
|
|
invoice_partials, exchange_diff_moves = self._get_reconciled_invoices_partials()
|
|
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:
|
|
dates.append(payment.date)
|
|
elif counterpart_line.move_id.statement_line_id:
|
|
dates.append(counterpart_line.move_id.date)
|
|
return dates
|