2
0

account_partner_required: black, isort, etc.

This commit is contained in:
Alexis de Lattre 2022-12-28 18:16:55 +01:00
parent 72d85397a5
commit b100c45dd4
6 changed files with 113 additions and 100 deletions

View File

@ -1,2 +1 @@
from . import models from . import models

View File

@ -1,2 +1 @@
from . import account from . import account

View File

@ -4,33 +4,31 @@
# @author Alexis de Lattre <alexis.delattre@akretion.com> # @author Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo import models, fields, api, _ from odoo import _, api, fields, models
from odoo.tools import float_is_zero
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo.tools import float_is_zero
class AccountAccountType(models.Model): class AccountAccountType(models.Model):
_inherit = "account.account.type" _inherit = "account.account.type"
partner_policy = fields.Selection( partner_policy = fields.Selection(
selection=[ selection=[("optional", "Optional"), ("always", "Always"), ("never", "Never")],
('optional', 'Optional'), string="Policy for Partner Field",
('always', 'Always'),
('never', 'Never')],
string='Policy for Partner Field',
required=True, required=True,
default='optional', default="optional",
help="Set the policy for the partner field : if you select " help="Set the policy for the partner field : if you select "
"'Optional', the accountant is free to put a partner " "'Optional', the accountant is free to put a partner "
"on an account move line with this type of account ; " "on an account move line with this type of account ; "
"if you select 'Always', the accountant will get an error " "if you select 'Always', the accountant will get an error "
"message if there is no partner ; if you select 'Never', " "message if there is no partner ; if you select 'Never', "
"the accountant will get an error message if a partner " "the accountant will get an error message if a partner "
"is present.") "is present.",
)
class AccountAccount(models.Model): class AccountAccount(models.Model):
_inherit = 'account.account' _inherit = "account.account"
@api.multi @api.multi
def get_partner_policy(self): def get_partner_policy(self):
@ -46,27 +44,26 @@ class AccountMoveLine(models.Model):
def _check_partner_required_msg(self): def _check_partner_required_msg(self):
prec = self.env.user.company_id.currency_id.rounding prec = self.env.user.company_id.currency_id.rounding
for line in self: for line in self:
if ( if float_is_zero(line.debit, precision_rounding=prec) and float_is_zero(
float_is_zero(line.debit, precision_rounding=prec) and line.credit, precision_rounding=prec
float_is_zero(line.credit, precision_rounding=prec)): ):
continue continue
policy = line.account_id.get_partner_policy() policy = line.account_id.get_partner_policy()
if policy == 'always' and not line.partner_id: if policy == "always" and not line.partner_id:
return _( return _(
"Partner policy is set to 'Always' with account '%s' but " "Partner policy is set to 'Always' with account '%s' but "
"the partner is missing in the account move line with " "the partner is missing in the account move line with "
"label '%s'.") % (line.account_id.name_get()[0][1], "label '%s'."
line.name) ) % (line.account_id.name_get()[0][1], line.name)
elif policy == 'never' and line.partner_id: elif policy == "never" and line.partner_id:
return _( return _(
"Partner policy is set to 'Never' with account '%s' but " "Partner policy is set to 'Never' with account '%s' but "
"the account move line with label '%s' has a partner " "the account move line with label '%s' has a partner "
"'%s'.") % (line.account_id.name_get()[0][1], "'%s'."
line.name, ) % (line.account_id.name_get()[0][1], line.name, line.partner_id.name)
line.partner_id.name)
@api.multi @api.multi
@api.constrains('partner_id', 'account_id', 'debit', 'credit') @api.constrains("partner_id", "account_id", "debit", "credit")
def _check_partner_required(self): def _check_partner_required(self):
for line in self: for line in self:
message = line._check_partner_required_msg() message = line._check_partner_required_msg()

View File

@ -1,2 +1 @@
from . import test_account_partner_required from . import test_account_partner_required

View File

@ -6,68 +6,86 @@
from datetime import datetime from datetime import datetime
from odoo.tests import common
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo.tests import common
class TestAccountPartnerRequired(common.TransactionCase): class TestAccountPartnerRequired(common.TransactionCase):
def setUp(self): def setUp(self):
super(TestAccountPartnerRequired, self).setUp() super(TestAccountPartnerRequired, self).setUp()
self.account_obj = self.env['account.account'] self.account_obj = self.env["account.account"]
self.account_type_obj = self.env['account.account.type'] self.account_type_obj = self.env["account.account.type"]
self.move_obj = self.env['account.move'] self.move_obj = self.env["account.move"]
self.move_line_obj = self.env['account.move.line'] self.move_line_obj = self.env["account.move.line"]
self.sale_journal = self.env['account.journal'].create({ self.sale_journal = self.env["account.journal"].create(
'type': 'sale', {
'code': 'SJXX', "type": "sale",
'name': 'Sale journal', "code": "SJXX",
}) "name": "Sale journal",
liq_acc_type = self.env.ref('account.data_account_type_liquidity') }
self.account1 = self.account_obj.create({ )
'code': '124242', liq_acc_type = self.env.ref("account.data_account_type_liquidity")
'name': 'Test 1', self.account1 = self.account_obj.create(
'user_type_id': liq_acc_type.id, {
}) "code": "124242",
self.account_type_custom = self.account_type_obj.create({ "name": "Test 1",
'name': 'acc type test', "user_type_id": liq_acc_type.id,
'type': 'other', }
'partner_policy': 'optional', )
}) self.account_type_custom = self.account_type_obj.create(
self.account2 = self.account_obj.create({ {
'code': '124243', "name": "acc type test",
'name': 'Test 2', "type": "other",
'user_type_id': self.account_type_custom.id, "partner_policy": "optional",
}) }
self.account3 = self.account_obj.create({ )
'code': '124244', self.account2 = self.account_obj.create(
'name': 'Test 3', {
'user_type_id': self.account_type_custom.id, "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): def _create_move(self, with_partner, amount=100):
date = datetime.now() date = datetime.now()
if with_partner: if with_partner:
partner_id = self.env.ref('base.res_partner_1').id partner_id = self.env.ref("base.res_partner_1").id
else: else:
partner_id = False partner_id = False
move_vals = { move_vals = {
'journal_id': self.sale_journal.id, "journal_id": self.sale_journal.id,
'date': date, "date": date,
'line_ids': [ "line_ids": [
(0, 0, { (
'name': '/', 0,
'debit': 0, 0,
'credit': amount, {
'account_id': self.account1.id, "name": "/",
}), "debit": 0,
(0, 0, { "credit": amount,
'name': '/', "account_id": self.account1.id,
'debit': amount, },
'credit': 0, ),
'account_id': self.account2.id, (
'partner_id': partner_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 = self.move_obj.create(move_vals)
@ -83,49 +101,51 @@ class TestAccountPartnerRequired(common.TransactionCase):
self._create_move(with_partner=True) self._create_move(with_partner=True)
def test_always_no_partner(self): def test_always_no_partner(self):
self.account_type_custom.partner_policy = 'always' self.account_type_custom.partner_policy = "always"
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self._create_move(with_partner=False) self._create_move(with_partner=False)
def test_always_no_partner_0(self): def test_always_no_partner_0(self):
# accept missing partner when debit=credit=0 # 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) self._create_move(with_partner=False, amount=0)
def test_always_with_partner(self): 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) self._create_move(with_partner=True)
def test_never_no_partner(self): 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) self._create_move(with_partner=False)
def test_never_with_partner(self): def test_never_with_partner(self):
self.account_type_custom.partner_policy = 'never' self.account_type_custom.partner_policy = "never"
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
self._create_move(with_partner=True) self._create_move(with_partner=True)
def test_never_with_partner_0(self): 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 # accept partner when debit=credit=0
self._create_move(with_partner=True, amount=0) self._create_move(with_partner=True, amount=0)
def test_always_remove_partner(self): def test_always_remove_partner(self):
# remove partner when policy is always # 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) line = self._create_move(with_partner=True)
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
line.write({'partner_id': False}) line.write({"partner_id": False})
def test_change_account(self): 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) line = self._create_move(with_partner=False)
# change account to an account with policy always but missing partner # 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): 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 # change account to an account with policy always with partner
line.write({ line.write(
'account_id': self.account3.id, {
'partner_id': self.env.ref('base.res_partner_1').id "account_id": self.account3.id,
}) "partner_id": self.env.ref("base.res_partner_1").id,
}
)

View File

@ -4,7 +4,6 @@
@author Stéphane Bidoul <stephane.bidoul@acsone.eu> @author Stéphane Bidoul <stephane.bidoul@acsone.eu>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
--> -->
<odoo> <odoo>
<record id="view_account_type_form" model="ir.ui.view"> <record id="view_account_type_form" model="ir.ui.view">