|
|
|
@ -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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|