Nouveau fonctionnement des négatifs

This commit is contained in:
Ludovic Stephan 2021-02-28 01:59:43 +01:00
parent 8743301105
commit ef8c1b8bf2
5 changed files with 30 additions and 113 deletions

View file

@ -14,7 +14,6 @@ from djconfig.forms import ConfigForm
from gestioncof.models import CofProfile
from kfet.models import (
Account,
AccountNegative,
Article,
ArticleCategory,
Checkout,
@ -158,17 +157,6 @@ class UserInfoForm(UserForm):
fields = ["first_name", "last_name"]
class AccountNegativeForm(forms.ModelForm):
class Meta:
model = AccountNegative
fields = [
"authz_overdraft_amount",
"authz_overdraft_until",
"comment",
]
widgets = {"authz_overdraft_until": DateTimeWidget()}
# -----
# Checkout forms
# -----

View file

@ -170,41 +170,23 @@ class Account(models.Model):
return data
def perms_to_perform_operation(self, amount):
overdraft_duration_max = kfet_config.overdraft_duration
overdraft_amount_max = kfet_config.overdraft_amount
perms = set()
stop_ope = False
# Checking is cash account
if self.is_cash:
# Yes, so no perms and no stop
return set(), False
if self.need_comment:
perms.add("kfet.perform_commented_operations")
new_balance = self.balance + amount
if new_balance < -kfet_config.overdraft_amount:
return set(), True
if new_balance < 0 and amount < 0:
# Retrieving overdraft amount limit
if (
hasattr(self, "negative")
and self.negative.authz_overdraft_amount is not None
):
overdraft_amount = -self.negative.authz_overdraft_amount
else:
overdraft_amount = -overdraft_amount_max
# Retrieving overdraft datetime limit
if (
hasattr(self, "negative")
and self.negative.authz_overdraft_until is not None
):
overdraft_until = self.negative.authz_overdraft_until
elif hasattr(self, "negative"):
overdraft_until = self.negative.start + overdraft_duration_max
else:
overdraft_until = timezone.now() + overdraft_duration_max
# Checking it doesn't break 1 rule
if new_balance < overdraft_amount or timezone.now() > overdraft_until:
stop_ope = True
perms.add("kfet.perform_negative_operations")
return perms, stop_ope
return perms, False
# Surcharge Méthode save() avec gestions de User et CofProfile
# Args:
@ -267,17 +249,26 @@ class Account(models.Model):
def update_negative(self):
if self.balance < 0:
if hasattr(self, "negative") and not self.negative.start:
# On met à jour le début de négatif seulement si la fin du négatif précédent
# est "vieille"
if (
hasattr(self, "negative")
and self.negative.end is not None
and timezone.now() > self.negative.end + kfet_config.cancel_duration
):
self.negative.start = timezone.now()
self.negative.end = None
self.negative.save()
elif not hasattr(self, "negative"):
self.negative = AccountNegative.objects.create(
account=self, start=timezone.now()
)
elif hasattr(self, "negative"):
# self.balance >= 0
# TODO: méchanisme pour éviter de contourner le délai de négatif ?
self.negative.delete()
if self.negative.end is None:
self.negative.end = timezone.now()
elif timezone.now() > self.negative.end + kfet_config.cancel_duration:
# Idem: on supprime le négatif après une légère période
self.negative.delete()
class UserHasAccount(Exception):
def __init__(self, trigramme):
@ -302,26 +293,11 @@ class AccountNegative(models.Model):
Account, on_delete=models.CASCADE, related_name="negative"
)
start = models.DateTimeField(blank=True, null=True, default=None)
authz_overdraft_amount = models.DecimalField(
"négatif autorisé",
max_digits=6,
decimal_places=2,
blank=True,
null=True,
default=None,
)
authz_overdraft_until = models.DateTimeField(
"expiration du négatif", blank=True, null=True, default=None
)
comment = models.CharField("commentaire", max_length=255, blank=True)
end = models.DateTimeField(blank=True, null=True, default=None)
class Meta:
permissions = (("view_negs", "Voir la liste des négatifs"),)
@property
def until_default(self):
return self.start + kfet_config.overdraft_duration
class CheckoutQuerySet(models.QuerySet):
def is_valid(self):

View file

@ -10,26 +10,12 @@
{{ negatives|length }}
<span class="sub">compte{{ negatives|length|pluralize }} en négatif</span>
</div>
<div class="text">
<b>Total:</b> {{ negatives_sum|floatformat:2 }}€
</div>
<div class="text">
<b>Plafond par défaut</b>
<ul class="list-unstyled">
<li>Montant: {{ kfet_config.overdraft_amount }}€</li>
<li>Pendant: {{ kfet_config.overdraft_duration }}</li>
</ul>
<div class="heading">
{{ negatives_sum|floatformat:2 }}€
<span class="sub">de négatif total</span>
</div>
</aside>
{% if perms.kfet.change_settings %}
<div class="buttons">
<div class="full">
<button type="button" class="btn btn-primary" href="{% url 'kfet.settings' %}">Modifier les valeurs par défaut</a>
</div>
</div>
{% endif %}
{% endblock %}
{% block main %}
@ -43,8 +29,6 @@
<td>Nom</td>
<td class="text-right">Balance</td>
<td data-sorter="shortDate">Début</td>
<td>Découvert autorisé</td>
<td data-sorter="shortDate">Jusqu'au</td>
</tr>
</thead>
<tbody>
@ -60,10 +44,6 @@
<td title="{{ neg.start }}">
{{ neg.start|date:'d/m/Y H:i'}}
</td>
<td>{{ neg.authz_overdraft_amount|default_if_none:'' }}</td>
<td title="{{ neg.authz_overdraft_until }}">
{{ neg.authz_overdraft_until|date:'d/m/Y H:i' }}
</td>
</tr>
{% endfor %}
</tbody>

View file

@ -35,23 +35,9 @@ Modification de mes informations
{% include 'kfet/form_snippet.html' with form=frozen_form %}
{% include 'kfet/form_snippet.html' with form=group_form %}
{% include 'kfet/form_snippet.html' with form=pwd_form %}
{% include 'kfet/form_snippet.html' with form=negative_form %}
{% if perms.kfet.is_team %}
{% include 'kfet/form_authentication_snippet.html' %}
{% endif %}
{% include 'kfet/form_submit_snippet.html' with value="Mettre à jour" %}
</form>
<script type="text/javascript">
$(document).ready(function () {
$('#id_authz_overdraft_until').datetimepicker({
format: 'YYYY-MM-DD HH:mm',
stepping: 5,
locale: 'fr',
});
});
</script>
{% endblock %}

View file

@ -39,7 +39,6 @@ from kfet.decorators import teamkfet_required
from kfet.forms import (
AccountForm,
AccountFrozenForm,
AccountNegativeForm,
AccountNoTriForm,
AccountPwdForm,
AccountStatForm,
@ -355,11 +354,6 @@ def account_update(request, trigramme):
frozen_form = AccountFrozenForm(request.POST, instance=account)
pwd_form = AccountPwdForm()
if hasattr(account, "negative"):
negative_form = AccountNegativeForm(instance=account.negative)
else:
negative_form = None
if request.method == "POST":
self_update = request.user == account.user
account_form = AccountForm(request.POST, instance=account)
@ -381,14 +375,6 @@ def account_update(request, trigramme):
elif group_form.has_changed():
warnings.append("statut d'équipe")
if hasattr(account, "negative"):
negative_form = AccountNegativeForm(request.POST, instance=account.negative)
if request.user.has_perm("kfet.change_accountnegative"):
forms.append(negative_form)
elif negative_form.has_changed():
warnings.append("négatifs")
# Il ne faut pas valider `pwd_form` si elle est inchangée
if pwd_form.has_changed():
if self_update or request.user.has_perm("kfet.change_account_password"):
@ -437,7 +423,6 @@ def account_update(request, trigramme):
"account_form": account_form,
"frozen_form": frozen_form,
"group_form": group_form,
"negative_form": negative_form,
"pwd_form": pwd_form,
},
)
@ -482,9 +467,11 @@ class AccountDelete(PermissionRequiredMixin, DeleteView):
class AccountNegativeList(ListView):
queryset = AccountNegative.objects.select_related(
"account", "account__cofprofile__user"
).exclude(account__trigramme="#13")
queryset = (
AccountNegative.objects.select_related("account", "account__cofprofile__user")
.filter(account__balance__lt=0)
.exclude(account__trigramme="#13")
)
template_name = "kfet/account_negative.html"
context_object_name = "negatives"