From 0551bbcac2db6d2abde98f5b08fcae0a98427329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 20 Feb 2023 18:38:57 +0100 Subject: [PATCH] Use specific error message for open period or fiscal year not found Closes #9335 --- modules/account/fiscalyear.py | 28 ++++++++++++++++++++-------- modules/account/message.xml | 10 ++++++++-- modules/account/period.py | 28 ++++++++++++++++++++-------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/modules/account/fiscalyear.py b/modules/account/fiscalyear.py index 4d1bc809c8..d4b835875c 100644 --- a/modules/account/fiscalyear.py +++ b/modules/account/fiscalyear.py @@ -245,6 +245,7 @@ class FiscalYear(Workflow, ModelSQL, ModelView): pool = Pool() Lang = pool.get('ir.lang') Date = pool.get('ir.date') + Company = pool.get('company.company') company_id = int(company) if company is not None else None if not date: @@ -258,8 +259,6 @@ class FiscalYear(Workflow, ModelSQL, ModelView): ('end_date', '>=', date), ('company', '=', company_id), ] - if test_state: - clause.append(('state', '=', 'open')) fiscalyears = cls.search( clause, order=[('start_date', 'DESC')], limit=1) if fiscalyears: @@ -269,12 +268,25 @@ class FiscalYear(Workflow, ModelSQL, ModelView): cls._find_cache.set(key, int(fiscalyear) if fiscalyear else None) elif fiscalyear is not None: fiscalyear = cls(fiscalyear) - if not fiscalyear and exception: - lang = Lang.get() - raise FiscalYearNotFoundError( - gettext('account.msg_no_fiscalyear_date', - date=lang.strftime(date))) - return fiscalyear + found = fiscalyear and (not test_state or fiscalyear.state == 'open') + if not found: + if exception: + lang = Lang.get() + if company is not None and not isinstance(company, Company): + company = Company(company) + if not fiscalyear: + raise FiscalYearNotFoundError( + gettext('account.msg_no_fiscalyear_date', + date=lang.strftime(date), + company=company.rec_name if company else '')) + else: + raise FiscalYearNotFoundError( + gettext('account.msg_no_open_fiscalyear_date', + date=lang.strftime(date), + fiscalyear=fiscalyear.rec_name, + company=company.rec_name if company else '')) + else: + return fiscalyear def get_deferral(self, account): 'Computes deferrals for accounts' diff --git a/modules/account/message.xml b/modules/account/message.xml index 3c0813cccb..3dd40bb127 100644 --- a/modules/account/message.xml +++ b/modules/account/message.xml @@ -4,7 +4,10 @@ this repository contains the full copyright notices and license terms. --> - To continue, you must create a period for the date "%(date)s". + To continue, you must create a period for the date "%(date)s" and company "%(company)s". + + + To continue, you must reopen the period "%(period)s" of company "%(company)s" for the date "%(date)s". You cannot modify or delete period "%(period)s" because it contains moves. @@ -55,7 +58,10 @@ this repository contains the full copyright notices and license terms. --> You cannot change the post move sequence on fiscal year "%(fiscalyear)s" because it contains posted moves. - To continue, you must create a fiscal year for the date "%(date)s". + To continue, you must create a fiscal year for the date "%(date)s" and company "%(company)s". + + + To continue, you must reopen the fiscal year "%(fiscalyear)s" of company "%(company)s"for the date "%(date)s". The fiscal years "%(first)s" and "%(second)s" overlap, you must use different dates. diff --git a/modules/account/period.py b/modules/account/period.py index 73b227c8a6..9738081ffe 100644 --- a/modules/account/period.py +++ b/modules/account/period.py @@ -191,6 +191,7 @@ class Period(Workflow, ModelSQL, ModelView): pool = Pool() Date = pool.get('ir.date') Lang = pool.get('ir.lang') + Company = pool.get('company.company') company_id = int(company) if company is not None else None if not date: @@ -205,8 +206,6 @@ class Period(Workflow, ModelSQL, ModelView): ('fiscalyear.company', '=', company_id), ('type', '=', 'standard'), ] - if test_state: - clause.append(('state', '=', 'open')) periods = cls.search( clause, order=[('start_date', 'DESC')], limit=1) if periods: @@ -216,12 +215,25 @@ class Period(Workflow, ModelSQL, ModelView): cls._find_cache.set(key, int(period) if period else None) elif period is not None: period = cls(period) - if not period and exception: - lang = Lang.get() - raise PeriodNotFoundError( - gettext('account.msg_no_period_date', - date=lang.strftime(date))) - return period + found = period and (not test_state or period.state == 'open') + if not found: + if exception: + lang = Lang.get() + if company is not None and not isinstance(company, Company): + company = Company(company) + if not period: + raise PeriodNotFoundError( + gettext('account.msg_no_period_date', + date=lang.strftime(date), + company=company.rec_name if company else '')) + else: + raise PeriodNotFoundError( + gettext('account.msg_no_open_period_date', + date=lang.strftime(date), + period=period.rec_name, + company=company.rec_name if company else '')) + else: + return period @classmethod def _check(cls, periods):