How to subtract two custom fields display result

Good day guys.
I have three custom fields on the parent table called " Task" ;
Total Amount,
Total Amount Paid,
Balance .
i want subtract Total Amount paid from Total Amount and displace result on balance fields i.e
Balance = Total Amount - Total Amount Paid
below is my code;
frappe.ui.form.on(‘Task’, {
balance:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var balance = ( d.total_amount - d.total_amount_paid);
frappe.model.set_value(cdt, cdn, “balance_remain”, balance);
refresh_field(“balance_remain”);
}
})

dear luben,
below is my sum script function i hope it will help, as i remember while writing my script, defining variable with calculation isn’t working . you should define variable first then make calculation.

cur_frm.cscript.calculate = function(frm, cdt, cdn){
  var row = locals[cdt][cdn];
  var total_item = 0;
  var total_installation = 0;
  cur_frm.doc.items.forEach(function(row) {
    total_item += (row.item_rate * row.qty);
    total_installation += (row.installation_rate * row.qty);
  });
  cur_frm.set_value('total_item_rate', total_item);
  cur_frm.set_value('total_installation_rate', total_installation);
  cur_frm.refresh_field('total_item_rate');
  cur_frm.refresh_field('total_installation_rate');
};