Math Operation on fields

Hye guys.

I want to create some mathematical operation on my fields in my new doctype.
The doctype name is Employee Receipt.

The operation is like this (fields):
total_need_to_pay = float number
payment = float number
balance_need_to_pay = total_need_to_pay - payment (read-only)

I am very confused with the client side scripting.
Can anyone show it to me?
This is the example:

Currently i tried using this code:

frappe.ui.form.on(“Employee Receipt”, “balance_need_to_pay”, function(frm) {
frappe.model.set_value(“balance_need_to_pay”, (frm.doc.total_need_to_pay - frm.doc.payment));
msgprint(frm.doc.balance_need_to_pay);
frappe.model.refresh()
});

but nothing happen in the form

Hi, please try this one and change value in “payment” field

frappe.ui.form.on(“Employee Receipt”, “payment”, function(frm) {

frappe.model.set_value(“balance_need_to_pay”, (frm.doc.total_need_to_pay - frm.doc.payment));

msgprint(frm.doc.balance_need_to_pay);

frappe.model.refresh()

});

Thank you for your reply,

I’ve been change the value in payment but nothing happen.
May i know what i have to set type of the field on “balance_need_to_pay”?
is i have to set read only or float?

The type of balance_need_to_pay should be float. Deciding upon whether it has to be read only or not depends on your usecase.

I don’t think that problem with field type but as for me float type is more logic and you also can make it read only if you want.

please try this one
frappe.ui.form.on(“Employee Receipt”, “payment”, function(frm) {
msgprint(frm.doc.total_need_to_pay - frm.doc.payment);

frappe.model.set_value(“balance_need_to_pay”, (frm.doc.total_need_to_pay - frm.doc.payment));
frm.refresh_field("balance_need_to_pay");
msgprint(frm.doc.balance_need_to_pay);

frappe.model.refresh()

});

May i know why i need to change the fieldname of “payment”, because the result will be on “balance_need_to_pay”.
and I already change the your given code but still not working.

here my code:

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

Thanks @strixaluco for your reply
I’ve been change the type of field to float. but not showing the result after the math logic.

have you received message? have you reload the page after changing the script?

yes, because client scripts works after changing some value, so if you don’t change value in the field you don’t receive any results… that’s why I think we should add script to the payment field

Each time i edit the custom script, I reload the page but nothing happen. And i received no message from the console.

and have you changed value in payment field? if you didn’t receive message you script doesn’t work, it isn’t running.

Im sorry. I think i got a little confused with changing value in payment field.
What do you mean by that?

add any value to the payment field

Yes i already put some value on that payment field. But the code is not running the math operation.

here is my example:

how about message? have you received something?

I received nothing.
:joy::sob:

is my code wrong?

Could you please try to run this script, don’t forget to reload the page and change value in payment field

I’ve been change the script.
here is the result:

hmmmm, very strange

please try this one

I just try copy and paste the code from this link https://discuss.erpnext.com/t/trigger-recalculation-after-change-in-customfield/7394/15?u=iqbalzahir

here is the code:

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", "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", "balance_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.balance_need_to_pay);
});

It worked when i edit the current employee receipt. but when i want to create the new employee receipt, it slow down the browser.
here is the pic:


it just stucked!