changeset ac23c07d6bc0 in modules/account_invoice:default
details: 
https://hg.tryton.org/modules/account_invoice?cmd=changeset&node=ac23c07d6bc0
description:
        Add warning to prevent posting invoices in the future

        issue10088
        review332311002
diffstat:

 CHANGELOG                                      |   1 +
 exceptions.py                                  |   4 +
 invoice.py                                     |  20 ++++++-
 message.xml                                    |   3 +
 tests/scenario_invoice_customer_sequential.rst |   6 +-
 tests/scenario_invoice_in_future.rst           |  71 ++++++++++++++++++++++++++
 tests/test_account_invoice.py                  |   4 +
 7 files changed, 105 insertions(+), 4 deletions(-)

diffs (183 lines):

diff -r 1fd0481addf9 -r ac23c07d6bc0 CHANGELOG
--- a/CHANGELOG Mon Aug 30 00:27:13 2021 +0200
+++ b/CHANGELOG Wed Sep 15 19:19:35 2021 +0200
@@ -1,3 +1,4 @@
+* Add warning to prevent posting invoices in the future
 * Add wizard to reschedule lines to pay of invoices
 * Create base tax line for manual taxes
 
diff -r 1fd0481addf9 -r ac23c07d6bc0 exceptions.py
--- a/exceptions.py     Mon Aug 30 00:27:13 2021 +0200
+++ b/exceptions.py     Wed Sep 15 19:19:35 2021 +0200
@@ -35,3 +35,7 @@
 
 class InvoicePaymentTermDateWarning(UserWarning):
     pass
+
+
+class InvoiceFutureWarning(UserWarning):
+    pass
diff -r 1fd0481addf9 -r ac23c07d6bc0 invoice.py
--- a/invoice.py        Mon Aug 30 00:27:13 2021 +0200
+++ b/invoice.py        Wed Sep 15 19:19:35 2021 +0200
@@ -30,7 +30,8 @@
 
 from .exceptions import (
     InvoiceTaxValidationError, InvoiceNumberError, InvoiceValidationError,
-    InvoiceLineValidationError, PayInvoiceError, InvoicePaymentTermDateWarning)
+    InvoiceLineValidationError, PayInvoiceError, InvoicePaymentTermDateWarning,
+    InvoiceFutureWarning)
 
 if config.getboolean('account_invoice', 'filestore', default=False):
     file_id = 'invoice_report_cache_id'
@@ -1528,6 +1529,23 @@
     @ModelView.button
     @Workflow.transition('posted')
     def post(cls, invoices):
+        pool = Pool()
+        Date = pool.get('ir.date')
+        Warning = pool.get('res.user.warning')
+        today = Date.today()
+        future_invoices = [
+            i for i in invoices
+            if i.type == 'out' and i.invoice_date and i.invoice_date > today]
+        if future_invoices:
+            names = ', '.join(m.rec_name for m in future_invoices[:5])
+            if len(future_invoices) > 5:
+                names += '...'
+            warning_key = Warning.format(
+                'invoice_date_future', future_invoices)
+            if Warning.check(warning_key):
+                raise InvoiceFutureWarning(warning_key,
+                    gettext('account_invoice.msg_invoice_date_future',
+                        invoices=names))
         cls._post(invoices)
 
     @classmethod
diff -r 1fd0481addf9 -r ac23c07d6bc0 message.xml
--- a/message.xml       Mon Aug 30 00:27:13 2021 +0200
+++ b/message.xml       Wed Sep 15 19:19:35 2021 +0200
@@ -75,6 +75,9 @@
         <record model="ir.message" id="msg_close_period_non_posted_invoices">
             <field name="text">To close the periods you must post the invoices 
"%(invoices)s".</field>
         </record>
+        <record model="ir.message" id="msg_invoice_date_future">
+            <field name="text">The invoices "%(invoices)s" have an invoice 
date in the future.</field>
+        </record>
     </data>
 </tryton>
 
diff -r 1fd0481addf9 -r ac23c07d6bc0 
tests/scenario_invoice_customer_sequential.rst
--- a/tests/scenario_invoice_customer_sequential.rst    Mon Aug 30 00:27:13 
2021 +0200
+++ b/tests/scenario_invoice_customer_sequential.rst    Wed Sep 15 19:19:35 
2021 +0200
@@ -16,7 +16,7 @@
     >>> from trytond.modules.account_invoice.tests.tools import \
     ...     set_fiscalyear_invoice_sequences
     >>> today = dt.date.today()
-    >>> next_year = today + relativedelta(years=1)
+    >>> past_year = today - relativedelta(years=1)
 
 Activate modules::
 
@@ -30,10 +30,10 @@
 Create fiscal years::
 
     >>> fiscalyear = set_fiscalyear_invoice_sequences(
-    ...     create_fiscalyear(company, today=today))
+    ...     create_fiscalyear(company, today=past_year))
     >>> fiscalyear.click('create_period')
     >>> next_fiscalyear = set_fiscalyear_invoice_sequences(
-    ...     create_fiscalyear(company, today=next_year))
+    ...     create_fiscalyear(company, today=today))
     >>> next_fiscalyear.click('create_period')
 
 Create chart of accounts::
diff -r 1fd0481addf9 -r ac23c07d6bc0 tests/scenario_invoice_in_future.rst
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/scenario_invoice_in_future.rst      Wed Sep 15 19:19:35 2021 +0200
@@ -0,0 +1,71 @@
+=================
+Invoice in Future
+=================
+
+Imports::
+
+    >>> import datetime
+    >>> from decimal import Decimal
+    >>> from proteus import Model
+    >>> from trytond.tests.tools import activate_modules
+    >>> from trytond.modules.company.tests.tools import (create_company,
+    ...     get_company)
+    >>> from trytond.modules.account.tests.tools import (create_fiscalyear,
+    ...     create_chart, get_accounts)
+    >>> from trytond.modules.account_invoice.tests.tools import (
+    ...     set_fiscalyear_invoice_sequences)
+    >>> today = datetime.date.today()
+    >>> tomorrow = today + datetime.timedelta(days=1)
+
+Activate modules::
+
+    >>> config = activate_modules('account_invoice')
+
+Create company::
+
+    >>> _ = create_company()
+    >>> company = get_company()
+
+Create fiscal year::
+
+    >>> fiscalyear = set_fiscalyear_invoice_sequences(
+    ...     create_fiscalyear(company))
+    >>> fiscalyear.click('create_period')
+
+Create chart of accounts::
+
+    >>> _ = create_chart(company)
+    >>> accounts = get_accounts(company)
+    >>> revenue = accounts['revenue']
+
+Create party::
+
+    >>> Party = Model.get('party.party')
+    >>> party = Party(name='Party')
+    >>> party.save()
+
+Create invoice::
+
+    >>> Invoice = Model.get('account.invoice')
+    >>> invoice = Invoice()
+    >>> invoice.party = party
+    >>> line = invoice.lines.new()
+    >>> line.account = revenue
+    >>> line.description = 'Test'
+    >>> line.quantity = 1
+    >>> line.unit_price = Decimal(20)
+
+Posting an invoice in the future raises a warning::
+
+    >>> invoice.invoice_date = tomorrow
+    >>> invoice.click('post')  # doctest: +IGNORE_EXCEPTION_DETAIL
+    Traceback (most recent call last):
+        ...
+    InvoiceFutureWarning: ...
+
+Post invoice::
+
+    >>> invoice.invoice_date = today
+    >>> invoice.click('post')
+    >>> invoice.state
+    'posted'
diff -r 1fd0481addf9 -r ac23c07d6bc0 tests/test_account_invoice.py
--- a/tests/test_account_invoice.py     Mon Aug 30 00:27:13 2021 +0200
+++ b/tests/test_account_invoice.py     Wed Sep 15 19:19:35 2021 +0200
@@ -315,6 +315,10 @@
             tearDown=doctest_teardown, encoding='utf-8',
             checker=doctest_checker,
             optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+    suite.addTests(doctest.DocFileSuite('scenario_invoice_in_future.rst',
+            tearDown=doctest_teardown, encoding='utf-8',
+            checker=doctest_checker,
+            optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
     suite.addTests(doctest.DocFileSuite(
             'scenario_renew_fiscalyear.rst',
             tearDown=doctest_teardown, encoding='utf-8',

Reply via email to