2017-10-13 17:27:59 +02:00
|
|
|
# Copyright 2015 Pedro M. Baeza
|
|
|
|
# Copyright 2017 Tecnativa - Vicent Cubells
|
2022-10-11 13:54:03 +02:00
|
|
|
# Copyright 2022 Tecnativa - Víctor Martínez
|
2015-08-07 18:05:03 +02:00
|
|
|
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
2020-11-26 15:23:56 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
2019-03-19 23:12:50 +01:00
|
|
|
import odoo.tests.common as common
|
2023-02-22 23:53:29 +01:00
|
|
|
from odoo.exceptions import UserError
|
2022-10-11 13:54:03 +02:00
|
|
|
from odoo.tests import Form, tagged
|
2015-08-07 18:05:03 +02:00
|
|
|
|
|
|
|
|
2023-02-22 23:53:29 +01:00
|
|
|
@tagged("post_install", "-at_install")
|
2022-10-11 13:54:03 +02:00
|
|
|
class TestAccountNetting(common.TransactionCase):
|
2017-10-13 17:27:59 +02:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2022-10-11 13:54:03 +02:00
|
|
|
super().setUpClass()
|
2023-02-22 23:53:29 +01:00
|
|
|
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
|
2020-03-19 09:13:15 +01:00
|
|
|
res_users_account_manager = cls.env.ref("account.group_account_manager")
|
|
|
|
partner_manager = cls.env.ref("base.group_partner_manager")
|
|
|
|
cls.env.user.write(
|
2020-03-19 09:13:16 +01:00
|
|
|
{"groups_id": [(6, 0, [res_users_account_manager.id, partner_manager.id])]}
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2022-10-11 13:54:03 +02:00
|
|
|
cls.company = cls.env.ref("base.main_company")
|
2017-10-13 17:27:59 +02:00
|
|
|
# only adviser can create an account
|
2022-10-11 13:54:03 +02:00
|
|
|
cls.aa_model = cls.env["account.account"]
|
2023-02-22 23:53:29 +01:00
|
|
|
cls.account_receivable = cls._get_account(cls, "asset_receivable")
|
|
|
|
cls.account_payable = cls._get_account(cls, "liability_payable")
|
|
|
|
cls.account_revenue = cls._get_account(cls, "income")
|
|
|
|
cls.account_expense = cls._get_account(cls, "expense")
|
2022-10-11 13:54:03 +02:00
|
|
|
cls.partner_model = cls.env["res.partner"]
|
|
|
|
cls.partner1 = cls._create_partner(cls, "Supplier/Customer 1")
|
2023-02-22 23:53:29 +01:00
|
|
|
cls.partner2 = cls._create_partner(cls, "Supplier/Customer 2")
|
2022-10-11 13:54:03 +02:00
|
|
|
cls.miscellaneous_journal = cls.env["account.journal"].search(
|
2023-02-22 23:53:29 +01:00
|
|
|
[("type", "=", "general"), ("company_id", "=", cls.company.id)], limit=1
|
2022-10-11 13:54:03 +02:00
|
|
|
)
|
|
|
|
# We need a product with taxes at 0 so that the amounts are as expected.
|
|
|
|
cls.account_tax = cls.env["account.tax"].create(
|
2023-02-22 23:53:29 +01:00
|
|
|
{
|
|
|
|
"name": "0%",
|
|
|
|
"amount_type": "fixed",
|
|
|
|
"type_tax_use": "sale",
|
|
|
|
"amount": 0,
|
|
|
|
"company_id": cls.company.id,
|
|
|
|
}
|
2022-10-11 13:54:03 +02:00
|
|
|
)
|
|
|
|
cls.product = cls.env["product.product"].create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
2022-10-11 13:54:03 +02:00
|
|
|
"name": "Product Test",
|
|
|
|
"list_price": 10,
|
|
|
|
"taxes_id": [(6, 0, [cls.account_tax.id])],
|
2020-03-19 09:13:15 +01:00
|
|
|
}
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
out_invoice_partner1 = cls._create_move(cls, "out_invoice", cls.partner1, 100)
|
|
|
|
out_invoice_partner1.action_post()
|
|
|
|
cls.move_line_1 = out_invoice_partner1.line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == cls.account_receivable
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
in_invoice_partner1 = cls._create_move(cls, "in_invoice", cls.partner1, 1200)
|
|
|
|
in_invoice_partner1.action_post()
|
|
|
|
cls.move_line_2 = in_invoice_partner1.line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == cls.account_payable
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
cls.move_line_3 = in_invoice_partner1.line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == cls.account_expense
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
in_invoice_partner2 = cls._create_move(cls, "in_invoice", cls.partner2, 200)
|
|
|
|
in_invoice_partner2.action_post()
|
|
|
|
cls.move_line_4 = in_invoice_partner2.line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == cls.account_payable
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
in_refund_partner2 = cls._create_move(cls, "in_refund", cls.partner2, 200)
|
|
|
|
in_refund_partner2.action_post()
|
|
|
|
cls.move_line_5 = in_refund_partner2.line_ids.filtered(
|
2022-10-11 13:54:03 +02:00
|
|
|
lambda x: x.account_id == cls.account_payable
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
in_refund_partner2 = cls._create_move(cls, "in_refund", cls.partner2, 200)
|
|
|
|
in_refund_partner2.action_post()
|
|
|
|
cls.move_line_6 = in_refund_partner2.line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == cls.account_payable
|
|
|
|
)
|
2022-10-11 13:54:03 +02:00
|
|
|
|
2023-02-22 23:53:29 +01:00
|
|
|
def _get_account(self, account_type):
|
2022-10-11 13:54:03 +02:00
|
|
|
return self.aa_model.search(
|
|
|
|
[
|
2023-02-22 23:53:29 +01:00
|
|
|
("account_type", "=", account_type),
|
2022-10-11 13:54:03 +02:00
|
|
|
("company_id", "=", self.company.id),
|
|
|
|
],
|
|
|
|
limit=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _create_partner(self, name):
|
|
|
|
return self.partner_model.create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
2022-10-11 13:54:03 +02:00
|
|
|
"name": name,
|
|
|
|
"property_account_receivable_id": self.account_receivable.id,
|
|
|
|
"property_account_payable_id": self.account_payable.id,
|
2020-03-19 09:13:15 +01:00
|
|
|
}
|
|
|
|
)
|
2022-10-11 13:54:03 +02:00
|
|
|
|
|
|
|
def _create_move(self, move_type, partner, price):
|
|
|
|
move_form = Form(
|
2023-02-22 23:53:29 +01:00
|
|
|
self.env["account.move"]
|
|
|
|
.with_company(self.company.id)
|
|
|
|
.with_context(
|
2022-10-11 13:54:03 +02:00
|
|
|
default_move_type=move_type,
|
|
|
|
)
|
2020-03-19 09:13:15 +01:00
|
|
|
)
|
2022-10-11 13:54:03 +02:00
|
|
|
move_form.partner_id = partner
|
|
|
|
move_form.invoice_date = datetime.now()
|
|
|
|
with move_form.invoice_line_ids.new() as line_form:
|
|
|
|
line_form.product_id = self.product
|
|
|
|
line_form.price_unit = price
|
|
|
|
return move_form.save()
|
2015-08-07 18:05:03 +02:00
|
|
|
|
|
|
|
def test_compensation(self):
|
2023-02-22 23:53:29 +01:00
|
|
|
# Test raise if 1 account.move.line selected
|
2020-03-19 09:13:15 +01:00
|
|
|
obj = self.env["account.move.make.netting"].with_context(
|
|
|
|
active_ids=[self.move_line_1.id]
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
with self.assertRaises(UserError):
|
2018-01-25 14:11:48 +01:00
|
|
|
wizard = obj.create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
|
|
|
"move_line_ids": [(6, 0, [self.move_line_1.id])],
|
|
|
|
"journal_id": self.miscellaneous_journal.id,
|
|
|
|
}
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
# Test raise if not all accounts are payable/receivable
|
2020-03-19 09:13:15 +01:00
|
|
|
obj = self.env["account.move.make.netting"].with_context(
|
|
|
|
active_ids=[self.move_line_1.id, self.move_line_3.id]
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
with self.assertRaises(UserError):
|
2018-01-25 14:11:48 +01:00
|
|
|
wizard = obj.create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
|
|
|
"move_line_ids": [
|
|
|
|
(6, 0, [self.move_line_1.id, self.move_line_3.id])
|
|
|
|
],
|
|
|
|
"journal_id": self.miscellaneous_journal.id,
|
|
|
|
}
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
# Test raise if same account
|
2020-03-19 09:13:15 +01:00
|
|
|
obj = self.env["account.move.make.netting"].with_context(
|
|
|
|
active_ids=[self.move_line_4.id, self.move_line_5.id]
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
with self.assertRaises(UserError):
|
2018-01-25 14:11:48 +01:00
|
|
|
wizard = obj.create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
|
|
|
"move_line_ids": [
|
|
|
|
(6, 0, [self.move_line_4.id, self.move_line_5.id])
|
|
|
|
],
|
|
|
|
"journal_id": self.miscellaneous_journal.id,
|
|
|
|
}
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
# Test raise if reconciled lines
|
2020-03-19 09:13:15 +01:00
|
|
|
moves = self.env["account.move.line"].browse(
|
|
|
|
[self.move_line_4.id, self.move_line_5.id]
|
|
|
|
)
|
2018-01-25 14:11:48 +01:00
|
|
|
moves.reconcile()
|
2020-03-19 09:13:15 +01:00
|
|
|
obj = self.env["account.move.make.netting"].with_context(
|
|
|
|
active_ids=[self.move_line_4.id, self.move_line_5.id]
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
with self.assertRaises(UserError):
|
2018-01-25 14:11:48 +01:00
|
|
|
wizard = obj.create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
|
|
|
"move_line_ids": [
|
|
|
|
(6, 0, [self.move_line_4.id, self.move_line_5.id])
|
|
|
|
],
|
|
|
|
"journal_id": self.miscellaneous_journal.id,
|
|
|
|
}
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
# Test raise if different partners
|
2020-03-19 09:13:15 +01:00
|
|
|
obj = self.env["account.move.make.netting"].with_context(
|
|
|
|
active_ids=[self.move_line_1.id, self.move_line_6.id]
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
with self.assertRaises(UserError):
|
2018-01-25 14:11:48 +01:00
|
|
|
wizard = obj.create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
|
|
|
"move_line_ids": [
|
|
|
|
(6, 0, [self.move_line_1.id, self.move_line_6.id])
|
|
|
|
],
|
|
|
|
"journal_id": self.miscellaneous_journal.id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
obj = self.env["account.move.make.netting"].with_context(
|
|
|
|
active_ids=[self.move_line_1.id, self.move_line_2.id]
|
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
wizard = obj.create(
|
2020-03-19 09:13:15 +01:00
|
|
|
{
|
|
|
|
"move_line_ids": [(6, 0, [self.move_line_1.id, self.move_line_2.id])],
|
|
|
|
"journal_id": self.miscellaneous_journal.id,
|
|
|
|
}
|
|
|
|
)
|
2023-02-22 23:53:29 +01:00
|
|
|
self.assertEqual(wizard.partner_id, self.partner1)
|
|
|
|
self.assertEqual(wizard.company_id, self.company)
|
2015-08-07 18:05:03 +02:00
|
|
|
res = wizard.button_compensate()
|
2020-03-19 09:13:15 +01:00
|
|
|
move = self.env["account.move"].browse(res["res_id"])
|
2015-08-07 18:05:03 +02:00
|
|
|
self.assertEqual(
|
2020-03-19 09:13:15 +01:00
|
|
|
len(move.line_ids), 2, "AR/AP netting move has an incorrect line number"
|
|
|
|
)
|
2017-10-13 17:27:59 +02:00
|
|
|
move_line_receivable = move.line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == self.account_receivable
|
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
self.assertEqual(
|
2020-03-19 09:13:15 +01:00
|
|
|
move_line_receivable.credit,
|
|
|
|
100,
|
|
|
|
"Incorrect credit amount for receivable move line",
|
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
self.assertTrue(
|
2020-03-19 09:13:15 +01:00
|
|
|
move_line_receivable.reconciled and move_line_receivable.full_reconcile_id,
|
|
|
|
"Receivable move line should be totally reconciled",
|
|
|
|
)
|
2017-10-13 17:27:59 +02:00
|
|
|
move_line_payable = move.line_ids.filtered(
|
2020-03-19 09:13:15 +01:00
|
|
|
lambda x: x.account_id == self.account_payable
|
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
self.assertEqual(
|
2020-03-19 09:13:15 +01:00
|
|
|
move_line_payable.debit, 100, "Incorrect debit amount for payable move line"
|
|
|
|
)
|
2015-08-07 18:05:03 +02:00
|
|
|
self.assertTrue(
|
2020-03-19 09:13:15 +01:00
|
|
|
move_line_payable.reconciled and not move_line_payable.full_reconcile_id,
|
|
|
|
"Receivable move line should be partially reconciled",
|
|
|
|
)
|