Get balance from table with condition

From the table i got the balance in a text
here is the code …

frappe.ui.form.on(‘Supplier’, {
validate: function(frm) {
var dca = 0;
$.each(frm.doc.payment_details, function(i, d) {
dca += flt(d.collected_amount);
});
frm.set_value(“debt_collected_amount”, dca);
}
});


i need to add condition … if payment mode is cash or online transfer it will calculate the amount but if payment mode is cheque, the cheque status should be clear then it should calculate the balance

Hi @GhadaEbrahim,

Please try it.

frappe.ui.form.on('Supplier', {
    validate: function(frm) {
        var dca = 0;
        $.each(frm.doc.payment_details, function(i, d) {
            if ((d.payment_mode === "Cash" || d.payment_mode === "Online Transfer") ||
                (d.payment_mode === "Cheque" && d.cheque_status === "Clear")) {
                dca += flt(d.collected_amount);
            }
        });
        frm.set_value("debt_collected_amount", dca);
    }
});

Please set your field name and value in the script accordingly.

Thank You!