Try ERPNext Buy Support Partners Foundation

Check-in Check-out, Days or Nights Calculation Using Custom Script

I have the below script.
The goal is:

  1. If Nights option is selected, calculate number of nights, this is the default behavior and it works.
  2. If Days option is selected, calculate number of days, i.e. subtract 1, to the default behavior in 1 above

Problem is, it’s not working.
Anyone done this, or can help. Think of bookings in a hotel.

 start_date: function (frm, cdt, cdn) {
        var row = locals[cdt][cdn];
        if (row.reservation_option === "Nights") { //For Nights
            if (row.days) { //End date Calculation on Start date change
                frappe.model.set_value(cdt, cdn, 'end_date', frappe.datetime.add_days(row.start_date, row.days));
                frm.fields_dict["end_date"].grid.grid_rows_by_docname[cdn].end_date.refresh();
            }
            if (row.end_date >= row.start_date) { //Days Calculation on Start date change
                frappe.model.set_value(cdt, cdn, 'days', frappe.datetime.get_day_diff(row.end_date, row.start_date));
                frm.fields_dict["days"].grid.grid_rows_by_docname[cdn].days.refresh();
            }
        }
        if (row.reservation_option === "Days") { //For Days
            if (row.days) { //End date Calculation on Start date change
                frappe.model.set_value(cdt, cdn, 'end_date', frappe.datetime.add_days(row.start_date, row.days));
                frm.fields_dict["end_date"].grid.grid_rows_by_docname[cdn].end_date.refresh();
            }
            if (row.end_date >= row.start_date) { //Days Calculation on Start date change
                frappe.model.set_value(cdt, cdn, 'days', frappe.datetime.get_day_diff(row.end_date, row.start_date));
                frm.fields_dict["days"].grid.grid_rows_by_docname[cdn].days.refresh();
            }
        }
    }

edit: highlight code