2018-07-07 22:01:06 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2018 Hugo Levy-Falk
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
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.
|
|
|
|
"""
|
2018-07-15 12:58:04 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _l("User Balance")
|
|
|
|
|
2018-07-03 14:22:38 +00:00
|
|
|
payment = models.OneToOneField(
|
|
|
|
Paiement,
|
2018-07-07 22:01:06 +00:00
|
|
|
on_delete=models.CASCADE,
|
2018-07-03 14:22:38 +00:00
|
|
|
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-05 13:23:10 +00:00
|
|
|
default=0,
|
2018-07-03 14:22:38 +00:00
|
|
|
)
|
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,
|
2018-07-13 15:52:49 +00:00
|
|
|
default=50,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
2018-07-03 17:30:31 +00:00
|
|
|
)
|
2018-07-16 18:14:26 +00:00
|
|
|
credit_balance_allowed = models.BooleanField(
|
|
|
|
verbose_name=_l("Allow user to credit their balance"),
|
|
|
|
default=False,
|
|
|
|
)
|
2018-07-03 14:22:38 +00:00
|
|
|
|
|
|
|
def end_payment(self, invoice, request):
|
2018-07-07 22:01:06 +00:00
|
|
|
"""Changes the user's balance to pay the invoice. If it is not
|
|
|
|
possible, shows an error and invalidates the invoice.
|
|
|
|
"""
|
2018-07-03 14:22:38 +00:00
|
|
|
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
|
|
|
|
)
|
2018-07-05 13:21:51 +00:00
|
|
|
|
|
|
|
def valid_form(self, form):
|
2018-07-07 22:01:06 +00:00
|
|
|
"""Checks that there is not already a balance payment method."""
|
2018-07-05 13:21:51 +00:00
|
|
|
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):
|
2018-07-07 22:01:06 +00:00
|
|
|
"""Register the payment as a balance payment."""
|
2018-07-05 13:21:51 +00:00
|
|
|
self.payment.is_balance = True
|
2018-07-13 18:19:57 +00:00
|
|
|
|
2018-07-13 20:03:22 +00:00
|
|
|
def check_price(self, price, user, *args, **kwargs):
|
|
|
|
"""Checks that the price meets the requirement to be paid with user
|
2018-07-13 18:19:57 +00:00
|
|
|
balance.
|
|
|
|
"""
|
2018-07-13 20:03:22 +00:00
|
|
|
return (
|
|
|
|
float(user.solde) - float(price) >= self.minimum_balance,
|
|
|
|
_("Your balance is too low for this operation.")
|
|
|
|
)
|
2018-07-16 18:14:26 +00:00
|
|
|
|
|
|
|
def can_credit_balance(self, user_request):
|
|
|
|
return (
|
|
|
|
len(Paiement.find_allowed_payments(user_request)
|
|
|
|
.exclude(is_balance=True)) > 0
|
|
|
|
) and self.credit_balance_allowed
|