How to fix "Error in formula or condition: float division by zero"

Hi all, I was scripting (client-side) to calculate the amount to deduct for Leave without Pay in Salary Slip. I couldn’t use the default calculation method in ERPNext. This is because under the labor law of my country:

  1. Salary below $1500: the deduction for Leave without Pay has to be based on the number of days in the month. e.g. 28 days during Feb.

  2. Salary above $1500: the deduction for Leave without Pay will be based on the company’s policy i.e. 22 days

So the calculation should for Feb 2019 (28 days)

  • Employee Zack: Took 2 days of Leave without Pay
    Basic (earning): $1500
    Leave without Pay (deduction): $1500 / 28 * 2 = $107.14
    Net Salary: $1500 - $107.14 = $1392.86

  • Employee Cindy: Took 2 days of Leave without Pay
    Basic (earning): $3000
    Leave without Pay (deduction): $3000 / 22 * 2 = $272.72
    Net Salary: $3000 - $272.72 = $2727.28

But during the creation of salary slip, I received an error: “Error in formula or condition: float division by zero”

  • This is the formula I put for Leave without Pay salary component (Note that ‘total_days_in_the_month’ is a custom field I inserted in Salary Slip doctype):

(basic / total_days_in_the_month * leave_without_pay) if basic < 1500 else (basic / 22 * leave_without_pay)

  • This is the custom script (client-side) for Salary Slip.
frappe.ui.form.on('Salary Slip', "refresh", function(frm, cdt, cdn){
        var d = locals[cdt][cdn];
        frappe.model.set_value(cdt, cdn, 'total_days_in_the_month', frappe.datetime.get_day_diff( d.end_date , d.start_date));
        refresh_field('total_days_in_the_month');
    });

Actually, this script here isn’t correct either because it calculated 27 days ( 28 Feb - 1 Feb = 27 days difference). I wanted to include start date in calculation (1 day is added)… I would really appreciate if there’s anyone that can guide me on this part as well. Thank you in advance.

Anyone?

Can you explain breifly how you want to calculate with examples?

Hi @arokia,

This is the screenshot of Salary Slip doctype:

This is the preview of Salary Slip:

Problem 1: As mentioned earlier, this is the custom script I used to calculate the total no of days in the month:

Problem 2: This is the formula in Leave without Pay salary component:

This is the error message:

I suspect the cause of this error message is that despite the fact I have inserted “Total Days in the Month” as float in the DocType, the actual data (XX days) is a string data. Therefore, the formula in the Leave without Pay salary component was unable to calculate.