Try ERPNext Buy Support Partners Foundation

Net Total for custom doctype

Hello all,

I have a sales order doctype that I would like to calculate the net total for the items table.

I currently have the following code, but it doesn’t seem to work. The child doctype for this table is Sales Order Product.

frappe.ui.form.on(“Sales Order Product”, “total1”, function (frm, cdt, cdn) {
var total1 = 0;
$.each(frm.doc.sales_order_product || [], function (i, d) {
total1 += flt(d.total);
});
frm.set_value(“total”, total1);
});

Hi
may this function help

let calculate_total_amount = function (frm, dt, dn){
        let total_amount = 0.0;
		var tbl = cur_frm.doc.items || [];
		for(var i = 0; i < tbl.length; i++) {
			total_amount+=flt(tbl[i].amount)
		}
	return total_amount
}

Would you apply this to the child or the parent doc? I am just using a custom script within ERPNext.

apply this code to both the child doc and the parent

frappe.ui.form.on('Child doctype', {
	amount: function(frm, dt, dn){
		calculate_total_amount(frm, dt, dn)
	},

});


frappe.ui.form.on('Parent Doctype', {
	onload_post_render: function(frm, dt, dn){
	  if(frm.doc.docstatus==0 || frm.doc.docstatus==2)
	  {
		calculate_total_amount(frm, dt, dn)
	  }
	},

});
		
		
let calculate_total_amount = function (frm, dt, dn){
		let total_amount = 0.0;
		var tbl = cur_frm.doc.items || [];
		for(var i = 0; i < tbl.length; i++) {
			total_amount+=flt(tbl[i].amount)
		}

		frm.set_value("total",total_amount);
		refresh_field("total")
}

this will ensure that every time you enter the doctype it will calculate the amount and set total
and when you change the amount in child doc also will calculate it again according to current number

So it didn’t allow me to use a function before defining it so I had to put the top two pieces of code at the bottom, but it doesn’t seem to have worked.

let calculate_total_amount = function (frm, dt, dn){
	let total_amount = 0.0;
	var tbl = cur_frm.doc.items || [];
	for(var i = 0; i < tbl.length; i++) {
		total_amount+=flt(tbl[i].amount);
	}

	frm.set_value("Total",total_amount);
	refresh_field("Total");

};

frappe.ui.form.on(‘Sales Order Product’, {
amount: function(frm, dt, dn){
calculate_total_amount(frm, dt, dn);
},

});

whoops heres the full code

let calculate_total_amount = function (frm, dt, dn){
	let total_amount = 0.0;
	var tbl = cur_frm.doc.items || [];
	for(var i = 0; i < tbl.length; i++) {
		total_amount+=flt(tbl[i].amount);
	}

	frm.set_value("Total",total_amount);
	refresh_field("Total");

};

frappe.ui.form.on('Sales Order Product', {
    amount: function(frm, dt, dn){
	  calculate_total_amount(frm, dt, dn);
},

});

frappe.ui.form.on('Sales Order 1', {
    onload_post_render: function(frm, dt, dn){
      if(frm.doc.docstatus==0 || frm.doc.docstatus==2)
  {
	calculate_total_amount(frm, dt, dn);
  }
},
});

Does it work with you ???

No. Tried changing some things around too. The total doesn’t display when I enter values in.

would you share the code or would you let me see it by anydesk or team viewer

Here you go

let calculate_total_amount = function (frm, dt, dn){
	let total_amount = 0.0;
	var tbl = cur_frm.doc.items || [];
	for(var i = 0; i < tbl.length; i++) {
		total_amount+=flt(tbl[i].amount);
	}

	frm.set_value("Total",total_amount);
	refresh_field("Total");
};

frappe.ui.form.on('Sales Order Product', {
    amount: function(frm, dt, dn){
	calculate_total_amount(frm, dt, dn);
  },

});


frappe.ui.form.on('Sales Order 1', {
  onload_post_render: function(frm, dt, dn){
  if(frm.doc.docstatus==0 || frm.doc.docstatus==2)
  {
	calculate_total_amount(frm, dt, dn);
  }
},

});

OK, So what happen when you enter the doctype or change amount
you can see what happen in console (by right click , inspect)
please share what happen in console

So here is the error that appeared multiple times in the console:

Uncaught (in promise) TypeError: Cannot read property ‘doc’ of undefined
at field.df.onchange (grid_row.js:399)
at o.field.df.onchange (grid_row.js:398)
at base_control.js:158

is this “Sales Order 1” the right name to parent doctype ?

Yeah it is and “Sales Order Product” is the child doctype.