diff --git a/modules/account/fiscalyear.py b/modules/account/fiscalyear.py index 5443454fc2..ddd5b6831b 100644 --- a/modules/account/fiscalyear.py +++ b/modules/account/fiscalyear.py @@ -115,6 +115,7 @@ class FiscalYear(Workflow, ModelSQL, ModelView): super().validate_fields(fiscalyears, field_names) cls.check_dates(fiscalyears, field_names) cls.check_post_move_sequence(fiscalyears, field_names) + cls.check_period_dates(fiscalyears, field_names) @classmethod def check_dates(cls, fiscalyears, field_names=None): @@ -174,6 +175,15 @@ class FiscalYear(Workflow, ModelSQL, ModelView): first=fiscalyear.rec_name, second=years[0].rec_name)) + @classmethod + def check_period_dates(cls, fiscalyears, field_names=None): + pool = Pool() + Period = pool.get('account.period') + if field_names and not (field_names & {'start_date', 'end_date'}): + return + periods = [p for f in fiscalyears for p in f.periods] + Period.check_fiscalyear_dates(periods, field_names={'fiscalyear'}) + @classmethod def create(cls, vlist): fiscalyears = super().create(vlist) diff --git a/modules/account/tests/test_module.py b/modules/account/tests/test_module.py index f687e6d3d4..952629a132 100644 --- a/modules/account/tests/test_module.py +++ b/modules/account/tests/test_module.py @@ -7,7 +7,8 @@ from decimal import Decimal from dateutil.relativedelta import relativedelta from trytond.exceptions import UserError -from trytond.modules.account.exceptions import FiscalYearDatesError +from trytond.modules.account.exceptions import ( + FiscalYearDatesError, PeriodDatesError) from trytond.modules.account.tax import TaxableMixin from trytond.modules.company.tests import ( CompanyTestMixin, PartyCompanyCheckEraseMixin, create_company, set_company) @@ -310,6 +311,27 @@ class AccountTestCase( with self.assertRaises(FiscalYearDatesError): earlier_fiscalyear.save() + @with_transaction() + def test_change_fiscalyear_dates_with_periods(self): + "Test change fiscal year dates" + company = create_company() + with set_company(company): + fiscalyear = get_fiscalyear( + company, + start_date=datetime.date(2023, 1, 1), + end_date=datetime.date(2023, 12, 31)) + fiscalyear.save() + fiscalyear.create_period([fiscalyear]) + + # Extend + fiscalyear.end_date = datetime.date(2024, 5, 31) + fiscalyear.save() + + # Shorten + with self.assertRaises(PeriodDatesError): + fiscalyear.end_date = datetime.date(2023, 5, 31) + fiscalyear.save() + @with_transaction() def test_account_debit_credit(self): 'Test account debit/credit'