Skip to content

Commit

Permalink
query_company_ids is Effective companies
Browse files Browse the repository at this point in the history
It is the intersection of selected companies
on the report and env.companies (which
apply as multi company record rules)

Hide the company_id field in multi_company reports.
  • Loading branch information
sbidoul committed Aug 13, 2021
1 parent d639257 commit 3175dbf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
18 changes: 10 additions & 8 deletions mis_builder/models/mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,23 +521,22 @@ def _compute_pivot_date(self):
)
company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
string="Allowed company",
default=lambda self: self.env.company,
required=False,
)
multi_company = fields.Boolean(
string="Multiple companies",
help="Check if you wish to specify "
"several companies to be searched for data.",
help="Check if you wish to specify several companies to be searched for data.",
default=False,
)
company_ids = fields.Many2many(
comodel_name="res.company",
string="Companies",
string="Allowed companies",
help="Select companies for which data will be searched.",
domain=lambda self: [("id", "in", self.env.companies.ids)],
)
query_company_ids = fields.Many2many(
string="Effective companies",
comodel_name="res.company",
compute="_compute_query_company_ids",
help="Companies for which data will be searched.",
Expand Down Expand Up @@ -593,13 +592,16 @@ def _onchange_company(self):
self.company_ids = False

@api.depends("multi_company", "company_id", "company_ids")
@api.depends_context("allowed_company_ids")
def _compute_query_company_ids(self):
for rec in self:
if rec.multi_company:
# If no companies defined use active companies.
rec.query_company_ids = rec.company_ids or self.env.companies
if not rec.company_ids:
rec.query_company_ids = self.env.companies
else:
rec.query_company_ids = rec.company_ids & self.env.companies
else:
rec.query_company_ids = rec.company_id
rec.query_company_ids = rec.company_id or self.env.company

@api.model
def get_filter_descriptions_from_context(self):
Expand Down
26 changes: 26 additions & 0 deletions mis_builder/tests/test_mis_report_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,32 @@ def test_kpi_expr_name_get_name_search(self):
r = self.env["mis.report.kpi.expression"].name_search("k4")
self.assertEqual([i[1] for i in r], ["kpi 4 (k4)"])

def test_query_company_ids(self):
# sanity check single company mode
assert not self.report_instance.multi_company
assert self.report_instance.company_id
assert self.report_instance.query_company_ids == self.report_instance.company_id
# create a second company
c1 = self.report_instance.company_id
c2 = self.env["res.company"].create(
dict(
name="company 2",
)
)
self.report_instance.write(dict(multi_company=True, company_id=False))
self.report_instance.company_ids |= c1
self.report_instance.company_ids |= c2
assert len(self.report_instance.company_ids) == 2
assert self.report_instance.query_company_ids == self.env.companies
# In a user context where there is only one company, ensure
# query_company_ids only has one company too.
assert (
self.report_instance.with_context(
allowed_company_ids=(c1.id,)
).query_company_ids
== c1
)

def test_multi_company_onchange(self):
# not multi company
self.assertTrue(self.report_instance.company_id)
Expand Down
11 changes: 8 additions & 3 deletions mis_builder/views/mis_report_instance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,25 @@
<group name="filters">
<field name="target_move" widget="radio" />
<field
name="company_id"
name="multi_company"
groups="base.group_multi_company"
attrs="{'required': [('multi_company', '=', False)]}"
/>
<field
name="multi_company"
name="company_id"
groups="base.group_multi_company"
attrs="{'required': [('multi_company', '=', False)], 'invisible': [('multi_company', '=', True)]}"
/>
<field
name="company_ids"
groups="base.group_multi_company"
widget="many2many_tags"
attrs="{'invisible': [('multi_company', '=', False)]}"
/>
<field
name="query_company_ids"
groups="base.group_multi_company"
widget="many2many_tags"
/>
<field
name="analytic_account_id"
groups="analytic.group_analytic_accounting"
Expand Down

0 comments on commit 3175dbf

Please sign in to comment.