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

View File

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

View File

@ -4,37 +4,35 @@
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# 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()

View File

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

View File

@ -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,
}
)

View File

@ -1,17 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--
© 2014-2016 Acsone (http://acsone.eu)
@author Stéphane Bidoul <stephane.bidoul@acsone.eu>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
-->
<odoo>
<record id="view_account_type_form" model="ir.ui.view">
<field name="name">account_partner_required.account_type_form</field>
<field name="model">account.account.type</field>
<field name="inherit_id" ref="account.view_account_type_form" />
<field name="arch" type="xml">
<field name="arch" type="xml">
<field name="type" position="after">
<field name="partner_policy" />
</field>
@ -22,7 +21,7 @@
<field name="name">account_partner_required.account_type_tree</field>
<field name="model">account.account.type</field>
<field name="inherit_id" ref="account.view_account_type_tree" />
<field name="arch" type="xml">
<field name="arch" type="xml">
<field name="type" position="after">
<field name="partner_policy" />
</field>