Fixes EcoAccount availability ordering
* adds db based table ordering for EcoAccountTable
This commit is contained in:
@@ -35,6 +35,12 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
|
||||
help_text="Amount of deductable surface - can be lower than the total surface due to deduction limitations",
|
||||
default=0,
|
||||
)
|
||||
deductable_rest = models.FloatField(
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text="Amount of deductable rest",
|
||||
default=0,
|
||||
)
|
||||
|
||||
legal = models.OneToOneField(
|
||||
"intervention.Legal",
|
||||
@@ -100,28 +106,22 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
|
||||
"""
|
||||
return self.after_states.all().aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
|
||||
def get_available_rest(self) -> (float, float):
|
||||
def __calculate_deductable_rest(self):
|
||||
""" Calculates available rest surface of the eco account
|
||||
|
||||
Args:
|
||||
|
||||
Returns:
|
||||
ret_val_total (float): Total amount
|
||||
ret_val_relative (float): Amount as percentage (0-100)
|
||||
"""
|
||||
deductions = self.deductions.filter(
|
||||
intervention__deleted=None,
|
||||
)
|
||||
deductions_surfaces = deductions.aggregate(Sum("surface"))["surface__sum"] or 0
|
||||
available_surfaces = self.deductable_surface or deductions_surfaces ## no division by zero
|
||||
ret_val_total = available_surfaces - deductions_surfaces
|
||||
ret_val = available_surfaces - deductions_surfaces
|
||||
|
||||
if available_surfaces > 0:
|
||||
ret_val_relative = int((ret_val_total / available_surfaces) * 100)
|
||||
else:
|
||||
ret_val_relative = 0
|
||||
|
||||
return ret_val_total, ret_val_relative
|
||||
return ret_val
|
||||
|
||||
def quality_check(self) -> EcoAccountQualityChecker:
|
||||
""" Quality check
|
||||
@@ -181,6 +181,29 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
|
||||
for team_id in shared_teams:
|
||||
celery_send_mail_deduction_changed_team.delay(self.identifier, self.title, team_id, data_change)
|
||||
|
||||
def update_deductable_rest(self):
|
||||
"""
|
||||
Updates deductable_rest, which holds the amount of rest surface for this account.
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.deductable_rest = self.__calculate_deductable_rest()
|
||||
self.save()
|
||||
|
||||
def get_deductable_rest_relative(self):
|
||||
"""
|
||||
Returns deductable_rest relative to deductable_surface mapped to [0,100]
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
try:
|
||||
ret_val = int((self.deductable_rest / (self.deductable_surface or 0)) * 100)
|
||||
except ZeroDivisionError:
|
||||
ret_val = 0
|
||||
return ret_val
|
||||
|
||||
|
||||
class EcoAccountDocument(AbstractDocument):
|
||||
"""
|
||||
@@ -272,3 +295,8 @@ class EcoAccountDeduction(BaseResource):
|
||||
self.intervention.mark_as_edited(user, edit_comment=DEDUCTION_REMOVED)
|
||||
self.account.mark_as_edited(user, edit_comment=DEDUCTION_REMOVED)
|
||||
super().delete(*args, **kwargs)
|
||||
self.account.update_deductable_rest()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
super().save(*args, **kwargs)
|
||||
self.account.update_deductable_rest()
|
||||
|
||||
Reference in New Issue
Block a user