From 4e758fbba0e09d1dd94c73960be31a0d4c15b0d0 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Thu, 18 Feb 2021 17:57:59 +0100 Subject: [PATCH 1/4] Delete `balance_offset` field --- kfet/forms.py | 1 - ...4_remove_accountnegative_balance_offset.py | 17 ++++++++ kfet/models.py | 28 ++---------- kfet/views.py | 43 ++----------------- 4 files changed, 24 insertions(+), 65 deletions(-) create mode 100644 kfet/migrations/0074_remove_accountnegative_balance_offset.py diff --git a/kfet/forms.py b/kfet/forms.py index f93ff068..4dd8a9bc 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -150,7 +150,6 @@ class AccountNegativeForm(forms.ModelForm): fields = [ "authz_overdraft_amount", "authz_overdraft_until", - "balance_offset", "comment", ] widgets = {"authz_overdraft_until": DateTimeWidget()} diff --git a/kfet/migrations/0074_remove_accountnegative_balance_offset.py b/kfet/migrations/0074_remove_accountnegative_balance_offset.py new file mode 100644 index 00000000..818adfb1 --- /dev/null +++ b/kfet/migrations/0074_remove_accountnegative_balance_offset.py @@ -0,0 +1,17 @@ +# Generated by Django 2.2.17 on 2021-02-18 16:48 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("kfet", "0073_2021"), + ] + + operations = [ + migrations.RemoveField( + model_name="accountnegative", + name="balance_offset", + ), + ] diff --git a/kfet/models.py b/kfet/models.py index 622c0ac9..7156ae52 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -129,12 +129,6 @@ class Account(models.Model): def balance_ukf(self): return to_ukf(self.balance, is_cof=self.is_cof) - @property - def real_balance(self): - if hasattr(self, "negative") and self.negative.balance_offset: - return self.balance - self.negative.balance_offset - return self.balance - @property def name(self): return self.user.get_full_name() @@ -275,7 +269,7 @@ class Account(models.Model): self.password = hash_password(clear_password) def update_negative(self): - if self.real_balance < 0: + if self.balance < 0: if hasattr(self, "negative") and not self.negative.start: self.negative.start = timezone.now() self.negative.save() @@ -284,15 +278,8 @@ class Account(models.Model): account=self, start=timezone.now() ) elif hasattr(self, "negative"): - # self.real_balance >= 0 - balance_offset = self.negative.balance_offset - if balance_offset: - ( - Account.objects.filter(pk=self.pk).update( - balance=F("balance") - balance_offset - ) - ) - self.refresh_from_db() + # self.balance >= 0 + # TODO: méchanisme pour éviter de contourner le délai de négatif ? self.negative.delete() class UserHasAccount(Exception): @@ -318,15 +305,6 @@ class AccountNegative(models.Model): Account, on_delete=models.CASCADE, related_name="negative" ) start = models.DateTimeField(blank=True, null=True, default=None) - balance_offset = models.DecimalField( - "décalage de balance", - help_text="Montant non compris dans l'autorisation de négatif", - max_digits=6, - decimal_places=2, - blank=True, - null=True, - default=None, - ) authz_overdraft_amount = models.DecimalField( "négatif autorisé", max_digits=6, diff --git a/kfet/views.py b/kfet/views.py index 0fe99ea4..5322082c 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -396,29 +396,12 @@ def account_update(request, trigramme): if request.user.has_perm("kfet.manage_perms") and group_form.is_valid(): group_form.save() - # Checking perm to manage negative - if hasattr(account, "negative"): - balance_offset_old = 0 - if account.negative.balance_offset: - balance_offset_old = account.negative.balance_offset if ( hasattr(account, "negative") and request.user.has_perm("kfet.change_accountnegative") and negative_form.is_valid() ): - balance_offset_new = negative_form.cleaned_data["balance_offset"] - if not balance_offset_new: - balance_offset_new = 0 - balance_offset_diff = balance_offset_new - balance_offset_old - Account.objects.filter(pk=account.pk).update( - balance=F("balance") + balance_offset_diff - ) negative_form.save() - if ( - Account.objects.get(pk=account.pk).balance >= 0 - and not balance_offset_new - ): - AccountNegative.objects.get(account=account).delete() success = True messages.success( @@ -513,8 +496,8 @@ class AccountNegativeList(ListView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - real_balances = (neg.account.real_balance for neg in self.object_list) - context["negatives_sum"] = sum(real_balances) + balances = (neg.account.balance for neg in self.object_list) + context["negatives_sum"] = sum(balances) return context @@ -1716,16 +1699,7 @@ def perform_transfers(request): balance=F("balance") + to_accounts_balances[account] ) account.refresh_from_db() - if account.balance < 0: - if hasattr(account, "negative"): - if not account.negative.start: - account.negative.start = timezone.now() - account.negative.save() - else: - negative = AccountNegative(account=account, start=timezone.now()) - negative.save() - elif hasattr(account, "negative") and not account.negative.balance_offset: - account.negative.delete() + account.update_negative() # Saving transfer group transfergroup.save() @@ -1827,16 +1801,7 @@ def cancel_transfers(request): balance=F("balance") + to_accounts_balances[account] ) account.refresh_from_db() - if account.balance < 0: - if hasattr(account, "negative"): - if not account.negative.start: - account.negative.start = timezone.now() - account.negative.save() - else: - negative = AccountNegative(account=account, start=timezone.now()) - negative.save() - elif hasattr(account, "negative") and not account.negative.balance_offset: - account.negative.delete() + account.update_negative() transfers = ( Transfer.objects.values("id", "canceled_at", "canceled_by__trigramme") From a421bec62598c4dc56590e94d0d4a4ce83cd2fd7 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Thu, 18 Feb 2021 17:58:08 +0100 Subject: [PATCH 2/4] Fix templates --- kfet/templates/kfet/account_negative.html | 16 +++------------- kfet/templates/kfet/left_account.html | 22 ++++++++++------------ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/kfet/templates/kfet/account_negative.html b/kfet/templates/kfet/account_negative.html index fa8b508d..9ca9cd99 100644 --- a/kfet/templates/kfet/account_negative.html +++ b/kfet/templates/kfet/account_negative.html @@ -35,20 +35,16 @@ {% block main %}
- +
- - @@ -61,11 +57,6 @@ - @@ -73,11 +64,10 @@ - {% endfor %}
Tri. Nom BalanceRéelle Début Découvert autorisé Jusqu'auBalance offset
{{ neg.account.name }} {{ neg.account.balance|floatformat:2 }}€ - {% if neg.balance_offset %} - {{ neg.account.real_balance|floatformat:2 }}€ - {% endif %} - {{ neg.start|date:'d/m/Y H:i'}} {{ neg.authz_overdraft_until|date:'d/m/Y H:i' }} {{ neg.balance_offset|default_if_none:'' }}
-{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/kfet/templates/kfet/left_account.html b/kfet/templates/kfet/left_account.html index 716c96cc..a058abf9 100644 --- a/kfet/templates/kfet/left_account.html +++ b/kfet/templates/kfet/left_account.html @@ -39,7 +39,8 @@
  • {{ account.departement }} {{ account.promo }}
  • {% if account.is_cof %} - Adhérent COF + Adhérent COF {% else %} Non-COF {% endif %} @@ -54,9 +55,6 @@ {% if account.negative.start %}
  • Depuis le {{ account.negative.start|date:"d/m/Y à H:i" }}
  • {% endif %} - {% if account.real_balance != account.balance %} -
  • Solde réel: {{ account.real_balance }} €
  • - {% endif %}
  • Plafond : {{ account.negative.authz_overdraft_amount|default:kfet_config.overdraft_amount }} € @@ -89,20 +87,20 @@ {% endif %} + }); + \ No newline at end of file From 1cf6f6f3e71bd2b5a953b649c5e4089b15f31406 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 23 Feb 2021 22:41:04 +0100 Subject: [PATCH 3/4] Fix migration conflict --- ...t.py => 0075_remove_accountnegative_balance_offset.py} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename kfet/migrations/{0074_remove_accountnegative_balance_offset.py => 0075_remove_accountnegative_balance_offset.py} (50%) diff --git a/kfet/migrations/0074_remove_accountnegative_balance_offset.py b/kfet/migrations/0075_remove_accountnegative_balance_offset.py similarity index 50% rename from kfet/migrations/0074_remove_accountnegative_balance_offset.py rename to kfet/migrations/0075_remove_accountnegative_balance_offset.py index 818adfb1..3bf3134c 100644 --- a/kfet/migrations/0074_remove_accountnegative_balance_offset.py +++ b/kfet/migrations/0075_remove_accountnegative_balance_offset.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.17 on 2021-02-18 16:48 +# Generated by Django 2.2.17 on 2021-02-23 21:40 from django.db import migrations @@ -6,12 +6,12 @@ from django.db import migrations class Migration(migrations.Migration): dependencies = [ - ("kfet", "0073_2021"), + ('kfet', '0074_auto_20210219_1337'), ] operations = [ migrations.RemoveField( - model_name="accountnegative", - name="balance_offset", + model_name='accountnegative', + name='balance_offset', ), ] From 1ab071d16e9569c9338ac34bc4708a0babf27256 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 23 Feb 2021 22:52:27 +0100 Subject: [PATCH 4/4] LINT --- .../0075_remove_accountnegative_balance_offset.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kfet/migrations/0075_remove_accountnegative_balance_offset.py b/kfet/migrations/0075_remove_accountnegative_balance_offset.py index 3bf3134c..bf06e9ae 100644 --- a/kfet/migrations/0075_remove_accountnegative_balance_offset.py +++ b/kfet/migrations/0075_remove_accountnegative_balance_offset.py @@ -6,12 +6,12 @@ from django.db import migrations class Migration(migrations.Migration): dependencies = [ - ('kfet', '0074_auto_20210219_1337'), + ("kfet", "0074_auto_20210219_1337"), ] operations = [ migrations.RemoveField( - model_name='accountnegative', - name='balance_offset', + model_name="accountnegative", + name="balance_offset", ), ]