From 741f4b4e1cada6d4463e982d1ea1ec15d1589acb Mon Sep 17 00:00:00 2001 From: Demesmaeker Date: Mon, 18 Dec 2023 09:15:29 +0000 Subject: [PATCH] [FIX] payment_custom: use correct payment reference When the payment reference is set as the customer reference, the warning message of the transaction still show the order reference instead. With this commit, we now ensure the expected reference is shown instead. opw-3461181 closes odoo/odoo#147143 X-original-commit: f25537dc83a6cb9193f78ab8f32c94a541fade0e Signed-off-by: Morgane Demesmaeker --- .../models/payment_transaction.py | 18 ++++++ addons/payment_custom/tests/__init__.py | 3 + .../tests/test_payment_transaction.py | 61 +++++++++++++++++++ .../views/payment_custom_templates.xml | 7 ++- 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 addons/payment_custom/tests/__init__.py create mode 100644 addons/payment_custom/tests/test_payment_transaction.py diff --git a/addons/payment_custom/models/payment_transaction.py b/addons/payment_custom/models/payment_transaction.py index dcb81ec4f90..9395bcd64d1 100644 --- a/addons/payment_custom/models/payment_transaction.py +++ b/addons/payment_custom/models/payment_transaction.py @@ -31,6 +31,24 @@ class PaymentTransaction(models.Model): 'reference': self.reference, } + def _get_communication(self): + """ Return the communication the user should use for their transaction. + + This communication might change according to the settings and the accounting localization. + + Note: self.ensure_one() + + :return: The selected communication. + :rtype: str + """ + self.ensure_one() + communication = "" + if hasattr(self, 'invoice_ids') and self.invoice_ids: + communication = self.invoice_ids[0].payment_reference + elif hasattr(self, 'sale_order_ids') and self.sale_order_ids: + communication = self.sale_order_ids[0].reference + return communication or self.reference + def _get_tx_from_notification_data(self, provider_code, notification_data): """ Override of payment to find the transaction based on custom data. diff --git a/addons/payment_custom/tests/__init__.py b/addons/payment_custom/tests/__init__.py new file mode 100644 index 00000000000..a7483b2cbcc --- /dev/null +++ b/addons/payment_custom/tests/__init__.py @@ -0,0 +1,3 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import test_payment_transaction diff --git a/addons/payment_custom/tests/test_payment_transaction.py b/addons/payment_custom/tests/test_payment_transaction.py new file mode 100644 index 00000000000..10e0a4844d3 --- /dev/null +++ b/addons/payment_custom/tests/test_payment_transaction.py @@ -0,0 +1,61 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import Command, fields +from odoo.tests import tagged + +from odoo.addons.payment.tests.common import PaymentCommon + + +@tagged('-at_install', 'post_install') +class TestPaymentTransaction(PaymentCommon): + + @classmethod + def setUpClass(cls): + super().setUpClass() + + cls.provider = cls._prepare_provider(code='custom') + cls.product = cls.env['product.product'].create({ + 'name': "test product", 'list_price': cls.amount + }) + + def test_communication_based_on_transaction_reference(self): + """ Test that the payment communication falls back to the transaction reference when there + is no linked invoice or sales order. """ + tx = self._create_transaction(flow='direct', reference="test") + + self.assertEqual(tx._get_communication(), "test") + + def test_communication_for_invoice(self): + """ Test that the communication displayed is the invoice payment reference. """ + account_payment_module = self.env['ir.module.module']._get('account_payment') + if account_payment_module.state != 'installed': + self.skipTest("account_payment module is not installed") + + invoice = self.env['account.move'].create({ + 'move_type': 'in_invoice', + 'partner_id': self.partner.id, + 'invoice_date': fields.Date.from_string('2019-01-01'), + 'currency_id': self.currency.id, + 'invoice_line_ids': [Command.create({'product_id': self.product.id, 'quantity': 1})], + }) + invoice.action_post() + tx = self._create_transaction(flow='direct', invoice_ids=[invoice.id]) + + invoice.payment_reference = "test" + self.assertEqual(tx._get_communication(), "test") + + def test_communication_for_sale_order(self): + """ Test that the communication displayed is the sale order reference. """ + sale_module = self.env['ir.module.module']._get('sale') + if sale_module.state != 'installed': + self.skipTest("sale module is not installed") + + sale_order = self.env['sale.order'].create({ + 'partner_id': self.partner.id, + 'order_line': [Command.create({'product_id': self.product.id, 'product_uom_qty': 1})], + }) + sale_order.action_confirm() + tx = self._create_transaction(flow='direct', sale_order_ids=[sale_order.id]) + + sale_order.reference = "test" + self.assertEqual(tx._get_communication(), "test") diff --git a/addons/payment_custom/views/payment_custom_templates.xml b/addons/payment_custom/views/payment_custom_templates.xml index d43759de4f8..3cd980fbc17 100644 --- a/addons/payment_custom/views/payment_custom_templates.xml +++ b/addons/payment_custom/views/payment_custom_templates.xml @@ -34,7 +34,7 @@
@@ -74,11 +74,12 @@
$0 - +
Communication: - +
+