diff --git a/kfet/forms.py b/kfet/forms.py index e0d32102..4f91680f 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -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 # ----- diff --git a/kfet/models.py b/kfet/models.py index 628e5de6..887d3701 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -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): diff --git a/kfet/templates/kfet/account_negative.html b/kfet/templates/kfet/account_negative.html index 9ca9cd99..c2390f6d 100644 --- a/kfet/templates/kfet/account_negative.html +++ b/kfet/templates/kfet/account_negative.html @@ -10,26 +10,12 @@ {{ negatives|length }} compte{{ negatives|length|pluralize }} en négatif -
- Total: {{ negatives_sum|floatformat:2 }}€ -
-
- Plafond par défaut - +
+ {{ negatives_sum|floatformat:2 }}€ + de négatif total
-{% if perms.kfet.change_settings %} -
-
-
-
-{% endif %} - {% endblock %} {% block main %} @@ -43,8 +29,6 @@ Nom Balance Début - Découvert autorisé - Jusqu'au @@ -60,10 +44,6 @@ {{ neg.start|date:'d/m/Y H:i'}} - {{ neg.authz_overdraft_amount|default_if_none:'' }} - - {{ neg.authz_overdraft_until|date:'d/m/Y H:i' }} - {% endfor %} diff --git a/kfet/templates/kfet/account_update.html b/kfet/templates/kfet/account_update.html index 2bab6c1d..65965d83 100644 --- a/kfet/templates/kfet/account_update.html +++ b/kfet/templates/kfet/account_update.html @@ -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" %} - - {% endblock %} \ No newline at end of file diff --git a/kfet/views.py b/kfet/views.py index 83bf380a..76643809 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -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"