From f71ee61b6dd4a162b4cf8b935e09e5adc862e162 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Wed, 15 Feb 2023 23:45:03 +0100 Subject: [PATCH] account_cutoff_start_end_dates: forecast is now a specific state To simplify the code and the user interface, the forecast mode doesn't use a boolean field any more; it is now a specific "state". Migration script is provided. --- .../migrations/16.0.1.0.0/pre-migration.py | 13 +++ .../models/account_cutoff.py | 37 ++++---- .../views/account_cutoff.xml | 91 ++++++------------- 3 files changed, 59 insertions(+), 82 deletions(-) create mode 100644 account_cutoff_start_end_dates/migrations/16.0.1.0.0/pre-migration.py diff --git a/account_cutoff_start_end_dates/migrations/16.0.1.0.0/pre-migration.py b/account_cutoff_start_end_dates/migrations/16.0.1.0.0/pre-migration.py new file mode 100644 index 00000000000..6cbb7e112d1 --- /dev/null +++ b/account_cutoff_start_end_dates/migrations/16.0.1.0.0/pre-migration.py @@ -0,0 +1,13 @@ +# Copyright 2023 Akretion France (http://www.akretion.com/) +# @author: Alexis de Lattre +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +def migrate(cr, version): + if not version: + return + + cr.execute( + "UPDATE account_cutoff SET state='forecast' " + "WHERE state='draft' AND forecast is true" + ) diff --git a/account_cutoff_start_end_dates/models/account_cutoff.py b/account_cutoff_start_end_dates/models/account_cutoff.py index 86c2c208a30..2e7549842cb 100644 --- a/account_cutoff_start_end_dates/models/account_cutoff.py +++ b/account_cutoff_start_end_dates/models/account_cutoff.py @@ -40,20 +40,15 @@ def _get_default_source_journals(self): check_company=True, domain="[('company_id', '=', company_id)]", ) - forecast = fields.Boolean( - readonly=True, - tracking=True, - help="The Forecast mode allows the user to compute " - "the prepaid revenue/expense between 2 dates in the future.", - ) - start_date = fields.Date() - end_date = fields.Date() + state = fields.Selection(selection_add=[("forecast", "Forecast")]) + start_date = fields.Date(help="This field is only for the forecast mode") + end_date = fields.Date(help="This field is only for the forecast mode") - @api.constrains("start_date", "end_date", "forecast") + @api.constrains("start_date", "end_date", "state") def _check_start_end_dates(self): for rec in self: if ( - rec.forecast + rec.state == "forecast" and rec.start_date and rec.end_date and rec.start_date > rec.end_date @@ -71,13 +66,14 @@ def forecast_enable(self): ) ) self.line_ids.unlink() - self.write({"forecast": True}) + # set cutoff_date to False to avoid issue with unicity sql constraint + self.write({"state": "forecast", "cutoff_date": False}) def forecast_disable(self): self.ensure_one() - assert self.state == "draft" + assert self.state == "forecast" self.line_ids.unlink() - self.write({"forecast": False}) + self.write({"state": "draft"}) def _prepare_date_cutoff_line(self, aml, mapping): self.ensure_one() @@ -147,7 +143,7 @@ def _prepare_date_prepaid_cutoff_line(self, aml, vals): end_date_dt = aml.end_date # Here, we compute the amount of the cutoff # That's the important part ! - if self.forecast: + if self.state == "forecast": out_days = 0 forecast_start_date_dt = self.start_date forecast_end_date_dt = self.end_date @@ -173,7 +169,7 @@ def _prepare_date_prepaid_cutoff_line(self, aml, vals): ) def get_lines(self): - res = super().get_lines() + super().get_lines() aml_obj = self.env["account.move.line"] line_obj = self.env["account.cutoff.line"] if not self.source_journal_ids: @@ -185,9 +181,17 @@ def get_lines(self): ("company_id", "=", self.company_id.id), ("balance", "!=", 0), ] + if self.source_move_state == "posted": + domain.append(("parent_state", "=", "posted")) + else: + domain.append(("parent_state", "in", ("draft", "posted"))) if self.cutoff_type in ["prepaid_expense", "prepaid_revenue"]: - if self.forecast: + if self.state == "forecast": + if not self.start_date or not self.end_date: + raise UserError( + _("Start date and end date are required for forecast mode.") + ) domain += [ ("start_date", "!=", False), ("start_date", "<=", self.end_date), @@ -208,4 +212,3 @@ def get_lines(self): amls = aml_obj.search(domain) for aml in amls: line_obj.create(self._prepare_date_cutoff_line(aml, mapping)) - return res diff --git a/account_cutoff_start_end_dates/views/account_cutoff.xml b/account_cutoff_start_end_dates/views/account_cutoff.xml index 62cdcce536a..d5162bdb806 100644 --- a/account_cutoff_start_end_dates/views/account_cutoff.xml +++ b/account_cutoff_start_end_dates/views/account_cutoff.xml @@ -11,28 +11,26 @@ - {'invisible': [('forecast', '=', True)]} + >{'invisible': [('state', '=', 'forecast')]} + + + state == 'forecast' @@ -46,66 +44,37 @@ name="forecast_enable" type="object" string="Enable Forecast Mode" - attrs="{'invisible': ['|', '|', ('state', '!=', 'draft'), ('forecast', '=', True), ('cutoff_type', 'not in', ('prepaid_revenue', 'prepaid_expense'))]}" + attrs="{'invisible': ['|', ('state', '!=', 'draft'), ('cutoff_type', 'not in', ('prepaid_revenue', 'prepaid_expense'))]}" /> - - - + + + - {'invisible': [('forecast', '=', True)], 'required': [('forecast', '=', False)]} - 0 + draft,done + + + draft,done - {'invisible': [('forecast', '=', True)]} + draft,done - - 0 - {'required': [('forecast', '=', False)]} - - - 0 - {'required': [('forecast', '=', False)]} - - - 0 - {'required': [('forecast', '=', False)]} - - - + + @@ -113,19 +82,11 @@ account.cutoff - - + - - -