eo_facture: move cancelation code into models (#61117)
This commit is contained in:
parent
a0b3a7ffe9
commit
3b892e1d1e
|
@ -404,6 +404,29 @@ class Facture(models.Model):
|
|||
self.pdf(template_name=template_name, base_uri=base_uri), facturx_ctx
|
||||
)
|
||||
|
||||
def cancel(self, creator):
|
||||
assert not self.annulation, 'cannot cancel a canceled invoice'
|
||||
|
||||
facture_avoir = Facture.objects.get(pk=self.pk)
|
||||
facture_avoir.pk = None
|
||||
facture_avoir.ordre = None
|
||||
facture_avoir.annulation = self
|
||||
facture_avoir.proforma = True
|
||||
facture_avoir.intitule = "AVOIR POUR LA FACTURE " + self.intitule
|
||||
facture_avoir.paid = False
|
||||
facture_avoir.creator = creator
|
||||
facture_avoir.account_on_previous_period = False
|
||||
facture_avoir.save()
|
||||
for ligne in self.lignes.all():
|
||||
ligne.pk = None
|
||||
ligne.facture = facture_avoir
|
||||
ligne.quantite = ligne.quantite * -1
|
||||
ligne.save()
|
||||
for payment in self.payments.all():
|
||||
payment.pk = None
|
||||
payment.save()
|
||||
return facture_avoir
|
||||
|
||||
class Meta:
|
||||
ordering = ("-id",)
|
||||
|
||||
|
|
|
@ -118,22 +118,5 @@ def send_to_chorus(request, facture_id):
|
|||
|
||||
def cancel(request, facture_id):
|
||||
facture = get_object_or_404(Facture, id=facture_id)
|
||||
facture_avoir = get_object_or_404(Facture, id=facture_id)
|
||||
facture_avoir.pk = None
|
||||
facture_avoir.ordre = None
|
||||
facture_avoir.annulation = facture
|
||||
facture_avoir.proforma = True
|
||||
facture_avoir.intitule = "AVOIR POUR LA FACTURE " + facture.intitule
|
||||
facture_avoir.paid = False
|
||||
facture_avoir.creator = request.user
|
||||
facture_avoir.account_on_previous_period = False
|
||||
facture_avoir.save()
|
||||
for ligne in facture.lignes.all():
|
||||
ligne.pk = None
|
||||
ligne.facture = facture_avoir
|
||||
ligne.quantite = ligne.quantite * -1
|
||||
ligne.save()
|
||||
for payment in facture.payments.all():
|
||||
payment.pk = None
|
||||
payment.save()
|
||||
facture_avoir = facture.cancel(request.user)
|
||||
return redirect('admin:eo_facture_facture_change', facture_avoir.id)
|
||||
|
|
|
@ -18,10 +18,24 @@ import io
|
|||
import xml.etree.ElementTree as ET
|
||||
|
||||
import facturx
|
||||
import pytest
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from eo_gestion.eo_facture.models import Facture
|
||||
|
||||
|
||||
def test_limitations(db):
|
||||
facture = Facture.objects.get(
|
||||
client__nom='c3f42bb0d75d379', contrat__intitule='1da9dc528d5c7191bc87c7', ordre=137
|
||||
)
|
||||
User = get_user_model()
|
||||
creator = User.objects.get(username='admin')
|
||||
facture_avoir = facture.cancel(creator)
|
||||
|
||||
with pytest.raises(AssertionError, match='cannot cancel a canceled invoice'):
|
||||
facture_avoir.cancel(creator)
|
||||
|
||||
|
||||
def test_facture_avoir(app):
|
||||
response = app.get("/eo_facture/facture/").follow()
|
||||
|
||||
|
@ -38,7 +52,6 @@ def test_facture_avoir(app):
|
|||
facture = [x for x in Facture.objects.all() if x.code() == 'F20190137'][0]
|
||||
assert facture.factures_avoir.count() == 0
|
||||
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()
|
||||
assert (
|
||||
|
@ -48,6 +61,7 @@ def test_facture_avoir(app):
|
|||
facture_avoir_page.html.find('input', {'id': 'id_intitule'})['value']
|
||||
== 'AVOIR POUR LA FACTURE 95e0eb4429394cd2'
|
||||
)
|
||||
# can't cancel a canceled invoice
|
||||
assert 'Annuler' not in [li.a.text for li in facture_avoir_page.html.find_all('li')]
|
||||
assert facture.factures_avoir.count() == 1
|
||||
facture_avoir = facture.factures_avoir.first()
|
||||
|
@ -142,3 +156,4 @@ def test_facture_avoir(app):
|
|||
],
|
||||
],
|
||||
]
|
||||
|
||||
|
|
Loading…
Reference in New Issue