diff --git a/mis_builder/models/mis_report_instance.py b/mis_builder/models/mis_report_instance.py index d5b096242..87e8c6f61 100644 --- a/mis_builder/models/mis_report_instance.py +++ b/mis_builder/models/mis_report_instance.py @@ -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 = [ @@ -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. @@ -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 diff --git a/mis_builder/tests/test_mis_report_instance.py b/mis_builder/tests/test_mis_report_instance.py index 42937d38d..d477fbebe 100644 --- a/mis_builder/tests/test_mis_report_instance.py +++ b/mis_builder/tests/test_mis_report_instance.py @@ -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() diff --git a/mis_builder/views/mis_report_instance.xml b/mis_builder/views/mis_report_instance.xml index f13fce50e..20ac73b61 100644 --- a/mis_builder/views/mis_report_instance.xml +++ b/mis_builder/views/mis_report_instance.xml @@ -414,6 +414,7 @@ options="{'model': 'source_aml_model_name'}" attrs="{'invisible': [('source_aml_model_name', '=', False)]}" /> +