diff --git a/l10n_it_riba/models/account.py b/l10n_it_riba/models/account.py index 3371e05a30f1..be73ea8b5fc5 100644 --- a/l10n_it_riba/models/account.py +++ b/l10n_it_riba/models/account.py @@ -148,6 +148,32 @@ def month_check(self, invoice_date_due, all_date_due): return True return False + def _post(self, soft=True): + inv_riba_no_bank = self.filtered( + lambda x: x.is_riba_payment + and x.move_type == "out_invoice" + and not x.riba_partner_bank_id + ) + if inv_riba_no_bank: + inv_details = ( + _( + 'Invoice %(name)s for customer "%(customer_name)s", ' + "total %(amount)s", + name=inv.display_name, + customer_name=inv.partner_id.display_name, + amount=inv.amount_total, + ) + for inv in inv_riba_no_bank + ) + raise UserError( + _( + "Cannot post invoices with C/O payments without bank. " + "Please check the following invoices:\n\n- " + + "\n- ".join(inv_details) + ) + ) + return super()._post(soft=soft) + def action_post(self): for invoice in self: # ---- Add a line with collection fees for each due date only for first due diff --git a/l10n_it_riba/tests/test_riba.py b/l10n_it_riba/tests/test_riba.py index 4ea00796e7b0..d700f1d31615 100644 --- a/l10n_it_riba/tests/test_riba.py +++ b/l10n_it_riba/tests/test_riba.py @@ -732,3 +732,35 @@ def test_file_substitute_forbidden_chars(self): file_content = base64.decodebytes(export_wizard.riba_txt).decode() self.assertNotIn("Via di là", file_content) self.assertIn("Via di la", file_content) + + def test_riba_inv_no_bank(self): + """ + Test that a riba invoice without a bank defined + cannot be confirmed (e.g. via the list view) + """ + self.invoice.company_id.due_cost_service_id = self.service_due_cost.id + self.invoice.riba_partner_bank_id = False + with self.assertRaises(UserError) as err: + self.invoice.action_post() + err_msg = err.exception.args[0] + self.assertIn("Cannot post invoices", err_msg) + self.assertIn(self.invoice.partner_id.display_name, err_msg) + # We have to add back a taxed collection fee for each payment term line + # because they have been added during `action_post` + # and recorded in the exception message, + # but `assertRaises` rolls them back + collection_fees = self.invoice.invoice_payment_term_id.riba_payment_cost + collection_fees_tax = self.invoice.fiscal_position_id.map_tax( + self.service_due_cost.taxes_id + ) + taxed_collection_fees = collection_fees_tax.compute_all(collection_fees)[ + "total_included" + ] + self.assertIn( + str( + self.invoice.amount_total + + len(self.invoice.invoice_payment_term_id.line_ids) + * taxed_collection_fees + ), + err_msg, + )