diff --git a/eo_gestion/eo_facture/models.py b/eo_gestion/eo_facture/models.py
index 142cb38..6c8c70a 100644
--- a/eo_gestion/eo_facture/models.py
+++ b/eo_gestion/eo_facture/models.py
@@ -406,6 +406,7 @@ class Facture(models.Model):
def cancel(self, creator):
assert not self.annulation, 'cannot cancel a canceled invoice'
+ assert not self.factures_avoir.count(), 'cannot cancel twice an invoice'
facture_avoir = Facture.objects.get(pk=self.pk)
facture_avoir.pk = None
diff --git a/eo_gestion/eo_facture/templates/admin/eo_facture/facture/change_form.html b/eo_gestion/eo_facture/templates/admin/eo_facture/facture/change_form.html
index c1a96c8..61c6e13 100644
--- a/eo_gestion/eo_facture/templates/admin/eo_facture/facture/change_form.html
+++ b/eo_gestion/eo_facture/templates/admin/eo_facture/facture/change_form.html
@@ -14,7 +14,7 @@
{% if original.client.chorus_structure and not original.proforma %}
Envoyer à Chorus
{% endif %}
- {% if not original.annulation %}
+ {% if not original.annulation and original.factures_avoir.count == 0 %}
Annuler
{% endif %}
diff --git a/tests/test_factures_avoir.py b/tests/test_factures_avoir.py
index 467db62..460b651 100644
--- a/tests/test_factures_avoir.py
+++ b/tests/test_factures_avoir.py
@@ -34,6 +34,8 @@ def test_limitations(db):
with pytest.raises(AssertionError, match='cannot cancel a canceled invoice'):
facture_avoir.cancel(creator)
+ with pytest.raises(AssertionError, match='cannot cancel twice an invoice'):
+ facture.cancel(creator)
def test_facture_avoir(app):
@@ -54,6 +56,11 @@ def test_facture_avoir(app):
assert 'Annuler' in [li.a.text for li in facture_0137_page.html.find_all('li')]
facture_avoir_page = facture_0137_page.click("Annuler")
facture_avoir_page = facture_avoir_page.follow()
+
+ # can't cancel invoice twice
+ facture_0137_page = factures_page.click('F20190137')
+ assert 'Annuler' not in [li.a.text for li in facture_0137_page.html.find_all('li')]
+
assert (
'Facture proforma du 2019-10-09' in facture_avoir_page.html.find('div', {'class': 'breadcrumbs'}).text
)
@@ -156,4 +163,3 @@ def test_facture_avoir(app):
],
],
]
-