From 1a1623918f907bdb6330d6a310cd801d12976860 Mon Sep 17 00:00:00 2001 From: Zina Rasoamanana Date: Wed, 28 Aug 2024 15:11:35 +0200 Subject: [PATCH] [IMP] mis_builder_budget: allow to specify a company on budget by kpi --- mis_builder_budget/models/mis_budget_item.py | 23 ++++++++++++++++++- .../models/mis_report_instance_period.py | 14 +++++++++++ .../security/mis_budget_item.xml | 2 +- mis_builder_budget/tests/test_mis_budget.py | 3 +++ mis_builder_budget/views/mis_budget_item.xml | 5 ++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/mis_builder_budget/models/mis_budget_item.py b/mis_builder_budget/models/mis_budget_item.py index 9a7d63b50..6a088cbe3 100644 --- a/mis_builder_budget/models/mis_budget_item.py +++ b/mis_builder_budget/models/mis_budget_item.py @@ -19,10 +19,30 @@ class MisBudgetItem(models.Model): ) ) + company_id = fields.Many2one( + comodel_name="res.company", + compute="_compute_company_id", + store=True, + readonly=False, + ) + budget_company_id = fields.Many2one( + string="#Technical field: Budget Company", related="budget_id.company_id" + ) + + @api.depends("budget_id.company_id") + def _compute_company_id(self): + for rec in self: + rec.company_id = rec.budget_id.company_id + def _prepare_overlap_domain(self): """Prepare a domain to check for overlapping budget items.""" domain = super()._prepare_overlap_domain() - domain.extend([("kpi_expression_id", "=", self.kpi_expression_id.id)]) + domain.extend( + [ + ("kpi_expression_id", "=", self.kpi_expression_id.id), + ("company_id", "=", self.company_id.id), + ] + ) return domain @api.constrains( @@ -31,6 +51,7 @@ def _prepare_overlap_domain(self): "date_to", "budget_id", "kpi_expression_id", + "company_id", ) def _check_dates(self): super()._check_dates() diff --git a/mis_builder_budget/models/mis_report_instance_period.py b/mis_builder_budget/models/mis_report_instance_period.py index 333a3a255..3880ff83f 100644 --- a/mis_builder_budget/models/mis_report_instance_period.py +++ b/mis_builder_budget/models/mis_report_instance_period.py @@ -2,6 +2,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import api, fields, models +from odoo.osv.expression import AND SRC_MIS_BUDGET = "mis_budget" SRC_MIS_BUDGET_BY_ACCOUNT = "mis_budget_by_account" @@ -69,4 +70,17 @@ def _get_additional_budget_item_filter(self): compatible with mis.budget.item.""" self.ensure_one() filters = self._get_additional_move_line_filter() + + query_companies = self.report_instance_id.query_company_ids + filters = AND( + [ + filters, + [ + "|", + ("company_id", "in", query_companies.ids), + ("company_id", "=", False), + ], + ] + ) + return filters diff --git a/mis_builder_budget/security/mis_budget_item.xml b/mis_builder_budget/security/mis_budget_item.xml index 72827ae74..0c6cfd02f 100644 --- a/mis_builder_budget/security/mis_budget_item.xml +++ b/mis_builder_budget/security/mis_budget_item.xml @@ -24,7 +24,7 @@ mis.budget.item multi company - ['|',('budget_id.company_id','=',False),('budget_id.company_id','in',company_ids)] + ['|',('company_id','=',False),('company_id','in',company_ids)] diff --git a/mis_builder_budget/tests/test_mis_budget.py b/mis_builder_budget/tests/test_mis_budget.py index 87be3331d..cefad4027 100644 --- a/mis_builder_budget/tests/test_mis_budget.py +++ b/mis_builder_budget/tests/test_mis_budget.py @@ -132,6 +132,9 @@ def test_drilldown(self): ("date_to", ">=", datetime.date(2017, 1, 1)), ("kpi_expression_id", "=", self.expr1.id), ("budget_id", "=", self.budget.id), + "|", + ("company_id", "in", [self.env.company.id]), + ("company_id", "=", False), ], ) diff --git a/mis_builder_budget/views/mis_budget_item.xml b/mis_builder_budget/views/mis_budget_item.xml index e694d2a1c..d547593c1 100644 --- a/mis_builder_budget/views/mis_budget_item.xml +++ b/mis_builder_budget/views/mis_budget_item.xml @@ -22,11 +22,16 @@ + +