forked from DGNum/gestioCOF
Nouveau fonctionnement des négatifs
This commit is contained in:
parent
8743301105
commit
ef8c1b8bf2
5 changed files with 30 additions and 113 deletions
|
@ -14,7 +14,6 @@ from djconfig.forms import ConfigForm
|
||||||
from gestioncof.models import CofProfile
|
from gestioncof.models import CofProfile
|
||||||
from kfet.models import (
|
from kfet.models import (
|
||||||
Account,
|
Account,
|
||||||
AccountNegative,
|
|
||||||
Article,
|
Article,
|
||||||
ArticleCategory,
|
ArticleCategory,
|
||||||
Checkout,
|
Checkout,
|
||||||
|
@ -158,17 +157,6 @@ class UserInfoForm(UserForm):
|
||||||
fields = ["first_name", "last_name"]
|
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
|
# Checkout forms
|
||||||
# -----
|
# -----
|
||||||
|
|
|
@ -170,41 +170,23 @@ class Account(models.Model):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def perms_to_perform_operation(self, amount):
|
def perms_to_perform_operation(self, amount):
|
||||||
overdraft_duration_max = kfet_config.overdraft_duration
|
|
||||||
overdraft_amount_max = kfet_config.overdraft_amount
|
|
||||||
perms = set()
|
perms = set()
|
||||||
stop_ope = False
|
|
||||||
# Checking is cash account
|
# Checking is cash account
|
||||||
if self.is_cash:
|
if self.is_cash:
|
||||||
# Yes, so no perms and no stop
|
# Yes, so no perms and no stop
|
||||||
return set(), False
|
return set(), False
|
||||||
|
|
||||||
if self.need_comment:
|
if self.need_comment:
|
||||||
perms.add("kfet.perform_commented_operations")
|
perms.add("kfet.perform_commented_operations")
|
||||||
|
|
||||||
new_balance = self.balance + amount
|
new_balance = self.balance + amount
|
||||||
|
if new_balance < -kfet_config.overdraft_amount:
|
||||||
|
return set(), True
|
||||||
|
|
||||||
if new_balance < 0 and amount < 0:
|
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")
|
perms.add("kfet.perform_negative_operations")
|
||||||
return perms, stop_ope
|
|
||||||
|
return perms, False
|
||||||
|
|
||||||
# Surcharge Méthode save() avec gestions de User et CofProfile
|
# Surcharge Méthode save() avec gestions de User et CofProfile
|
||||||
# Args:
|
# Args:
|
||||||
|
@ -267,17 +249,26 @@ class Account(models.Model):
|
||||||
|
|
||||||
def update_negative(self):
|
def update_negative(self):
|
||||||
if self.balance < 0:
|
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.start = timezone.now()
|
||||||
|
self.negative.end = None
|
||||||
self.negative.save()
|
self.negative.save()
|
||||||
elif not hasattr(self, "negative"):
|
elif not hasattr(self, "negative"):
|
||||||
self.negative = AccountNegative.objects.create(
|
self.negative = AccountNegative.objects.create(
|
||||||
account=self, start=timezone.now()
|
account=self, start=timezone.now()
|
||||||
)
|
)
|
||||||
elif hasattr(self, "negative"):
|
elif hasattr(self, "negative"):
|
||||||
# self.balance >= 0
|
if self.negative.end is None:
|
||||||
# TODO: méchanisme pour éviter de contourner le délai de négatif ?
|
self.negative.end = timezone.now()
|
||||||
self.negative.delete()
|
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):
|
class UserHasAccount(Exception):
|
||||||
def __init__(self, trigramme):
|
def __init__(self, trigramme):
|
||||||
|
@ -302,26 +293,11 @@ class AccountNegative(models.Model):
|
||||||
Account, on_delete=models.CASCADE, related_name="negative"
|
Account, on_delete=models.CASCADE, related_name="negative"
|
||||||
)
|
)
|
||||||
start = models.DateTimeField(blank=True, null=True, default=None)
|
start = models.DateTimeField(blank=True, null=True, default=None)
|
||||||
authz_overdraft_amount = models.DecimalField(
|
end = models.DateTimeField(blank=True, null=True, default=None)
|
||||||
"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)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
permissions = (("view_negs", "Voir la liste des négatifs"),)
|
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):
|
class CheckoutQuerySet(models.QuerySet):
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
|
|
|
@ -10,26 +10,12 @@
|
||||||
{{ negatives|length }}
|
{{ negatives|length }}
|
||||||
<span class="sub">compte{{ negatives|length|pluralize }} en négatif</span>
|
<span class="sub">compte{{ negatives|length|pluralize }} en négatif</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="text">
|
<div class="heading">
|
||||||
<b>Total:</b> {{ negatives_sum|floatformat:2 }}€
|
{{ negatives_sum|floatformat:2 }}€
|
||||||
</div>
|
<span class="sub">de négatif total</span>
|
||||||
<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>
|
</div>
|
||||||
</aside>
|
</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 %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block main %}
|
{% block main %}
|
||||||
|
@ -43,8 +29,6 @@
|
||||||
<td>Nom</td>
|
<td>Nom</td>
|
||||||
<td class="text-right">Balance</td>
|
<td class="text-right">Balance</td>
|
||||||
<td data-sorter="shortDate">Début</td>
|
<td data-sorter="shortDate">Début</td>
|
||||||
<td>Découvert autorisé</td>
|
|
||||||
<td data-sorter="shortDate">Jusqu'au</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -60,10 +44,6 @@
|
||||||
<td title="{{ neg.start }}">
|
<td title="{{ neg.start }}">
|
||||||
{{ neg.start|date:'d/m/Y H:i'}}
|
{{ neg.start|date:'d/m/Y H:i'}}
|
||||||
</td>
|
</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>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
|
@ -35,23 +35,9 @@ Modification de mes informations
|
||||||
{% include 'kfet/form_snippet.html' with form=frozen_form %}
|
{% 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=group_form %}
|
||||||
{% include 'kfet/form_snippet.html' with form=pwd_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' %}
|
{% include 'kfet/form_authentication_snippet.html' %}
|
||||||
{% endif %}
|
|
||||||
{% include 'kfet/form_submit_snippet.html' with value="Mettre à jour" %}
|
{% include 'kfet/form_submit_snippet.html' with value="Mettre à jour" %}
|
||||||
</form>
|
</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 %}
|
{% endblock %}
|
|
@ -39,7 +39,6 @@ from kfet.decorators import teamkfet_required
|
||||||
from kfet.forms import (
|
from kfet.forms import (
|
||||||
AccountForm,
|
AccountForm,
|
||||||
AccountFrozenForm,
|
AccountFrozenForm,
|
||||||
AccountNegativeForm,
|
|
||||||
AccountNoTriForm,
|
AccountNoTriForm,
|
||||||
AccountPwdForm,
|
AccountPwdForm,
|
||||||
AccountStatForm,
|
AccountStatForm,
|
||||||
|
@ -355,11 +354,6 @@ def account_update(request, trigramme):
|
||||||
frozen_form = AccountFrozenForm(request.POST, instance=account)
|
frozen_form = AccountFrozenForm(request.POST, instance=account)
|
||||||
pwd_form = AccountPwdForm()
|
pwd_form = AccountPwdForm()
|
||||||
|
|
||||||
if hasattr(account, "negative"):
|
|
||||||
negative_form = AccountNegativeForm(instance=account.negative)
|
|
||||||
else:
|
|
||||||
negative_form = None
|
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
self_update = request.user == account.user
|
self_update = request.user == account.user
|
||||||
account_form = AccountForm(request.POST, instance=account)
|
account_form = AccountForm(request.POST, instance=account)
|
||||||
|
@ -381,14 +375,6 @@ def account_update(request, trigramme):
|
||||||
elif group_form.has_changed():
|
elif group_form.has_changed():
|
||||||
warnings.append("statut d'équipe")
|
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
|
# Il ne faut pas valider `pwd_form` si elle est inchangée
|
||||||
if pwd_form.has_changed():
|
if pwd_form.has_changed():
|
||||||
if self_update or request.user.has_perm("kfet.change_account_password"):
|
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,
|
"account_form": account_form,
|
||||||
"frozen_form": frozen_form,
|
"frozen_form": frozen_form,
|
||||||
"group_form": group_form,
|
"group_form": group_form,
|
||||||
"negative_form": negative_form,
|
|
||||||
"pwd_form": pwd_form,
|
"pwd_form": pwd_form,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -482,9 +467,11 @@ class AccountDelete(PermissionRequiredMixin, DeleteView):
|
||||||
|
|
||||||
|
|
||||||
class AccountNegativeList(ListView):
|
class AccountNegativeList(ListView):
|
||||||
queryset = AccountNegative.objects.select_related(
|
queryset = (
|
||||||
"account", "account__cofprofile__user"
|
AccountNegative.objects.select_related("account", "account__cofprofile__user")
|
||||||
).exclude(account__trigramme="#13")
|
.filter(account__balance__lt=0)
|
||||||
|
.exclude(account__trigramme="#13")
|
||||||
|
)
|
||||||
template_name = "kfet/account_negative.html"
|
template_name = "kfet/account_negative.html"
|
||||||
context_object_name = "negatives"
|
context_object_name = "negatives"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue