mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2025-02-18 21:47:55 +00:00
Permet de vérifier que une facture peut être payée pourvalider le form.
This commit is contained in:
parent
557f2b43fb
commit
71663a9dcf
3 changed files with 42 additions and 51 deletions
|
@ -24,6 +24,7 @@ from django.urls import reverse
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.utils.translation import ugettext_lazy as _l
|
from django.utils.translation import ugettext_lazy as _l
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
from django.forms import ValidationError
|
||||||
|
|
||||||
|
|
||||||
from cotisations.models import Paiement
|
from cotisations.models import Paiement
|
||||||
|
@ -96,23 +97,11 @@ class BalancePayment(PaymentMethodMixin, models.Model):
|
||||||
"""Register the payment as a balance payment."""
|
"""Register the payment as a balance payment."""
|
||||||
self.payment.is_balance = True
|
self.payment.is_balance = True
|
||||||
|
|
||||||
def check_invoice(self, invoice_form):
|
def check_price(self, price, user, *args, **kwargs):
|
||||||
"""Checks that a invoice meets the requirement to be paid with user
|
"""Checks that the price meets the requirement to be paid with user
|
||||||
balance.
|
balance.
|
||||||
|
|
||||||
Args:
|
|
||||||
invoice_form: The invoice_form which is to be checked.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if the form is valid for this payment.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
user = invoice_form.instance.user
|
return (
|
||||||
total_price = invoice_form.instance.prix_total()
|
float(user.solde) - float(price) >= self.minimum_balance,
|
||||||
if float(user.solde) - float(total_price) < self.minimum_balance:
|
|
||||||
invoice_form.add_error(
|
|
||||||
'paiement',
|
|
||||||
_("Your balance is too low for this operation.")
|
_("Your balance is too low for this operation.")
|
||||||
)
|
)
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
|
@ -95,22 +95,10 @@ class ComnpayPayment(PaymentMethodMixin, models.Model):
|
||||||
}
|
}
|
||||||
return render(request, 'cotisations/payment.html', r)
|
return render(request, 'cotisations/payment.html', r)
|
||||||
|
|
||||||
def check_invoice(self, invoice_form):
|
def check_price(self, price, *args, **kwargs):
|
||||||
"""Checks that a invoice meets the requirement to be paid with ComNpay.
|
"""Checks that the price meets the requirement to be paid with ComNpay.
|
||||||
|
|
||||||
Args:
|
|
||||||
invoice_form: The invoice_form which is to be checked.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
True if the form is valid for ComNpay.
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if invoice_form.instance.prix_total() < self.minimum_payment:
|
return ((price >= self.minimum_payment),
|
||||||
invoice_form.add_error(
|
|
||||||
'paiement',
|
|
||||||
_('In order to pay your invoice with ComNpay'
|
_('In order to pay your invoice with ComNpay'
|
||||||
', the price must be grater than {} €')
|
', the price must be grater than {} €')
|
||||||
.format(self.minimum_payment)
|
.format(self.minimum_payment))
|
||||||
)
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ from .forms import (
|
||||||
)
|
)
|
||||||
from .tex import render_invoice
|
from .tex import render_invoice
|
||||||
from .payment_methods.forms import payment_method_factory
|
from .payment_methods.forms import payment_method_factory
|
||||||
from cotisations.utils import find_payment_method
|
from .utils import find_payment_method
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -121,11 +121,13 @@ def new_facture(request, user, userid):
|
||||||
new_invoice_instance.save()
|
new_invoice_instance.save()
|
||||||
# Building a purchase for each article sold
|
# Building a purchase for each article sold
|
||||||
purchases = []
|
purchases = []
|
||||||
|
total_price = 0
|
||||||
for art_item in articles:
|
for art_item in articles:
|
||||||
if art_item.cleaned_data:
|
if art_item.cleaned_data:
|
||||||
article = art_item.cleaned_data['article']
|
article = art_item.cleaned_data['article']
|
||||||
quantity = art_item.cleaned_data['quantity']
|
quantity = art_item.cleaned_data['quantity']
|
||||||
new_purchase = Vente.objects.create(
|
total_price += article.prix*quantity
|
||||||
|
new_purchase = Vente(
|
||||||
facture=new_invoice_instance,
|
facture=new_invoice_instance,
|
||||||
name=article.name,
|
name=article.name,
|
||||||
prix=article.prix,
|
prix=article.prix,
|
||||||
|
@ -135,17 +137,21 @@ def new_facture(request, user, userid):
|
||||||
)
|
)
|
||||||
purchases.append(new_purchase)
|
purchases.append(new_purchase)
|
||||||
p = find_payment_method(new_invoice_instance.paiement)
|
p = find_payment_method(new_invoice_instance.paiement)
|
||||||
if hasattr(p, 'check_invoice'):
|
if hasattr(p, 'check_price'):
|
||||||
accept = p.check_invoice(invoice_form)
|
price_ok, msg = p.check_price(total_price, user)
|
||||||
|
invoice_form.add_error(None, msg)
|
||||||
else:
|
else:
|
||||||
accept = True
|
price_ok = True
|
||||||
if accept:
|
if price_ok:
|
||||||
|
new_invoice_instance.save()
|
||||||
|
for p in purchases:
|
||||||
|
p.facture = new_invoice_instance
|
||||||
|
p.save()
|
||||||
|
|
||||||
return new_invoice_instance.paiement.end_payment(
|
return new_invoice_instance.paiement.end_payment(
|
||||||
new_invoice_instance,
|
new_invoice_instance,
|
||||||
request
|
request
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
new_invoice_instance.delete()
|
|
||||||
else:
|
else:
|
||||||
messages.error(
|
messages.error(
|
||||||
request,
|
request,
|
||||||
|
@ -693,8 +699,16 @@ def credit_solde(request, user, **_kwargs):
|
||||||
"""
|
"""
|
||||||
refill_form = RechargeForm(request.POST or None, user=request.user)
|
refill_form = RechargeForm(request.POST or None, user=request.user)
|
||||||
if refill_form.is_valid():
|
if refill_form.is_valid():
|
||||||
|
price = refill_form.cleaned_data['value']
|
||||||
invoice = Facture(user=user)
|
invoice = Facture(user=user)
|
||||||
invoice.paiement = refill_form.cleaned_data['payment']
|
invoice.paiement = refill_form.cleaned_data['payment']
|
||||||
|
p = find_payment_method(invoice.paiement)
|
||||||
|
if hasattr(p, 'check_price'):
|
||||||
|
price_ok, msg = p.check_price(price, user)
|
||||||
|
refill_form.add_error(None, msg)
|
||||||
|
else:
|
||||||
|
price_ok = True
|
||||||
|
if price_ok:
|
||||||
invoice.valid = True
|
invoice.valid = True
|
||||||
invoice.save()
|
invoice.save()
|
||||||
Vente.objects.create(
|
Vente.objects.create(
|
||||||
|
|
Loading…
Add table
Reference in a new issue