diff --git a/mis_builder/models/mis_report_instance.py b/mis_builder/models/mis_report_instance.py index 9acf6fe63..178196e2f 100644 --- a/mis_builder/models/mis_report_instance.py +++ b/mis_builder/models/mis_report_instance.py @@ -423,8 +423,7 @@ def _check_mode_source(self): if rec.mode == MODE_NONE: raise DateFilterRequired( self.env._( - "A date filter is mandatory for this source " - "in column %s.", + "A date filter is mandatory for this source in column %s.", rec.name, ) ) @@ -432,8 +431,7 @@ def _check_mode_source(self): if rec.mode != MODE_NONE: raise DateFilterForbidden( self.env._( - "No date filter is allowed for this source " - "in column %s.", + "No date filter is allowed for this source in column %s.", rec.name, ) ) @@ -460,8 +458,7 @@ def _check_source_cmpcol(self): ): raise ValidationError( self.env._( - "Columns to compare must belong to the same report " - "in %s", + "Columns to compare must belong to the same report in %s", rec.name, ) ) @@ -499,7 +496,7 @@ def _compute_pivot_date(self): sequence = fields.Integer(default=10) description = fields.Char(related="report_id.description") date = fields.Date( - string="Base date", help="Report base date " "(leave empty to use current date)" + string="Base date", help="Report base date (leave empty to use current date)" ) pivot_date = fields.Date(compute="_compute_pivot_date") report_id = fields.Many2one("mis.report", required=True, string="Report") @@ -765,9 +762,7 @@ def get_views(self, views, options=None): context.get("from_dashboard") and context.get("active_model") == "mis.report.instance" ): - view_id = self.env.ref( - "mis_builder." "mis_report_instance_result_view_form" - ) + view_id = self.env.ref("mis_builder.mis_report_instance_result_view_form") mis_report_form_view = view_id and [view_id.id, "form"] for view in views: if view and view[1] == "form": @@ -778,7 +773,7 @@ def get_views(self, views, options=None): def preview(self): self.ensure_one() - view_id = self.env.ref("mis_builder." "mis_report_instance_result_view_form") + view_id = self.env.ref("mis_builder.mis_report_instance_result_view_form") return { "type": "ir.actions.act_window", "res_model": "mis.report.instance", @@ -883,7 +878,11 @@ def _compute_matrix(self): self.ensure_one() aep = self.report_id._prepare_aep(self.query_company_ids, self.currency_id) multi_company = self.multi_company and len(self.query_company_ids) > 1 - kpi_matrix = self.report_id.prepare_kpi_matrix(multi_company) + # Use the report instance's company context so that company-dependent + # fields (e.g. account code in Odoo 18) are read correctly when + # the user's current company differs from the report's company. + report = self.report_id.with_company(self.query_company_ids[0]) + kpi_matrix = report.prepare_kpi_matrix(multi_company) for period in self.period_ids: description = None if period.mode == MODE_NONE: diff --git a/mis_builder/tests/test_mis_report_instance.py b/mis_builder/tests/test_mis_report_instance.py index ba8d83aed..8a074d4d2 100644 --- a/mis_builder/tests/test_mis_report_instance.py +++ b/mis_builder/tests/test_mis_report_instance.py @@ -490,6 +490,75 @@ def test_drilldown_views(self): [[False, "list"], [False, "form"], [False, "pivot"], [False, "graph"]], ) + def test_account_code_multicompany_context(self): + """Account codes should display correctly when the report instance + belongs to a different company than the user's current company.""" + # Create a second company + company2 = self.env["res.company"].create({"name": "Test Co 2"}) + # Create an account in company2 with a code + account = ( + self.env["account.account"] + .with_company(company2) + .create( + { + "name": "Test Account", + "code": "999001", + "account_type": "expense", + "company_ids": [(6, 0, [company2.id])], + } + ) + ) + # Verify the code is set for company2 + acct_c2 = account.with_company(company2) + self.assertEqual(acct_c2.code, "999001") + # Verify the code is NOT visible from main company + acct_c1 = account.with_company(self.env.ref("base.main_company")) + self.assertFalse(acct_c1.code) + # Create a simple report + instance for company2 + report = self.env["mis.report"].create({"name": "test mc report"}) + self.env["mis.report.kpi"].create( + { + "report_id": report.id, + "name": "exp", + "description": "Expenses", + "auto_expand_accounts": True, + "sequence": 1, + "expression_ids": [ + (0, 0, {"name": "balp[999%]"}), + ], + } + ) + instance = self.env["mis.report.instance"].create( + { + "name": "test mc instance", + "report_id": report.id, + "company_id": company2.id, + "period_ids": [ + ( + 0, + 0, + { + "name": "p1", + "mode": "fix", + "manual_date_from": "2024-01-01", + "manual_date_to": "2024-12-31", + }, + ), + ], + } + ) + # Compute the matrix — the user's current company is main_company, + # but the report belongs to company2. Account codes should still + # display correctly. + matrix = instance.compute() + body = matrix.get("body", []) + # Find any row with "999001" or "False" in the label + has_false = any("False" in (r.get("label") or "") for r in body) + self.assertFalse( + has_false, + "Account codes should not show as 'False' in multi-company reports", + ) + def test_qweb(self): self.report_instance.print_pdf() # get action test_reports.try_report(