From 8a7c9c88d8efab7f419ad2f258083afedee6bc24 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Sun, 27 Dec 2020 00:40:33 +0100 Subject: [PATCH] account_payment_mode: use _check_company_auto = True on account.payment.mode --- .../models/account_payment_mode.py | 18 +++--------------- .../tests/test_account_payment_mode.py | 12 ++++++------ 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/account_payment_mode/models/account_payment_mode.py b/account_payment_mode/models/account_payment_mode.py index 903e18b1b..fee92cafd 100644 --- a/account_payment_mode/models/account_payment_mode.py +++ b/account_payment_mode/models/account_payment_mode.py @@ -14,6 +14,7 @@ class AccountPaymentMode(models.Model): _name = "account.payment.mode" _description = "Payment Modes" _order = "name" + _check_company_auto = True name = fields.Char(required=True, translate=True) company_id = fields.Many2one( @@ -41,6 +42,7 @@ class AccountPaymentMode(models.Model): string="Fixed Bank Journal", domain=[("type", "=", "bank")], ondelete="restrict", + check_company=True, ) # I need to explicitly define the table name # because I have 2 M2M fields pointing to account.journal @@ -121,20 +123,6 @@ class AccountPaymentMode(models.Model): ) ) - @api.constrains("company_id", "fixed_journal_id") - def company_id_fixed_journal_id_constrains(self): - for mode in self.filtered( - lambda x: x.fixed_journal_id - and x.company_id != x.fixed_journal_id.company_id - ): - raise ValidationError( - _( - "The company of the payment mode '%s', does not match " - "with the company of journal '%s'." - ) - % (mode.name, mode.fixed_journal_id.name) - ) - @api.constrains("company_id", "variable_journal_ids") def company_id_variable_journal_ids_constrains(self): for mode in self: @@ -142,7 +130,7 @@ class AccountPaymentMode(models.Model): raise ValidationError( _( "The company of the payment mode '%s', does not match " - "with the one of the Allowed Bank Journals." + "with one of the Allowed Bank Journals." ) % mode.name ) diff --git a/account_payment_mode/tests/test_account_payment_mode.py b/account_payment_mode/tests/test_account_payment_mode.py index 56fc3c839..48b561ea7 100644 --- a/account_payment_mode/tests/test_account_payment_mode.py +++ b/account_payment_mode/tests/test_account_payment_mode.py @@ -1,7 +1,7 @@ # Copyright 2016-2020 ForgeFlow S.L. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). -from odoo.exceptions import ValidationError +from odoo.exceptions import UserError, ValidationError from odoo.tests.common import TransactionCase @@ -48,9 +48,9 @@ class TestAccountPaymentMode(TransactionCase): def test_payment_mode_company_consistency_change(self): # Assertion on the constraints to ensure the consistency # for company dependent fields - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): self.payment_mode_c1.write({"fixed_journal_id": self.journal_c2.id}) - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): self.payment_mode_c1.write( { "variable_journal_ids": [ @@ -72,7 +72,7 @@ class TestAccountPaymentMode(TransactionCase): def test_payment_mode_company_consistency_create(self): # Assertion on the constraints to ensure the consistency # for company dependent fields - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): self.payment_mode_model.create( { "name": "Direct Debit of suppliers from Bank 2", @@ -83,7 +83,7 @@ class TestAccountPaymentMode(TransactionCase): } ) - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): self.payment_mode_model.create( { "name": "Direct Debit of suppliers from Bank 3", @@ -94,7 +94,7 @@ class TestAccountPaymentMode(TransactionCase): } ) - with self.assertRaises(ValidationError): + with self.assertRaises(UserError): self.payment_mode_model.create( { "name": "Direct Debit of suppliers from Bank 4",