Refactoring
* moves adding of deduction into Intervention and EcoAccount model * hardens against circular import issues
This commit is contained in:
@@ -290,14 +290,12 @@ class NewDeductionModalForm(BaseModalForm):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.form_title = _("New Deduction")
|
||||
self.form_caption = _("Enter the information for a new deduction from a chosen eco-account")
|
||||
self.is_intervention_initially = False
|
||||
|
||||
# Check for Intervention or EcoAccount
|
||||
if isinstance(self.instance, Intervention):
|
||||
# Form has been called with a given intervention
|
||||
self.initialize_form_field("intervention", self.instance)
|
||||
self.disable_form_field("intervention")
|
||||
self.is_intervention_initially = True
|
||||
elif isinstance(self.instance, EcoAccount):
|
||||
# Form has been called with a given account --> make it initial in the form and read-only
|
||||
self.initialize_form_field("account", self.instance)
|
||||
@@ -314,10 +312,7 @@ class NewDeductionModalForm(BaseModalForm):
|
||||
is_valid (bool)
|
||||
"""
|
||||
super_result = super().is_valid()
|
||||
if self.is_intervention_initially:
|
||||
acc = self.cleaned_data["account"]
|
||||
else:
|
||||
acc = self.instance
|
||||
acc = self.cleaned_data["account"]
|
||||
|
||||
if not acc.recorded:
|
||||
self.add_error(
|
||||
@@ -331,7 +326,7 @@ class NewDeductionModalForm(BaseModalForm):
|
||||
sum_surface_deductions = acc.get_deductions_surface()
|
||||
rest_surface = deductable_surface - sum_surface_deductions
|
||||
form_surface = float(self.cleaned_data["surface"])
|
||||
is_valid_surface = form_surface < rest_surface
|
||||
is_valid_surface = form_surface <= rest_surface
|
||||
if not is_valid_surface:
|
||||
self.add_error(
|
||||
"surface",
|
||||
@@ -344,35 +339,7 @@ class NewDeductionModalForm(BaseModalForm):
|
||||
return is_valid_surface and super_result
|
||||
|
||||
def save(self):
|
||||
with transaction.atomic():
|
||||
# Create log entry
|
||||
user_action_edit = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.EDITED
|
||||
)
|
||||
user_action_create = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.CREATED
|
||||
)
|
||||
self.instance.log.add(user_action_edit)
|
||||
self.instance.modified = user_action_edit
|
||||
self.instance.save()
|
||||
|
||||
# Create deductions depending on Intervention or EcoAccount as the initial instance
|
||||
if self.is_intervention_initially:
|
||||
deduction = EcoAccountDeduction.objects.create(
|
||||
intervention=self.instance,
|
||||
account=self.cleaned_data["account"],
|
||||
surface=self.cleaned_data["surface"],
|
||||
created=user_action_create,
|
||||
)
|
||||
else:
|
||||
deduction = EcoAccountDeduction.objects.create(
|
||||
intervention=self.cleaned_data["intervention"],
|
||||
account=self.instance,
|
||||
surface=self.cleaned_data["surface"],
|
||||
created=user_action_create,
|
||||
)
|
||||
deduction = self.instance.add_deduction(self)
|
||||
return deduction
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ from django.db import models, transaction
|
||||
from django.db.models import QuerySet
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from compensation.models import EcoAccountDeduction
|
||||
from intervention.managers import InterventionManager
|
||||
from intervention.models.legal import Legal
|
||||
from intervention.models.responsibility import Responsibility
|
||||
@@ -213,7 +214,7 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
|
||||
""" Adds a new revocation to the intervention
|
||||
|
||||
Args:
|
||||
form (NewPaymentForm): The form holding the data
|
||||
form (NewRevocationModalForm): The form holding the data
|
||||
|
||||
Returns:
|
||||
|
||||
@@ -249,6 +250,40 @@ class Intervention(BaseObject, ShareableObjectMixin, RecordableObjectMixin, Chec
|
||||
)
|
||||
return revocation
|
||||
|
||||
def add_deduction(self, form):
|
||||
""" Adds a new deduction to the intervention
|
||||
|
||||
Args:
|
||||
form (NewDeductionModalForm): The form holding the data
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
form_data = form.cleaned_data
|
||||
user = form.user
|
||||
|
||||
with transaction.atomic():
|
||||
# Create log entry
|
||||
user_action_edit = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.EDITED
|
||||
)
|
||||
user_action_create = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.CREATED
|
||||
)
|
||||
self.log.add(user_action_edit)
|
||||
self.modified = user_action_edit
|
||||
self.save()
|
||||
|
||||
deduction = EcoAccountDeduction.objects.create(
|
||||
intervention=self,
|
||||
account=form_data["account"],
|
||||
surface=form_data["surface"],
|
||||
created=user_action_create,
|
||||
)
|
||||
return deduction
|
||||
|
||||
|
||||
class InterventionDocument(AbstractDocument):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user