Sum Custom column in child table and show total in parent (custom field)

Dear All
I have custom fields in Sales Invoice Items I need to show the total of these fields in Sales Invoice but my code duplicates the total
please advise me
my code is :

frappe.ui.form.on(“Sales Invoice Item”, {
discount_amount:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.amount_before_discount; });
frm.set_value(“total_amount_before_discount”, total);
refresh_field(“total_amount_before_discount”);
},
items_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.amount_before_discount; });
frm.set_value(“total_amount_before_discount”, total);
refresh_field(“total_amount_before_discount”);
}
});

frappe.ui.form.on(“Sales Invoice Item”, {
qty:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.amount_before_discount; });
frm.set_value(“total_amount_before_discount”, total);
refresh_field(“total_amount_before_discount”);
},
items_remove:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.items.forEach(function(d) { total += d.amount_before_discount; });
frm.set_value(“total_amount_before_discount”, total);
refresh_field(“total_amount_before_discount”);
}
});

@msiam Try the following code…
It will update the total_amount_before_discount when the amount_before_discount is changed in a row and when the row is removed…

frappe.ui.form.on('Sales Invoice Item', {
    items_remove: function(frm, cdt, cdn) {
        update_total_amount_before_discount(frm);
    },
    amount_before_discount: function(frm, cdt, cdn) {
        update_total_amount_before_discount(frm);
    },
});

function update_total_amount_before_discount(frm) {
    var total = 0;
    frm.doc.items.forEach(function(d) {
        total += flt(d.amount_before_discount);
    });
    frm.set_value('total_amount_before_discount', total);
}

the same issue
the total is duplicate
@kid1194

@msiam What do you mean by duplicate? Can you please explain more?

the total must equal 215
my code show an equal 330
I don’t know why to duplicate the total @kid1194

@msiam If the total must be 215 then double that is 430 and not 330…
There must be something that cause the value to be different…

You can use the following code and when the total is updated, check the console to see the actual total that the code has calculated…
If the total appears in console is the right value, then the code works perfectly and there is something else that causes this problem…

frappe.ui.form.on('Sales Invoice Item', {
    items_remove: function(frm, cdt, cdn) {
        update_total_amount_before_discount(frm);
    },
    amount_before_discount: function(frm, cdt, cdn) {
        update_total_amount_before_discount(frm);
    },
});

function update_total_amount_before_discount(frm) {
    var total = 0;
    frm.doc.items.forEach(function(d) {
        total += flt(d.amount_before_discount);
    });
    console.log('The total amount before discount is:', total);
    frm.set_value('total_amount_before_discount', total);
}

the console doesn’t appear
plz check your inbox

@msiam I have replied to your message…
I hope that your problem is resolved…

not working plz check your inbox bro