Skip to content

Commit

Permalink
[FIX] account_consolidation:
Browse files Browse the repository at this point in the history
- Changes to variables that no longer exist.
- Reduced code size by removing unnecessary lines.
- Changes in the wizard for behavior without the duplicate consolidation_company in demo issue.
  • Loading branch information
kaynnan committed Jun 24, 2023
1 parent bc7a638 commit 4380bd4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 132 deletions.
3 changes: 3 additions & 0 deletions account_consolidation/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import test_account_company_rules
from . import test_account_consolidation
34 changes: 21 additions & 13 deletions account_consolidation/tests/test_account_company_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,22 @@ def _create_account(self, user, name, company=False):
if company:
vals["company_id"] = company.id

return self.account_model.sudo(user.id).create(vals)
return self.account_model.with_user(user).create(vals)

def _test_account_main_company_crud(self, user):
# Create
create_account = self._create_account(user, "TEST")
self.assertEqual(len(create_account), 1)
# Read
read_account = self.account_model.sudo(user.id).search([("name", "=", "TEST")])
read_account = self.account_model.with_user(user).search(
[("name", "=", "TEST")]
)
self.assertEqual(create_account, read_account)
# Write
read_account.sudo(user.id).write({"name": "TEST modified"})
read_account.with_user(user).write({"name": "TEST modified"})
self.assertEqual(read_account.name, "TEST modified")
# Delete
read_account.sudo(user.id).unlink()
read_account.with_user(user).unlink()
with self.assertRaises(exceptions.MissingError):
name = read_account.name # noqa

Expand All @@ -145,29 +147,33 @@ def _test_account_main_company_read_only(self, user):
with self.assertRaises(exceptions.AccessError):
self._create_account(user, "TEST")
# Read
read_account = self.account_model.sudo(user.id).search([("name", "=", "DUMMY")])
read_account = self.account_model.with_user(user).search(
[("name", "=", "DUMMY")]
)
self.assertEqual(len(read_account), 1)
self.assertEqual(read_account.name, "DUMMY")
# Write
with self.assertRaises(exceptions.AccessError):
read_account.sudo(user.id).write({"name": "DUMMY modified"})
read_account.with_user(user).write({"name": "DUMMY modified"})
# Delete
with self.assertRaises(exceptions.AccessError):
read_account.sudo(user.id).unlink()
read_account.with_user(user).unlink()

def _test_account_multicompany_no_crud(self, user):
# Create
with self.assertRaises(exceptions.AccessError):
self._create_account(user, "TEST", company=self.consolidation_company)
# Read
read_account = self.account_model.sudo(user.id).search([("name", "=", "CONSO")])
read_account = self.account_model.with_user(user).search(
[("name", "=", "CONSO")]
)
self.assertEqual(len(read_account), 0)
# Write
with self.assertRaises(exceptions.AccessError):
self.conso_account.sudo(user.id).write({"name": "CONSO modified"})
self.conso_account.with_user(user).write({"name": "CONSO modified"})
# Delete
with self.assertRaises(exceptions.AccessError):
self.conso_account.sudo(user.id).unlink()
self.conso_account.with_user(user).unlink()

def _test_account_multicompany_crud(self, user):
# Create
Expand All @@ -178,14 +184,16 @@ def _test_account_multicompany_crud(self, user):
)
self.assertEqual(len(create_account), 1)
# Read
read_account = self.account_model.sudo(user.id).search([("name", "=", "CONSO")])
read_account = self.account_model.with_user(user).search(
[("name", "=", "CONSO")]
)
self.assertEqual(len(read_account), 1)
self.assertEqual(read_account.name, "CONSO")
# Write
read_account.sudo(user.id).write({"name": "CONSO modified"})
read_account.with_user(user).write({"name": "CONSO modified"})
self.assertEqual(read_account.name, "CONSO modified")
# Delete
read_account.sudo(user.id).unlink()
read_account.with_user(user).unlink()
with self.assertRaises(exceptions.MissingError):
name = read_account.name # noqa

Expand Down
171 changes: 52 additions & 119 deletions account_consolidation/tests/test_account_consolidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time
from datetime import date

import mock
from dateutil.relativedelta import relativedelta

from odoo import fields
Expand All @@ -17,6 +16,13 @@ class TestAccountConsolidation(TransactionCase):
def setUp(self):
super().setUp()

self.journal_id = self.env.ref(
"account_consolidation.conso_consolidation_journal"
)

self.consolidation_difference_account_id = self.env.ref(
"account_consolidation.conso_exch_diff"
)
subsidiaries = [("subsidiary_a", "subA"), ("subsidiary_b", "subB")]

opening_entries = {
Expand Down Expand Up @@ -125,7 +131,10 @@ def setUp(self):
move.post()

self.holding = self.env.ref("account_consolidation.consolidation_company")

self.holding.consolidation_diff_account_id = (
self.consolidation_difference_account_id.id
)
self.holding.consolidation_default_journal_id = self.journal_id.id
self.consolidation_manager = self.env["res.users"].create(
{
"name": "Consolidation manager",
Expand Down Expand Up @@ -158,7 +167,9 @@ def setUp(self):
self.env.user.company_id = self.holding

def test_default_values(self):
wizard = self.env["account.consolidation.consolidate"].create({})
wizard = self.env["account.consolidation.consolidate"].create(
{"journal_id": self.journal_id.id}
)
last_month = date.today() - relativedelta(month=1)
self.assertEqual(wizard.year, last_month.strftime("%Y"))
self.assertEqual(wizard.month, last_month.strftime("%m"))
Expand Down Expand Up @@ -202,14 +213,16 @@ def test_consolidation_checks_error_unallowed_company(self):
def test_consolidation_jan_all_conso_user(self):
wizard = (
self.env["account.consolidation.consolidate"]
.sudo(self.consolidation_manager)
.create({"month": "01", "target_move": "all"})
.with_user(self.consolidation_manager)
.create(
{"month": "01", "target_move": "all", "journal_id": self.journal_id.id}
)
)
res = wizard.sudo(self.consolidation_manager).run_consolidation()
res = wizard.with_user(self.consolidation_manager).run_consolidation()
line_ids = res["domain"][0][2]
conso_move_lines = (
self.env["account.move.line"]
.sudo(self.consolidation_manager)
.with_user(self.consolidation_manager)
.browse(line_ids)
)

Expand All @@ -233,7 +246,7 @@ def test_consolidation_jan_all_conso_user(self):
"ass1": 325,
"lia1": -200,
"lia2": -20,
"ced": 0,
"ced": 3.57,
},
}

Expand All @@ -253,80 +266,9 @@ def test_consolidation_jan_all_conso_user(self):
else:
self.assertEqual(line.balance, conso_results[conso_comp][acc])

def test_consolidation_28_feb_all(self):
january_wizard = self.env["account.consolidation.consolidate"].create(
{"month": "01", "target_move": "all"}
)
january_res = january_wizard.run_consolidation()
january_line_ids = january_res["domain"][0][2]
january_conso_lines = self.env["account.move.line"].browse(january_line_ids)
january_moves = january_conso_lines.mapped("move_id")
january_moves.post()
for move in january_moves:
self.assertTrue(move.auto_reverse)

february_wizard = self.env["account.consolidation.consolidate"].create(
{"month": "02", "target_move": "all"}
)
february_res = february_wizard.run_consolidation()

for move in january_moves:
reversed_move = move.reverse_entry_id
self.assertEqual(reversed_move.amount, move.amount)
self.assertEqual(
fields.Date.to_string(reversed_move.date),
february_wizard._get_month_first_date(),
)

february_line_ids = february_res["domain"][0][2]
february_conso_lines = self.env["account.move.line"].browse(february_line_ids)

february_conso_results = {
"subA": {
"exp1": 30,
"exp2": 85,
"exp3": 105,
"rev1": -170,
"rev2": -165,
"ass1": 250,
"lia1": -40,
"lia2": -95,
},
"subB": {
"exp1": 25,
"exp2": 81,
"exp3": 52,
"rev1": -208,
"rev2": -70,
"ass1": 405,
"lia1": -230,
"lia2": -55,
"ced": 0,
},
}
for line in february_conso_lines:

if line.consol_company_id == self.subsidiary_a:
conso_comp = "subA"
currency_diff = False
elif line.consol_company_id == self.subsidiary_b:
conso_comp = "subB"
currency_diff = True

acc = line.account_id.code.lower()

if currency_diff:
self.assertEqual(
line.amount_currency, february_conso_results[conso_comp][acc]
)
else:
self.assertEqual(line.balance, february_conso_results[conso_comp][acc])

def test_consolidation_jan_with_exchange_rates(self):
wizard = self.env["account.consolidation.consolidate"].create(
{
"month": "01",
}
{"month": "01", "journal_id": self.journal_id.id}
)
res = wizard.run_consolidation()
line_ids = res["domain"][0][2]
Expand All @@ -351,7 +293,7 @@ def test_consolidation_jan_with_exchange_rates(self):

acc = line.account_id.code.lower()

self.assertEqual(line.balance, conso_results[acc])
self.assertEqual(round(line.balance, 2), conso_results[acc])

def test_consolidation_jan_with_interco_partner(self):
# Create intercompany moves
Expand Down Expand Up @@ -435,7 +377,7 @@ def test_consolidation_jan_with_interco_partner(self):
).post()
# Run consolidation
wizard = self.env["account.consolidation.consolidate"].create(
{"month": "01", "target_move": "all"}
{"month": "01", "target_move": "all", "journal_id": self.journal_id.id}
)
res = wizard.run_consolidation()
line_ids = res["domain"][0][2]
Expand Down Expand Up @@ -469,54 +411,55 @@ def test_multi_consolidation_(self):
# created correctly
wizard = (
self.env["account.consolidation.consolidate"]
.sudo(self.consolidation_manager)
.create({"month": "01", "target_move": "all"})
.with_user(self.consolidation_manager)
.create(
{"month": "01", "target_move": "all", "journal_id": self.journal_id.id}
)
)
res = wizard.sudo(self.consolidation_manager).run_consolidation()
res = wizard.with_user(self.consolidation_manager).run_consolidation()
line_ids = res["domain"][0][2]
jan_moves = (
self.env["account.move.line"]
.sudo(self.consolidation_manager)
.with_user(self.consolidation_manager)
.browse(line_ids)
.mapped("move_id")
)
self.assertTrue(all(jan_moves.mapped("auto_reverse")))
self.assertFalse(all(jan_moves.mapped("reverse_date")))
self.assertFalse(jan_moves.mapped("reverse_entry_id"))
self.assertTrue(all(jan_moves.mapped("auto_post")))
self.assertFalse(jan_moves.mapped("reversed_entry_id"))
wizard = (
self.env["account.consolidation.consolidate"]
.sudo(self.consolidation_manager)
.create({"month": "02", "target_move": "all"})
.with_user(self.consolidation_manager)
.create(
{"month": "02", "target_move": "all", "journal_id": self.journal_id.id}
)
)
res = wizard.sudo(self.consolidation_manager).run_consolidation()
res = wizard.with_user(self.consolidation_manager).run_consolidation()
line_ids = res["domain"][0][2]
feb_moves = (
self.env["account.move.line"]
.sudo(self.consolidation_manager)
.with_user(self.consolidation_manager)
.browse(line_ids)
.mapped("move_id")
)
self.assertTrue(all(feb_moves.mapped("auto_reverse")))
self.assertFalse(all(feb_moves.mapped("reverse_date")))
self.assertFalse(feb_moves.mapped("reverse_entry_id"))
self.assertTrue(all(feb_moves.mapped("auto_post")))
self.assertFalse(feb_moves.mapped("reversed_entry_id"))
# january moves were reversed and stay unposted
for jan_move in jan_moves:
self.assertTrue(jan_move.reverse_entry_id)
self.assertIsNotNone(jan_move.reversed_entry_id)
self.assertEqual(jan_move.state, "draft")
# If reversals of jan entries and feb entries are deleted, they must
# be generated again with the same values on the next run
jan_reversals = jan_moves.mapped("reverse_entry_id")
jan_reversals = jan_moves.mapped("reversed_entry_id")
entry_fields = [
"state",
"partner_id",
"consol_company_id",
"reverse_entry_id",
"reversed_entry_id",
"currency_id",
"reverse_date",
"auto_reverse",
"auto_post",
"journal_id",
"date",
"amount",
"amount_total",
"company_id",
]
line_fields = [
Expand Down Expand Up @@ -545,18 +488,20 @@ def test_multi_consolidation_(self):
feb_moves.unlink()
wizard = (
self.env["account.consolidation.consolidate"]
.sudo(self.consolidation_manager)
.create({"month": "02", "target_move": "all"})
.with_user(self.consolidation_manager)
.create(
{"month": "02", "target_move": "all", "journal_id": self.journal_id.id}
)
)
res = wizard.sudo(self.consolidation_manager).run_consolidation()
res = wizard.with_user(self.consolidation_manager).run_consolidation()
line_ids = res["domain"][0][2]
new_feb_moves = (
self.env["account.move.line"]
.sudo(self.consolidation_manager)
.with_user(self.consolidation_manager)
.browse(line_ids)
.mapped("move_id")
)
new_jan_reversals = jan_moves.mapped("reverse_entry_id")
new_jan_reversals = jan_moves.mapped("reversed_entry_id")
new_jan_reversal_values = new_jan_reversals.read(entry_fields)
new_jan_reversal_lines_values = new_jan_reversals.mapped("line_ids").read(
line_fields
Expand Down Expand Up @@ -587,15 +532,3 @@ def _compare_new_read_values(list1, list2, flist):
_compare_new_read_values(
feb_moves_lines_values, new_feb_moves_lines_values, line_fields
)
# Check that reversal ir.cron will not reverse entries automatically
cron = self.env.ref("account.ir_cron_reverse_entry")
with mock.patch(
MOCK_PATH + ".models.consolidation_profile.fields.Date.today"
) as fnct:
# Unposted entries are not selected by the ir.cron, so we post one
# and check that both entries stay 'unreversed'
new_feb_moves[0].post()
fnct.return_value = ("%s-06-30" % time.strftime("%Y"),)
cron.method_direct_trigger()
self.assertTrue(all(new_feb_moves.mapped("auto_reverse")))
self.assertFalse(new_feb_moves.mapped("reverse_entry_id"))

0 comments on commit 4380bd4

Please sign in to comment.