In our application, we need to change each item price rate when saving a Delivery Note, and then we need to calculate a discount amount from the new grand total. But the problem is, the grand total is not updated yet. How can I make sure that the grand total is updated right after I have changed all the item price rate?
Here’s an example of my custom script.
frappe.ui.form.on(“Delivery Note”, {
validate: function(frm) {
cur_frm.doc.items.forEach(function(item){
item.rate = 0.75*item.price_list_rate;
})
msgprint(frm.doc.grand_total); // Still showing old value of Grand Total here.
}});
Thanx. But didn’t work. I modified the code to be like this:
frappe.ui.form.on("Delivery Note", {
validate: function(frm) {
cur_frm.doc.items.forEach(function(item){
item.rate = 0.75*item.price_list_rate;
})
cur_frm.refresh_field('items');
cur_frm.refresh_fields();
msgprint(frm.doc.grand_total); // Still showing old value of Grand Total here.
}});
This code is not working because after changing rate the further calculations may not be triggered you can manually trigger those by adding following line
frm.cscript.recalculate()
But I am not sure if its the right way to do this, Can you please share the use case ?
It seems that you are applying the discount on the Price List Rate. In that case please use the Pricing Rule instade, Please check Pricing Rule