From 3253be905ab1cbda4def13017bdd65c8509fecee Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 23 Jul 2021 17:45:39 +0200
Subject: [PATCH] caluire-axel: desactivate online payement on obsolete
 invoices (#55803)

---
 passerelle/contrib/caluire_axel/utils.py | 12 +++++++++++-
 tests/test_caluire_axel.py               |  4 +++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/passerelle/contrib/caluire_axel/utils.py b/passerelle/contrib/caluire_axel/utils.py
index 12a78354..a36f624c 100644
--- a/passerelle/contrib/caluire_axel/utils.py
+++ b/passerelle/contrib/caluire_axel/utils.py
@@ -10,16 +10,22 @@
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Affero General Public License for more details.
 #
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import datetime
+
+from django.utils.timezone import localtime, now
+
+from passerelle.contrib.utils import axel
+
 
 def get_reference_year_from_date(booking_date):
     if booking_date.month <= 8:
         # between january and august, reference year is the year just before
         return booking_date.year - 1
     return booking_date.year
 
 
@@ -43,17 +49,21 @@ def normalize_invoice(invoice, family_id, historical=False, vendor_base=None):
         data.update(
             {
                 'amount': 0,
                 'pay_limit_date': '',
                 'online_payment': False,
             }
         )
     else:
+        date_now = localtime(now()).date()
+        date_pay_limit = datetime.datetime.strptime(invoice['ECHEANCE'], axel.json_date_format).date()
         data.update(
             {
                 'amount': max(0, invoice['MONTANT'] - invoice['ENCAISSE']),
                 'amount_paid': invoice['ENCAISSE'],
                 'pay_limit_date': invoice['ECHEANCE'],
-                'online_payment': bool(invoice['MONTANT'] - invoice['ENCAISSE'] > 0),
+                'online_payment': bool(
+                    invoice['MONTANT'] - invoice['ENCAISSE'] > 0 and date_now <= date_pay_limit
+                ),
             }
         )
     return data
diff --git a/tests/test_caluire_axel.py b/tests/test_caluire_axel.py
index c524d08a..e2eb53d7 100644
--- a/tests/test_caluire_axel.py
+++ b/tests/test_caluire_axel.py
@@ -2773,16 +2773,17 @@ def test_invoices_endpoint_no_invoice(app, resource):
     </GETFACTURESAPAYER>
 </PORTAIL>'''
     with mock_data(content, 'GetFacturesaPayer'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoices?NameID=yyy')
     assert resp.json['err'] == 0
     assert resp.json['data'] == []
 
 
+@freezegun.freeze_time('2019-12-05')
 def test_invoices_endpoint(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
     with open(filepath) as xml:
         content = (
             '''
 <PORTAIL>
   <GETFACTURESAPAYER>
@@ -2797,17 +2798,17 @@ def test_invoices_endpoint(app, resource):
     assert resp.json['data'] == [
         {
             'id': 'XXX-42',
             'display_id': '42',
             'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
             'amount': '4.94',
             'total_amount': '44.94',
             'amount_paid': '40.00',
-            'online_payment': True,
+            'online_payment': False,
             'created': '2019-11-12',
             'pay_limit_date': '2019-12-04',
             'has_pdf': True,
             'paid': False,
             'vendor': {
                 'caluire-axel': {
                     'IDFACTURE': 42,
                     'MONTANT': '44.94',
@@ -3030,16 +3031,17 @@ def test_invoice_endpoint_no_result(app, resource):
         % invoices
     )
     with mock_data(content, 'GetListFactures'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-44?NameID=yyy')
     assert resp.json['err_desc'] == "Invoice not found"
     assert resp.json['err'] == 'not-found'
 
 
+@freezegun.freeze_time('2019-12-04')
 def test_invoice_endpoint(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     filepath = os.path.join(os.path.dirname(__file__), 'data/caluire_axel/invoices.xml')
     with open(filepath) as xml:
         invoices = xml.read()
     content = (
         '''<PORTAIL>
   <GETFACTURESAPAYER>
-- 
2.30.2

