Custom calculation for Amount Sales order

Hi Need help on the below topic.

I have added new field area_in_sqft in the sales order item. I need amount to be updated as rate * Area_in_sqft.

I have written custom script in Sales order doc type as below.

frappe.ui.form.on(“Sales order”, “amount”, function(frm) {
frm.set_value(“amount”, frm.doc.area_in_sqft * frm.doc.rate);
});

But amount doesn’t get updated.
Please suggest if something is wrong.

Try something like this:

frappe.ui.form.on("Sales Order Item", "area_in_sqft", function(frm, cdt, cdn){
  var d = locals[cdt][cdn];

  var total_amount = 0;
  frm.doc.items.forEach(function(d) { total_amount += (d.area_in_sqft*d.rate); });

  frm.set_value('amount', total_amount);

});
1 Like

I did put this custom script in Sales Order. I am getting message as ‘amount’ not found. This custom script is it supposed to be for Sales order Item? or Sales order itself.

I have to update the amount on the Sales order Item field only.

Example like below.

In the above pic… the amount should be 2000 * 100 = 200000. But even after putting that script, no updates.

The Actual Problem as mentioned in the Diagram. Also when I click save the calculation goes off.

I have made some changes to the Script you have posted as below. After rate updation the amount gets updated but lost on save.

frappe.ui.form.on(“Sales Order Item”, “rate”, function(frm, cdt, cdn){
var d = locals[cdt][cdn];

var total_amount = 0;
frm.doc.items.forEach(function(d) { total_amount += (d.area_in_sqft*d.rate); d.amount = total_amount;});

cur_frm.refresh();
//frm.set_value(‘amount’, total_amount);

});

@bhaskar_tiwari - Did you ever get round the issue of every refresh other than rate, resulting in the total amount being changed back to Quantity* Rate

I’m trying to do the same and I have run into the same challenge.

@KanchanChauhan - do you have any suggestions why the changes are discarded after the document is saved to draft?

Just to add, this is happening on v10.0.17

@bhaskar_tiwari sir did you resolve this problem even i am going through the same the total isn’t updating self once amount changes in item table…
if you already solve that please help me with your suggestions.