Get total amount

hi

please need help to calculate the total amount

Which doctype is this.

if its a custom, You can assign a callback in js.

You can refer the code in sales invoice/purchase invoice etc.

Regards,

Parth

You can use custom script or python file for that.

how ?

https://erpnext.org/docs/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples

https://frappe.io/docs/user/en/tutorial/controllers

https://frappe.io/docs/user/en/tutorial/

I write this script but not working

frappe.ui.form.on("Expenses Details", {
    amount: function(frm, cdt, cdn) {
        var d = locals[cdt][cdn];
        var total = 0;
        frm.doc.items.forEach(function(d) {total += d.amount; });
        frm.set_value('total_exp', total);
    }
});

@Kamel try this in your Parent Doctype.

frappe.ui.form.on("Child Table Name", "amount", function(frm, cdt, cdn) {

   var table = frm.doc.child_table_field_name;
   var total = 0;
   for(var i in table) {
       total = total + table[i].amount;
	}

	frm.set_value("total_exp",total);
        
});
frappe.ui.form.on("Expense Claim", {
	amount: function(frm){
                total=0
		$.each(frm.doc.expenses_detail|| [], function(i, d) {
			total+=d.warehouse;
		});
		frm.set_value(‘total_exp’, total);
	}
});

where Expense Claim is parent doctype.
expenses_detail is child table field name in parent doctype

it’s not working this the structure for doc

@Kamel
& can you share your tried script here?

Is your script looks like this?

frappe.ui.form.on("Expenses Details", "amount", function(frm, cdt, cdn) {

   var table = frm.doc.expenses_detail;
   var total = 0;
   for(var i in table) {
       total = total + table[i].amount;
	}

	frm.set_value("total_exp",total);
        
});

Note : Put this script on Parent Doctype, not in Expenses Details.

3 Likes

Thanks , Many Thanks , it’s working

Hi Shahid,

I’m trying the same script to calculate the total amount of Material request items.

my fields are -
Child Table : Material Request Item
Child Table amount : amount
Total in Parent : total

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

is there anything missing?, since i’m not getting neither a result nor an error

Use the same syntax, i.e for loop

can you please paste the code which is working

Hello, I try your proposed script for my form , As a result, it only prints the number zero and does not display the total sum.
Where do you think the problem comes from?
Thank you in advance for your help

@erfaneh_sahraii , hope this will help.

frappe.ui.form.on('Material Request', {
	refresh(frm) {
      frappe.ui.form.on("Material Request Item", {
        qty:function(frm, cdt, cdn){
            var d = locals[cdt][cdn];
            var total = 0;
            frm.doc.items.forEach(function(d) { total += d.qty; });
            frm.set_value("total_qty", total);
            refresh_field("total_qty");
            },
        items_remove:function(frm, cdt, cdn){
            var d = locals[cdt][cdn];
            var total = 0;
            frm.doc.items.forEach(function(d) { total += d.qty; });
            frm.set_value("total_qty", total);
            refresh_field("total_qty");
            }
        });
    }
})

Hello, I wrote the script you suggested but it didn’t work.
Instead of ‘Material Request’ , I should write the parent or child doctype name?
I tried with both but it didn’t work . :frowning_face: :frowning_face:

“Material Request” is Parent.

@Suresh_Thakor
I tried Parent but it didn’t work .

Hi

Try this

frappe.ui.form.on('Material Request', {
	calculate_total_quantity: frm => {
	    let total_quantity = flt(0.0);
	    frm.doc.items.forEach(item => {
	        if(item.qty) {
                total_quantity += flt(item.qty);
	        }
	    });
	    frm.set_value('total_quantity', total_quantity);
	}
});

frappe.ui.form.on('Material Request Item', {
	qty: function(frm) {
	    frm.trigger('calculate_total_quantity');
	},
	item_code: function(frm) {
	    frm.trigger('calculate_total_quantity');
	}
});

frappe.ui.form.on('Material Request', {
	before_save: function(frm) {
	    frm.trigger('calculate_total_quantity');
	}
});