Wanted total of BOM qty!

code I tried

//setting total qty

frappe.ui.form.on(‘BOM’, ‘validate’, function(frm) {
set_total_qty(frm);
})

frappe.ui.form.on(‘BOM Item’, ‘qty’, function(frm, cdt, cdn) {
set_total_qty(frm);
})

var set_total_qty = function(frm) {
var total_qty = 0.0;
$.each(frm.doc.items, functions(i, row) {
total_qty += flt(row.qty);
})
frm.set_value(‘total_qty’, total_qty);
frm.refresh();
}

Here is an example for getting the total qty
I have done this on the stock entry form and its working fine
Iam on V12

example
stock entry form
stock entry details — child

create a field “total qty” in “stock entry”
go to custom script
select stock entry
enter this code

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);
});
hope this helps
regards
Hemanth

Hi Hemanth,

I tried it unfortunately it still giving errors on my end the qty I want summation of is the child of the parent doctype which is “BOM Item” .

you should try this.

frappe.ui.form.on('BOM Item', {
    qty: function(frm,cdt,cdn) {
	
	set_total_qty(cur_frm)

    },
    item_code: function(frm,cdt,cdn){
	 setTimeout(function(){ 	set_total_qty(cur_frm) },1500);

    }
})
frappe.ui.form.on('BOM', {
    validate: function(frm,cdt,cdn) {

	set_total_qty(frm)
   }

})

function set_total_qty(frm) {
	var doc = locals[frm.doc.doctype][frm.doc.name]
        var total_qty = 0
        $.each(doc.items, function(i, d) {
            total_qty += flt(d.qty)
        });
	frm.set_value("total_qty",total_qty)
}

Hi,

This gives no errors but the value aint computing also.

I have tried this out but the page aint loading and can’t figure out the mistake

frappe.ui.form.on(‘BOM’, {
refresh(frm) {
set_total_qty(frm);
}
});

frappe.ui.form.on(‘BOM Item’, {
refresh(frm) {
set_total_qty(frm);
}
});

var set_total_qty = function(frm){
var total_qty=0.0;
$.each(frm.doc.items, function(i,row){
total_qty += flt(row.qty);
});
frm.set_value(‘total_qty’, total_qty);
frm.refresh();
};

share you custom script screen shot and also check any error in console??

There is no error and but the total value still shows 0.
I have also checked the namingeverything seems fine.

what doctype did you select in custom script??

BOM is the doc type

you try this one … item_code and qty change event necessary which you not added.

frappe.ui.form.on('BOM Item', {
    qty: function(frm,cdt,cdn) {
	
	set_total_qty(cur_frm)

    },
    item_code: function(frm,cdt,cdn){
	 setTimeout(function(){ 	set_total_qty(cur_frm) },1500);

    }
})
frappe.ui.form.on('BOM', {
    validate: function(frm,cdt,cdn) {

	set_total_qty(frm)
   }

})

function set_total_qty(frm) {
	var doc = locals[frm.doc.doctype][frm.doc.name]
        var total_qty = 0
        $.each(doc.items, function(i, d) {
            total_qty += flt(d.qty)
        });
	frm.set_value("total_qty",total_qty)
}

Thanks a lot this worked

Another question:
How will the script for total cost (currency) divided by total qty (float) be like could you help out with this ?

given I want to find rate per kg for the same

what the ( BOM Item ) in the first line mean ??? is it the from of BOM