From 1be7ab6ceb984d613b601280e83b0acaab49473b Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 4 Aug 2021 12:28:25 +0200
Subject: [PATCH] caluire-axel: delay online payement on created invoices
 (#55803)

---
 passerelle/contrib/caluire_axel/utils.py |  4 +-
 tests/data/caluire_axel/invoices.xml     |  9 ++++
 tests/test_caluire_axel.py               | 53 ++++++++++++++++++++++--
 3 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/passerelle/contrib/caluire_axel/utils.py b/passerelle/contrib/caluire_axel/utils.py
index 63da6c1e..989cf21a 100644
--- a/passerelle/contrib/caluire_axel/utils.py
+++ b/passerelle/contrib/caluire_axel/utils.py
@@ -50,20 +50,22 @@ def normalize_invoice(invoice, family_id, historical=False, vendor_base=None):
             {
                 'amount': 0,
                 'pay_limit_date': '',
                 'online_payment': False,
             }
         )
     else:
         date_now = localtime().date()
+        date_creation = datetime.datetime.strptime(invoice['EMISSION'], axel.json_date_format).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 and date_now <= date_pay_limit
+                    invoice['MONTANT'] - invoice['ENCAISSE'] > 0
+                    and date_creation <= date_now <= date_pay_limit
                 ),
             }
         )
     return data
diff --git a/tests/data/caluire_axel/invoices.xml b/tests/data/caluire_axel/invoices.xml
index a6582904..65968962 100644
--- a/tests/data/caluire_axel/invoices.xml
+++ b/tests/data/caluire_axel/invoices.xml
@@ -12,8 +12,17 @@
   <IDFACTURE>43</IDFACTURE>
   <MONTANT>44.94</MONTANT>
   <ENCAISSE>0.00</ENCAISSE>
   <FACTURATION>PRESTATIONS PERISCOLAIRES NOVEMBRE 2019</FACTURATION>
   <EMISSION>12/12/2019</EMISSION>
   <ECHEANCE>04/01/2020</ECHEANCE>
   <EXISTEPDF>n</EXISTEPDF>
 </FACTURE>
+<FACTURE>
+  <IDFACTURE>44</IDFACTURE>
+  <MONTANT>44.44</MONTANT>
+  <ENCAISSE>0.00</ENCAISSE>
+  <FACTURATION>PRESTATIONS PERISCOLAIRES DECEMBRE 2019</FACTURATION>
+  <EMISSION>12/01/2020</EMISSION>
+  <ECHEANCE>04/02/2020</ECHEANCE>
+  <EXISTEPDF>n</EXISTEPDF>
+</FACTURE>
diff --git a/tests/test_caluire_axel.py b/tests/test_caluire_axel.py
index f95c823f..dd417baa 100644
--- a/tests/test_caluire_axel.py
+++ b/tests/test_caluire_axel.py
@@ -2815,17 +2815,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')
+@freezegun.freeze_time('2019-12-12')
 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>
@@ -2881,16 +2881,40 @@ def test_invoices_endpoint(app, resource):
                     'MONTANT': '44.94',
                     'ENCAISSE': '0.00',
                     'ECHEANCE': '2020-01-04',
                     'EMISSION': '2019-12-12',
                     'EXISTEPDF': False,
                 }
             },
         },
+        {
+            'id': 'XXX-44',
+            'display_id': '44',
+            'label': 'PRESTATIONS PERISCOLAIRES DECEMBRE 2019',
+            'amount': '44.44',
+            'total_amount': '44.44',
+            'amount_paid': '0.00',
+            'online_payment': False,
+            'created': '2020-01-12',
+            'pay_limit_date': '2020-02-04',
+            'has_pdf': False,
+            'paid': False,
+            'vendor': {
+                'caluire-axel': {
+                    'IDFACTURE': 44,
+                    'FACTURATION': 'PRESTATIONS PERISCOLAIRES DECEMBRE 2019',
+                    'MONTANT': '44.44',
+                    'ENCAISSE': '0.00',
+                    'ECHEANCE': '2020-02-04',
+                    'EMISSION': '2020-01-12',
+                    'EXISTEPDF': False,
+                }
+            },
+        },
     ]
 
 
 def test_invoices_history_endpoint_axel_error(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     with mock.patch('passerelle.contrib.caluire_axel.schemas.get_list_factures') as operation:
         operation.side_effect = AxelError('FooBar')
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoices/history?NameID=yyy')
@@ -2995,16 +3019,39 @@ def test_invoices_history_endpoint(app, resource):
                     'MONTANT': '44.94',
                     'ENCAISSE': '0.00',
                     'ECHEANCE': '2020-01-04',
                     'EMISSION': '2019-12-12',
                     'EXISTEPDF': False,
                 }
             },
         },
+        {
+            'id': 'historical-XXX-44',
+            'display_id': '44',
+            'label': 'PRESTATIONS PERISCOLAIRES DECEMBRE 2019',
+            'amount': 0,
+            'created': '2020-01-12',
+            'total_amount': '44.44',
+            'online_payment': False,
+            'pay_limit_date': '',
+            'has_pdf': False,
+            'paid': False,
+            'vendor': {
+                'caluire-axel': {
+                    'IDFACTURE': 44,
+                    'FACTURATION': 'PRESTATIONS PERISCOLAIRES DECEMBRE 2019',
+                    'MONTANT': '44.44',
+                    'ENCAISSE': '0.00',
+                    'ECHEANCE': '2020-02-04',
+                    'EMISSION': '2020-01-12',
+                    'EXISTEPDF': False,
+                }
+            },
+        },
     ]
 
 
 def test_invoice_endpoint_axel_error(app, resource):
     Link.objects.create(resource=resource, name_id='yyy', family_id='XXX', person_id='42')
     with mock.patch('passerelle.contrib.caluire_axel.schemas.get_factures_a_payer') as operation:
         operation.side_effect = AxelError('FooBar')
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-42?NameID=yyy')
@@ -3068,17 +3115,17 @@ def test_invoice_endpoint_no_result(app, resource):
         '''<PORTAIL>
     <GETLISTFACTURES>
         %s
     </GETLISTFACTURES>
 </PORTAIL>'''
         % invoices
     )
     with mock_data(content, 'GetListFactures'):
-        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-44?NameID=yyy')
+        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/historical-XXX-41?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')
@@ -3210,17 +3257,17 @@ def test_invoice_pdf_endpoint_no_result(app, resource):
             % xml.read()
         )
     with mock_data(content, 'GetFacturesaPayer'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-35/pdf?NameID=yyy', status=404)
     assert resp.json['err_desc'] == "Invoice not found"
     assert resp.json['err'] == 'not-found'
 
     with mock_data(content, 'GetFacturesaPayer'):
-        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-44/pdf?NameID=yyy', status=404)
+        resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-41/pdf?NameID=yyy', status=404)
     assert resp.json['err_desc'] == "Invoice not found"
     assert resp.json['err'] == 'not-found'
 
     with mock_data(content, 'GetFacturesaPayer'):
         resp = app.get('/caluire-axel/test/regie/MAREGIE/invoice/XXX-43/pdf?NameID=yyy', status=404)
     assert resp.json['err_desc'] == "PDF not available"
     assert resp.json['err'] == 'not-available'
 
-- 
2.30.2

