diff --git a/cotisations/forms.py b/cotisations/forms.py index b59eb6c3..5659d495 100644 --- a/cotisations/forms.py +++ b/cotisations/forms.py @@ -91,8 +91,7 @@ class CreditSoldeForm(NewFactureForm): super(CreditSoldeForm, self).__init__(*args, **kwargs) # TODO : change solde to balance self.fields['paiement'].queryset = Paiement.objects.exclude( - moyen='solde' - ).exclude(moyen='Solde') + is_balance=True) montant = forms.DecimalField(max_digits=5, decimal_places=2, required=True) diff --git a/cotisations/migrations/0038_paiement_is_balance.py b/cotisations/migrations/0038_paiement_is_balance.py new file mode 100644 index 00000000..bea02a17 --- /dev/null +++ b/cotisations/migrations/0038_paiement_is_balance.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-07-04 16:30 +from __future__ import unicode_literals + +from django.db import migrations, models + +def update_balance(apps, _): + Payment = apps.get_model('cotisations', 'Paiement') + try: + balance = Payment.objects.get(moyen="solde") + balance.is_balance = True + balance.save() + except Payment.DoesNotExist: + pass + +class Migration(migrations.Migration): + + dependencies = [ + ('cotisations', '0037_auto_20180703_1202'), + ] + + operations = [ + migrations.AddField( + model_name='paiement', + name='is_balance', + field=models.BooleanField(default=False, editable=False, help_text='There should be only one balance payment method.', verbose_name='Is user balance'), + ), + migrations.RunPython(update_balance) + ] diff --git a/cotisations/migrations/0039_auto_20180704_1147.py b/cotisations/migrations/0039_auto_20180704_1147.py new file mode 100644 index 00000000..3f982430 --- /dev/null +++ b/cotisations/migrations/0039_auto_20180704_1147.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-07-04 16:47 +from __future__ import unicode_literals + +import cotisations.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cotisations', '0038_paiement_is_balance'), + ] + + operations = [ + migrations.AlterField( + model_name='paiement', + name='is_balance', + field=models.BooleanField(default=False, editable=False, help_text='There should be only one balance payment method.', validators=[cotisations.models.check_no_balance], verbose_name='Is user balance'), + ), + ] diff --git a/cotisations/models.py b/cotisations/models.py index 58e0f2d3..d6280045 100644 --- a/cotisations/models.py +++ b/cotisations/models.py @@ -594,6 +594,17 @@ class Banque(RevMixin, AclMixin, models.Model): return self.name +def check_no_balance(): + """This functions checks that no Paiement with is_balance=True exists + + :raises ValidationError: if such a Paiement exists. + """ + p = Paiement.objects.filter(is_balance=True) + if len(p)>0: + raise ValidationError( + _("There are already payment method(s) for user balance") + ) + # TODO : change Paiement to Payment class Paiement(RevMixin, AclMixin, models.Model): """ @@ -624,6 +635,13 @@ class Paiement(RevMixin, AclMixin, models.Model): default=False, verbose_name=_l("Is available for every user") ) + is_balance = models.BooleanField( + default=False, + editable=False, + verbose_name=_l("Is user balance"), + help_text=_l("There should be only one balance payment method."), + validators=[check_no_balance] + ) class Meta: permissions = ( diff --git a/cotisations/payment_methods/balance/models.py b/cotisations/payment_methods/balance/models.py index 957244d7..ae4d2f0b 100644 --- a/cotisations/payment_methods/balance/models.py +++ b/cotisations/payment_methods/balance/models.py @@ -55,3 +55,14 @@ class BalancePayment(PaymentMethodMixin, models.Model): request, use_payment_method=False ) + + def valid_form(self, form): + p = Paiement.objects.filter(is_balance=True) + if len(p) > 0: + form.add_error( + 'payment_method', + _("There is already a payment type for user balance") + ) + + def alter_payment(self, payment): + self.payment.is_balance = True diff --git a/cotisations/payment_methods/forms.py b/cotisations/payment_methods/forms.py index 37653c3a..2b712439 100644 --- a/cotisations/payment_methods/forms.py +++ b/cotisations/payment_methods/forms.py @@ -51,16 +51,27 @@ class PaymentMethodForm(forms.Form): else: self.fields = {} - def save(self, *args, payment=None, **kwargs): - commit = kwargs.pop('commit', True) + def clean(self): + super(PaymentMethodForm, self).clean() choice = self.cleaned_data['payment_method'] if choice=='': return choice = int(choice) model = PAYMENT_METHODS[choice].PaymentMethod form = forms.modelform_factory(model, fields='__all__')(self.data, prefix=self.prefix) - payment_method = form.save(commit=False) - payment_method.payment = payment + self.payment_method = form.save(commit=False) + if hasattr(self.payment_method, 'valid_form'): + self.payment_method.valid_form(self) + return self.cleaned_data + + + + def save(self, payment, *args, **kwargs): + commit = kwargs.pop('commit', True) + self.payment_method.payment = payment + if hasattr(self.payment_method, 'alter_payment'): + self.payment_method.alter_payment(payment) if commit: - payment_method.save() - return payment_method + payment.save() + self.payment_method.save() + return self.payment_method diff --git a/cotisations/templates/cotisations/facture.html b/cotisations/templates/cotisations/facture.html index 03a4225b..081f3f51 100644 --- a/cotisations/templates/cotisations/facture.html +++ b/cotisations/templates/cotisations/facture.html @@ -31,6 +31,9 @@ with this program; if not, write to the Free Software Foundation, Inc., {% block content %} {% bootstrap_form_errors factureform %} +{% if payment_method %} +{% bootstrap_form_errors payment_method %} +{% endif %} {% if title %}