Custom Doctype - how to calculate a Total from Values in a Child table?

I want to create a custom DocType and have a question on this.

The goal is to

  1. collect due Purchase Invoices. This has to be flexible enough to only pick particular PINV and to not pick others.
  2. calculate the total sum due of all the invoices collected in step #1

I have created a doctype which has a Child table linking to “Purchase invoice” Doctype. So I can choose invoices and list them with the Total Sum Each invoice. Now what I am struggling with is to make an addition of these values and produce a TOTAL for all invoices collected.

To me it looks like as if this should work similar to how an Invoice creates the Grand Total but when you look at that via the Customize Form Tool there is not hint whatsoever on how the calculation is being done.

Can anybody point me into the right direction on how to achieve this? Maybe you even need more then just setting fields correctly (like a custom script on top that does the calculation)?

1 Like

Hi,

  • Version 12, you can go for custom script.
  • Version 13, use server scripts as they let you run python code.

Regards
@hrwx

 frappe.ui.form.on("Quotation", {
  validate: function(frm) {
	var total_price_list_rate = 0;
            var total_margin_amount = 0;
	$.each(frm.doc.items || [], function(i, d) {
		total_price_list_rate+= flt(d.price_list_rate) * flt(d.qty);
	});
	frm.set_value("total_price_list_rate", total_price_list_rate);
}
});

This is how i calculated total pricelist rate in quotation

I am yet in v11, but Custom Scripts exist there as well.

I can not write any though … will try figuring something out, thanks for the pointer anyway.


that is a custom script u use in the Quotation DocType, right?

I almost don’t dare to ask, but … what language is that to begin with? python, js ?

@vrms yes ,that is a custom script i used in Quotation Doctype

when i came to work on erpnext software im just familiar with js . so i started learning with custom script js

After that im compelled to code in serverside also.so i started learning python

i worked on .net plat form before coming into this community

Hi @vrms,

In v11, Custom Scripts is the way to go.
And that script is in js.

Regards
@hrwx