Merge branch 'Aufinal/delete_balance_offset' into 'master'

Supprime le champ `balance_offset` et harmonise la gestion des négatifs

Closes #281

See merge request klub-dev-ens/gestioCOF!489
This commit is contained in:
Tom Hubrecht 2021-03-16 22:42:13 +01:00
commit 06005014f9
6 changed files with 37 additions and 90 deletions

View file

@ -150,7 +150,6 @@ class AccountNegativeForm(forms.ModelForm):
fields = [
"authz_overdraft_amount",
"authz_overdraft_until",
"balance_offset",
"comment",
]
widgets = {"authz_overdraft_until": DateTimeWidget()}

View file

@ -0,0 +1,17 @@
# Generated by Django 2.2.17 on 2021-02-23 21:40
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("kfet", "0074_auto_20210219_1337"),
]
operations = [
migrations.RemoveField(
model_name="accountnegative",
name="balance_offset",
),
]

View file

@ -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,

View file

@ -35,20 +35,16 @@
{% block main %}
<div class="table-responsive">
<table
class="table table-hover table-condensed sortable"
{# Initial sort: [(trigramme,asc)] #}
<table class="table table-hover table-condensed sortable" {# Initial sort: [(trigramme,asc)] #}
data-sortlist="[[0,0]]">
<thead>
<tr>
<td class="text-center">Tri.</td>
<td>Nom</td>
<td class="text-right">Balance</td>
<td class="text-right">Réelle</td>
<td data-sorter="shortDate">Début</td>
<td>Découvert autorisé</td>
<td data-sorter="shortDate">Jusqu'au</td>
<td>Balance offset</td>
</tr>
</thead>
<tbody>
@ -61,11 +57,6 @@
</td>
<td>{{ neg.account.name }}</td>
<td class="text-right">{{ neg.account.balance|floatformat:2 }}€</td>
<td class="text-right">
{% if neg.balance_offset %}
{{ neg.account.real_balance|floatformat:2 }}€
{% endif %}
</td>
<td title="{{ neg.start }}">
{{ neg.start|date:'d/m/Y H:i'}}
</td>
@ -73,7 +64,6 @@
<td title="{{ neg.authz_overdraft_until }}">
{{ neg.authz_overdraft_until|date:'d/m/Y H:i' }}
</td>
<td>{{ neg.balance_offset|default_if_none:'' }}</td>
</tr>
{% endfor %}
</tbody>

View file

@ -39,7 +39,8 @@
<li>{{ account.departement }} {{ account.promo }}</li>
<li>
{% if account.is_cof %}
<span title="Réduction de {{ kfet_config.reduction_cof }} % sur tes commandes" data-toggle="tooltip" data-placement="right">Adhérent COF</span>
<span title="Réduction de {{ kfet_config.reduction_cof }} % sur tes commandes" data-toggle="tooltip"
data-placement="right">Adhérent COF</span>
{% else %}
Non-COF
{% endif %}
@ -54,9 +55,6 @@
{% if account.negative.start %}
<li>Depuis le <b>{{ account.negative.start|date:"d/m/Y à H:i" }}</b></li>
{% endif %}
{% if account.real_balance != account.balance %}
<li>Solde réel: {{ account.real_balance }} €</li>
{% endif %}
<li>
Plafond :
<b>{{ account.negative.authz_overdraft_amount|default:kfet_config.overdraft_amount }} €</b>

View file

@ -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")