I’m trying to calculate total amount using client script function. However, some of the caluclations return wrong values or do not refresh unless triggered multiple times:

I have different discount bands depending on total days:

Band A: Above 7 days = discount is 31,500

Band B: Above 30 days = discount is 29,750

Band C: Above 90 days = discount is 28,000

Band D: Above 365 days - discount is 26,250

If total days = Band A, calculation is correct

If total days is however greater than Band A, initial result returned is wrong. I have to trigger change of total_days multiple times before calculation recognises the changes in the new band. And sometimes the calculation returns with fractional errors. I have been trying to figure out where by script is wrong but can’t find it. Any help will be appreciated. Below is my script.

```
total_days: function(frm) {
var a = (frm.doc.total_days * frm.doc.rate) + frm.doc.caution_fee;
var b = (frm.doc.total_days * frm.doc.discount_amount) + frm.doc.caution_fee;
var c = ((frm.doc.total_days * frm.doc.discount_amount) * 0.10);
if (frm.doc.total_days * frm.doc.discount_amount < 1000000) {
frm.set_value("caution_fee", 60000);
}
if (frm.doc.total_days * frm.doc.discount_amount > 999999) {
frm.set_value("caution_fee", c);
}
if (frm.doc.total_days < 7) {
frm.set_value("total_amount", a);
frm.set_value("total_received", a);
}
if (frm.doc.total_days > 6 && frm.doc.total_days < 31) {
frm.set_value("discount_amount", 31500);
frm.refresh_field("discount_amount");
frm.set_value("total_amount", b);
frm.set_value("total_received", b);
}
if (frm.doc.total_days > 30 && frm.doc.total_days < 91) {
frm.set_value("discount_amount", 29750);
frm.refresh_field("discount_amount");
frm.set_value("total_amount", b);
frm.set_value("total_received", b);
}
else if (frm.doc.total_days > 90 && frm.doc.total_days < 366) {
frm.set_value("discount_amount", 28000);
frm.set_value("total_amount", b);
frm.set_value("total_received", b);
}
else if (frm.doc.total_days > 365) {
frm.set_value("discount_amount", 26250);
frm.set_value("total_amount", b);
frm.set_value("total_received", b);
}
frm.refresh_field("discount_amount")
frm.refresh_field("caution_fee")
frm.refresh_field("total_amount");
frm.refresh_field("total_received");
},
```