Skip to content

Commit

Permalink
[IMP][16.0] mis_builder: add possibility to dynamically hide a period…
Browse files Browse the repository at this point in the history
… depending on instance date
  • Loading branch information
AnizR committed Aug 28, 2024
1 parent 3aa42b4 commit aa976ee
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
22 changes: 21 additions & 1 deletion mis_builder/models/mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ def _compute_dates(self):
help="A domain to additionally filter move lines considered in this column.",
)

hide_period_based_on_instance_date = fields.Boolean(
help="Dynamically hide this period depending on the base date of the instance",
)

_order = "sequence, id"

_sql_constraints = [
Expand Down Expand Up @@ -843,6 +847,19 @@ def _add_column(self, aep, kpi_matrix, period, label, description):
elif period.source == SRC_CMPCOL:
return self._add_column_cmpcol(aep, kpi_matrix, period, label, description)

def _get_periods(self):
periods = self.env["mis.report.instance.period"]

for period in self.period_ids:
if (
period.hide_period_based_on_instance_date
and self.pivot_date < period.date_to
):
continue
periods += period

return periods

def _compute_matrix(self):
"""Compute a report and return a KpiMatrix.
Expand All @@ -852,7 +869,10 @@ def _compute_matrix(self):
self.ensure_one()
aep = self.report_id._prepare_aep(self.query_company_ids, self.currency_id)
kpi_matrix = self.report_id.prepare_kpi_matrix(self.multi_company)
for period in self.period_ids:

periods = self._get_periods()

for period in periods:
description = None
if period.mode == MODE_NONE:
pass
Expand Down
42 changes: 42 additions & 0 deletions mis_builder/tests/test_mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,48 @@ def test_mis_report_analytic_filters(self):
elif row.kpi.name == "k4":
self.assertEqual(vals, [AccountingNone, AccountingNone, 1.0])

def test_hide_period(self):
instance = self.report_instance
_, p2 = instance.period_ids
p2.hide_period_based_on_instance_date = True

self.assertTrue(p2.date_to <= instance.pivot_date)

matrix = instance.compute()
periods_header = matrix["header"][0]["cols"]
self.assertEqual(len(periods_header), 2)

# set date to exactly the end of period should keep period
instance.write({"date": "2014-12-31"})

self.assertTrue(p2.date_to <= instance.pivot_date)

matrix = instance.compute()
periods_header = matrix["header"][0]["cols"]
self.assertEqual(len(periods_header), 2)

# set date to a date in period should remove the period

instance.write({"date": "2014-12-30"})

self.assertFalse(p2.date_to <= instance.pivot_date)

matrix = instance.compute()
periods_header = matrix["header"][0]["cols"]
self.assertEqual(len(periods_header), 1)
self.assertEqual(periods_header[0]["label"], "p1")

# set date before period should also remove the period

instance.write({"date": "2013-12-30"})

self.assertFalse(p2.date_to <= instance.pivot_date)

matrix = instance.compute()
periods_header = matrix["header"][0]["cols"]
self.assertEqual(len(periods_header), 1)
self.assertEqual(periods_header[0]["label"], "p1")

def test_raise_when_unknown_kpi_value_type(self):
with self.assertRaises(SubKPIUnknownTypeError):
self.report_instance_2.compute()
Expand Down
1 change: 1 addition & 0 deletions mis_builder/views/mis_report_instance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@
options="{'model': 'source_aml_model_name'}"
attrs="{'invisible': [('source_aml_model_name', '=', False)]}"
/>
<field name="hide_period_based_on_instance_date" />
</group>
</form>
</field>
Expand Down

0 comments on commit aa976ee

Please sign in to comment.