From db21d38fd64f32eb049a5b9aae0d2e9bbcd183e0 Mon Sep 17 00:00:00 2001 From: Horilla Date: Mon, 15 Jul 2024 15:24:02 +0530 Subject: [PATCH] [UPDT] PAYROLL: Added Allowance and deduction condition for range --- payroll/forms/component_forms.py | 73 +++++++++++++++++++++- payroll/methods/payslip_calc.py | 10 ++- payroll/models/models.py | 13 ++++ payroll/templates/payroll/common/form.html | 14 +++++ 4 files changed, 105 insertions(+), 5 deletions(-) diff --git a/payroll/forms/component_forms.py b/payroll/forms/component_forms.py index 888cd2e5a..47483ca1f 100644 --- a/payroll/forms/component_forms.py +++ b/payroll/forms/component_forms.py @@ -72,6 +72,11 @@ class AllowanceForm(forms.ModelForm): } kwargs["initial"] = initial super().__init__(*args, **kwargs) + self.fields["if_condition"].widget.attrs.update( + { + "onchange": "rangeToggle($(this))", + } + ) reload_queryset(self.fields) self.fields["style"].widget = widget.StyleWidget(form=self) @@ -83,6 +88,36 @@ class AllowanceForm(forms.ModelForm): table_html = render_to_string("common_form.html", context) return table_html + def clean(self, *args, **kwargs): + cleaned_data = super().clean(*args, **kwargs) + + if cleaned_data.get("if_condition") == "range": + cleaned_data["if_amount"] = 0 + start_range = cleaned_data.get("start_range") + end_range = cleaned_data.get("end_range") + if start_range and end_range and end_range <= start_range: + raise forms.ValidationError( + {"end_range": "End range cannot be less than start range."} + ) + if not start_range and not end_range: + raise forms.ValidationError( + { + "start_range": 'This field is required when condition is "range".', + "end_range": 'This field is required when condition is "range".', + } + ) + elif not start_range: + raise forms.ValidationError( + {"start_range": 'This field is required when condition is "range".'} + ) + elif not end_range: + raise forms.ValidationError( + {"end_range": 'This field is required when condition is "range".'} + ) + else: + cleaned_data["start_range"] = None + cleaned_data["end_range"] = None + def save(self, commit: bool = ...) -> Any: super().save(commit) other_conditions = self.data.getlist("other_conditions") @@ -140,10 +175,44 @@ class DeductionForm(forms.ModelForm): } kwargs["initial"] = initial super().__init__(*args, **kwargs) + self.fields["if_condition"].widget.attrs.update( + { + "onchange": "rangeToggle($(this))", + } + ) reload_queryset(self.fields) self.fields["style"].widget = widget.StyleWidget(form=self) - def clean(self): + def clean(self, *args, **kwargs): + cleaned_data = super().clean(*args, **kwargs) + if cleaned_data.get("if_condition") == "range": + cleaned_data["if_amount"] = 0 + start_range = cleaned_data.get("start_range") + end_range = cleaned_data.get("end_range") + + if start_range and end_range and int(end_range) <= int(start_range): + raise forms.ValidationError( + {"end_range": "End range cannot be less than start range."} + ) + if not start_range and not end_range: + raise forms.ValidationError( + { + "start_range": 'This field is required when condition is "range".', + "end_range": 'This field is required when condition is "range".', + } + ) + elif not start_range: + raise forms.ValidationError( + {"start_range": 'This field is required when condition is "range".'} + ) + elif not end_range: + raise forms.ValidationError( + {"end_range": 'This field is required when condition is "range".'} + ) + else: + cleaned_data["start_range"] = None + cleaned_data["end_range"] = None + if ( self.data.get("update_compensation") is not None and self.data.get("update_compensation") != "" @@ -165,7 +234,7 @@ class DeductionForm(forms.ModelForm): ) if self.data.get("amount") is None and self.data.get("amount") == "": raise forms.ValidationError({"amount": _("This field is required.")}) - return super().clean() + return cleaned_data def as_p(self): """ diff --git a/payroll/methods/payslip_calc.py b/payroll/methods/payslip_calc.py index 08d471c80..bbf78a2cc 100644 --- a/payroll/methods/payslip_calc.py +++ b/payroll/methods/payslip_calc.py @@ -733,10 +733,14 @@ def if_condition_on(*_args, **kwargs): gross_pay = calculate_gross_pay( **kwargs, )["gross_pay"] - operator_func = operator_mapping.get(component.if_condition) condition_value = basic_pay if component.if_choice == "basic_pay" else gross_pay - if not operator_func(condition_value, component.if_amount): - amount = 0 + if component.if_condition == "range": + if not component.start_range <= condition_value <= component.end_range: + amount = 0 + else: + operator_func = operator_mapping.get(component.if_condition) + if not operator_func(condition_value, component.if_amount): + amount = 0 return amount diff --git a/payroll/models/models.py b/payroll/models/models.py index 052e8c130..fb4c29626 100644 --- a/payroll/models/models.py +++ b/payroll/models/models.py @@ -639,6 +639,7 @@ IF_CONDITION_CHOICE = [ ("gt", _("Greater Than (>)")), ("le", _("Less Than or Equal To (<=)")), ("ge", _("Greater Than or Equal To (>=)")), + ("range", _("Range")), ] FIELD_CHOICE = [ ("children", _("Children")), @@ -882,6 +883,12 @@ class Allowance(HorillaModel): if_amount = models.FloatField( default=0.00, help_text=_("The amount of the pay-head") ) + start_range = models.FloatField( + blank=True, null=True, help_text=_("The start amount of the pay-head range") + ) + end_range = models.FloatField( + blank=True, null=True, help_text=_("The end amount of the pay-head range") + ) company_id = models.ForeignKey( Company, null=True, editable=False, on_delete=models.PROTECT ) @@ -1191,6 +1198,12 @@ class Deduction(HorillaModel): if_amount = models.FloatField( default=0.00, help_text=_("The amount of the pay-head") ) + start_range = models.FloatField( + blank=True, null=True, help_text=_("The start amount of the pay-head range") + ) + end_range = models.FloatField( + blank=True, null=True, help_text=_("The end amount of the pay-head range") + ) company_id = models.ForeignKey( Company, null=True, editable=False, on_delete=models.PROTECT ) diff --git a/payroll/templates/payroll/common/form.html b/payroll/templates/payroll/common/form.html index d8411c686..a62cc315d 100644 --- a/payroll/templates/payroll/common/form.html +++ b/payroll/templates/payroll/common/form.html @@ -6,6 +6,19 @@