From 2b7e1f07d296d680b6e455de53cbc158603c0e97 Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Wed, 7 Oct 2020 15:46:16 +0200 Subject: [PATCH] [IMP] - add an option to enable forecasts by company --- contract_forecast/__manifest__.py | 1 + contract_forecast/models/__init__.py | 1 + contract_forecast/models/contract.py | 5 +-- contract_forecast/models/contract_line.py | 8 +++-- contract_forecast/models/res_company.py | 3 ++ .../models/res_config_settings.py | 29 +++++++++++++++ .../views/res_config_settings.xml | 36 +++++++++++++++++++ 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 contract_forecast/models/res_config_settings.py create mode 100644 contract_forecast/views/res_config_settings.xml diff --git a/contract_forecast/__manifest__.py b/contract_forecast/__manifest__.py index b2cc9ac8a..104e5b8c2 100644 --- a/contract_forecast/__manifest__.py +++ b/contract_forecast/__manifest__.py @@ -12,6 +12,7 @@ "depends": ["base", "contract", "queue_job"], "data": [ "security/contract_line_forecast_period.xml", + "views/res_config_settings.xml", "views/contract_line_forecast_period.xml", "views/contract.xml", ], diff --git a/contract_forecast/models/__init__.py b/contract_forecast/models/__init__.py index 9fd5685c3..df82b65fe 100644 --- a/contract_forecast/models/__init__.py +++ b/contract_forecast/models/__init__.py @@ -2,3 +2,4 @@ from . import contract from . import contract_line from . import contract_line_forecast_period from . import res_company +from . import res_config_settings diff --git a/contract_forecast/models/contract.py b/contract_forecast/models/contract.py index f5c001385..31f5b6c79 100644 --- a/contract_forecast/models/contract.py +++ b/contract_forecast/models/contract.py @@ -37,6 +37,7 @@ class ContractContract(models.Model): ] ): for rec in self: - for contract_line in rec.contract_line_ids: - contract_line.with_delay()._generate_forecast_periods() + if rec.company_id.enable_contract_forecast: + for contract_line in rec.contract_line_ids: + contract_line.with_delay()._generate_forecast_periods() return res diff --git a/contract_forecast/models/contract_line.py b/contract_forecast/models/contract_line.py index 99ae59cf6..e15c9ac8e 100644 --- a/contract_forecast/models/contract_line.py +++ b/contract_forecast/models/contract_line.py @@ -52,6 +52,8 @@ class ContractLine(models.Model): @api.multi def _get_generate_forecast_periods_criteria(self, period_date_end): self.ensure_one() + if not self.contract_id.company_id.enable_contract_forecast: + return False if self.is_canceled or not self.active: return False contract_forecast_end_date = self._get_contract_forecast_end_date() @@ -111,7 +113,8 @@ class ContractLine(models.Model): def create(self, values): contract_lines = super(ContractLine, self).create(values) for contract_line in contract_lines: - contract_line.with_delay()._generate_forecast_periods() + if contract_line.contract_id.company_id.enable_contract_forecast: + contract_line.with_delay()._generate_forecast_periods() return contract_lines @api.model @@ -144,5 +147,6 @@ class ContractLine(models.Model): ] ): for rec in self: - rec.with_delay()._generate_forecast_periods() + if rec.contract_id.company_id.enable_contract_forecast: + rec.with_delay()._generate_forecast_periods() return res diff --git a/contract_forecast/models/res_company.py b/contract_forecast/models/res_company.py index 1adbadf69..6c55be337 100644 --- a/contract_forecast/models/res_company.py +++ b/contract_forecast/models/res_company.py @@ -14,3 +14,6 @@ class ResCompany(models.Model): contract_forecast_rule_type = fields.Selection( [("monthly", "Month(s)"), ("yearly", "Year(s)")], default="monthly" ) + enable_contract_forecast = fields.Boolean( + string="Enable contract forecast", default=True + ) diff --git a/contract_forecast/models/res_config_settings.py b/contract_forecast/models/res_config_settings.py new file mode 100644 index 000000000..fb0c486c8 --- /dev/null +++ b/contract_forecast/models/res_config_settings.py @@ -0,0 +1,29 @@ +# Copyright 2020 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + + _inherit = "res.config.settings" + + enable_contract_forecast = fields.Boolean( + string="Enable contract forecast", + default=True, + readonly=False, + related="company_id.enable_contract_forecast", + ) + contract_forecast_interval = fields.Integer( + string="Number of contract forecast Periods", + default=12, + related="company_id.contract_forecast_interval", + readonly=False, + + ) + contract_forecast_rule_type = fields.Selection( + [("monthly", "Month(s)"), ("yearly", "Year(s)")], + default="monthly", + related="company_id.contract_forecast_rule_type", + readonly=False, + ) diff --git a/contract_forecast/views/res_config_settings.xml b/contract_forecast/views/res_config_settings.xml new file mode 100644 index 000000000..550f43cf7 --- /dev/null +++ b/contract_forecast/views/res_config_settings.xml @@ -0,0 +1,36 @@ + + + + + + + res.config.settings + + + +
+
+
+ +
+
+
+
+
+
+
+
+
+
+ + +