Commit 17f627c4 authored by Hugo LEVY-FALK's avatar Hugo LEVY-FALK

Plus de nom de paiement hardcodés !

parent 193dfb67
...@@ -91,8 +91,7 @@ class CreditSoldeForm(NewFactureForm): ...@@ -91,8 +91,7 @@ class CreditSoldeForm(NewFactureForm):
super(CreditSoldeForm, self).__init__(*args, **kwargs) super(CreditSoldeForm, self).__init__(*args, **kwargs)
# TODO : change solde to balance # TODO : change solde to balance
self.fields['paiement'].queryset = Paiement.objects.exclude( self.fields['paiement'].queryset = Paiement.objects.exclude(
moyen='solde' is_balance=True)
).exclude(moyen='Solde')
montant = forms.DecimalField(max_digits=5, decimal_places=2, required=True) montant = forms.DecimalField(max_digits=5, decimal_places=2, required=True)
......
# -*- 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)
]
# -*- 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'),
),
]
...@@ -594,6 +594,17 @@ class Banque(RevMixin, AclMixin, models.Model): ...@@ -594,6 +594,17 @@ class Banque(RevMixin, AclMixin, models.Model):
return self.name 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 # TODO : change Paiement to Payment
class Paiement(RevMixin, AclMixin, models.Model): class Paiement(RevMixin, AclMixin, models.Model):
""" """
...@@ -624,6 +635,13 @@ class Paiement(RevMixin, AclMixin, models.Model): ...@@ -624,6 +635,13 @@ class Paiement(RevMixin, AclMixin, models.Model):
default=False, default=False,
verbose_name=_l("Is available for every user") 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: class Meta:
permissions = ( permissions = (
......
...@@ -55,3 +55,14 @@ class BalancePayment(PaymentMethodMixin, models.Model): ...@@ -55,3 +55,14 @@ class BalancePayment(PaymentMethodMixin, models.Model):
request, request,
use_payment_method=False 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
...@@ -51,16 +51,27 @@ class PaymentMethodForm(forms.Form): ...@@ -51,16 +51,27 @@ class PaymentMethodForm(forms.Form):
else: else:
self.fields = {} self.fields = {}
def save(self, *args, payment=None, **kwargs): def clean(self):
commit = kwargs.pop('commit', True) super(PaymentMethodForm, self).clean()
choice = self.cleaned_data['payment_method'] choice = self.cleaned_data['payment_method']
if choice=='': if choice=='':
return return
choice = int(choice) choice = int(choice)
model = PAYMENT_METHODS[choice].PaymentMethod model = PAYMENT_METHODS[choice].PaymentMethod
form = forms.modelform_factory(model, fields='__all__')(self.data, prefix=self.prefix) form = forms.modelform_factory(model, fields='__all__')(self.data, prefix=self.prefix)
payment_method = form.save(commit=False) self.payment_method = form.save(commit=False)
payment_method.payment = payment 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: if commit:
payment_method.save() payment.save()
return payment_method self.payment_method.save()
return self.payment_method
...@@ -31,6 +31,9 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -31,6 +31,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% block content %} {% block content %}
{% bootstrap_form_errors factureform %} {% bootstrap_form_errors factureform %}
{% if payment_method %}
{% bootstrap_form_errors payment_method %}
{% endif %}
{% if title %} {% if title %}
<h3>{{title}}</h3> <h3>{{title}}</h3>
{% endif %} {% endif %}
......
...@@ -409,7 +409,7 @@ def add_paiement(request): ...@@ -409,7 +409,7 @@ def add_paiement(request):
) )
if payment.is_valid() and payment_method.is_valid(): if payment.is_valid() and payment_method.is_valid():
payment = payment.save() payment = payment.save()
payment_method.save(payment=payment) payment_method.save(payment)
messages.success( messages.success(
request, request,
_("The payment method has been successfully created.") _("The payment method has been successfully created.")
...@@ -688,7 +688,7 @@ def new_facture_solde(request, userid): ...@@ -688,7 +688,7 @@ def new_facture_solde(request, userid):
""" """
user = request.user user = request.user
invoice = Facture(user=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 invoice.paiement = payment
# The template needs the list of articles (for the JS part) # The template needs the list of articles (for the JS part)
article_list = Article.objects.filter( article_list = Article.objects.filter(
......
...@@ -118,10 +118,7 @@ class OptionalUser(AclMixin, PreferencesModel): ...@@ -118,10 +118,7 @@ class OptionalUser(AclMixin, PreferencesModel):
def clean(self): def clean(self):
"""Creation du mode de paiement par solde""" """Creation du mode de paiement par solde"""
if self.user_solde: if self.user_solde:
p = cotisations.models.Paiement.objects.filter(moyen="Solde") cotisations.models.Paiement.objects.get_or_create(is_balance=True)
if not len(p):
c = cotisations.models.Paiement(moyen="Solde")
c.save()
@receiver(post_save, sender=OptionalUser) @receiver(post_save, sender=OptionalUser)
......
...@@ -428,7 +428,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, ...@@ -428,7 +428,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
def solde(self): def solde(self):
""" Renvoie le solde d'un user. """ Renvoie le solde d'un user.
Somme les crédits de solde et retire les débit payés par solde""" 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( somme_debit = Vente.objects.filter(
facture__in=Facture.objects.filter( facture__in=Facture.objects.filter(
user=self, user=self,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment