How to add column values base on boolean values of another column in child table

Hello guys.
i need assistance

I have child table called payment instalment. i want to add amount column when paid field is checked true and also add amount values when paid field is unchecked. below is the picture

Client script of doctype (your main doctype)

frappe.ui.form.on('CHILD_TABLE_DOCTYPE', {
	PAID_CHECKBOX_NAME: function(frm) {
		let total = 0;
		let paid = 0;
		for (let i in frm.doc.CHILD_TABLE_NAME) {
			total += frm.doc.CHILD_TABLE_NAME[i].AMOUNT_DUE_NAME;
			if (frm.doc.CHILD_TABLE_NAME[i].PAID_CHECKBOX_NAME) {
				paid+= frm.doc.AMOUNT_PAID_NAME;
			}
		}
		frm.doc.AMOUNT_DUE_NAME = total - paid;
		frm.doc.AMOUNT_PAID_NAME = paid;
		refresh_field("AMOUNT_DUE_NAME");
		refresh_field("AMOUNT_PAID_NAME");
	}
})

You may also want change this to a main function and call it when the document loads/refresh based on your needs

thank for your response
i want to explain something for you on picture
Amount Due = summation of amount field values when paid check box is unchecked
Amount Paid = summation of amount field values when paid check box is checked

The code is functionally the same as you described. if you really want, you can change the way it calculates to the other way around.

frappe.ui.form.on('CHILD_TABLE_DOCTYPE', {
	PAID_CHECKBOX_NAME: function(frm) {
		let dueAmount = 0;
		let paid = 0;
		for (let i in frm.doc.CHILD_TABLE_NAME) {
			if (frm.doc.CHILD_TABLE_NAME[i].PAID_CHECKBOX_NAME) {
				paid += frm.doc.AMOUNT_PAID_NAME;
			} else {
				dueAmount += frm.doc.CHILD_TABLE_NAME[i].AMOUNT_DUE_NAME;
			}
		}
		frm.doc.AMOUNT_DUE_NAME = dueAmount;
		frm.doc.AMOUNT_PAID_NAME = paid;
		refresh_field("AMOUNT_DUE_NAME");
		refresh_field("AMOUNT_PAID_NAME");
	}
})

This would recalculate the whole table when any checkbox is checked or unchecked. If you want to add it, you can also add the event to the amount cells for when they change, the totals will update as well

thank you very much for your effort.
I have not yet gotten the result sir. the script is not calculating Amount Paid . below is what i have
frappe.ui.form.on(‘Memo Instalment Payment’, {
paid: function(frm){
let dueAmount = 0;
let paidAmount = 0;
for (let i in frm.doc.instalments) {
if (frm.doc.instalments[i].paid) {
paidAmount += frm.doc.amount_paid;
} else {
dueAmount += frm.doc.instalments[i].amount_due;
}
}
frm.doc.amount_due = dueAmount;
frm.doc.amount_paid = paidAmount;
refresh_field(“amount_due”);
refresh_field(“amount_paid”);
}
});

can you look at this script;
//Payment Instalments
frappe.ui.form.on(‘Memo Instalment Payment’, {
//sum the amount due & amount paid

amount: function(frm, cdt, cdn){
 let d = locals[cdt][cdn];
 let amountDue = 0;
 let amountPaid = 0;
 let paidCheckBox = frm.doc.instalments[i].paid_check;
 
if (paidCheckBox.checked == true){
    frm.doc.instalments.forEach(function(d){amountPaid += d.amount;});
    frm.set_value("amount_paid", amountPaid);
    refresh_field("amount_paid");
}else{
    frm.doc.instalments.forEach(function(d){amountDue += d.amount;});
    frm.set_value("amount_due", amountDue);
    refresh_field("amount_due");
}
 
 
},

//Remove from Instalment payment
instalments_remove: function(frm, cdt, cdn){
    let d = locals[cdt][cdn];
    let amountDue = 0;
    let amountPaid = 0;
    let paidCheckBox = frm.doc.instalments[i].paid_check;
    if (paidCheckBox.checked == true){
         frm.doc.instalments.forEach(function(d){
        amountPaid += d.amount;
    });
    frm.set_value("amount_paid", amountPaid);
    refresh_field("amount_paid");
    
    }else{
         frm.doc.instalments.forEach(function(d){
         amountDue += d.amount;
    });
    frm.set_value("amount_due", amountDue);
    refresh_field("amount_due");
    }
   
},

});

Best practice if you are calculating a full sum not to trust the data stored in the input. So create a total in the backend. If you want to keep data like this, then I suggest using data attributes instead.

When user checks a box, logically both due and paid values will change. You would get the current value +/- both based on if checked or unchecked and then update+refresh both due and paid boxes

Just as a side note, since you already set d to locals[cdt][cdn], your defined functions shouldn’t use the same variable name just to keep confusion at bay and make the code more readable for your future.