From becc8ac426b5be4acd418cc0e75e70993f34732e Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Thu, 14 Apr 2022 11:16:37 +0200 Subject: [PATCH] [14.0][IMP] account_payment_order: Improve normal communication if there is a credit note If there is a credit note that partially cancel an invoice, the payment communication should be the combination of the invoice reference and the credit note one. --- .../models/account_move_line.py | 6 +++ .../tests/test_payment_order_outbound.py | 46 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/account_payment_order/models/account_move_line.py b/account_payment_order/models/account_move_line.py index 2d90c29d0..5389b1c9d 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -71,6 +71,12 @@ class AccountMoveLine(models.Model): elif "out" in self.move_id.move_type: # Force to only put invoice number here communication = self.move_id.name + # If we have credit note(s) - reversal_move_id is a one2many + if self.move_id.reversal_move_id: + references = self.move_id.reversal_move_id.filtered( + lambda r: r.ref + ).mapped("ref") + communication += " " + " ".join(references) return communication_type, communication def _prepare_payment_line_vals(self, payment_order): diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index 1e91f3b83..2e2ab8d7d 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -6,6 +6,7 @@ from datetime import date, datetime, timedelta from odoo import fields from odoo.exceptions import UserError, ValidationError +from odoo.tests.common import Form from odoo.addons.account.tests.common import AccountTestInvoicingCommon @@ -85,6 +86,20 @@ class TestPaymentOrderOutboundBase(AccountTestInvoicingCommon): return invoice + def _create_supplier_refund(self, move): + wizard = ( + self.env["account.move.reversal"] + .with_context(active_model="account.move", active_ids=move.ids) + .create( + { + "date_mode": "custom", + "refund_method": "refund", + } + ) + ) + wizard.reverse_moves() + return wizard.new_move_ids + class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): def test_creation_due_date(self): @@ -261,3 +276,34 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): self.assertEqual( outbound_order.payment_line_ids[1].bank_line_id.date, date.today() ) + + def test_supplier_refund(self): + """ + Confirm the supplier invoice + Create a credit note based on that one with an inferior amount + Confirm the credit note + Create the payment order + The communication should be a combination of the invoice reference + and the credit note one + """ + self.invoice.action_post() + self.refund = self._create_supplier_refund(self.invoice) + with Form(self.refund) as refund_form: + refund_form.ref = "R1234" + with refund_form.invoice_line_ids.edit(0) as line_form: + line_form.price_unit = 75.0 + + self.refund.action_post() + + self.env["account.invoice.payment.line.multi"].with_context( + active_model="account.move", active_ids=self.invoice.ids + ).create({}).run() + + payment_order = self.env["account.payment.order"].search(self.domain) + self.assertEqual(len(payment_order), 1) + + payment_order.write({"journal_id": self.bank_journal.id}) + + self.assertEqual(len(payment_order.payment_line_ids), 1) + + self.assertEqual("F1242 R1234", payment_order.payment_line_ids.communication)