diff --git a/cotisations/forms.py b/cotisations/forms.py index 9d638221..b59eb6c3 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -42,10 +42,10 @@ from django.core.validators import MinValueValidator from django.utils.translation import ugettext as _ from django.utils.translation import ugettext_lazy as _l -from preferences.models import OptionalUser from re2o.field_permissions import FieldPermissionFormMixin from re2o.mixins import FormRevMixin from .models import Article, Paiement, Facture, Banque +from .payment_methods import balance class NewFactureForm(FormRevMixin, ModelForm): @@ -362,34 +362,41 @@ class RechargeForm(FormRevMixin, Form): min_value=0.01, validators=[] ) + payment = forms.ModelChoiceField( + queryset=Paiement.objects.none(), + label=_l("Payment method") + ) def __init__(self, *args, **kwargs): self.user = kwargs.pop('user') super(RechargeForm, self).__init__(*args, **kwargs) + self.fields['payment'].empty_label = \ + _("Select a payment method") + self.fields['payment'].queryset = Paiement.objects.filter( + pk__in=map(lambda x: x.pk, + Paiement.find_allowed_payments(self.user)) + ) def clean_value(self): """ - Returns a cleaned vlaue from the received form by validating + Returns a cleaned value from the received form by validating the value is well inside the possible limits """ value = self.cleaned_data['value'] - if value < OptionalUser.get_cached_value('min_online_payment'): + 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': OptionalUser.get_cached_value( - 'min_online_payment' - ) + 'min_online_amount': balance_method.minimum_balance } ) - if value + self.user.solde > \ - OptionalUser.get_cached_value('max_solde'): + if 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': OptionalUser.get_cached_value( - 'max_solde' - ) + 'max_online_balance': balance_method.maximum_balance } ) return value diff --git a/cotisations/migrations/0037_auto_20180703_1202.py b/cotisations/migrations/0037_auto_20180703_1202.py new file mode 100644 index 00000000..3860ef87 --- /dev/null +++ b/cotisations/migrations/0037_auto_20180703_1202.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-07-03 17:02 +from __future__ import unicode_literals + +from django.db import migrations, models + +def create_max_balance(apps, schema_editor): + OptionalUser = apps.get_model('preferences', 'OptionalUser') + Payment = apps.get_model('cotisations', 'Paiement') + + balance, _created = Payment.objects.get_or_create(moyen='solde') + options, _created = OptionalUser.objects.get_or_create() + + balance.maximum_balance = options.max_solde + balance.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('cotisations', '0036_auto_20180703_1056'), + ] + + operations = [ + migrations.AlterModelOptions( + name='paiement', + options={'permissions': (('view_paiement', "Can see a payement's details"), ('use_every_payment', 'Can use every payement')), 'verbose_name': 'Payment method', 'verbose_name_plural': 'Payment methods'}, + ), + migrations.AddField( + model_name='balancepayment', + name='maximum_balance', + field=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'), + ), + migrations.RunPython(create_max_balance) + ] diff --git a/cotisations/payment_methods/balance/models.py b/cotisations/payment_methods/balance/models.py index b2ff96cc..957244d7 100644 --- a/cotisations/payment_methods/balance/models.py +++ b/cotisations/payment_methods/balance/models.py @@ -28,6 +28,13 @@ class BalancePayment(PaymentMethodMixin, models.Model): max_digits=5, decimal_places=2, ) + maximum_balance = models.DecimalField( + verbose_name=_l("Maximum balance"), + help_text=_l("The maximal amount of money allowed for the balance."), + max_digits=5, + decimal_places=2, + default=50 + ) def end_payment(self, invoice, request): user = invoice.user diff --git a/cotisations/templates/cotisations/payment.html b/cotisations/templates/cotisations/payment.html index 57940485..577ca64f 100644 --- a/cotisations/templates/cotisations/payment.html +++ b/cotisations/templates/cotisations/payment.html @@ -37,7 +37,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
diff --git a/cotisations/views.py b/cotisations/views.py index 40b09303..dc906686 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -821,24 +821,11 @@ def recharge(request): """ View used to refill the balance by using online payment. """ - if AssoOption.get_cached_value('payment') == 'NONE': - messages.error( - request, - _("Online payment is disabled.") - ) - return redirect(reverse( - 'users:profil', - kwargs={'userid': request.user.id} - )) refill_form = RechargeForm(request.POST or None, user=request.user) if refill_form.is_valid(): invoice = Facture(user=request.user) - payment, _created = Paiement.objects.get_or_create( - moyen='Rechargement en ligne' - ) - invoice.paiement = payment + invoice.paiement = refill_form.cleaned_data['payment'] invoice.valid = False - invoice.save() purchase = Vente.objects.create( facture=invoice, name='solde', @@ -846,10 +833,8 @@ def recharge(request): number=1 ) purchase.save() - # content = online_payment.PAYMENT_SYSTEM[ - # AssoOption.get_cached_value('payment') - # ](invoice, request) - return render(request, 'cotisations/payment.html', content) + invoice.save() + return invoice.paiement.end_payment(invoice, request) return form({ 'rechargeform': refill_form, 'solde': request.user.solde diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html index 6acc633a..72491b68 100644 --- a/users/templates/users/profil.html +++ b/users/templates/users/profil.html @@ -166,7 +166,7 @@ non adhérent{% endif %} et votre connexion est {% if users.has_access %}