# How Do I Make Total Price and Grand Total Auto-Calculate Its Values?

I created a DocType from the Desk but when an item is added, the quantity and the unit price do not get multiplied to automatically show up in the Total Price, how do I solve this and also make it work in Grand Total?

Hi, you will have to write client script to achieve this,

frappe.ui.form.on("Your Child Table Name Here!", {
item_code: function (frm, cdt, cdn) {
var child = locals[cdt][cdn];
if (child.item_code) {
calculateGrandTotal(frm);
}
},

qty: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "amount", d.qty * d.rate);
calculateGrandTotal(frm);
},

rate: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "amount", d.qty * d.rate);
calculateGrandTotal(frm);
},
});

function calculateGrandTotal(frm) {
var grand_total = 0;
frm.doc.items.forEach(function (item) {
grand_total += item.amount;
});
frm.set_value("grand_total", grand_total);
}

I have provided a code that I used in one of my projects. Kindly change the Child Table Doctype name to what you have and change the field names according to what you have. Thanks.!

Thank you very much, I will work on it.

Thank very much, it worked perfectly.

Please, can you show me how to connect the VAT table to the set up?

Also, if the customerâ€™s default current is for example Euro, how can I make my DocType convert from the default Dollars to Euro?

Please can anyone help me to conclude on this?

I have a custom doctype and the script above is used to calculate total and grand total. VAT from Tax table when applied isnâ€™t working. Can anyone help me with connecting the VAT table or custom script to make it work? I sincerely need this to work please.

I used this code but one thing is missed when I remove row from child table the grand_total doesnâ€™t updated automatically while if I add new row the grand_total is updated automatically I think the code need to add code line to update the grand_total automatically when remove row I tried by this code but it doesnâ€™t work
frappe.ui.form.on(â€śBudget_Lineâ€ť, {
budget_line_add(frm, cdt, cdn) { runCalculation(frm, locals[cdt][cdn]); },
budget_line_remove(frm, cdt, cdn) { runCalculation(frm, locals[cdt][cdn]); }
});
Where :
â€śBudget_Lineâ€ť is the child Doctype name
budget_line is the child table name in main Doctype â€śProject_Budgetâ€ť

Solved
items_remove: function (frm) {
calculateGrandTotal(frm);
}
It will be as following :
frappe.ui.form.on(â€śYour Child Table Name Here!â€ť, {
item_code: function (frm, cdt, cdn) {
var child = locals[cdt][cdn];
if (child.item_code) {
calculateGrandTotal(frm);
}
},

qty: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, â€śamountâ€ť, d.qty * d.rate);
calculateGrandTotal(frm);
},

rate: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, â€śamountâ€ť, d.qty * d.rate);
calculateGrandTotal(frm);
},
items_remove: function (frm) {
calculateGrandTotal(frm);
}
});

function calculateGrandTotal(frm) {
var grand_total = 0;
frm.doc.items.forEach(function (item) {
grand_total += item.amount;
});
frm.set_value(â€śgrand_totalâ€ť, grand_total);
}
rate: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, â€śamountâ€ť, d.qty * d.rate);
calculateGrandTotal(frm);
},

1 Like