forked from DGNum/gestioCOF
Fewer db requests with AccountNegative handling.
- AccountNegative use new AccountNegativeManager which select_related account, cofprofile and user for instances accessed via AccountNegative.objects. - Compute sum of negatives with python instead of an SQL statement (since we already got the data with a previous query). - Fix bug on account property `real_balance` (happened if an account has a relative AccountNegative instance but balance_offset to NULL). - More compliant to PEP8
This commit is contained in:
parent
f8b71b604c
commit
afdb08b424
2 changed files with 36 additions and 22 deletions
|
@ -96,7 +96,8 @@ 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 is not None):
|
||||||
return self.balance - self.negative.balance_offset
|
return self.balance - self.negative.balance_offset
|
||||||
return self.balance
|
return self.balance
|
||||||
|
|
||||||
|
@ -229,27 +230,43 @@ class Account(models.Model):
|
||||||
def __init__(self, trigramme):
|
def __init__(self, trigramme):
|
||||||
self.trigramme = trigramme
|
self.trigramme = trigramme
|
||||||
|
|
||||||
|
|
||||||
|
class AccountNegativeManager(models.Manager):
|
||||||
|
"""Manager for AccountNegative model."""
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
return (
|
||||||
|
super().get_queryset()
|
||||||
|
.select_related('account__cofprofile__user')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AccountNegative(models.Model):
|
class AccountNegative(models.Model):
|
||||||
|
objects = AccountNegativeManager()
|
||||||
|
|
||||||
account = models.OneToOneField(
|
account = models.OneToOneField(
|
||||||
Account, on_delete = models.PROTECT,
|
Account, on_delete=models.PROTECT,
|
||||||
related_name = "negative")
|
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(
|
balance_offset = models.DecimalField(
|
||||||
"décalage de balance",
|
"décalage de balance",
|
||||||
help_text="Montant non compris dans l'autorisation de négatif",
|
help_text="Montant non compris dans l'autorisation de négatif",
|
||||||
max_digits = 6, decimal_places = 2,
|
max_digits=6, decimal_places=2,
|
||||||
blank = True, null = True, default = None)
|
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, decimal_places = 2,
|
max_digits=6, decimal_places=2,
|
||||||
blank = True, null = True, default = None)
|
blank=True, null=True, default=None,
|
||||||
|
)
|
||||||
authz_overdraft_until = models.DateTimeField(
|
authz_overdraft_until = models.DateTimeField(
|
||||||
"expiration du négatif",
|
"expiration du négatif",
|
||||||
blank = True, null = True, default = None)
|
blank=True, null=True, default=None,
|
||||||
comment = models.CharField("commentaire", max_length = 255, blank = True)
|
)
|
||||||
|
comment = models.CharField("commentaire", max_length=255, blank=True)
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
|
||||||
class Checkout(models.Model):
|
class Checkout(models.Model):
|
||||||
created_by = models.ForeignKey(
|
created_by = models.ForeignKey(
|
||||||
Account, on_delete = models.PROTECT,
|
Account, on_delete = models.PROTECT,
|
||||||
|
|
|
@ -540,10 +540,13 @@ class AccountGroupUpdate(UpdateView):
|
||||||
success_message = 'Groupe modifié : %(name)s'
|
success_message = 'Groupe modifié : %(name)s'
|
||||||
success_url = reverse_lazy('kfet.account.group')
|
success_url = reverse_lazy('kfet.account.group')
|
||||||
|
|
||||||
|
|
||||||
class AccountNegativeList(ListView):
|
class AccountNegativeList(ListView):
|
||||||
queryset = (AccountNegative.objects
|
queryset = (
|
||||||
|
AccountNegative.objects
|
||||||
.select_related('account', 'account__cofprofile__user')
|
.select_related('account', 'account__cofprofile__user')
|
||||||
.exclude(account__trigramme='#13'))
|
.exclude(account__trigramme='#13')
|
||||||
|
)
|
||||||
template_name = 'kfet/account_negative.html'
|
template_name = 'kfet/account_negative.html'
|
||||||
context_object_name = 'negatives'
|
context_object_name = 'negatives'
|
||||||
|
|
||||||
|
@ -553,14 +556,8 @@ class AccountNegativeList(ListView):
|
||||||
'overdraft_amount': Settings.OVERDRAFT_AMOUNT(),
|
'overdraft_amount': Settings.OVERDRAFT_AMOUNT(),
|
||||||
'overdraft_duration': Settings.OVERDRAFT_DURATION(),
|
'overdraft_duration': Settings.OVERDRAFT_DURATION(),
|
||||||
}
|
}
|
||||||
negs_sum = (AccountNegative.objects
|
real_balances = (neg.account.real_balance for neg in self.object_list)
|
||||||
.exclude(account__trigramme='#13')
|
context['negatives_sum'] = sum(real_balances)
|
||||||
.aggregate(
|
|
||||||
bal = Coalesce(Sum('account__balance'),0),
|
|
||||||
offset = Coalesce(Sum('balance_offset'),0),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
context['negatives_sum'] = negs_sum['bal'] - negs_sum['offset']
|
|
||||||
return context
|
return context
|
||||||
|
|
||||||
# -----
|
# -----
|
||||||
|
|
Loading…
Reference in a new issue