Fix acc neg

This commit is contained in:
Aurélien Delobelle 2016-09-05 19:19:09 +02:00
parent cd64f20275
commit db4ae73dfd
2 changed files with 7 additions and 2 deletions

View file

@ -84,7 +84,7 @@ class Account(models.Model):
@property
def real_balance(self):
if (hasattr(self, 'negative')):
return self.balance + self.negative.balance_offset
return self.balance - self.negative.balance_offset
return self.balance
@property

View file

@ -354,6 +354,9 @@ def account_update(request, trigramme):
account_form = AccountForm(instance=account)
cof_form = CofRestrictForm(instance=account.cofprofile)
pwd_form = AccountPwdForm()
if account.balance < 0 and not hasattr(account, 'negative'):
AccountNegative.objects.create(account=account, start=timezone.now())
account.refresh_from_db()
if hasattr(account, 'negative'):
negative_form = AccountNegativeForm(instance=account.negative)
else:
@ -417,7 +420,7 @@ def account_update(request, trigramme):
balance_offset_new = negative_form.cleaned_data['balance_offset']
if not balance_offset_new:
balance_offset_new = 0
balance_offset_diff = balance_offset_old - balance_offset_new
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()
@ -430,7 +433,9 @@ def account_update(request, trigramme):
if request.user == account.user:
missing_perm = False
account.refresh_from_db()
user_form = UserRestrictForm(request.POST, instance=account.user)
account_form = AccountRestrictForm(request.POST, instance=account)
if user_form.is_valid() and account_form.is_valid():
user_form.save()