diff --git a/account_payment_order/models/account_move_line.py b/account_payment_order/models/account_move_line.py index 656276abbb8..13ba897e74e 100644 --- a/account_payment_order/models/account_move_line.py +++ b/account_payment_order/models/account_move_line.py @@ -71,10 +71,10 @@ def _prepare_payment_line_vals(self, payment_order): # in this case if payment_order.payment_type == "outbound": amount_currency *= -1 - partner_bank_id = self.partner_bank_id.id or first(self.partner_id.bank_ids).id + partner_bank = self.partner_bank_id or first(self.partner_id.bank_ids) vals = { "order_id": payment_order.id, - "partner_bank_id": partner_bank_id, + "partner_bank_id": partner_bank.id, "partner_id": self.partner_id.id, "move_line_id": self.id, "communication": communication, diff --git a/account_payment_order/models/account_payment_line.py b/account_payment_order/models/account_payment_line.py index a5558824f25..683adc3ed48 100644 --- a/account_payment_order/models/account_payment_line.py +++ b/account_payment_order/models/account_payment_line.py @@ -3,6 +3,7 @@ from odoo import _, api, fields, models from odoo.exceptions import UserError +from odoo.fields import first class AccountPaymentLine(models.Model): @@ -249,3 +250,18 @@ def action_open_business_doc(self): if not self.move_line_id: return False return self.move_line_id.action_open_business_doc() + + 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 7b740b8e3c4..7ffb2db1119 100644 --- a/account_payment_order/models/account_payment_order.py +++ b/account_payment_order/models/account_payment_order.py @@ -8,6 +8,7 @@ from odoo import _, api, fields, models from odoo.exceptions import UserError, ValidationError +from odoo.tools import str2bool class AccountPaymentOrder(models.Model): @@ -338,6 +339,12 @@ def draft2open(self): for payline in order.payment_line_ids: try: payline.draft2open_payment_line_check() + if str2bool( + self.env["ir.config_parameter"] + .sudo() + .get_param("account_payment_order.use_allow_out_payment") + ): + payline._check_bank_allows_out_payments() except UserError as e: payline_err_text.append(e.args[0]) # Compute requested payment date diff --git a/account_payment_order/tests/test_payment_order_outbound.py b/account_payment_order/tests/test_payment_order_outbound.py index 462bc9cba68..8e5dde6fb1f 100644 --- a/account_payment_order/tests/test_payment_order_outbound.py +++ b/account_payment_order/tests/test_payment_order_outbound.py @@ -21,6 +21,10 @@ def setUpClass(cls, chart_template_ref=None): super().setUpClass(chart_template_ref=chart_template_ref) cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT)) cls.company = cls.company_data["company"] + + cls.env["ir.config_parameter"].sudo().set_param( + "account_payment_order.use_allow_out_payment", True + ) cls.env.user.company_id = cls.company.id cls.partner = cls.env["res.partner"].create( { @@ -29,9 +33,7 @@ def setUpClass(cls, chart_template_ref=None): ( 0, 0, - { - "acc_number": "TEST-NUMBER", - }, + {"acc_number": "TEST-NUMBER", "allow_out_payment": True}, ) ], } @@ -79,6 +81,13 @@ def setUpClass(cls, chart_template_ref=None): ("company_id", "=", cls.env.user.company_id.id), ] cls.env["account.payment.order"].search(cls.domain).unlink() + cls.partner_bank = cls.env["res.partner.bank"].create( + { + "acc_number": "1234", + "partner_id": cls.partner.id, + "allow_out_payment": True, + } + ) def _create_supplier_invoice(self, ref): invoice = self.env["account.move"].create( @@ -227,7 +236,7 @@ def order_creation(self, date_prefered): order.payment_line_ids.partner_bank_id.action_unarchive() self.assertFalse(order.partner_banks_archive_msg) order.draft2open() - self.assertEqual(order.payment_ids[0].partner_bank_id, self.partner.bank_ids) + self.assertEqual(order.payment_ids[0].partner_bank_id, self.partner.bank_ids[0]) order.open2generated() order.generated2uploaded() self.assertEqual(order.move_ids[0].date, order.payment_ids[0].date) @@ -240,6 +249,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) @@ -573,3 +583,47 @@ def test_action_open_business_document(self): self.assertEqual(invoice_action["res_id"], self.invoice.id) manual_line_action = order.payment_line_ids[1].action_open_business_doc() self.assertFalse(manual_line_action) + + def test_check_allow_out_payment(self): + """Check that, in case option "Send Money" is not enabled on + the bank, out payments are not allowed. + """ + # Open invoice + self.invoice.action_post() + + # 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 + + 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) + payment_order.write({"journal_id": self.bank_journal.id}) + # Add to payment order using the wizard: error raised + with self.assertRaises(UserError): + payment_order.draft2open() + + 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() diff --git a/account_payment_order/wizard/res_config_settings.py b/account_payment_order/wizard/res_config_settings.py index 8655efd8753..f18a05d71f7 100644 --- a/account_payment_order/wizard/res_config_settings.py +++ b/account_payment_order/wizard/res_config_settings.py @@ -10,3 +10,7 @@ class ResConfigSettings(models.TransientModel): transfer_journal_id = fields.Many2one( related="company_id.transfer_journal_id", readonly=False ) + use_allow_out_payment = fields.Boolean( + string=" Use allow out payment on partner bank", + config_parameter=("account_payment_order.use_allow_out_payment"), + ) diff --git a/account_payment_order/wizard/res_config_settings.xml b/account_payment_order/wizard/res_config_settings.xml index 99a43f62282..9853b5e84c3 100644 --- a/account_payment_order/wizard/res_config_settings.xml +++ b/account_payment_order/wizard/res_config_settings.xml @@ -23,6 +23,19 @@ +