From c0c4cb6885e0b4b93f1676f3c3521c5ba447174d 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 14:05:28 +0100
Subject: [PATCH 1/2] lingo: display amount_paid if provided (#40170)

---
 combo/apps/lingo/models.py                    |  5 ++++-
 .../lingo/templates/lingo/combo/item.html     |  7 +++++++
 tests/test_lingo_remote_regie.py              | 19 +++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py
index 49240aa..fc9354a 100644
--- a/combo/apps/lingo/models.py
+++ b/combo/apps/lingo/models.py
@@ -80,6 +80,7 @@ def build_remote_item(data, regie):
                       display_id=data.get('display_id'),
                       total_amount=data.get('total_amount'),
                       amount=data.get('amount'),
+                      amount_paid=data.get('amount_paid'),
                       subject=data.get('label'),
                       has_pdf=data.get('has_pdf'),
                       online_payment=data.get('online_payment'),
@@ -410,7 +411,7 @@ class RemoteItem(object):
     payment_date = None
 
     def __init__(self, id, regie, creation_date, payment_limit_date,
-                 total_amount, amount, display_id, subject, has_pdf,
+                 total_amount, amount, amount_paid, display_id, subject, has_pdf,
                  online_payment, paid, payment_date, no_online_payment_reason):
         self.id = id
         self.regie = regie
@@ -418,6 +419,8 @@ class RemoteItem(object):
         self.payment_limit_date = dateparse.parse_date(payment_limit_date)
         self.total_amount = Decimal(total_amount)
         self.amount = Decimal(amount)
+        if amount_paid:
+            self.amount_paid = Decimal(amount_paid)
         self.display_id = display_id or self.id
         self.subject = subject
         self.has_pdf = has_pdf
diff --git a/combo/apps/lingo/templates/lingo/combo/item.html b/combo/apps/lingo/templates/lingo/combo/item.html
index c0c2732..3963ece 100644
--- a/combo/apps/lingo/templates/lingo/combo/item.html
+++ b/combo/apps/lingo/templates/lingo/combo/item.html
@@ -27,6 +27,13 @@
           {% endblocktrans %}
         </div>
         {% endif %}
+        {% if item.amount_paid %}
+        <div class="amount_paid">
+          {% blocktrans with amount=item.amount_paid|floatformat:"2" %}
+          Amount already paid: <span class="amount">{{ amount }}€</span>
+          {% endblocktrans %}
+        </div>
+        {% endif %}
         <div class="issued">{% trans "Issue date:" %} <span class="timestamp">{{ item.creation_date|date:"SHORT_DATE_FORMAT" }}</span></div>
         {% if item.payment_limit_date %}
         <div class="invoice-payment-limit-date">{% trans "Payment due date:" %}
diff --git a/tests/test_lingo_remote_regie.py b/tests/test_lingo_remote_regie.py
index 9e3be6c..0eadda4 100644
--- a/tests/test_lingo_remote_regie.py
+++ b/tests/test_lingo_remote_regie.py
@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+import copy
 import json
 import pytest
 import mock
@@ -178,11 +179,29 @@ def test_remote_regie_past_invoices_cell(mock_send, remote_regie):
 def test_anonymous_successful_item_payment(mock_get, mock_pay_invoice, app, remote_regie):
     assert remote_regie.is_remote() == True
     encrypt_id = aes_hex_encrypt(settings.SECRET_KEY, 'F201601')
+    # invoice with amount_paid
+    invoices = copy.deepcopy(INVOICES)
+    invoices[0]['amount'] = '100.00'
+    invoices[0]['amount_paid'] = '23.45'
+    mock_json = mock.Mock()
+    mock_json.json.return_value = {'err': 0, 'data': invoices[0]}
+    mock_get.return_value = mock_json
+    mock_pay_invoice.return_value = mock.Mock(status_code=200)
+    resp = app.get('/lingo/item/%s/%s/' % (remote_regie.id, encrypt_id))
+    assert 'Total amount: <span class="amount">123.45€</span>' in resp.text
+    assert 'Amount to pay: <span class="amount">100.00€</span>' in resp.text
+    assert 'Amount already paid: <span class="amount">23.45€</span>' in resp.text
+
+    # invoice without amount_paid
     mock_json = mock.Mock()
     mock_json.json.return_value = {'err': 0, 'data': INVOICES[0]}
     mock_get.return_value = mock_json
     mock_pay_invoice.return_value = mock.Mock(status_code=200)
     resp = app.get('/lingo/item/%s/%s/' % (remote_regie.id, encrypt_id))
+    assert 'Total amount: <span class="amount">123.45€</span>' in resp.text
+    assert 'Amount to pay: <span class="amount">123.45€</span>' in resp.text
+    assert 'Amount already paid>' not in resp.text
+
     form = resp.form
 
     assert 'email' in form.fields
-- 
2.20.1

