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
|
# Propriétés supplémentaires
|
||||||
@property
|
@property
|
||||||
def real_balance(self):
|
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 - self.negative.balance_offset
|
||||||
return self.balance
|
return self.balance
|
||||||
|
|
||||||
|
@ -210,6 +210,29 @@ class Account(models.Model):
|
||||||
def delete(self, *args, **kwargs):
|
def delete(self, *args, **kwargs):
|
||||||
pass
|
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):
|
class UserHasAccount(Exception):
|
||||||
def __init__(self, trigramme):
|
def __init__(self, trigramme):
|
||||||
self.trigramme = trigramme
|
self.trigramme = trigramme
|
||||||
|
|
|
@ -1105,22 +1105,15 @@ def kpsul_perform_operations(request):
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
# If not cash account,
|
# If not cash account,
|
||||||
# saving account's balance and adding to Negative if not in
|
# saving account's balance and adding to Negative if not in
|
||||||
if not operationgroup.on_acc.is_cash:
|
on_acc = operationgroup.on_acc
|
||||||
Account.objects.filter(pk=operationgroup.on_acc.pk).update(
|
if not on_acc.is_cash:
|
||||||
balance=F('balance') + operationgroup.amount)
|
(
|
||||||
operationgroup.on_acc.refresh_from_db()
|
Account.objects
|
||||||
if operationgroup.on_acc.balance < 0:
|
.filter(pk=on_acc.pk)
|
||||||
if hasattr(operationgroup.on_acc, 'negative'):
|
.update(balance=F('balance') + operationgroup.amount)
|
||||||
if not operationgroup.on_acc.negative.start:
|
)
|
||||||
operationgroup.on_acc.negative.start = timezone.now()
|
on_acc.refresh_from_db()
|
||||||
operationgroup.on_acc.negative.save()
|
on_acc.update_negative()
|
||||||
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()
|
|
||||||
|
|
||||||
# Updating checkout's balance
|
# Updating checkout's balance
|
||||||
if to_checkout_balance:
|
if to_checkout_balance:
|
||||||
|
@ -1311,8 +1304,15 @@ def kpsul_cancel_operations(request):
|
||||||
(Operation.objects.filter(pk__in=opes)
|
(Operation.objects.filter(pk__in=opes)
|
||||||
.update(canceled_by=canceled_by, canceled_at=canceled_at))
|
.update(canceled_by=canceled_by, canceled_at=canceled_at))
|
||||||
for account in to_accounts_balances:
|
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:
|
for checkout in to_checkouts_balances:
|
||||||
Checkout.objects.filter(pk=checkout.pk).update(
|
Checkout.objects.filter(pk=checkout.pk).update(
|
||||||
balance = F('balance') + to_checkouts_balances[checkout])
|
balance = F('balance') + to_checkouts_balances[checkout])
|
||||||
|
|
Loading…
Reference in a new issue