Calculate Total Qty of items table of Sales Invoice

Hi.

I wanted a total quantity in stock entry(material receipt). So I added custom field with title Total Qty as float and read only. Then added below script to stock entry but not showing anything.

frappe.ui.form.on(“Stock Entry”, “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);
});

Am I doing any thing wrong?

Also while adding section and column break before Total Qty it is asking me to add label! What to do?

Thanks for a great software.

It should be “Stock Entry Detail”

Enter a label for Section and Column breaks too

Hi nabin.
I tried but not showing Total Qty yet. Also column break name and section break name showing before total qty?
Please see attached.

Anyone?

try using this one, its working for me

 frappe.ui.form.on("Stock Entry Detail", {
	qty: function(frm, cdt, cdn) {
		var d = locals[cdt][cdn];
		var total = 0;
frappe.model.set_value(d.doctype, d.name, "qty", d.qty);        
frm.doc.items.forEach(function(d) { total += d.qty; });
        frm.set_value('total_qty', total);
	}
});
3 Likes

Hi. I tried a code you sent, but not working.
Is it because of column & section break not set properly? Why both’s lable is visible above Total Qty??? :confused:

Hi.
Got it.
It was Column and Section Break making issue. I dont know how but I tried to delete lable, and it worked.

Will add this script to every forms I require and ask if stuck.

Thanks.

1 Like

@nabinhait
Hi Nabin,

Computer doesnt make mistake, but I am getting an issue in total qty! Total qty should be 263043 but showing 518598?
Below is script I used as you instruct me. Custom field name is Total Qty(total_qty)

frappe.ui.form.on(“Stock Entry Detail”, “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);
});

@mdwala Hi

for (var i = 0; i < cur_frm.get_field(“YOUR_CHILD_TABLE_FIELD”).grid.grid_rows.length; i++) {
total_qty += cur_frm.get_field(“YOUR_CHILD_TABLE_FIELD”).grid.grid_rows[i].doc.field_under_child_table;
}

Hi. Solved,
Use @nabinhait’s script again. What I did was deleted script, field, Added field, added script and worked again.

@johnskywalker, I didnt get any result from your script. After using your script new material entry page stucked. Why?

2 Likes

Can you send us the logs? Anyway this is solved already right? :slight_smile:

hi
Iam facing a similar situation
need to create a total qty in stock entry
did the following

  1. created a field “Total Qty” (total_qty) in “STOCK ENTRY”
  2. created a custom script for STOCK ENTRY and entered the following code
    frappe.ui.form.on(“Stock Entry Detail”, “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);
    });

but the results are not being displayed
if you have solved it, could you please help me with a step by step procedure
Regards
Hemanth

Try this if it works

frappe.ui.form.on("Stock Entry", "refresh", 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);
    });
2 Likes

Hi jof2jc
thanks a lot
that code worked like charm
it works only with new stock entries
older stock entries are throwing up error
what language is used here
can you guide me to some learning material
thanks a lot again
Hemanth

1 Like

This topic was automatically closed after 24 hours. New replies are no longer allowed.