Calculate Total Qty of items table of Sales Invoice

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.

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