Skip to content

Commit

Permalink
[IMP] mis_builder: use company of report.instance when querying
Browse files Browse the repository at this point in the history
  • Loading branch information
AnizR committed Oct 22, 2024
1 parent 3aa42b4 commit 4d3dca5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions mis_builder/models/mis_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
8 changes: 7 additions & 1 deletion mis_builder/models/mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
19 changes: 19 additions & 0 deletions mis_builder/tests/test_mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
1 change: 1 addition & 0 deletions mis_builder/views/mis_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
name="date_field"
domain="[('model_id', '=', model_id), ('ttype', 'in', ('date', 'datetime'))]"
/>
<field name="company_field_id" />
<field name="domain" />
</tree>
</field>
Expand Down

0 comments on commit 4d3dca5

Please sign in to comment.