Sales Invoice Tax Calculation Customization

Hello,

I need to change the tax rate of an item based on a condition. I have written a custom script which is updating the fields in the UI but once I save, the tax values are not updated in the Sales Invoice totals.

How can I resolve this?

Have a look at my custom script below:

frappe.ui.form.on(“Sales Invoice Item”, “rate”, function (frm, cdt, cdn) {

console.log("doc");

if (frm.doc.items[0].item_tax_template == "B") {

   

    console.log("item is B");



    if (frm.doc.items[0].base_amount < frm.doc.items[0].rate) {

        // change vat to 26.52

                     

        const str = '{"VAT - ORBS-": 16.0}';

        const replaced = str.replace(/[0-9]/g, '26.52');

       

        frm.doc.items[0].item_tax_rate = replaced.slice(0, 20) +"}";

        //frm.doc.set_value("item_tax_rate", replaced.slice(0, 20) + "}");

        //frappe.model.set_value(cdt, cdn, "item_tax_rate", replaced.slice(0, 20) + "}"); \

       

        console.log(replaced.slice(0, 20) + "}");

        //frm.doc.set_value(.item_tax_rate, replaced.slice(0, 20) + "}");

        //frm.doc.items[0].set_value("item_tax_rate",replaced.slice(0, 20) +"}");

       

    } else {

        // keep defailt vat of 16.00

        console.log("item does not match condition")

    }

} else {

    console.log("nothing happened");

}

});