changeset f17e81152e14 in modules/account:default
details: https://hg.tryton.org/modules/account?cmd=changeset&node=f17e81152e14
description:
        Prevent creating fiscal year before closed one

        issue4509
        review385711006
diffstat:

 CHANGELOG             |   1 +
 fiscalyear.py         |  19 +++++++++++++++++--
 message.xml           |   3 +++
 tests/test_account.py |  40 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 2 deletions(-)

diffs (121 lines):

diff -r d80510557e5e -r f17e81152e14 CHANGELOG
--- a/CHANGELOG Mon Apr 11 23:24:20 2022 +0200
+++ b/CHANGELOG Tue Apr 12 10:26:25 2022 +0200
@@ -1,3 +1,4 @@
+* Prevent creating fiscal year before closed one
 * Add automatic reconciliation of suggestions
 * Prevent to change the type of journal with posted moves
 * Mute reconciled payable/receivable lines
diff -r d80510557e5e -r f17e81152e14 fiscalyear.py
--- a/fiscalyear.py     Mon Apr 11 23:24:20 2022 +0200
+++ b/fiscalyear.py     Tue Apr 12 10:26:25 2022 +0200
@@ -108,7 +108,7 @@
     @classmethod
     def check_dates(cls, fiscalyears, field_names=None):
         if field_names and not (field_names & {
-                    'start_date', 'end_date', 'company'}):
+                    'start_date', 'end_date', 'state', 'company'}):
             return
         transaction = Transaction()
         connection = transaction.connection
@@ -132,6 +132,20 @@
                     gettext('account.msg_fiscalyear_overlap',
                         first=year.rec_name,
                         second=second.rec_name))
+            if year.state == 'open':
+                fiscalyears = cls.search([
+                        ('start_date', '>=', year.end_date),
+                        ('state', 'in', ['close', 'locked']),
+                        ('company', '=', year.company.id),
+                        ],
+                    limit=1,
+                    order=[('start_date', 'ASC')])
+                if fiscalyears:
+                    fiscalyear, = fiscalyears
+                    raise FiscalYearDatesError(
+                        gettext('account.msg_open_fiscalyear_earlier',
+                            open=year.rec_name,
+                            close=fiscalyear.rec_name))
 
     @classmethod
     def check_post_move_sequence(cls, fiscalyears, field_names=None):
@@ -277,7 +291,8 @@
         Account = pool.get('account.account')
         Deferral = pool.get('account.account.deferral')
 
-        # Lock period to be sure no new period will be created in between.
+        # Prevent create new fiscal year or period
+        cls.lock()
         Period.lock()
 
         deferrals = []
diff -r d80510557e5e -r f17e81152e14 message.xml
--- a/message.xml       Mon Apr 11 23:24:20 2022 +0200
+++ b/message.xml       Tue Apr 12 10:26:25 2022 +0200
@@ -60,6 +60,9 @@
         <record model="ir.message" id="msg_fiscalyear_overlap">
             <field name="text">The fiscal years "%(first)s" and "%(second)s" 
overlap, you must use different dates.</field>
         </record>
+        <record model="ir.message" id="msg_open_fiscalyear_earlier">
+            <field name="text">The open fiscal year "%(open)s" can not be 
before the close fiscal year "%(closed)s".</field>
+        </record>
         <record model="ir.message" 
id="msg_fiscalyear_different_post_move_sequence">
             <field name="text">The fiscal years "%(first)s" and "%(second)s" 
cannot have the same post move sequence, you must use different 
sequences.</field>
         </record>
diff -r d80510557e5e -r f17e81152e14 tests/test_account.py
--- a/tests/test_account.py     Mon Apr 11 23:24:20 2022 +0200
+++ b/tests/test_account.py     Tue Apr 12 10:26:25 2022 +0200
@@ -18,6 +18,8 @@
     ModuleTestCase, doctest_checker, doctest_teardown, with_transaction)
 from trytond.transaction import Transaction
 
+from ..exceptions import FiscalYearDatesError
+
 
 def create_chart(company, tax=False, chart='account.account_template_root_en'):
     pool = Pool()
@@ -275,6 +277,44 @@
                         fiscalyear.periods[:-1], fiscalyear.periods[1:])))
 
     @with_transaction()
+    def test_open_fiscalyear_before_open(self):
+        "Test create open fiscal year before an open one"
+        company = create_company()
+        with set_company(company):
+            create_chart(company)
+            fiscalyear = get_fiscalyear(
+                company,
+                start_date=datetime.date(2022, 1, 1),
+                end_date=datetime.date(2022, 12, 31))
+            fiscalyear.save()
+
+            earlier_fiscalyear = get_fiscalyear(
+                company,
+                start_date=datetime.date(2021, 1, 1),
+                end_date=datetime.date(2021, 12, 31))
+            earlier_fiscalyear.save()
+
+    @with_transaction()
+    def test_open_fiscalyear_before_close(self):
+        "Test create open fiscal year before a close one"
+        company = create_company()
+        with set_company(company):
+            create_chart(company)
+            fiscalyear = get_fiscalyear(
+                company,
+                start_date=datetime.date(2022, 1, 1),
+                end_date=datetime.date(2022, 12, 31))
+            fiscalyear.save()
+            close_fiscalyear(fiscalyear)
+
+            earlier_fiscalyear = get_fiscalyear(
+                company,
+                start_date=datetime.date(2021, 1, 1),
+                end_date=datetime.date(2021, 12, 31))
+            with self.assertRaises(FiscalYearDatesError):
+                earlier_fiscalyear.save()
+
+    @with_transaction()
     def test_account_debit_credit(self):
         'Test account debit/credit'
         pool = Pool()

Reply via email to