mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-23 07:23:46 +00:00
Plus de nom de paiement hardcodés !
This commit is contained in:
parent
193dfb67d1
commit
17f627c4c4
10 changed files with 104 additions and 15 deletions
|
@ -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)
|
||||
|
||||
|
|
29
cotisations/migrations/0038_paiement_is_balance.py
Normal file
29
cotisations/migrations/0038_paiement_is_balance.py
Normal file
|
@ -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)
|
||||
]
|
21
cotisations/migrations/0039_auto_20180704_1147.py
Normal file
21
cotisations/migrations/0039_auto_20180704_1147.py
Normal file
|
@ -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'),
|
||||
),
|
||||
]
|
|
@ -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 = (
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 %}
|
||||
<h3>{{title}}</h3>
|
||||
{% endif %}
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue