From a53e026d01d9ecb79009062db63e14cc0dbef7c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 25 Feb 2020 11:14:21 +0100
Subject: [PATCH 2/2] toulouse_axel: return amount_paid in invoice endpoint
 (#40163)

---
 passerelle/contrib/toulouse_axel/utils.py |  1 +
 tests/data/toulouse_axel/invoices.xml     |  2 +-
 tests/test_toulouse_axel.py               | 14 +++++++++-----
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/passerelle/contrib/toulouse_axel/utils.py b/passerelle/contrib/toulouse_axel/utils.py
index 90d7efe8..3a58d1f5 100644
--- a/passerelle/contrib/toulouse_axel/utils.py
+++ b/passerelle/contrib/toulouse_axel/utils.py
@@ -165,6 +165,7 @@ def normalize_invoice(invoice, dui, historical=False, vendor_base=None):
         data.update({
             'amount': invoice['RESTEAPAYER'],
             'total_amount': invoice['MONTANTTOTAL'],
+            'amount_paid': max(0, invoice['MONTANTTOTAL'] - invoice['RESTEAPAYER']) or '',
             'created': invoice['DATEEMISSION'],
             'pay_limit_date': invoice['DATEECHEANCE'],
             'has_pdf': invoice['EXISTEPDF'] == '1',
diff --git a/tests/data/toulouse_axel/invoices.xml b/tests/data/toulouse_axel/invoices.xml
index 8a9295c3..83725a16 100644
--- a/tests/data/toulouse_axel/invoices.xml
+++ b/tests/data/toulouse_axel/invoices.xml
@@ -10,7 +10,7 @@
       <LIBELLE>PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019</LIBELLE>
       <IDFACTURATION>4242-35AA</IDFACTURATION>
       <MONTANTTOTAL>44.94</MONTANTTOTAL>
-      <RESTEAPAYER>44.94</RESTEAPAYER>
+      <RESTEAPAYER>4.94</RESTEAPAYER>
       <IDREGIE>MAREGIE</IDREGIE>
       <EXISTEPDF>1</EXISTEPDF>
       <IDFACTURE>42</IDFACTURE>
diff --git a/tests/test_toulouse_axel.py b/tests/test_toulouse_axel.py
index 2a02bd1c..5f5e5ba0 100644
--- a/tests/test_toulouse_axel.py
+++ b/tests/test_toulouse_axel.py
@@ -1517,8 +1517,9 @@ def test_invoices_endpoint(app, resource):
             'id': 'XXX-42',
             'display_id': '42',
             'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
-            'amount': '44.94',
+            'amount': '4.94',
             'total_amount': '44.94',
+            'amount_paid': '40.00',
             'online_payment': False,
             'created': '2019-11-12',
             'pay_limit_date': '2019-12-04',
@@ -1535,7 +1536,7 @@ def test_invoices_endpoint(app, resource):
                     'LIBELLE': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
                     'MONTANTTOTAL': '44.94',
                     'NUMFACTURE': 42,
-                    'RESTEAPAYER': '44.94',
+                    'RESTEAPAYER': '4.94',
                  }
             }
         },
@@ -1545,6 +1546,7 @@ def test_invoices_endpoint(app, resource):
             'label': 'PRESTATIONS PERISCOLAIRES NOVEMBRE 2019',
             'amount': '44.94',
             'total_amount': '44.94',
+            'amount_paid': '',
             'online_payment': False,
             'created': '2019-12-12',
             'pay_limit_date': '2020-01-04',
@@ -1576,6 +1578,7 @@ def test_invoices_endpoint(app, resource):
             'label': 'PRESTATIONS PERISCOLAIRES DECEMBRE 2019',
             'amount': '44.94',
             'total_amount': '44.94',
+            'amount_paid': '',
             'online_payment': False,
             'created': '2020-01-12',
             'pay_limit_date': '2020-01-15',
@@ -1733,8 +1736,9 @@ def test_invoice_endpoint(app, resource):
         'id': 'XXX-42',
         'display_id': '42',
         'label': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
-        'amount': '44.94',
+        'amount': '4.94',
         'total_amount': '44.94',
+        'amount_paid': '40.00',
         'online_payment': False,
         'created': '2019-11-12',
         'pay_limit_date': '2019-12-04',
@@ -1751,7 +1755,7 @@ def test_invoice_endpoint(app, resource):
                 'LIBELLE': 'PRESTATIONS PERISCOLAIRES SEPTEMBRE-OCTOBRE 2019',
                 'MONTANTTOTAL': '44.94',
                 'NUMFACTURE': 42,
-                'RESTEAPAYER': '44.94',
+                'RESTEAPAYER': '4.94',
             }
         }
     }
@@ -1924,7 +1928,7 @@ def test_pay_invoice_endpoint(app, resource):
                 'DATEPAIEMENT': '01/01/2020 12:00:00',
                 'IDFACTURE': 42,
                 'IDREGIEENCAISSEMENT': '',
-                'MONTANTPAYE': decimal.Decimal('44.94'),
+                'MONTANTPAYE': decimal.Decimal('4.94'),
                 'REFERENCE': 'foo',
             }
         }
-- 
2.20.1

