diff --git a/mis_builder/models/mis_report.py b/mis_builder/models/mis_report.py
index b2a6996ec..6f2e7ee0a 100644
--- a/mis_builder/models/mis_report.py
+++ b/mis_builder/models/mis_report.py
@@ -399,6 +399,13 @@ def _compute_field_names(self):
report_id = fields.Many2one(
comodel_name="mis.report", required=True, ondelete="cascade"
)
+ company_field_id = fields.Many2one(
+ comodel_name="ir.model.fields",
+ ondelete="set null",
+ domain="[('model_id', '=', model_id)]",
+ help="Field that defines company on related model."
+ "When set, it will be automatically be added in search domain of query.",
+ )
_order = "name"
diff --git a/mis_builder/models/mis_report_instance.py b/mis_builder/models/mis_report_instance.py
index d5b096242..ed0049f85 100644
--- a/mis_builder/models/mis_report_instance.py
+++ b/mis_builder/models/mis_report_instance.py
@@ -10,6 +10,7 @@
from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError
+from odoo.osv.expression import AND
from .aep import AccountingExpressionProcessor as AEP
from .expression_evaluator import ExpressionEvaluator
@@ -406,7 +407,12 @@ def _get_additional_query_filter(self, query):
Returns an Odoo domain expression (a python list)
compatible with the model of the query."""
self.ensure_one()
- return []
+ domain = []
+ if (company_field := query.sudo().company_field_id) and (
+ instance_companies := self.report_instance_id.query_company_ids
+ ):
+ domain = AND([domain, [(company_field.name, "in", instance_companies.ids)]])
+ return domain
@api.constrains("mode", "source")
def _check_mode_source(self):
diff --git a/mis_builder/tests/test_mis_report_instance.py b/mis_builder/tests/test_mis_report_instance.py
index 42937d38d..4c14ad6bc 100644
--- a/mis_builder/tests/test_mis_report_instance.py
+++ b/mis_builder/tests/test_mis_report_instance.py
@@ -633,3 +633,22 @@ def test_unprivileged(self):
self.env, "mis_you", groups="base.group_user,account.group_account_readonly"
)
self.report_instance.with_user(test_user).compute()
+
+ def test_query_company(self):
+ c1 = self.report_instance.company_id
+
+ query = self.report.query_ids
+ self.assertEqual(len(query), 1)
+
+ period = self.report_instance.period_ids[0]
+ domain = period.with_company(c1)._get_additional_query_filter(query)
+
+ self.assertEqual(domain, [])
+
+ query.company_field_id = self.env["ir.model.fields"].search(
+ [("name", "=", "company_id"), ("model", "=", "res.partner")]
+ )
+
+ domain = period.with_company(c1)._get_additional_query_filter(query)
+
+ self.assertEqual(domain, [("company_id", "in", c1.ids)])
diff --git a/mis_builder/views/mis_report.xml b/mis_builder/views/mis_report.xml
index 9ce492d4c..aa230b98a 100644
--- a/mis_builder/views/mis_report.xml
+++ b/mis_builder/views/mis_report.xml
@@ -69,6 +69,7 @@
name="date_field"
domain="[('model_id', '=', model_id), ('ttype', 'in', ('date', 'datetime'))]"
/>
+