Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP][16.0] mis_builder: use company of report.instance when querying #625

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This domain seems correct, but I wonder why it is not on the date_field above.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think query_company_ids cannot be empty. And if it was, it would be surprising if the company field on the query was ignored. So may be we could simply assert self.report_instance_id.query_company_ids if company_field_id is set.

):
domain = AND([domain, [(company_field.name, "in", instance_companies.ids)]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doing an AND with empty domain?

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
Loading