Calculate Child table Values

Hi,
I’ve a Doctype named “Payroll Reporting Form” & under it i have Child Table named “Payroll Table”
I want to calculate Child tables Fields name “hours” & “rate” and result should be in “gross”
NOTE: above three mentioned fields are child table fields.

I’ve tried this script on Doctype “Payroll Reporting Form” but its not calculating the result and this error popups when function runs.

Capture

Script :
NOTE: I’ve wrote this script on Doctype “Payroll Reporting Form” as well as also used with Child Table “Payroll Table” but not working :frowning:

frappe.ui.form.on("Payroll Table", {
	hours: function(frm){
		calculate_total(frm);
	},
	overtime_hours: function(frm){
		calculate_total(frm);
	}
});
var calculate_total = function(frm) {
	frm.set_value("gross", frm.doc.hours * frm.doc.rate);
}

Try this

frappe.ui.form.on("Payroll Table", {
	hours: function(frm,cdt, cdn){
		calculate_total(frm, cdt, cdn);
	},
	overtime_hours: function(frm, cdt, cdn){
		calculate_total(frm, cdt, cdn);
	}
});
var calculate_total = function(frm, cdt, cdn) {
	var child = locals[cdt][cdn];
	frappe.model.set_value(cdt, cdn, "gross", child.hours * child.rate);
}

frm.set_value won’t work for gross because the field is part of the child form and not the ‘Payroll Reporting’ form.

5 Likes

Many thanks for your reply @Zlash65 But its not working, without showing any error.

Can you try it again, I missed out some changes earlier.

@Zlash65 Many thanks buddy, its working.