Custom script on how to perform calculations for the child table

Hello Everyone,

I have created a doctype called “Vomax Cycle Order Form”.

This doctype has a child table called “Items Table1”.

This “Items Table1” doctype contains below mentioned fields.

  1. Category

  2. Item

  3. XS

  4. S

  5. M

  6. L

  7. XL

  8. XXL

  9. 3XL

  10. 4XL

  11. Pad Type

  12. Zipper Size

  13. Zipper Colour

  14. Price Each

  15. Total Price

I want to do the calculation for the child table.

The formula is Total Price= ((XS+S+M+L+XL+XXL+3XL+4XL) * (Price Each))

Also, there is a field called “Total” in the parent doctype.

Whenever the above calculation is performed, the field value of the “Total” should be calculated automatically.

I have tried with different custom scripts as shown below, but it is not working.

Script 1: frappe.ui.form.on(‘Items_Table1’, {
xs: function(frm, cdt, cdn)
s: function(frm, cdt, cdn)
m: function(frm, cdt, cdn)
l: function(frm, cdt, cdn)
xl: function(frm, cdt, cdn)
xxl: function(frm, cdt, cdn)
3xl: function(frm, cdt, cdn)
4xl: function(frm, cdt, cdn){
let child = locals[cdt][cdn];
let total_price = ((child.xs + child.s + child.m + child.l + child.xl + child.xxl + child.3xl + child.4xl) * (child.price_each));
frappe.model.set_value(cdt, cdn, “total_price”, total_price);
calculate_total_price(frm);
},
price_each: function(frm, cdt, cdn) {
let child = locals[cdt][cdn];
let total_price = ((child.xs + child.s + child.m + child.l + child.xl + child.xxl + child.3xl + child.4xl) * (child.price_each));
frappe.model.set_value(cdt, cdn, “total_price”, total_price);
frappe.model.set_value(cdt, cdn, “total_price”, total_price);
calculate_total_price(frm);
}
});

// frappe.ui.form.on(“Items_Table1”, {
// xs: function(frm, cdt, cdn)
s: function(frm, cdt, cdn)
m: function(frm, cdt, cdn)
l: function(frm, cdt, cdn)
xl: function(frm, cdt, cdn)
xxl: function(frm, cdt, cdn)
3xl: function(frm, cdt, cdn)
4xl: function(frm, cdt, cdn){
// let row = locals[cdt][cdn];
// {
// frappe.model.set_value(cdt, cdn, ‘total_price’, flt(child.xs + child.s + child.m + child.l + child.xl + child.xxl + child.3xl + child.4xl) * (child.price_each))
// frm.events.calculate_total_price(frm);
// }
// },

// price_each: function(frm, cdt, cdn) {
// let row = locals[cdt][cdn];
// {
// frappe.model.set_value(cdt, cdn, ‘total_price’, flt(child.xs + child.s + child.m + child.l + child.xl + child.xxl + child.3xl + child.4xl) * (child.price_each))
// frm.events.calculate_total_price(frm);
// }
// },
// })

Script 2: cur.frm.cscript.xs=function(doc,cdt,cdn)
cur.frm.cscript.s=function(doc,cdt,cdn)
cur.frm.cscript.m=function(doc,cdt,cdn)
cur.frm.cscript.l=function(doc,cdt,cdn)
cur.frm.cscript.xl=function(doc,cdt,cdn)
cur.frm.cscript.xxl=function(doc,cdt,cdn)
cur.frm.cscript.3xl=function(doc,cdt,cdn)
cur.frm.cscript.4xl=function(doc,cdt,cdn)
{
var d=locals[cdt][cdn]
var total_price_value=((d.xs + d.s + d.m + d.l + d.xl + d.xxl + d.3xl + d.4xl) * (d.price_each));
d.total_price=total_price_value;
refresh_field(“Items_Table1”);
}

Script 3: frappe.ui.form.on(“Items Table1”, “xs”, “s”, “m”, “l”, “xl”, “xxl”, “3xl”, “4xl”, function(frm, cdt, cdn) { var child = locals[cdt][cdn]; child.total_price = (((child.xs + child.s + child.m + child.l + child.xl + child.3xl + child.4xl) * (child.price_each)); cur_frm.refresh(); });

Here is the screenshots of both parent doctype and child table doctype.

Please help.

Thanks & Regards
Sujay

Hello guys,

I have created a custom script and now I am able to calculate the Total Price in the child table, but I am not getting any idea on how to calculate the field called “Total”, which is available in the parent doctype.

Formula is Total = sum of (Total Price of child tables) + Shipping Charges.

Please help.

Hi Sujay,

Did you find a solution for this?
Also, Kindly help with script that you used in the above calculations.

Thanks

I think this will help to your problem you can loop in the childtable to get the total of it

1 Like