From bff6d3493612daa5d7635f9ee399d562ed4a4562 Mon Sep 17 00:00:00 2001 From: Alejandro Ji Cheung Date: Wed, 27 Sep 2023 17:10:17 +0200 Subject: [PATCH] [IMP] account_payment_order: adds restriction for duplicated move lines in payment order --- .../models/account_payment_order.py | 14 ++++++++++++ .../tests/test_payment_order_inbound.py | 22 +++++++++++++++++++ .../tests/test_payment_order_outbound.py | 17 ++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/account_payment_order/models/account_payment_order.py b/account_payment_order/models/account_payment_order.py index eec8ddbf9..cab62cdc3 100644 --- a/account_payment_order/models/account_payment_order.py +++ b/account_payment_order/models/account_payment_order.py @@ -221,6 +221,20 @@ class AccountPaymentOrder(models.Model): ) ) + @api.constrains("payment_line_ids") + def _check_payment_lines(self): + for order in self: + move_line_ids = [x.move_line_id.id for x in order.payment_line_ids] + if len(move_line_ids) != len(set(move_line_ids)): + raise ValidationError( + _( + "There are several lines pointing to the same pending " + "balance. This is probably caused by a manual line creation. " + "Please remove this duplication for being able to save the " + "order." + ) + ) + @api.depends("payment_line_ids", "payment_line_ids.amount_company_currency") def _compute_total(self): for rec in self: diff --git a/account_payment_order/tests/test_payment_order_inbound.py b/account_payment_order/tests/test_payment_order_inbound.py index fef633ebd..775ac8cb4 100644 --- a/account_payment_order/tests/test_payment_order_inbound.py +++ b/account_payment_order/tests/test_payment_order_inbound.py @@ -83,6 +83,16 @@ class TestPaymentOrderInboundBase(AccountTestInvoicingCommon): @tagged("-at_install", "post_install") class TestPaymentOrderInbound(TestPaymentOrderInboundBase): + def _line_creation(self, inbound_order): + vals = { + "order_id": inbound_order.id, + "partner_id": self.partner.id, + "currency_id": inbound_order.payment_mode_id.company_id.currency_id.id, + "amount_currency": 200.38, + "move_line_id": self.invoice.invoice_line_ids[0].id, + } + return self.env["account.payment.line"].create(vals) + def test_constrains_type(self): with self.assertRaises(ValidationError): order = self.env["account.payment.order"].create( @@ -136,3 +146,15 @@ class TestPaymentOrderInbound(TestPaymentOrderInboundBase): payment_order.cancel2draft() payment_order.unlink() self.assertEqual(len(self.payment_order_obj.search(self.domain)), 0) + + def test_constrains_payment_line(self): + inbound_order = self.env["account.payment.order"].create( + {"payment_mode_id": self.inbound_mode.id, "journal_id": self.journal.id} + ) + # Create two payment lines with the same + # move line id and try to assign them to inbound order + payment_line_1 = self._line_creation(inbound_order) + inbound_order.payment_line_ids |= payment_line_1 + with self.assertRaises(ValidationError): + payment_line_2 = self._line_creation(inbound_order) + inbound_order.payment_line_ids |= payment_line_2 diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index 03be242b1..f922d612f 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -225,6 +225,16 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): self.assertEqual(order.move_ids[0].date, order.payment_ids[0].date) self.assertEqual(order.state, "uploaded") + def _line_creation(self, outbound_order): + vals = { + "order_id": outbound_order.id, + "partner_id": self.partner.id, + "currency_id": outbound_order.payment_mode_id.company_id.currency_id.id, + "amount_currency": 200.38, + "move_line_id": self.invoice.invoice_line_ids[0].id, + } + return self.env["account.payment.line"].create(vals) + def test_account_payment_line_creation_without_payment_mode(self): self.invoice.payment_mode_id = False self.invoice.action_post() @@ -276,6 +286,13 @@ class TestPaymentOrderOutbound(TestPaymentOrderOutboundBase): ) with self.assertRaises(ValidationError): outbound_order.date_scheduled = date.today() - timedelta(days=2) + # Create two payment lines with the same + # move line id and try to assign them to outbound order + payment_line_1 = self._line_creation(outbound_order) + outbound_order.payment_line_ids |= payment_line_1 + with self.assertRaises(ValidationError): + payment_line_2 = self._line_creation(outbound_order) + outbound_order.payment_line_ids |= payment_line_2 def test_invoice_communication_01(self): self.assertEqual(