diff --git a/cotisations/forms.py b/cotisations/forms.py index 5d432849..19ab2fe7 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -284,7 +284,7 @@ class RechargeForm(FormRevMixin, Form): _("Select a payment method") self.fields['payment'].queryset = Paiement.find_allowed_payments(user) - def clean_value(self): + def clean(self): """ Returns a cleaned value from the received form by validating the value is well inside the possible limits @@ -292,18 +292,12 @@ class RechargeForm(FormRevMixin, Form): value = self.cleaned_data['value'] balance_method, _created = balance.PaymentMethod\ .objects.get_or_create() - if value < balance_method.minimum_balance: - raise forms.ValidationError( - _("Requested amount is too small. Minimum amount possible : \ - %(min_online_amount)s €.") % { - 'min_online_amount': balance_method.minimum_balance - } - ) - if value + self.user.solde > balance_method.maximum_balance: + if balance_method.maximum_balance is not None and \ + value + self.user.solde > balance_method.maximum_balance: raise forms.ValidationError( _("Requested amount is too high. Your balance can't exceed \ %(max_online_balance)s €.") % { 'max_online_balance': balance_method.maximum_balance } ) - return value + return self.cleaned_data diff --git a/cotisations/migrations/0030_custom_payment.py b/cotisations/migrations/0030_custom_payment.py index 1f54fc1d..bdd7cde9 100644 --- a/cotisations/migrations/0030_custom_payment.py +++ b/cotisations/migrations/0030_custom_payment.py @@ -112,7 +112,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('minimum_balance', models.DecimalField(decimal_places=2, default=0, help_text='The minimal amount of money allowed for the balance at the end of a payment. You can specify negative amount.', max_digits=5, verbose_name='Minimum balance')), ('payment', models.OneToOneField(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='payment_method', to='cotisations.Paiement')), - ('maximum_balance', models.DecimalField(decimal_places=2, default=50, help_text='The maximal amount of money allowed for the balance.', max_digits=5, verbose_name='Maximum balance')), + ('maximum_balance', models.DecimalField(decimal_places=2, default=50, help_text='The maximal amount of money allowed for the balance.', max_digits=5, verbose_name='Maximum balance', null=True, blank=True)), ], bases=(cotisations.payment_methods.mixins.PaymentMethodMixin, models.Model), ), diff --git a/cotisations/payment_methods/balance/models.py b/cotisations/payment_methods/balance/models.py index 4d2875d8..958906fa 100644 --- a/cotisations/payment_methods/balance/models.py +++ b/cotisations/payment_methods/balance/models.py @@ -55,7 +55,9 @@ class BalancePayment(PaymentMethodMixin, models.Model): help_text=_l("The maximal amount of money allowed for the balance."), max_digits=5, decimal_places=2, - default=50 + default=50, + blank=True, + null=True, ) def end_payment(self, invoice, request): diff --git a/cotisations/templates/cotisations/facture.html b/cotisations/templates/cotisations/facture.html index 7e4d6db0..01933277 100644 --- a/cotisations/templates/cotisations/facture.html +++ b/cotisations/templates/cotisations/facture.html @@ -39,6 +39,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {% else %}
{% trans "Current balance :" %} {{ balance }} € diff --git a/cotisations/views.py b/cotisations/views.py index 40e10e36..f561147b 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -32,7 +32,7 @@ from __future__ import unicode_literals import os from django.urls import reverse -from django.shortcuts import render, redirect +from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth.decorators import login_required from django.contrib import messages from django.db.models import ProtectedError @@ -696,9 +696,11 @@ def credit_solde(request, user, **_kwargs): number=1 ) return invoice.paiement.end_payment(invoice, request) + p = get_object_or_404(Paiement, is_balance=True) return form({ 'factureform': refill_form, 'balance': request.user.solde, 'title': _("Refill your balance"), - 'action_name': _("Pay") + 'action_name': _("Pay"), + 'max_balance': p.payment_method.maximum_balance, }, 'cotisations/facture.html', request)