Calculate Net Total For Items Table

Hello everyone,

I am trying to calculate the net total for the items in this table.

I am using the Custom Script in ERPNext. Here is what the code that I have tried using so far:

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);
  }
},

});

Any help would be appreciated.

Hey @JohnDoe try the below-given code please change the variable according to your requirement

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

    function calculateTotalAmountOfChild(){
            var totalAmount = 0;
            for(var i = 0; i < cur_frm.doc['items'].length; i++)
            {
                totalAmount += cur_frm.doc['items'][i].amount;
            }
            return parseFloat(totalAmount)
        }

    function setReceiptPaymentAmount(totalAmount){
        if(cur_frm.doc.entry_type == 'Receipt'){
            cur_frm.set_value("receipt_amount",parseFloat(totalAmount))
            cur_frm.refresh_field("receipt_amount")
        }
        else {
            cur_frm.set_value("grand_total",parseFloat(totalAmount))
            cur_frm.refresh_field("gran_total")
        }
    }

Hey @ROHAN_JAIN1, thanks for your reply.

So, does something like this look right to you?

I tried this, but it doesn’t seem to work even if I submit the form.

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

    function calculateTotalAmountOfChild(){
            var grand_total = 0;
            for(var i = 0; i < cur_frm.doc['items'].length; i++)
            {
                grand_total += cur_frm.doc['items'][i].amount;
            }
            return parseFloat(grand_total);
    }

Dear JohnDoe,

we have 2 extra cloumn in our prices one is item rate other is installation rate we are showing total at bottom fields one is total_item_rate other is total_installation_rate. my calculate script at below you can modify and call in your function as you need.

cur_frm.cscript.calculate = function(frm, cdt, cdn){
  var row = locals[cdt][cdn];
  var total_item = 0;
  var total_installation = 0;
  cur_frm.doc.items.forEach(function(row) {
    total_item += (row.item_rate * row.qty);
    total_installation += (row.installation_rate * row.qty);
  });
  cur_frm.set_value('total_item_rate', total_item);
  cur_frm.set_value('total_installation_rate', total_installation);
  cur_frm.refresh_field('total_item_rate');
  cur_frm.refresh_field('total_installation_rate');
};

I don’t think you understand what I need. I have a table of products which you can put multiple rows of the products each with their own rates and quantity. I want to calculate all the totals of the rate * quantity and I want it to calculate it even though you don’t know the number of products in the table.

I already have a code that calculates quantity * rate for each row. The outcome for each row is the Amount column.

So like I want amount1 + amount2 +… Maybe this photo will help.

cur_frm.doc.items.forEach(function(row) already do calculation for unknown number of row and my script can easy tweaked in your use like below…

cur_frm.cscript.calculate = function(frm, cdt, cdn){
  var row = locals[cdt][cdn];
  var total_amount = 0;
  cur_frm.doc.items.forEach(function(row) {
    total_amount += row.amount;
  });
  cur_frm.set_value('your_total_amount_field_in_main_doctype', total_amount);
  cur_frm.refresh_field('your_total_amount_field_in_main_doctype');
};