Delete balance_offset field

This commit is contained in:
Ludovic Stephan 2021-02-18 17:57:59 +01:00
parent 2350109a33
commit 4e758fbba0
4 changed files with 24 additions and 65 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-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",
),
]

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

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