From e8280af6ed3a2dec0fbda2e9233c7050d401694b Mon Sep 17 00:00:00 2001 From: Julien Guenat Date: Fri, 8 Nov 2024 12:50:03 +0100 Subject: [PATCH 1/4] [FIX] mis_builder: branche and matrix value computation --- mis_builder/models/aep.py | 13 ++++++++++++- mis_builder/tests/test_aep.py | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mis_builder/models/aep.py b/mis_builder/models/aep.py index f3588bdf1..ed4a69688 100644 --- a/mis_builder/models/aep.py +++ b/mis_builder/models/aep.py @@ -364,7 +364,18 @@ def do_queries( ): # in initial mode, ignore accounts with 0 balance continue - self._data[key][acc["account_id"][0]] = (debit * rate, credit * rate) + # due to branches, it's possible to have multiple acc + # with the same account_id + if acc["account_id"][0] in self._data[key]: + existing_debit, existing_credit = self._data[key][ + acc["account_id"][0] + ] + else: + existing_debit, existing_credit = (0.0, 0.0) + self._data[key][acc["account_id"][0]] = ( + existing_debit + debit * rate, + existing_credit + credit * rate, + ) # compute ending balances by summing initial and variation for key in ends: domain, mode = key diff --git a/mis_builder/tests/test_aep.py b/mis_builder/tests/test_aep.py index 74f2ffebc..f0b355105 100644 --- a/mis_builder/tests/test_aep.py +++ b/mis_builder/tests/test_aep.py @@ -395,3 +395,28 @@ def test_invalid_field(self): datetime.date(self.prev_year, 12, 1), ) assert "Error while querying move line source" in str(cm.exception) + + def test_aep_branch(self): + # create branch + self.branch = self.res_company.create( + { + "name": "AEP Branch", + "parent_id": self.company.id, + } + ) + # create branch move in March this year + branch_move = self._create_move( + date=datetime.date(self.curr_year, 3, 1), + amount=50, + debit_acc=self.account_ar, + credit_acc=self.account_in, + ) + branch_move.company_id = self.branch + self.aep = AEP(self.company | self.branch) + self.aep.parse_expr("balp[]") + self.aep.done_parsing() + self._do_queries( + datetime.date(self.curr_year, 3, 1), datetime.date(self.curr_year, 3, 31) + ) + variation = self._eval_by_account_id("balp[]") + self.assertEqual(variation, {self.account_ar.id: 550, self.account_in.id: -550}) From ab46bbd8ef94ff5f67870bca9bd056cb98543df3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 11 Nov 2024 11:02:57 +0100 Subject: [PATCH 2/4] [IMP] simplify when same account used by multiple companies --- mis_builder/models/aep.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/mis_builder/models/aep.py b/mis_builder/models/aep.py index ed4a69688..e3780dbb9 100644 --- a/mis_builder/models/aep.py +++ b/mis_builder/models/aep.py @@ -12,6 +12,7 @@ from odoo.tools.safe_eval import datetime, dateutil, safe_eval, time from .accounting_none import AccountingNone +from .simple_array import SimpleArray _logger = logging.getLogger(__name__) @@ -316,7 +317,7 @@ def do_queries( aml_model = aml_model.with_context(active_test=False) company_rates = self._get_company_rates(date_to) # {(domain, mode): {account_id: (debit, credit)}} - self._data = defaultdict(dict) + self._data = defaultdict(lambda: defaultdict(lambda: SimpleArray((0.0, 0.0)))) domain_by_mode = {} ends = [] for key in self._map_account_ids: @@ -366,16 +367,7 @@ def do_queries( continue # due to branches, it's possible to have multiple acc # with the same account_id - if acc["account_id"][0] in self._data[key]: - existing_debit, existing_credit = self._data[key][ - acc["account_id"][0] - ] - else: - existing_debit, existing_credit = (0.0, 0.0) - self._data[key][acc["account_id"][0]] = ( - existing_debit + debit * rate, - existing_credit + credit * rate, - ) + self._data[key][acc["account_id"][0]] += (debit * rate, credit * rate) # compute ending balances by summing initial and variation for key in ends: domain, mode = key From 5fd5e69cb98a9110528d2eb22c1cdb9a36ebbf1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 11 Nov 2024 12:55:46 +0100 Subject: [PATCH 3/4] [IMP] add a few more tests And refine a defaultdict declaration in AEP. The previous version was not giving wrong results, but this one will be more robust. --- mis_builder/models/aep.py | 6 +++++- mis_builder/tests/test_aep.py | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mis_builder/models/aep.py b/mis_builder/models/aep.py index e3780dbb9..be00bec44 100644 --- a/mis_builder/models/aep.py +++ b/mis_builder/models/aep.py @@ -317,7 +317,11 @@ def do_queries( aml_model = aml_model.with_context(active_test=False) company_rates = self._get_company_rates(date_to) # {(domain, mode): {account_id: (debit, credit)}} - self._data = defaultdict(lambda: defaultdict(lambda: SimpleArray((0.0, 0.0)))) + self._data = defaultdict( + lambda: defaultdict( + lambda: SimpleArray((AccountingNone, AccountingNone)), + ) + ) domain_by_mode = {} ends = [] for key in self._map_account_ids: diff --git a/mis_builder/tests/test_aep.py b/mis_builder/tests/test_aep.py index f0b355105..411162864 100644 --- a/mis_builder/tests/test_aep.py +++ b/mis_builder/tests/test_aep.py @@ -83,6 +83,7 @@ def setUp(self): self.aep.parse_expr("bali[700IN]") self.aep.parse_expr("bale[700IN]") self.aep.parse_expr("balp[700IN]") + self.aep.parse_expr("balp[700NA]") # account that does not exist self.aep.parse_expr("bali[400AR]") self.aep.parse_expr("bale[400AR]") self.aep.parse_expr("balp[400AR]") @@ -193,6 +194,8 @@ def test_aep_basic(self): # check ending balance self.assertEqual(self._eval("bale[400AR]"), 400) self.assertEqual(self._eval("bale[700IN]"), -300) + # check result for non existing account + self.assertIs(self._eval("bale[700NA]"), AccountingNone) # let's query for March self._do_queries( @@ -227,9 +230,14 @@ def test_aep_basic(self): # unallocated p&l from previous year self.assertEqual(self._eval("balu[]"), -100) - # TODO allocate profits, and then... + # let's query for December where there is no data + self._do_queries( + datetime.date(self.curr_year, 12, 1), datetime.date(self.curr_year, 12, 31) + ) + self.assertIs(self._eval("balp[700IN]"), AccountingNone) + def test_aep_by_account(self): self.aep.done_parsing() self._do_queries( From d13f45a0ab8825267298af682119ea02c7d694fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 11 Nov 2024 13:08:54 +0100 Subject: [PATCH 4/4] [TST] remove test that is for 17.0+ --- mis_builder/tests/test_aep.py | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/mis_builder/tests/test_aep.py b/mis_builder/tests/test_aep.py index 411162864..ab762a515 100644 --- a/mis_builder/tests/test_aep.py +++ b/mis_builder/tests/test_aep.py @@ -403,28 +403,3 @@ def test_invalid_field(self): datetime.date(self.prev_year, 12, 1), ) assert "Error while querying move line source" in str(cm.exception) - - def test_aep_branch(self): - # create branch - self.branch = self.res_company.create( - { - "name": "AEP Branch", - "parent_id": self.company.id, - } - ) - # create branch move in March this year - branch_move = self._create_move( - date=datetime.date(self.curr_year, 3, 1), - amount=50, - debit_acc=self.account_ar, - credit_acc=self.account_in, - ) - branch_move.company_id = self.branch - self.aep = AEP(self.company | self.branch) - self.aep.parse_expr("balp[]") - self.aep.done_parsing() - self._do_queries( - datetime.date(self.curr_year, 3, 1), datetime.date(self.curr_year, 3, 31) - ) - variation = self._eval_by_account_id("balp[]") - self.assertEqual(variation, {self.account_ar.id: 550, self.account_in.id: -550})