diff --git a/account_partner_required/__init__.py b/account_partner_required/__init__.py index a9e337226..0650744f6 100644 --- a/account_partner_required/__init__.py +++ b/account_partner_required/__init__.py @@ -1,2 +1 @@ - from . import models diff --git a/account_partner_required/models/__init__.py b/account_partner_required/models/__init__.py index 612263b74..2b77ae28f 100644 --- a/account_partner_required/models/__init__.py +++ b/account_partner_required/models/__init__.py @@ -1,2 +1 @@ - from . import account diff --git a/account_partner_required/models/account.py b/account_partner_required/models/account.py index 2eace4376..e98e1b0a7 100644 --- a/account_partner_required/models/account.py +++ b/account_partner_required/models/account.py @@ -4,37 +4,35 @@ # @author Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) -from odoo import models, fields, api, _ -from odoo.tools import float_is_zero +from odoo import _, api, fields, models from odoo.exceptions import ValidationError +from odoo.tools import float_is_zero class AccountAccountType(models.Model): _inherit = "account.account.type" partner_policy = fields.Selection( - selection=[ - ('optional', 'Optional'), - ('always', 'Always'), - ('never', 'Never')], - string='Policy for Partner Field', + selection=[("optional", "Optional"), ("always", "Always"), ("never", "Never")], + string="Policy for Partner Field", required=True, - default='optional', + default="optional", help="Set the policy for the partner field : if you select " - "'Optional', the accountant is free to put a partner " - "on an account move line with this type of account ; " - "if you select 'Always', the accountant will get an error " - "message if there is no partner ; if you select 'Never', " - "the accountant will get an error message if a partner " - "is present.") + "'Optional', the accountant is free to put a partner " + "on an account move line with this type of account ; " + "if you select 'Always', the accountant will get an error " + "message if there is no partner ; if you select 'Never', " + "the accountant will get an error message if a partner " + "is present.", + ) class AccountAccount(models.Model): - _inherit = 'account.account' + _inherit = "account.account" @api.multi def get_partner_policy(self): - """ Extension point to obtain partner policy for an account """ + """Extension point to obtain partner policy for an account""" self.ensure_one() return self.user_type_id.partner_policy @@ -46,27 +44,26 @@ class AccountMoveLine(models.Model): def _check_partner_required_msg(self): prec = self.env.user.company_id.currency_id.rounding for line in self: - if ( - float_is_zero(line.debit, precision_rounding=prec) and - float_is_zero(line.credit, precision_rounding=prec)): + if float_is_zero(line.debit, precision_rounding=prec) and float_is_zero( + line.credit, precision_rounding=prec + ): continue policy = line.account_id.get_partner_policy() - if policy == 'always' and not line.partner_id: + if policy == "always" and not line.partner_id: return _( "Partner policy is set to 'Always' with account '%s' but " "the partner is missing in the account move line with " - "label '%s'.") % (line.account_id.name_get()[0][1], - line.name) - elif policy == 'never' and line.partner_id: + "label '%s'." + ) % (line.account_id.name_get()[0][1], line.name) + elif policy == "never" and line.partner_id: return _( "Partner policy is set to 'Never' with account '%s' but " "the account move line with label '%s' has a partner " - "'%s'.") % (line.account_id.name_get()[0][1], - line.name, - line.partner_id.name) + "'%s'." + ) % (line.account_id.name_get()[0][1], line.name, line.partner_id.name) @api.multi - @api.constrains('partner_id', 'account_id', 'debit', 'credit') + @api.constrains("partner_id", "account_id", "debit", "credit") def _check_partner_required(self): for line in self: message = line._check_partner_required_msg() diff --git a/account_partner_required/tests/__init__.py b/account_partner_required/tests/__init__.py index 993220265..88f3a6dcf 100644 --- a/account_partner_required/tests/__init__.py +++ b/account_partner_required/tests/__init__.py @@ -1,2 +1 @@ - from . import test_account_partner_required diff --git a/account_partner_required/tests/test_account_partner_required.py b/account_partner_required/tests/test_account_partner_required.py index 0fdc8aa8f..904d49ee1 100644 --- a/account_partner_required/tests/test_account_partner_required.py +++ b/account_partner_required/tests/test_account_partner_required.py @@ -6,70 +6,88 @@ from datetime import datetime -from odoo.tests import common + from odoo.exceptions import ValidationError +from odoo.tests import common class TestAccountPartnerRequired(common.TransactionCase): - def setUp(self): super(TestAccountPartnerRequired, self).setUp() - self.account_obj = self.env['account.account'] - self.account_type_obj = self.env['account.account.type'] - self.move_obj = self.env['account.move'] - self.move_line_obj = self.env['account.move.line'] - self.sale_journal = self.env['account.journal'].create({ - 'type': 'sale', - 'code': 'SJXX', - 'name': 'Sale journal', - }) - liq_acc_type = self.env.ref('account.data_account_type_liquidity') - self.account1 = self.account_obj.create({ - 'code': '124242', - 'name': 'Test 1', - 'user_type_id': liq_acc_type.id, - }) - self.account_type_custom = self.account_type_obj.create({ - 'name': 'acc type test', - 'type': 'other', - 'partner_policy': 'optional', - }) - self.account2 = self.account_obj.create({ - 'code': '124243', - 'name': 'Test 2', - 'user_type_id': self.account_type_custom.id, - }) - self.account3 = self.account_obj.create({ - 'code': '124244', - 'name': 'Test 3', - 'user_type_id': self.account_type_custom.id, - }) + self.account_obj = self.env["account.account"] + self.account_type_obj = self.env["account.account.type"] + self.move_obj = self.env["account.move"] + self.move_line_obj = self.env["account.move.line"] + self.sale_journal = self.env["account.journal"].create( + { + "type": "sale", + "code": "SJXX", + "name": "Sale journal", + } + ) + liq_acc_type = self.env.ref("account.data_account_type_liquidity") + self.account1 = self.account_obj.create( + { + "code": "124242", + "name": "Test 1", + "user_type_id": liq_acc_type.id, + } + ) + self.account_type_custom = self.account_type_obj.create( + { + "name": "acc type test", + "type": "other", + "partner_policy": "optional", + } + ) + self.account2 = self.account_obj.create( + { + "code": "124243", + "name": "Test 2", + "user_type_id": self.account_type_custom.id, + } + ) + self.account3 = self.account_obj.create( + { + "code": "124244", + "name": "Test 3", + "user_type_id": self.account_type_custom.id, + } + ) def _create_move(self, with_partner, amount=100): date = datetime.now() if with_partner: - partner_id = self.env.ref('base.res_partner_1').id + partner_id = self.env.ref("base.res_partner_1").id else: partner_id = False move_vals = { - 'journal_id': self.sale_journal.id, - 'date': date, - 'line_ids': [ - (0, 0, { - 'name': '/', - 'debit': 0, - 'credit': amount, - 'account_id': self.account1.id, - }), - (0, 0, { - 'name': '/', - 'debit': amount, - 'credit': 0, - 'account_id': self.account2.id, - 'partner_id': partner_id, - }) - ], - } + "journal_id": self.sale_journal.id, + "date": date, + "line_ids": [ + ( + 0, + 0, + { + "name": "/", + "debit": 0, + "credit": amount, + "account_id": self.account1.id, + }, + ), + ( + 0, + 0, + { + "name": "/", + "debit": amount, + "credit": 0, + "account_id": self.account2.id, + "partner_id": partner_id, + }, + ), + ], + } move = self.move_obj.create(move_vals) move_line = False for line in move.line_ids: @@ -83,49 +101,51 @@ class TestAccountPartnerRequired(common.TransactionCase): self._create_move(with_partner=True) def test_always_no_partner(self): - self.account_type_custom.partner_policy = 'always' + self.account_type_custom.partner_policy = "always" with self.assertRaises(ValidationError): self._create_move(with_partner=False) def test_always_no_partner_0(self): # accept missing partner when debit=credit=0 - self.account_type_custom.partner_policy = 'always' + self.account_type_custom.partner_policy = "always" self._create_move(with_partner=False, amount=0) def test_always_with_partner(self): - self.account_type_custom.partner_policy = 'always' + self.account_type_custom.partner_policy = "always" self._create_move(with_partner=True) def test_never_no_partner(self): - self.account_type_custom.partner_policy = 'never' + self.account_type_custom.partner_policy = "never" self._create_move(with_partner=False) def test_never_with_partner(self): - self.account_type_custom.partner_policy = 'never' + self.account_type_custom.partner_policy = "never" with self.assertRaises(ValidationError): self._create_move(with_partner=True) def test_never_with_partner_0(self): - self.account_type_custom.partner_policy = 'never' + self.account_type_custom.partner_policy = "never" # accept partner when debit=credit=0 self._create_move(with_partner=True, amount=0) def test_always_remove_partner(self): # remove partner when policy is always - self.account_type_custom.partner_policy = 'always' + self.account_type_custom.partner_policy = "always" line = self._create_move(with_partner=True) with self.assertRaises(ValidationError): - line.write({'partner_id': False}) + line.write({"partner_id": False}) def test_change_account(self): - self.account_type_custom.partner_policy = 'optional' + self.account_type_custom.partner_policy = "optional" line = self._create_move(with_partner=False) # change account to an account with policy always but missing partner - self.account_type_custom.partner_policy = 'always' + self.account_type_custom.partner_policy = "always" with self.assertRaises(ValidationError): - line.write({'account_id': self.account3.id}) + line.write({"account_id": self.account3.id}) # change account to an account with policy always with partner - line.write({ - 'account_id': self.account3.id, - 'partner_id': self.env.ref('base.res_partner_1').id - }) + line.write( + { + "account_id": self.account3.id, + "partner_id": self.env.ref("base.res_partner_1").id, + } + ) diff --git a/account_partner_required/views/account.xml b/account_partner_required/views/account.xml index 3858fe87a..967a713d3 100644 --- a/account_partner_required/views/account.xml +++ b/account_partner_required/views/account.xml @@ -1,17 +1,16 @@ - + - account_partner_required.account_type_form account.account.type - + @@ -22,7 +21,7 @@ account_partner_required.account_type_tree account.account.type - +