Calculate Total Qty of items table of Sales Invoice

I wrote the following custom script but it does not work.

frappe.ui.form.on(“Sales Invoice”, {
validate: function(frm) {
total_qty = 0;
$.each(frm.doc.items), function(i, d) {
total_qty += flt(d.qty);
});
frm.doc.net_weight = total_qty;
}
});

@2002m what is the issue?

Do you can see any error in browser console?
try use console.log to check the values

frappe.ui.form.on("Sales Invoice", {
	validate: function(frm) {
		total_qty = 0;
		$.each(frm.doc.items || [], function(i, d) {
			total_qty += flt(d.qty);
		});
		frm.set_value("net_weight", total_qty);
	}
});
2 Likes

Net Weight does not display in Sales Invoice form.

It displace after saving but I want onchange of qty.

frappe.ui.form.on("Sales Invoice Item", "qty", function(frm, cdt, cdn) {
	// code for calculate total and set on parent field.
	total_qty = 0;
	$.each(frm.doc.items || [], function(i, d) {
		total_qty += flt(d.qty);
	});
	frm.set_value("net_weight", total_qty);
});
3 Likes

Hello Nabinhait!

Can we also use this formula to calculate total quantity of items for Purchase orders ? I tried to do so by replacing “sales invoice” by “purchase order” but it didn’t work.

Thank you !

Yes, you can use the same function replacing the doctype name. Please ensure that you are using proper case while replacing doctype name, it should be “Purchase Order Item”. And in Custom Script, select doctype as “Purchase Order”.

Hi Nabinhait,

I have tried the following script by changing the field type to float, table and data but none work. Is my script wrong ?

Thank you in advance !

frappe.ui.form.on(“Purchase Order Item”, “qty”, function(frm, cdt, cdn) {
// code for calculate total and set on parent field.
total_qty = 0;
$.each(frm.doc.items || [], function(i, d) {
total_qty += flt(d.qty);
});
frm.set_value(“UOM”, total_qty);
});

Why are you setting the total_qty in UOM field? You should add a custom field for “Total Qty” in Purchase Order form and then set the total qty in that field.

1 Like

I have added a float custom field for “Total Qty” in Purchase Order form and created a custom script as below for purchase order. Is it what you meant by setting the total qty in Purchase order ? I feel it is maybe because my custom script is not linked to my custom field. How can I link both together ?

frappe.ui.form.on(“Purchase Order Item”, “qty”, function(frm, cdt, cdn) {
// code for calculate total and set on parent field.
total_qty = 0;
$.each(frm.doc.items || [], function(i, d) {
total_qty += flt(d.qty);
});
frm.set_value(“total_qty” , total_qty);
});

It should work. Just check that in which field you want to set the total qty. Is the fieldname of that field “total_qty”? Check you custom field record.

Hallo guys…this is very interesting! could you make a step-by-step guide for dummies please?

The custom field Total Quantity we are creating know, what data type the custom field Total Quantity should be? Please help me

Generally it should be of type “Float”

Hello @nabinhait,

i need to automaticly copy value from custom field in SI to same custom field in SI Item. Could you help me with this.

Thanks in advance.

frappe.ui.form.on("Sales Invoice", "si_field", function(frm) {
	$.each(frm.doc.items || [], function(i, d) {
		frappe.model.set_value("Sales Invoice Item", d.name, "si_item_field", frm.doc.si_field);
	});
})

The above code will run on change of custom_field in SI and it will assign the same value in all rows in SI Item.

Thank you for help. I made a mistake in my text above. I was thinking about delivery note and delivery note item. I modified your code. Is this corect + caould you tell me where to put this code because this is first time i use script. I suppose in DN custom field (broj vaucera)

frappe.ui.form.on(“Delivery Note”, “broj_vaucera”, function(frm) {
$.each(frm.doc.items || [], function(i, d) {
frappe.model.set_value(“Delivery note Item”, d.name, “broj_vaucera”, frm.doc.broj_vaucera);
});
})

It should be “Delivery Note Item”, it is case sensitive.

And you should create a new Custom Script record, select doctype as “Delivery Note” and paste the code there in script field.

2 Likes

Its working! Great! Thank you.

He don’t copy value from DN while inserting in SI item but (only first row). but at the end i go back in DN field and update value. This also helps me a lot. Thanks once more.