From a02bcff92a8a26b4d99752e614d962283343bf81 Mon Sep 17 00:00:00 2001 From: Lindsay Date: Wed, 11 Sep 2024 09:06:00 +0200 Subject: [PATCH] [16.0][IMP] Take allow_out_payment into account on payment order A previous MR existed but only when payment order was triggered from invoices. We added the case also directement from confirm button on payment order original PR: https://github.com/OCA/bank-payment/pull/1177 --- .../tests/test_invoice_mandate.py | 1 + ...st_account_banking_mandate_sale_contact.py | 1 + .../tests/test_sdd.py | 2 ++ account_payment_order/models/account_move.py | 18 ++--------- .../models/account_move_line.py | 18 ++++++++++- .../models/account_payment_order.py | 2 ++ .../tests/test_payment_order_outbound.py | 30 +++++++++++++++++-- 7 files changed, 52 insertions(+), 20 deletions(-) diff --git a/account_banking_mandate/tests/test_invoice_mandate.py b/account_banking_mandate/tests/test_invoice_mandate.py index e29058f488e1..a4d82b39309c 100644 --- a/account_banking_mandate/tests/test_invoice_mandate.py +++ b/account_banking_mandate/tests/test_invoice_mandate.py @@ -215,6 +215,7 @@ def setUp(self): "partner_id": self.partner.id, "bank_id": self.acme_bank.id, "company_id": self.company.id, + "allow_out_payment": True, } ) diff --git a/account_banking_mandate_sale_contact/tests/test_account_banking_mandate_sale_contact.py b/account_banking_mandate_sale_contact/tests/test_account_banking_mandate_sale_contact.py index 048db6ea5851..3d7b4f002bad 100644 --- a/account_banking_mandate_sale_contact/tests/test_account_banking_mandate_sale_contact.py +++ b/account_banking_mandate_sale_contact/tests/test_account_banking_mandate_sale_contact.py @@ -82,6 +82,7 @@ def _create_res_partner_bank(cls, partner_id, acc_number): res_partner_bank_form = Form(cls.env["res.partner.bank"]) res_partner_bank_form.partner_id = partner_id res_partner_bank_form.acc_number = acc_number + res_partner_bank_form.allow_out_payment = True return res_partner_bank_form.save() @classmethod diff --git a/account_banking_sepa_direct_debit/tests/test_sdd.py b/account_banking_sepa_direct_debit/tests/test_sdd.py index aa2fdaeb3581..d7b49efe83c6 100644 --- a/account_banking_sepa_direct_debit/tests/test_sdd.py +++ b/account_banking_sepa_direct_debit/tests/test_sdd.py @@ -119,6 +119,7 @@ def setUpClass(cls): "acc_type": "iban", } ) + bank1.allow_out_payment = True cls.mandate12 = cls.env.ref( "account_banking_sepa_direct_debit.res_partner_12_mandate" ).copy( @@ -136,6 +137,7 @@ def setUpClass(cls): "acc_type": "iban", } ) + bank2.allow_out_payment = True cls.mandate2 = cls.env.ref( "account_banking_sepa_direct_debit.res_partner_2_mandate" ).copy( diff --git a/account_payment_order/models/account_move.py b/account_payment_order/models/account_move.py index 74d2054edcf4..31289201109e 100644 --- a/account_payment_order/models/account_move.py +++ b/account_payment_order/models/account_move.py @@ -5,7 +5,6 @@ from odoo import _, api, fields, models from odoo.exceptions import UserError -from odoo.fields import first class AccountMove(models.Model): @@ -154,22 +153,9 @@ def create_account_payment_line(self): ) # Check that the bank allows out payments - for line in applicable_lines.filtered( + applicable_lines.filtered( lambda l: l.account_id.account_type == "liability_payable" - ): - bank = line.partner_bank_id or first(line.partner_id.bank_ids) - if bank and not bank.allow_out_payment: - raise UserError( - _( - 'The option "Send Money" is not enabled on the bank ' - "account %(bank_account)s of partner %(partner)s." - ) - % { - "bank_account": bank.bank_name, - "partner": line.partner_id.name, - } - ) - + )._check_bank_allows_out_payments() for payment_mode in payment_modes: payorder = apoo.search( move.get_account_payment_domain(payment_mode), limit=1 diff --git a/account_payment_order/models/account_move_line.py b/account_payment_order/models/account_move_line.py index 656276abbb87..faf541228820 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -2,7 +2,8 @@ # © 2014 Serv. Tecnol. Avanzados - Pedro M. Baeza # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import UserError from odoo.fields import first @@ -91,3 +92,18 @@ def create_payment_line_from_move_line(self, payment_order): for mline in self: vals_list.append(mline._prepare_payment_line_vals(payment_order)) return self.env["account.payment.line"].create(vals_list) + + def _check_bank_allows_out_payments(self): + for line in self: + bank = line.partner_bank_id or first(line.partner_id.bank_ids) + if bank and not bank.allow_out_payment: + raise UserError( + _( + 'The option "Send Money" is not enabled on the bank ' + "account %(bank_account)s of partner %(partner)s." + ) + % { + "bank_account": bank.acc_number, + "partner": line.partner_id.name, + } + ) diff --git a/account_payment_order/models/account_payment_order.py b/account_payment_order/models/account_payment_order.py index cab62cdc3677..cac89f3f89d7 100644 --- a/account_payment_order/models/account_payment_order.py +++ b/account_payment_order/models/account_payment_order.py @@ -336,8 +336,10 @@ def draft2open(self): for payline in order.payment_line_ids: try: payline.draft2open_payment_line_check() + payline.move_line_id._check_bank_allows_out_payments() except UserError as e: payline_err_text.append(e.args[0]) + # Compute requested payment date if order.date_prefered == "due": requested_date = payline.ml_maturity_date or payline.date or today diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index 44007e0b32c7..1bf6db172cb0 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -28,9 +28,7 @@ def setUpClass(cls, chart_template_ref=None): ( 0, 0, - { - "acc_number": "TEST-NUMBER", - }, + {"acc_number": "TEST-NUMBER", "allow_out_payment": True}, ) ], } @@ -239,6 +237,7 @@ def _line_creation(self, outbound_order): "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, + "partner_bank_id": self.partner_bank.id, } return self.env["account.payment.line"].create(vals) @@ -533,9 +532,34 @@ def test_check_allow_out_payment(self): # Do not allow out payments self.partner_bank.allow_out_payment = False + for line in self.invoice.line_ids: + for bank in line.partner_id.bank_ids: + bank.allow_out_payment = False # Add to payment order using the wizard: error raised with self.assertRaises(UserError): self.env["account.invoice.payment.line.multi"].with_context( active_model="account.move", active_ids=self.invoice.ids ).create({}).run() + + def test_check_allow_out_payment_from_payment_order(self): + """Check that, in case option "Send Money" is not enabled on + the bank, out payments are not allowed. + """ + self.partner_bank.allow_out_payment = False + outbound_order = self.env["account.payment.order"].create( + { + "date_prefered": "due", + "payment_type": "outbound", + "payment_mode_id": self.mode.id, + "journal_id": self.bank_journal.id, + "description": "order with manual line", + } + ) + payment_line_1 = self._line_creation(outbound_order) + + payment_line_1.partner_bank_id = self.partner_bank.id + + # Add to payment order using the wizard: error raised + with self.assertRaises(UserError): + outbound_order.draft2open()