From 17f627c4c453a12d3655092626d6eeedc85330d6 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 5 Jul 2018 15:21:51 +0200 Subject: [PATCH] =?UTF-8?q?Plus=20de=20nom=20de=20paiement=20hardcod=C3=A9?= =?UTF-8?q?s=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotisations/forms.py | 3 +- .../migrations/0038_paiement_is_balance.py | 29 +++++++++++++++++++ .../migrations/0039_auto_20180704_1147.py | 21 ++++++++++++++ cotisations/models.py | 18 ++++++++++++ cotisations/payment_methods/balance/models.py | 11 +++++++ cotisations/payment_methods/forms.py | 23 +++++++++++---- .../templates/cotisations/facture.html | 3 ++ cotisations/views.py | 4 +-- preferences/models.py | 5 +--- users/models.py | 2 +- 10 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 cotisations/migrations/0038_paiement_is_balance.py create mode 100644 cotisations/migrations/0039_auto_20180704_1147.py 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 %}

{{title}}

{% endif %} diff --git a/cotisations/views.py b/cotisations/views.py index bb391d13..80f89ea1 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -409,7 +409,7 @@ def add_paiement(request): ) if payment.is_valid() and payment_method.is_valid(): payment = payment.save() - payment_method.save(payment=payment) + payment_method.save(payment) messages.success( request, _("The payment method has been successfully created.") @@ -688,7 +688,7 @@ def new_facture_solde(request, userid): """ user = request.user invoice = Facture(user=user) - payment, _created = Paiement.objects.get_or_create(moyen='Solde') + payment, _created = Paiement.objects.get_or_create(is_balance=True) invoice.paiement = payment # The template needs the list of articles (for the JS part) article_list = Article.objects.filter( diff --git a/preferences/models.py b/preferences/models.py index eeae30f9..4a22edbe 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -118,10 +118,7 @@ class OptionalUser(AclMixin, PreferencesModel): def clean(self): """Creation du mode de paiement par solde""" if self.user_solde: - p = cotisations.models.Paiement.objects.filter(moyen="Solde") - if not len(p): - c = cotisations.models.Paiement(moyen="Solde") - c.save() + cotisations.models.Paiement.objects.get_or_create(is_balance=True) @receiver(post_save, sender=OptionalUser) diff --git a/users/models.py b/users/models.py index 66dd0011..699c1abe 100644 --- a/users/models.py +++ b/users/models.py @@ -428,7 +428,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, def solde(self): """ Renvoie le solde d'un user. Somme les crédits de solde et retire les débit payés par solde""" - solde_objects = Paiement.objects.filter(moyen='solde') + solde_objects = Paiement.objects.filter(is_balance=True) somme_debit = Vente.objects.filter( facture__in=Facture.objects.filter( user=self,