diff --git a/account_payment_order/__manifest__.py b/account_payment_order/__manifest__.py index a68df93dc..a9c2cc112 100644 --- a/account_payment_order/__manifest__.py +++ b/account_payment_order/__manifest__.py @@ -27,6 +27,7 @@ "wizard/account_payment_line_create_view.xml", "wizard/account_invoice_payment_line_multi_view.xml", "views/account_payment_mode.xml", + "views/account_payment_views.xml", "views/account_payment_order.xml", "views/account_payment_line.xml", "views/account_move_line.xml", diff --git a/account_payment_order/models/account_move.py b/account_payment_order/models/account_move.py index d38410468..964410263 100644 --- a/account_payment_order/models/account_move.py +++ b/account_payment_order/models/account_move.py @@ -39,6 +39,31 @@ class AccountMove(models.Model): payment_mode = move.payment_mode_id move.payment_order_ok = payment_mode.payment_order_ok + def _get_payment_order_communication(self): + """ + Retrieve the communication string for the payment order + """ + communication = self.payment_reference or self.ref or self.name or "" + if self.is_invoice(): + if (self.reference_type or "none") != "none": + communication = self.ref + elif self.is_purchase_document(): + communication = self.ref or self.payment_reference + else: + communication = self.payment_reference or self.name + # If we have credit note(s) - reversal_move_id is a one2many + if self.reversal_move_id: + references = [] + references.extend( + [ + move._get_payment_order_communication() + for move in self.reversal_move_id + if move.payment_reference or move.ref + ] + ) + communication += " " + " ".join(references) + return communication + def _prepare_new_payment_order(self, payment_mode=None): self.ensure_one() if payment_mode is None: diff --git a/account_payment_order/models/account_move_line.py b/account_payment_order/models/account_move_line.py index d72acd389..64c6b8807 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -53,16 +53,7 @@ class AccountMoveLine(models.Model): # 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 + reference_moves |= self.move_id.reversal_move_id # Retrieve partial payments - e.g.: manual credit notes for ( _, @@ -73,7 +64,7 @@ class AccountMoveLine(models.Model): 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) + references.append(payment_move._get_payment_order_communication()) return references def _get_communication(self): @@ -83,22 +74,14 @@ class AccountMoveLine(models.Model): aplo = self.env["account.payment.line"] # default values for communication_type and communication communication_type = "normal" - communication = self.ref or self.name or "" + communication = self.move_id._get_payment_order_communication() # change these default values if move line is linked to an invoice if self.move_id.is_invoice(): if (self.move_id.reference_type or "none") != "none": - communication = self.move_id.ref ref2comm_type = aplo.invoice_reference_type2communication_type() communication_type = ref2comm_type[self.move_id.reference_type] else: - if ( - self.move_id.move_type in ("in_invoice", "in_refund") - and self.move_id.ref - ): - communication = self.move_id.payment_reference or self.move_id.ref - 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 + # Add references to communication from lines move references = self._get_linked_move_communication() if references: communication += " " + " ".join(references) diff --git a/account_payment_order/tests/test_payment_order_inbound.py b/account_payment_order/tests/test_payment_order_inbound.py index b6d730bc5..8710e5618 100644 --- a/account_payment_order/tests/test_payment_order_inbound.py +++ b/account_payment_order/tests/test_payment_order_inbound.py @@ -90,6 +90,19 @@ class TestPaymentOrderInbound(TestPaymentOrderInboundBase): with self.assertRaises(ValidationError): self.inbound_order.date_scheduled = date.today() - timedelta(days=1) + def test_invoice_communication_01(self): + self.assertEqual( + self.invoice.name, self.invoice._get_payment_order_communication() + ) + self.invoice.ref = "R1234" + self.assertEqual( + self.invoice.name, self.invoice._get_payment_order_communication() + ) + + def test_invoice_communication_02(self): + self.invoice.payment_reference = "R1234" + self.assertEqual("R1234", self.invoice._get_payment_order_communication()) + def test_creation(self): payment_order = self.inbound_order self.assertEqual(len(payment_order.ids), 1) diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index 8c1fad44f..bd5be8885 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -251,6 +251,15 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): with self.assertRaises(ValidationError): outbound_order.date_scheduled = date.today() - timedelta(days=2) + def test_invoice_communication_01(self): + self.assertEqual("F1242", self.invoice._get_payment_order_communication()) + self.invoice.ref = "F1243" + self.assertEqual("F1243", self.invoice._get_payment_order_communication()) + + def test_invoice_communication_02(self): + self.invoice.payment_reference = "R1234" + self.assertEqual("F1242", self.invoice._get_payment_order_communication()) + def test_manual_line_and_manual_date(self): # Create payment order outbound_order = self.env["account.payment.order"].create( @@ -319,7 +328,9 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): """ # Open both invoices self.invoice.action_post() + self.assertEqual("F1242", self.invoice._get_payment_order_communication()) self.invoice_02.action_post() + self.assertEqual("F1243", self.invoice_02._get_payment_order_communication()) # Add to payment order using the wizard self.env["account.invoice.payment.line.multi"].with_context( @@ -371,6 +382,7 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): and the credit note one """ self.invoice.action_post() + self.assertEqual("F1242", self.invoice._get_payment_order_communication()) self.refund = self._create_supplier_refund(self.invoice) with Form(self.refund) as refund_form: refund_form.ref = "R1234" @@ -378,6 +390,7 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): line_form.price_unit = 75.0 self.refund.action_post() + self.assertEqual("R1234", self.refund._get_payment_order_communication()) self.env["account.invoice.payment.line.multi"].with_context( active_model="account.move", active_ids=self.invoice.ids @@ -404,13 +417,16 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): """ self.invoice.payment_reference = "F/1234" self.invoice.action_post() + self.assertEqual("F1242", self.invoice._get_payment_order_communication()) self.refund = self._create_supplier_refund(self.invoice) with Form(self.refund) as refund_form: refund_form.ref = "R1234" + refund_form.payment_reference = "FR/1234" with refund_form.invoice_line_ids.edit(0) as line_form: line_form.price_unit = 75.0 self.refund.action_post() + self.assertEqual("R1234", self.refund._get_payment_order_communication()) # The user add the outstanding payment to the invoice invoice_line = self.invoice.line_ids.filtered( @@ -432,7 +448,8 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): self.assertEqual(len(payment_order.payment_line_ids), 1) - self.assertEqual("F/1234 R1234", payment_order.payment_line_ids.communication) + self.assertEqual("F1242 R1234", payment_order.payment_line_ids.communication) + self.assertNotIn("FR/1234", payment_order.payment_line_ids.communication) def test_supplier_manual_refund(self): """ @@ -445,11 +462,13 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): and the credit note one """ self.invoice.action_post() + self.assertEqual("F1242", self.invoice._get_payment_order_communication()) 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.assertEqual("R1234", self.refund._get_payment_order_communication()) (self.invoice.line_ids + self.refund.line_ids).filtered( lambda line: line.account_internal_type == "payable" diff --git a/account_payment_order/views/account_payment_order.xml b/account_payment_order/views/account_payment_order.xml index 40a163632..8f3cfa3f1 100644 --- a/account_payment_order/views/account_payment_order.xml +++ b/account_payment_order/views/account_payment_order.xml @@ -126,7 +126,12 @@ string="Payment Transactions" attrs="{'invisible': [('state', 'in', ('draft', 'cancel'))]}" > - + diff --git a/account_payment_order/views/account_payment_views.xml b/account_payment_order/views/account_payment_views.xml new file mode 100644 index 000000000..8dd2d67c1 --- /dev/null +++ b/account_payment_order/views/account_payment_views.xml @@ -0,0 +1,27 @@ + + + + account.payment.tree + account.payment + primary + + + + + + + + + account.payment.form + account.payment + + + + + + + +