forked from DGNum/gestioCOF
Merge branch 'aureplop/fix_negative' into 'master'
Check negative on cancellation. Closes #156 See merge request !223
This commit is contained in:
commit
55b509b3dd
2 changed files with 42 additions and 19 deletions
|
@ -81,7 +81,7 @@ class Account(models.Model):
|
|||
# Propriétés supplémentaires
|
||||
@property
|
||||
def real_balance(self):
|
||||
if (hasattr(self, 'negative')):
|
||||
if hasattr(self, 'negative') and self.negative.balance_offset:
|
||||
return self.balance - self.negative.balance_offset
|
||||
return self.balance
|
||||
|
||||
|
@ -210,6 +210,29 @@ class Account(models.Model):
|
|||
def delete(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def update_negative(self):
|
||||
if self.real_balance < 0:
|
||||
if hasattr(self, 'negative') and not self.negative.start:
|
||||
self.negative.start = timezone.now()
|
||||
self.negative.save()
|
||||
elif not hasattr(self, 'negative'):
|
||||
self.negative = (
|
||||
AccountNegative.objects.create(
|
||||
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.negative.delete()
|
||||
|
||||
class UserHasAccount(Exception):
|
||||
def __init__(self, trigramme):
|
||||
self.trigramme = trigramme
|
||||
|
|
|
@ -1105,22 +1105,15 @@ def kpsul_perform_operations(request):
|
|||
with transaction.atomic():
|
||||
# If not cash account,
|
||||
# saving account's balance and adding to Negative if not in
|
||||
if not operationgroup.on_acc.is_cash:
|
||||
Account.objects.filter(pk=operationgroup.on_acc.pk).update(
|
||||
balance=F('balance') + operationgroup.amount)
|
||||
operationgroup.on_acc.refresh_from_db()
|
||||
if operationgroup.on_acc.balance < 0:
|
||||
if hasattr(operationgroup.on_acc, 'negative'):
|
||||
if not operationgroup.on_acc.negative.start:
|
||||
operationgroup.on_acc.negative.start = timezone.now()
|
||||
operationgroup.on_acc.negative.save()
|
||||
else:
|
||||
negative = AccountNegative(
|
||||
account=operationgroup.on_acc, start=timezone.now())
|
||||
negative.save()
|
||||
elif (hasattr(operationgroup.on_acc, 'negative') and
|
||||
not operationgroup.on_acc.negative.balance_offset):
|
||||
operationgroup.on_acc.negative.delete()
|
||||
on_acc = operationgroup.on_acc
|
||||
if not on_acc.is_cash:
|
||||
(
|
||||
Account.objects
|
||||
.filter(pk=on_acc.pk)
|
||||
.update(balance=F('balance') + operationgroup.amount)
|
||||
)
|
||||
on_acc.refresh_from_db()
|
||||
on_acc.update_negative()
|
||||
|
||||
# Updating checkout's balance
|
||||
if to_checkout_balance:
|
||||
|
@ -1311,8 +1304,15 @@ def kpsul_cancel_operations(request):
|
|||
(Operation.objects.filter(pk__in=opes)
|
||||
.update(canceled_by=canceled_by, canceled_at=canceled_at))
|
||||
for account in to_accounts_balances:
|
||||
Account.objects.filter(pk=account.pk).update(
|
||||
balance = F('balance') + to_accounts_balances[account])
|
||||
(
|
||||
Account.objects
|
||||
.filter(pk=account.pk)
|
||||
.update(balance=F('balance') + to_accounts_balances[account])
|
||||
)
|
||||
if not account.is_cash:
|
||||
# Should always be true, but we want to be sure
|
||||
account.refresh_from_db()
|
||||
account.update_negative()
|
||||
for checkout in to_checkouts_balances:
|
||||
Checkout.objects.filter(pk=checkout.pk).update(
|
||||
balance = F('balance') + to_checkouts_balances[checkout])
|
||||
|
|
Loading…
Reference in a new issue