Withdraw to deduct

* refactors Models and attributes
pull/16/head
mipel 3 years ago
parent cd5fb9cad6
commit d569be80b3

@ -1,7 +1,7 @@
from django.contrib import admin from django.contrib import admin
from compensation.models import Compensation, CompensationAction, CompensationState, Payment, \ from compensation.models import Compensation, CompensationAction, CompensationState, Payment, \
EcoAccountWithdraw, EcoAccount EcoAccountDeduction, EcoAccount
class CompensationStateAdmin(admin.ModelAdmin): class CompensationStateAdmin(admin.ModelAdmin):
@ -61,4 +61,4 @@ admin.site.register(Payment, PaymentAdmin)
admin.site.register(CompensationAction, CompensationActionAdmin) admin.site.register(CompensationAction, CompensationActionAdmin)
admin.site.register(CompensationState, CompensationStateAdmin) admin.site.register(CompensationState, CompensationStateAdmin)
admin.site.register(EcoAccount, EcoAccountAdmin) admin.site.register(EcoAccount, EcoAccountAdmin)
admin.site.register(EcoAccountWithdraw, EcoAccountDeductionAdmin) admin.site.register(EcoAccountDeduction, EcoAccountDeductionAdmin)

@ -238,7 +238,7 @@ class EcoAccount(AbstractCompensation):
Returns: Returns:
sum_surface (float) sum_surface (float)
""" """
return self.withdraws.all().aggregate(Sum("surface"))["surface__sum"] or 0 return self.deductions.all().aggregate(Sum("surface"))["surface__sum"] or 0
def get_available_rest(self, as_percentage: bool = False): def get_available_rest(self, as_percentage: bool = False):
""" Calculates available rest surface of the eco account """ Calculates available rest surface of the eco account
@ -250,7 +250,7 @@ class EcoAccount(AbstractCompensation):
""" """
ret_val = 0 ret_val = 0
deductions = self.withdraws.filter( deductions = self.deductions.filter(
intervention__deleted=None, intervention__deleted=None,
) )
deductions_surfaces = deductions.aggregate(Sum("surface"))["surface__sum"] or 0 deductions_surfaces = deductions.aggregate(Sum("surface"))["surface__sum"] or 0
@ -299,7 +299,7 @@ class EcoAccount(AbstractCompensation):
return ret_msgs return ret_msgs
class EcoAccountWithdraw(BaseResource): class EcoAccountDeduction(BaseResource):
""" """
A deduction object for eco accounts A deduction object for eco accounts
""" """
@ -309,7 +309,7 @@ class EcoAccountWithdraw(BaseResource):
null=True, null=True,
blank=True, blank=True,
help_text="Deducted from", help_text="Deducted from",
related_name="withdraws", related_name="deductions",
) )
surface = models.FloatField( surface = models.FloatField(
null=True, null=True,
@ -325,7 +325,7 @@ class EcoAccountWithdraw(BaseResource):
null=True, null=True,
blank=True, blank=True,
help_text="Deducted for", help_text="Deducted for",
related_name="withdraws", related_name="deductions",
) )
def __str__(self): def __str__(self):

@ -97,7 +97,7 @@ def open_view(request: HttpRequest, id: str):
# Calculate rest of available surface for deductions # Calculate rest of available surface for deductions
available = acc.get_available_rest(as_percentage=True) available = acc.get_available_rest(as_percentage=True)
deductions = acc.withdraws.filter( deductions = acc.deductions.filter(
intervention__deleted=None, intervention__deleted=None,
) )
@ -156,7 +156,7 @@ def deduction_remove_view(request: HttpRequest, id: str, deduction_id: str):
""" """
acc = get_object_or_404(EcoAccount, id=id) acc = get_object_or_404(EcoAccount, id=id)
try: try:
eco_deduction = acc.withdraws.get(id=deduction_id) eco_deduction = acc.deductions.get(id=deduction_id)
except ObjectDoesNotExist: except ObjectDoesNotExist:
raise Http404("Unknown deduction") raise Http404("Unknown deduction")

@ -14,7 +14,7 @@ from django.db import transaction
from django.urls import reverse from django.urls import reverse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from compensation.models import EcoAccountWithdraw, EcoAccount from compensation.models import EcoAccountDeduction, EcoAccount
from intervention.models import Intervention, Revocation from intervention.models import Intervention, Revocation
from konova.forms import BaseForm, BaseModalForm from konova.forms import BaseForm, BaseModalForm
from konova.models import Document from konova.models import Document
@ -568,14 +568,14 @@ class NewDeductionForm(BaseModalForm):
# Create deductions depending on Intervention or EcoAccount as the initial instance # Create deductions depending on Intervention or EcoAccount as the initial instance
if self.is_intervention_initially: if self.is_intervention_initially:
deduction = EcoAccountWithdraw.objects.create( deduction = EcoAccountDeduction.objects.create(
intervention=self.instance, intervention=self.instance,
account=self.cleaned_data["account"], account=self.cleaned_data["account"],
surface=self.cleaned_data["surface"], surface=self.cleaned_data["surface"],
created=user_action_create, created=user_action_create,
) )
else: else:
deduction = EcoAccountWithdraw.objects.create( deduction = EcoAccountDeduction.objects.create(
intervention=self.cleaned_data["intervention"], intervention=self.cleaned_data["intervention"],
account=self.instance, account=self.instance,
surface=self.cleaned_data["surface"], surface=self.cleaned_data["surface"],

@ -4,7 +4,7 @@
<div class="row"> <div class="row">
<div class="col-sm-6"> <div class="col-sm-6">
<h5> <h5>
<span class="badge badge-light">{{intervention.withdraws.count}}</span> <span class="badge badge-light">{{intervention.deductions.count}}</span>
{% trans 'Eco Account Deductions' %} {% trans 'Eco Account Deductions' %}
</h5> </h5>
</div> </div>
@ -39,7 +39,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for deduction in intervention.withdraws.all %} {% for deduction in intervention.deductions.all %}
<tr {% if deduction.account.deleted %}class="align-middle alert-danger" title="{% trans 'Eco-account deleted! Deduction invalid!' %}" {% elif not deduction.account.recorded %}class="align-middle alert-danger" title="{% trans 'Eco-account not recorded! Deduction invalid!' %}" {% endif %}> <tr {% if deduction.account.deleted %}class="align-middle alert-danger" title="{% trans 'Eco-account deleted! Deduction invalid!' %}" {% elif not deduction.account.recorded %}class="align-middle alert-danger" title="{% trans 'Eco-account not recorded! Deduction invalid!' %}" {% endif %}>
<td class="align-middle"> <td class="align-middle">
<a href="{% url 'compensation:acc-open' deduction.account.id %}"> <a href="{% url 'compensation:acc-open' deduction.account.id %}">

Loading…
Cancel
Save