From 71663a9dcf6dc78d71c260c4e30c0334e7d2e9d7 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Fri, 13 Jul 2018 22:03:22 +0200 Subject: [PATCH] =?UTF-8?q?Permet=20de=20v=C3=A9rifier=20que=20une=20factu?= =?UTF-8?q?re=20peut=20=C3=AAtre=20pay=C3=A9e=20pourvalider=20le=20form.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/payment_methods/balance/models.py | 25 +++------- cotisations/payment_methods/comnpay/models.py | 20 ++------ cotisations/views.py | 48 ++++++++++++------- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/cotisations/payment_methods/balance/models.py b/cotisations/payment_methods/balance/models.py index b4ae7e40..4a80fea7 100644 --- a/cotisations/payment_methods/balance/models.py +++ b/cotisations/payment_methods/balance/models.py @@ -24,6 +24,7 @@ from django.urls import reverse from django.utils.translation import ugettext as _ from django.utils.translation import ugettext_lazy as _l from django.contrib import messages +from django.forms import ValidationError from cotisations.models import Paiement @@ -96,23 +97,11 @@ class BalancePayment(PaymentMethodMixin, models.Model): """Register the payment as a balance payment.""" self.payment.is_balance = True - def check_invoice(self, invoice_form): - """Checks that a invoice meets the requirement to be paid with user + def check_price(self, price, user, *args, **kwargs): + """Checks that the price meets the requirement to be paid with user 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 - total_price = invoice_form.instance.prix_total() - if float(user.solde) - float(total_price) < self.minimum_balance: - invoice_form.add_error( - 'paiement', - _("Your balance is too low for this operation.") - ) - return False - return True + return ( + float(user.solde) - float(price) >= self.minimum_balance, + _("Your balance is too low for this operation.") + ) diff --git a/cotisations/payment_methods/comnpay/models.py b/cotisations/payment_methods/comnpay/models.py index 4dfe62de..0801df3b 100644 --- a/cotisations/payment_methods/comnpay/models.py +++ b/cotisations/payment_methods/comnpay/models.py @@ -95,22 +95,10 @@ class ComnpayPayment(PaymentMethodMixin, models.Model): } return render(request, 'cotisations/payment.html', r) - def check_invoice(self, invoice_form): - """Checks that a invoice 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. - + def check_price(self, price, *args, **kwargs): + """Checks that the price meets the requirement to be paid with ComNpay. """ - if invoice_form.instance.prix_total() < self.minimum_payment: - invoice_form.add_error( - 'paiement', + return ((price >= self.minimum_payment), _('In order to pay your invoice with ComNpay' ', the price must be grater than {} €') - .format(self.minimum_payment) - ) - return False - return True + .format(self.minimum_payment)) diff --git a/cotisations/views.py b/cotisations/views.py index 2d7de6c6..cb2f5f15 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -74,7 +74,7 @@ from .forms import ( ) from .tex import render_invoice from .payment_methods.forms import payment_method_factory -from cotisations.utils import find_payment_method +from .utils import find_payment_method @login_required @@ -121,11 +121,13 @@ def new_facture(request, user, userid): new_invoice_instance.save() # Building a purchase for each article sold purchases = [] + total_price = 0 for art_item in articles: if art_item.cleaned_data: article = art_item.cleaned_data['article'] quantity = art_item.cleaned_data['quantity'] - new_purchase = Vente.objects.create( + total_price += article.prix*quantity + new_purchase = Vente( facture=new_invoice_instance, name=article.name, prix=article.prix, @@ -135,17 +137,21 @@ def new_facture(request, user, userid): ) purchases.append(new_purchase) p = find_payment_method(new_invoice_instance.paiement) - if hasattr(p, 'check_invoice'): - accept = p.check_invoice(invoice_form) + if hasattr(p, 'check_price'): + price_ok, msg = p.check_price(total_price, user) + invoice_form.add_error(None, msg) else: - accept = True - if accept: + price_ok = True + 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( new_invoice_instance, request ) - else: - new_invoice_instance.delete() else: messages.error( request, @@ -693,17 +699,25 @@ def credit_solde(request, user, **_kwargs): """ refill_form = RechargeForm(request.POST or None, user=request.user) if refill_form.is_valid(): + price = refill_form.cleaned_data['value'] invoice = Facture(user=user) invoice.paiement = refill_form.cleaned_data['payment'] - invoice.valid = True - invoice.save() - Vente.objects.create( - facture=invoice, - name='solde', - prix=refill_form.cleaned_data['value'], - number=1 - ) - return invoice.paiement.end_payment(invoice, request) + 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.save() + Vente.objects.create( + facture=invoice, + name='solde', + prix=refill_form.cleaned_data['value'], + number=1 + ) + return invoice.paiement.end_payment(invoice, request) p = get_object_or_404(Paiement, is_balance=True) return form({ 'factureform': refill_form,