From e346c66e4bdc832214f5077f78c2aeca5fa7042b Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Tue, 19 Apr 2022 11:50:52 +0200 Subject: [PATCH 1/2] [14.0][IMP] account_payment_order: Add existing payment references to communication If some movements have been reconciled with the original invoice, their references should be added in communication too. e.g.: Manual credit notes --- .../models/account_move_line.py | 25 ++++++-- .../tests/test_payment_order_outbound.py | 60 ++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/account_payment_order/models/account_move_line.py b/account_payment_order/models/account_move_line.py index 0638c4ddc..bc9a5e80a 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -58,6 +58,7 @@ class AccountMoveLine(models.Model): communication = self.ref or self.name # change these default values if move line is linked to an invoice if self.move_id.is_invoice(): + references = [] if (self.move_id.reference_type or "none") != "none": communication = self.move_id.ref ref2comm_type = aplo.invoice_reference_type2communication_type() @@ -73,11 +74,25 @@ class AccountMoveLine(models.Model): communication = self.move_id.payment_reference or self.move_id.name # If we have credit note(s) - reversal_move_id is a one2many if self.move_id.reversal_move_id: - references = [ - move.payment_reference or move.ref - for move in self.move_id.reversal_move_id - if move.payment_reference or move.ref - ] + references.extend( + [ + move.payment_reference or move.ref + for move in self.move_id.reversal_move_id + if move.payment_reference or move.ref + ] + ) + # Retrieve partial payments - e.g.: manual credit notes + for ( + _, + _, + payment_move_line, + ) in self.move_id._get_reconciled_invoices_partials(): + payment_move = payment_move_line.move_id + if payment_move.payment_reference or payment_move.ref: + references.append( + payment_move.payment_reference or payment_move.ref + ) + if references: communication += " " + " ".join(references) return communication_type, communication diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index 99cf6f188..dd243b9fd 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -86,7 +86,31 @@ class TestPaymentOrderOutboundBase(AccountTestInvoicingCommon): return invoice - def _create_supplier_refund(self, move): + def _create_supplier_refund(self, move, manual=False): + if manual: + # Do the supplier refund manually + vals = { + "partner_id": self.partner.id, + "move_type": "in_refund", + "ref": move.ref, + "payment_mode_id": self.mode.id, + "invoice_date": fields.Date.today(), + "invoice_line_ids": [ + ( + 0, + None, + { + "product_id": self.env.ref("product.product_product_4").id, + "quantity": 1.0, + "price_unit": 90.0, + "name": "refund of 90.0", + "account_id": self.invoice_line_account.id, + }, + ) + ], + } + move = self.env["account.move"].create(vals) + return move wizard = ( self.env["account.move.reversal"] .with_context(active_model="account.move", active_ids=move.ids) @@ -341,3 +365,37 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): self.assertEqual(len(payment_order.payment_line_ids), 1) self.assertEqual("F/1234 R1234", payment_order.payment_line_ids.communication) + + def test_supplier_manual_refund(self): + """ + Confirm the supplier invoice with reference + Create a credit note manually + Confirm the credit note + Reconcile move lines together + Create the payment order + The communication should be a combination of the invoice payment reference + and the credit note one + """ + self.invoice.action_post() + self.refund = self._create_supplier_refund(self.invoice, manual=True) + with Form(self.refund) as refund_form: + refund_form.ref = "R1234" + + self.refund.action_post() + + (self.invoice.line_ids + self.refund.line_ids).filtered( + lambda line: line.account_internal_type == "payable" + ).reconcile() + + 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) From c4e2f64334919ae433ebaa6bfbee13a0a058039c Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Fri, 22 Apr 2022 14:14:53 +0200 Subject: [PATCH 2/2] [14.0][FIX] account_payment_order: Don't duplicate communication reference --- .../models/account_move_line.py | 56 ++++++++++++------- .../tests/test_payment_order_outbound.py | 9 +++ 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/account_payment_order/models/account_move_line.py b/account_payment_order/models/account_move_line.py index bc9a5e80a..0e2a6ff6f 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -48,6 +48,40 @@ class AccountMoveLine(models.Model): else: ml.partner_bank_id = ml.partner_bank_id + def _get_linked_move_communication(self): + """ + This will collect the references from referral moves: + - Reversal moves + - Partial payments + """ + self.ensure_one() + references = [] + # Build a recordset to gather moves from which references have already + # taken in order to avoid duplicates + reference_moves = self.env["account.move"].browse() + # If we have credit note(s) - reversal_move_id is a one2many + if self.move_id.reversal_move_id: + references.extend( + [ + move.payment_reference or move.ref + for move in self.move_id.reversal_move_id + if move.payment_reference or move.ref + ] + ) + reference_moves |= self.move_id.reversal_move_id + # Retrieve partial payments - e.g.: manual credit notes + for ( + _, + _, + payment_move_line, + ) in self.move_id._get_reconciled_invoices_partials(): + payment_move = payment_move_line.move_id + if payment_move not in reference_moves and ( + payment_move.payment_reference or payment_move.ref + ): + references.append(payment_move.payment_reference or payment_move.ref) + return references + def _get_communication(self): """ Retrieve the communication string for the payment order @@ -58,7 +92,6 @@ class AccountMoveLine(models.Model): communication = self.ref or self.name # change these default values if move line is linked to an invoice if self.move_id.is_invoice(): - references = [] if (self.move_id.reference_type or "none") != "none": communication = self.move_id.ref ref2comm_type = aplo.invoice_reference_type2communication_type() @@ -72,26 +105,7 @@ class AccountMoveLine(models.Model): elif "out" in self.move_id.move_type: # Force to only put invoice number here communication = self.move_id.payment_reference or self.move_id.name - # If we have credit note(s) - reversal_move_id is a one2many - if self.move_id.reversal_move_id: - references.extend( - [ - move.payment_reference or move.ref - for move in self.move_id.reversal_move_id - if move.payment_reference or move.ref - ] - ) - # Retrieve partial payments - e.g.: manual credit notes - for ( - _, - _, - payment_move_line, - ) in self.move_id._get_reconciled_invoices_partials(): - payment_move = payment_move_line.move_id - if payment_move.payment_reference or payment_move.ref: - references.append( - payment_move.payment_reference or payment_move.ref - ) + references = self._get_linked_move_communication() if references: communication += " " + " ".join(references) return communication_type, communication diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index dd243b9fd..2e42500cc 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -353,6 +353,15 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): self.refund.action_post() + # The user add the outstanding payment to the invoice + invoice_line = self.invoice.line_ids.filtered( + lambda line: line.account_internal_type == "payable" + ) + refund_line = self.refund.line_ids.filtered( + lambda line: line.account_internal_type == "payable" + ) + (invoice_line | refund_line).reconcile() + self.env["account.invoice.payment.line.multi"].with_context( active_model="account.move", active_ids=self.invoice.ids ).create({}).run()