2022-12-28 20:35:47 +01:00
|
|
|
# Copyright 2014-2022 Acsone (http://acsone.eu)
|
|
|
|
# Copyright 2016-2022 Akretion (http://www.akretion.com/)
|
2016-12-07 15:30:10 +01:00
|
|
|
# @author Stéphane Bidoul <stephane.bidoul@acsone.eu>
|
|
|
|
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
2014-04-18 14:53:38 +02:00
|
|
|
|
|
|
|
|
2016-12-07 15:30:10 +01:00
|
|
|
from odoo.exceptions import ValidationError
|
2022-12-28 20:35:47 +01:00
|
|
|
from odoo.tests import tagged
|
|
|
|
from odoo.tests.common import TransactionCase
|
2014-04-18 14:53:38 +02:00
|
|
|
|
|
|
|
|
2022-12-28 20:35:47 +01:00
|
|
|
@tagged("post_install", "-at_install")
|
|
|
|
class TestAccountPartnerRequired(TransactionCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
super().setUpClass()
|
|
|
|
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
|
|
|
|
cls.account_obj = cls.env["account.account"]
|
|
|
|
cls.move_obj = cls.env["account.move"]
|
|
|
|
cls.move_line_obj = cls.env["account.move.line"]
|
|
|
|
cls.company_id = cls.env.ref("base.main_company").id
|
|
|
|
cls.sale_journal = cls.env["account.journal"].create(
|
2022-12-28 18:16:55 +01:00
|
|
|
{
|
|
|
|
"type": "sale",
|
|
|
|
"code": "SJXX",
|
|
|
|
"name": "Sale journal",
|
2022-12-28 20:35:47 +01:00
|
|
|
"company_id": cls.company_id,
|
2022-12-28 18:16:55 +01:00
|
|
|
}
|
|
|
|
)
|
2022-12-28 20:35:47 +01:00
|
|
|
cls.account1 = cls.account_obj.create(
|
2022-12-28 18:16:55 +01:00
|
|
|
{
|
|
|
|
"code": "124242",
|
|
|
|
"name": "Test 1",
|
2022-12-28 20:35:47 +01:00
|
|
|
"account_type": "asset_cash",
|
|
|
|
"company_id": cls.company_id,
|
2022-12-28 18:16:55 +01:00
|
|
|
}
|
|
|
|
)
|
2022-12-28 20:35:47 +01:00
|
|
|
cls.account2 = cls.account_obj.create(
|
2022-12-28 18:16:55 +01:00
|
|
|
{
|
|
|
|
"code": "124243",
|
|
|
|
"name": "Test 2",
|
2022-12-28 20:35:47 +01:00
|
|
|
"account_type": "asset_receivable",
|
|
|
|
"company_id": cls.company_id,
|
2022-12-28 18:16:55 +01:00
|
|
|
}
|
|
|
|
)
|
2022-12-28 20:35:47 +01:00
|
|
|
cls.account3 = cls.account_obj.create(
|
2022-12-28 18:16:55 +01:00
|
|
|
{
|
|
|
|
"code": "124244",
|
|
|
|
"name": "Test 3",
|
2022-12-28 20:35:47 +01:00
|
|
|
"account_type": "liability_payable",
|
|
|
|
"company_id": cls.company_id,
|
2022-12-28 18:16:55 +01:00
|
|
|
}
|
|
|
|
)
|
2014-04-18 14:53:38 +02:00
|
|
|
|
|
|
|
def _create_move(self, with_partner, amount=100):
|
2016-12-07 15:30:10 +01:00
|
|
|
if with_partner:
|
2022-12-28 18:16:55 +01:00
|
|
|
partner_id = self.env.ref("base.res_partner_1").id
|
2016-12-07 15:30:10 +01:00
|
|
|
else:
|
|
|
|
partner_id = False
|
2014-04-18 14:53:38 +02:00
|
|
|
move_vals = {
|
2022-12-28 20:35:47 +01:00
|
|
|
"company_id": self.company_id,
|
2022-12-28 18:16:55 +01:00
|
|
|
"journal_id": self.sale_journal.id,
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
}
|
2016-12-07 15:30:10 +01:00
|
|
|
move = self.move_obj.create(move_vals)
|
|
|
|
move_line = False
|
|
|
|
for line in move.line_ids:
|
|
|
|
if line.account_id == self.account2:
|
|
|
|
move_line = line
|
|
|
|
break
|
|
|
|
return move_line
|
2014-04-18 14:53:38 +02:00
|
|
|
|
|
|
|
def test_optional(self):
|
|
|
|
self._create_move(with_partner=False)
|
|
|
|
self._create_move(with_partner=True)
|
|
|
|
|
|
|
|
def test_always_no_partner(self):
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "always"
|
2016-12-07 15:30:10 +01:00
|
|
|
with self.assertRaises(ValidationError):
|
2014-04-18 14:53:38 +02:00
|
|
|
self._create_move(with_partner=False)
|
|
|
|
|
|
|
|
def test_always_no_partner_0(self):
|
|
|
|
# accept missing partner when debit=credit=0
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "always"
|
2014-04-18 14:53:38 +02:00
|
|
|
self._create_move(with_partner=False, amount=0)
|
|
|
|
|
|
|
|
def test_always_with_partner(self):
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "always"
|
2014-04-18 14:53:38 +02:00
|
|
|
self._create_move(with_partner=True)
|
|
|
|
|
|
|
|
def test_never_no_partner(self):
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "never"
|
2014-04-18 14:53:38 +02:00
|
|
|
self._create_move(with_partner=False)
|
|
|
|
|
|
|
|
def test_never_with_partner(self):
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "never"
|
2016-12-07 15:30:10 +01:00
|
|
|
with self.assertRaises(ValidationError):
|
2014-04-18 14:53:38 +02:00
|
|
|
self._create_move(with_partner=True)
|
|
|
|
|
|
|
|
def test_never_with_partner_0(self):
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "never"
|
2014-04-18 14:53:38 +02:00
|
|
|
# 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
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "always"
|
2016-12-07 15:30:10 +01:00
|
|
|
line = self._create_move(with_partner=True)
|
|
|
|
with self.assertRaises(ValidationError):
|
2022-12-28 18:16:55 +01:00
|
|
|
line.write({"partner_id": False})
|
2014-04-18 14:53:38 +02:00
|
|
|
|
|
|
|
def test_change_account(self):
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account2.partner_policy = "optional"
|
2016-12-07 15:30:10 +01:00
|
|
|
line = self._create_move(with_partner=False)
|
|
|
|
# change account to an account with policy always but missing partner
|
2022-12-28 20:35:47 +01:00
|
|
|
self.account3.partner_policy = "always"
|
2016-12-07 15:30:10 +01:00
|
|
|
with self.assertRaises(ValidationError):
|
2022-12-28 18:16:55 +01:00
|
|
|
line.write({"account_id": self.account3.id})
|
2016-12-07 15:30:10 +01:00
|
|
|
# change account to an account with policy always with partner
|
2022-12-28 18:16:55 +01:00
|
|
|
line.write(
|
|
|
|
{
|
|
|
|
"account_id": self.account3.id,
|
|
|
|
"partner_id": self.env.ref("base.res_partner_1").id,
|
|
|
|
}
|
|
|
|
)
|