Calculation on custom fields

Guys. I need your help. I was try so many ways to calculate my custom field to get the result.

Here is what I want to calculate:

Doctype : Employee Receipt
Fields:
total_need_to_pay = float
payment = float
balance_need_to_pay = total_need_to_pay - payment

I already try this code and it worked for existing receipt. but it stucked when i want to create a new receipt.

frappe.ui.form.on("Employee Receipt", "total_need_to_pay", function(frm) {
       cur_frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay - frm.doc.payment));
       frappe.msgprint(frm.doc.total_need_to_pay);
});
frappe.ui.form.on("Employee Receipt", "payment", function(frm) {
       cur_frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay - frm.doc.payment));
       frappe.msgprint(frm.doc.payment);
});
frappe.ui.form.on("Employee Receipt", "balance_need_to_pay", function(frm) {
       cur_frm.set_value("balance_need_to_pay", (frm.doc.payment - frm.doc.total_need_to_pay));
       frappe.msgprint(frm.doc.balance_need_to_pay);
});

Please help me. Please.

It’s okay guys. It’s already fixed.

Great! Could you please share your solution?

Here is my code:

frappe.ui.form.on("Employee Receipt", {
    refresh: function(frm) {
        frm.set_value("balance_need_to_pay", (frm.doc.total_need_to_pay - frm.doc.payment));
        // use the __islocal value of doc, to check if the doc is saved or not
	frm.set_df_property("balance_need_to_pay", "read_only", frm.doc.__islocal ? 0 : 1);
    }
});

I think the code i paste on the question is not valid anymore. So i just change with the new style of code. If i’m wrong please correct me. Because i want to learn.

Thanks.

1 Like