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 = [ fields = [
"authz_overdraft_amount", "authz_overdraft_amount",
"authz_overdraft_until", "authz_overdraft_until",
"balance_offset",
"comment", "comment",
] ]
widgets = {"authz_overdraft_until": DateTimeWidget()} 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): def balance_ukf(self):
return to_ukf(self.balance, is_cof=self.is_cof) 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 @property
def name(self): def name(self):
return self.user.get_full_name() return self.user.get_full_name()
@ -275,7 +269,7 @@ class Account(models.Model):
self.password = hash_password(clear_password) self.password = hash_password(clear_password)
def update_negative(self): def update_negative(self):
if self.real_balance < 0: if self.balance < 0:
if hasattr(self, "negative") and not self.negative.start: if hasattr(self, "negative") and not self.negative.start:
self.negative.start = timezone.now() self.negative.start = timezone.now()
self.negative.save() self.negative.save()
@ -284,15 +278,8 @@ class Account(models.Model):
account=self, start=timezone.now() account=self, start=timezone.now()
) )
elif hasattr(self, "negative"): elif hasattr(self, "negative"):
# self.real_balance >= 0 # self.balance >= 0
balance_offset = self.negative.balance_offset # TODO: méchanisme pour éviter de contourner le délai de négatif ?
if balance_offset:
(
Account.objects.filter(pk=self.pk).update(
balance=F("balance") - balance_offset
)
)
self.refresh_from_db()
self.negative.delete() self.negative.delete()
class UserHasAccount(Exception): class UserHasAccount(Exception):
@ -318,15 +305,6 @@ 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)
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( authz_overdraft_amount = models.DecimalField(
"négatif autorisé", "négatif autorisé",
max_digits=6, max_digits=6,

View file

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

View file

@ -39,7 +39,8 @@
<li>{{ account.departement }} {{ account.promo }}</li> <li>{{ account.departement }} {{ account.promo }}</li>
<li> <li>
{% if account.is_cof %} {% 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 %} {% else %}
Non-COF Non-COF
{% endif %} {% endif %}
@ -54,9 +55,6 @@
{% if account.negative.start %} {% if account.negative.start %}
<li>Depuis le <b>{{ account.negative.start|date:"d/m/Y à H:i" }}</b></li> <li>Depuis le <b>{{ account.negative.start|date:"d/m/Y à H:i" }}</b></li>
{% endif %} {% endif %}
{% if account.real_balance != account.balance %}
<li>Solde réel: {{ account.real_balance }} €</li>
{% endif %}
<li> <li>
Plafond : Plafond :
<b>{{ account.negative.authz_overdraft_amount|default:kfet_config.overdraft_amount }} €</b> <b>{{ account.negative.authz_overdraft_amount|default:kfet_config.overdraft_amount }} €</b>
@ -89,20 +87,20 @@
{% endif %} {% endif %}
<script type="text/javascript"> <script type="text/javascript">
$( function() { $(function () {
// Tooltips // Tooltips
$('[data-toggle="tooltip"]').tooltip(); $('[data-toggle="tooltip"]').tooltip();
// Opened tab button // Opened tab button
let tabs_buttons = $('.tabs-buttons a'); let tabs_buttons = $('.tabs-buttons a');
tabs_buttons.click( function() { tabs_buttons.click(function () {
tabs_buttons.removeClass('focus'); tabs_buttons.removeClass('focus');
$(this).addClass('focus'); $(this).addClass('focus');
}); });
// Delete button // Delete button
$('#button-delete').click(function() { $('#button-delete').click(function () {
$.confirm({ $.confirm({
title: 'Confirmer la suppression', title: 'Confirmer la suppression',
content: ` content: `
@ -115,10 +113,10 @@ $( function() {
animation: 'top', animation: 'top',
closeAnimation: 'bottom', closeAnimation: 'bottom',
keyboardEnabled: true, keyboardEnabled: true,
confirm: function() { confirm: function () {
$('#account-delete-form').submit(); $('#account-delete-form').submit();
} }
}) })
}) })
}); });
</script> </script>

View file

@ -396,29 +396,12 @@ def account_update(request, trigramme):
if request.user.has_perm("kfet.manage_perms") and group_form.is_valid(): if request.user.has_perm("kfet.manage_perms") and group_form.is_valid():
group_form.save() 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 ( if (
hasattr(account, "negative") hasattr(account, "negative")
and request.user.has_perm("kfet.change_accountnegative") and request.user.has_perm("kfet.change_accountnegative")
and negative_form.is_valid() 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() 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 success = True
messages.success( messages.success(
@ -513,8 +496,8 @@ class AccountNegativeList(ListView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
real_balances = (neg.account.real_balance for neg in self.object_list) balances = (neg.account.balance for neg in self.object_list)
context["negatives_sum"] = sum(real_balances) context["negatives_sum"] = sum(balances)
return context return context
@ -1716,16 +1699,7 @@ def perform_transfers(request):
balance=F("balance") + to_accounts_balances[account] balance=F("balance") + to_accounts_balances[account]
) )
account.refresh_from_db() account.refresh_from_db()
if account.balance < 0: account.update_negative()
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()
# Saving transfer group # Saving transfer group
transfergroup.save() transfergroup.save()
@ -1827,16 +1801,7 @@ def cancel_transfers(request):
balance=F("balance") + to_accounts_balances[account] balance=F("balance") + to_accounts_balances[account]
) )
account.refresh_from_db() account.refresh_from_db()
if account.balance < 0: account.update_negative()
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()
transfers = ( transfers = (
Transfer.objects.values("id", "canceled_at", "canceled_by__trigramme") Transfer.objects.values("id", "canceled_at", "canceled_by__trigramme")