Custom script to calculate total amount

Hi guys,
I did a Custom Script to calculate total rows in two tables that I have in a doctype, how I did the calculation is done only after save. How I can get that the totals for each row in the table are calculated as soon as the Rate field in the table are completed like are in the ERP?

cur_frm.cscript.custom_validate = function(doc) {

var monto_total=0.0;
var total_material=0.0;
var total_mano_obra=0.0;

$.each(doc.material, function(i, d) {

 d.total= d.precio*d.cantidad;
 
monto_total=monto_total+d.total;
total_material=total_material+d.total;

});

$.each(doc.mano_de_obra, function(i, d) {

d.total= d.precio*d.cantidad;
monto_total=monto_total+d.total;
total_mano_obra=total_mano_obra+d.total;

});
doc.monto_total=monto_total;
doc.total_materiales=total_material;
doc.monto_total_mano_de_obra=total_mano_obra;
}

Say your parent doctype is Purchase Invoice, and you wanted to initiate the calculation once a user modifies the ‘rate’ value which is within a table that is called from the Purchase Invoice Item doctype you could use the following:

frappe.ui.form.on("Purchase Invoice Item", "rate", function(frm, doctype, name) {
	[your calculation]
});

This client script should be created for the Purchase Invoice doctype and not the Purchase Invoice Item doctype.

4 Likes

Thanks very much!!! @bohlian

hi guys,
need help to calculate the two value in task doc i have added three currency field now i don’t know how to total it purchased_amount - budgeted_amount = total mount ?

this is my code please guys need advice to solve this code
how can i total this value
cur_frm.add_fetch(“budgeted_amount”, “purchased_amount”, “excess_amount”);
frappe.ui.form.on(“budgeted_amount”, “purchased_amount”, function(frm, doctype, name) {

cur_frm.cscript.custom_validate = function(doc) {

var budgeted_amount=budgeted_amount;
var purchased_amount=purchased_amount;
var excess_amount=0;

doc.excess_amount=purchased_amount-budgeted_amount;

doc.budgeted_amount=budgeted_amount;
doc.purchased_amount=purchased_amount;

}

});