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
|
2018-06-23 17:54:20 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-07-03 14:22:38 +00:00
|
|
|
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:
|
2018-06-23 17:54:20 +00:00
|
|
|
verbose_name = _("user balance")
|
2018-07-15 12:58:04 +00:00
|
|
|
|
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(
|
2019-03-21 15:40:53 +00:00
|
|
|
verbose_name=_("minimum balance"),
|
2018-06-23 17:54:20 +00:00
|
|
|
help_text=_("The minimal amount of money allowed for the balance"
|
2018-07-03 14:22:38 +00:00
|
|
|
" 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(
|
2019-03-21 15:40:53 +00:00
|
|
|
verbose_name=_("maximum balance"),
|
2018-06-23 17:54:20 +00:00
|
|
|
help_text=_("The maximal amount of money allowed for the balance."),
|
2018-07-03 17:30:31 +00:00
|
|
|
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(
|
2019-03-21 15:40:53 +00:00
|
|
|
verbose_name=_("allow user to credit their balance"),
|
2018-07-16 18:14:26 +00:00
|
|
|
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()
|
2018-10-02 00:22:04 +00:00
|
|
|
if user.solde - total_price < self.minimum_balance:
|
2018-07-03 14:22:38 +00:00
|
|
|
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',
|
2018-06-23 17:54:20 +00:00
|
|
|
_("There is already a payment method for user balance.")
|
2018-07-05 13:21:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 (
|
2018-10-02 00:22:04 +00:00
|
|
|
user.solde - price >= self.minimum_balance,
|
2018-07-13 20:03:22 +00:00
|
|
|
_("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
|
2018-06-23 17:54:20 +00:00
|
|
|
|