diff --git a/mis_builder/models/aep.py b/mis_builder/models/aep.py index c28c0486d..d89240e77 100644 --- a/mis_builder/models/aep.py +++ b/mis_builder/models/aep.py @@ -56,6 +56,13 @@ def __init__(self, custom_field_names=()): custom_field: AccountingNone for custom_field in custom_field_names } + def has_data(self): + return ( + self.debit is not AccountingNone + or self.credit is not AccountingNone + or any(v is not AccountingNone for v in self.custom_fields.values()) + ) + def add_debit_credit(self, debit, credit): self.debit += debit self.credit += credit @@ -579,7 +586,7 @@ def f(mo): key = (ml_domain, mode) account_ids_data = self._data[key] for account_id in self._account_ids_by_acc_domain[acc_domain]: - if account_id in account_ids_data: + if account_ids_data[account_id].has_data(): account_ids.add(account_id) for account_id in account_ids: diff --git a/mis_builder/tests/test_aep.py b/mis_builder/tests/test_aep.py index 8802b4c4f..3e7424324 100644 --- a/mis_builder/tests/test_aep.py +++ b/mis_builder/tests/test_aep.py @@ -48,6 +48,14 @@ def setUp(self): "account_type": "income", } ) + self.account_in_no_data = self.account_model.create( + { + "company_id": self.company.id, + "code": "700INNODATA", + "name": "Income (no data)", + "account_type": "income", + } + ) # create journal self.journal = self.journal_model.create( { @@ -96,6 +104,7 @@ def setUp(self): self.aep.parse_expr("crdp[700I%]") self.aep.parse_expr("bali[400%]") self.aep.parse_expr("bale[700%]") + self.aep.parse_expr("balp[700I%]") self.aep.parse_expr("fldp.quantity[700%]") self.aep.parse_expr("balp[]" "[('account_id.code', '=', '400AR')]") self.aep.parse_expr( @@ -299,6 +308,17 @@ def test_aep_by_account(self): end = self._eval_by_account_id("bale[]") self.assertEqual(end, {self.account_ar.id: 900, self.account_in.id: -800}) + def test_aep_by_account_no_data(self): + """Test that accounts with no data are not returned.""" + self.aep.done_parsing() + self._do_queries( + datetime.date(self.curr_year, 3, 1), datetime.date(self.curr_year, 3, 31) + ) + variation = self._eval("balp[700I%]") + self.assertEqual(variation, -500) + variation_by_account = self._eval_by_account_id("balp[700I%]") + self.assertEqual(variation_by_account, {self.account_in.id: -500}) + def test_aep_convenience_methods(self): initial = AEP.get_balances_initial(self.company, time.strftime("%Y") + "-03-01") self.assertEqual( @@ -351,10 +371,13 @@ def test_get_account_ids_for_expr(self): self.assertEqual(account_ids, {self.account_in.id}) expr = "balp[700%]" account_ids = self.aep.get_account_ids_for_expr(expr) - self.assertEqual(account_ids, {self.account_in.id}) + self.assertEqual(account_ids, {self.account_in.id, self.account_in_no_data.id}) expr = "bali[400%], bale[700%]" # subkpis combined expression account_ids = self.aep.get_account_ids_for_expr(expr) - self.assertEqual(account_ids, {self.account_in.id, self.account_ar.id}) + self.assertEqual( + account_ids, + {self.account_in.id, self.account_ar.id, self.account_in_no_data.id}, + ) def test_get_aml_domain_for_expr(self): self.aep.done_parsing()