[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

@@ -6,7 +6,7 @@ from odoo.addons.account.tests.common import AccountTestInvoicingCommon
@tagged("post_install", "-at_install")
class TestAccountMoveLastPayment(AccountTestInvoicingCommon):
"""Test the last payment date and method stored on invoices."""
"""Test the last payment date stored on invoices."""
@classmethod
def setUpClass(cls):
@@ -85,32 +85,26 @@ class TestAccountMoveLastPayment(AccountTestInvoicingCommon):
return statement_line
def test_register_payment(self):
"""Explicit payment: fields match the payment date and method."""
"""Explicit payment: the field matches the payment date."""
invoice = self._create_posted_invoice()
payment = self._register_payment(invoice)
self.assertEqual(invoice.last_payment_date, payment.date)
self.assertEqual(
invoice.last_payment_method, payment.payment_method_line_id.name
)
def test_bank_statement_reconciliation(self):
"""Direct statement reconciliation: statement date + Bank Transfer."""
"""Direct statement reconciliation: the statement date."""
invoice = self._create_posted_invoice()
yesterday = fields.Date.subtract(fields.Date.today(), days=1)
statement_line = self._reconcile_statement_line(
invoice, invoice.amount_total, date=yesterday
)
self.assertEqual(invoice.last_payment_date, statement_line.date)
self.assertEqual(invoice.last_payment_method, "Bank Transfer")
def test_list_view_and_export(self):
"""Both fields are stored and shown in the invoice list view."""
"""The date is stored; date and preferred payment method are listed."""
self.assertTrue(self.env["account.move"]._fields["last_payment_date"].store)
self.assertTrue(self.env["account.move"]._fields["last_payment_method"].store)
view = self.env["ir.ui.view"]._get_view_id("account.view_invoice_tree")
arch = self.env["account.move"].get_view(view_id=view, view_type="list")["arch"]
self.assertIn('name="last_payment_date"', arch)
self.assertIn('name="last_payment_method"', arch)
def test_multiple_payments_keeps_latest(self):
"""With several payments, the most recent candidate wins."""
@@ -120,7 +114,15 @@ class TestAccountMoveLastPayment(AccountTestInvoicingCommon):
self._register_payment(invoice, date=older_date, amount=400.0)
self._register_payment(invoice, date=newer_date, amount=600.0)
self.assertEqual(invoice.last_payment_date, newer_date)
self.assertEqual(invoice.last_payment_method, self.inbound_method_line.name)
def test_statement_newer_than_payment(self):
"""A more recent statement reconciliation wins over an older payment."""
invoice = self._create_posted_invoice()
older_date = fields.Date.subtract(fields.Date.today(), days=10)
newer_date = fields.Date.subtract(fields.Date.today(), days=2)
self._register_payment(invoice, date=older_date, amount=400.0)
self._reconcile_statement_line(invoice, 600.0, date=newer_date)
self.assertEqual(invoice.last_payment_date, newer_date)
def test_statement_line_date_change_recompute(self):
"""Changing the statement line date updates the stored fields."""
@@ -135,12 +137,11 @@ class TestAccountMoveLastPayment(AccountTestInvoicingCommon):
self.assertEqual(invoice.last_payment_date, older_date)
def test_unreconcile_clears_fields(self):
"""Unreconciling the invoice clears both fields."""
"""Unreconciling the invoice clears the field."""
invoice = self._create_posted_invoice()
self._register_payment(invoice)
invoice.line_ids.remove_move_reconcile()
self.assertFalse(invoice.last_payment_date)
self.assertFalse(invoice.last_payment_method)
def _setup_eur_rates(self, invoice_date, payment_date):
"""Set deterministic EUR rates so the payment creates an
@@ -168,9 +169,8 @@ class TestAccountMoveLastPayment(AccountTestInvoicingCommon):
return eur
def test_multicurrency_invoice(self):
"""A foreign currency payment keeps its real date and method,
and the generated exchange difference move is not taken as a
payment candidate."""
"""A foreign currency payment keeps its real date, and the
generated exchange difference move is not taken as a payment."""
payment_date = fields.Date.subtract(fields.Date.today(), days=5)
invoice_date = fields.Date.from_string("2019-01-01")
eur = self._setup_eur_rates(invoice_date, payment_date)
@@ -183,22 +183,18 @@ class TestAccountMoveLastPayment(AccountTestInvoicingCommon):
amount=invoice.amount_residual,
)
self.assertTrue(invoice.line_ids.mapped("matched_credit_ids.exchange_move_id"))
candidates = invoice._get_last_payment_candidates()
self.assertEqual(len(candidates), 1)
dates = invoice._get_last_payment_dates()
self.assertEqual(dates, [payment.date])
self.assertEqual(invoice.last_payment_date, payment.date)
self.assertEqual(
invoice.last_payment_method, payment.payment_method_line_id.name
)
def test_draft_invoice_is_empty(self):
"""A draft invoice has never been reconciled, so both fields
stay empty."""
"""A draft invoice has never been reconciled, so the field
stays empty."""
invoice = self.init_invoice("out_invoice", post=False, amounts=[1000.0])
self.assertFalse(invoice.last_payment_date)
self.assertFalse(invoice.last_payment_method)
def test_non_invoice_moves_are_empty(self):
"""Journal entries and credit notes never carry the fields."""
"""Journal entries and credit notes never carry the field."""
invoice = self._create_posted_invoice()
self._register_payment(invoice)
refund = self._create_posted_invoice(move_type="out_refund")
@@ -233,7 +229,5 @@ class TestAccountMoveLastPayment(AccountTestInvoicingCommon):
)
entry.action_post()
self.assertFalse(refund.last_payment_date)
self.assertFalse(refund.last_payment_method)
self.assertFalse(entry.last_payment_date)
self.assertFalse(entry.last_payment_method)
self.assertTrue(invoice.last_payment_date)
self.assertTrue(invoice.last_payment_date)