2018-07-03 14:22:38 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.utils.translation import ugettext_lazy as _l
|
|
|
|
from django.contrib import messages
|
|
|
|
|
|
|
|
|
|
|
|
from cotisations.models import Paiement
|
|
|
|
from cotisations.payment_methods.mixins import PaymentMethodMixin
|
|
|
|
|
|
|
|
|
|
|
|
class BalancePayment(PaymentMethodMixin, models.Model):
|
|
|
|
"""
|
|
|
|
The model allowing you to pay with a cheque.
|
|
|
|
"""
|
|
|
|
payment = models.OneToOneField(
|
|
|
|
Paiement,
|
|
|
|
related_name='payment_method',
|
|
|
|
editable=False
|
|
|
|
)
|
|
|
|
minimum_balance = models.DecimalField(
|
|
|
|
verbose_name=_l("Minimum balance"),
|
|
|
|
help_text=_l("The minimal amount of money allowed for the balance"
|
|
|
|
" at the end of a payment. You can specify negative "
|
|
|
|
"amount."
|
|
|
|
),
|
|
|
|
max_digits=5,
|
|
|
|
decimal_places=2,
|
|
|
|
)
|
2018-07-03 17:30:31 +00:00
|
|
|
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
|
|
|
|
)
|
2018-07-03 14:22:38 +00:00
|
|
|
|
|
|
|
def end_payment(self, invoice, request):
|
|
|
|
user = invoice.user
|
|
|
|
total_price = invoice.prix_total()
|
|
|
|
if float(user.solde) - float(total_price) < self.minimum_balance:
|
|
|
|
invoice.valid = False
|
|
|
|
invoice.save()
|
|
|
|
messages.error(
|
|
|
|
request,
|
|
|
|
_("Your balance is too low for this operation.")
|
|
|
|
)
|
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': user.id}
|
|
|
|
))
|
|
|
|
return invoice.paiement.end_payment(
|
|
|
|
invoice,
|
|
|
|
request,
|
|
|
|
use_payment_method=False
|
|
|
|
)
|