Custom Script for dynamic calculation of line total in Purchase Invoice Item

Hello,
I am struggling with a custom script that is supposed to do some dynamic calculations on each line of the purchase invoice item table. I have added 4 new custom columns/fields called Discount 1 (discount_1), Discount 2 (discount_2), Discount 3 (discount_3), Line Total (line_total) and what I want is for the line_total to take a value which depends on the values of the discount fields as well as the value of the Accepted Quantity (qty) and the Rate per item (rate). This should happen while the user fills out the fields dynamically. I don’t want to use Price Lists with discounts, because it can be that the same item from the same supplier comes with a different discount every time.

What I have done so far is this:

I have created a Custom Script on docType “Purchase Invoice” (there is no docType called “Purchase Invoice Item”) and I have added the following:

// calculate the line total for a Purchase Invoice Item
frappe.ui.form.on(‘Purchase Invoice’, {
validate: function(frm){
line_total = ((( (qty * rate) * (1 - discount_1)) * (1 - discount_2)) * (1 - discount_3));
}
});

But this doesn’t work. I have also tried to use the onBlur event without success.
My primary concern is that my code is not executed during the event of focusing out of the field. For example the built-in field amount, takes its value dynamically from the multiplied values of Accepted Quantity (qty) and Rate (rate). I am trying to achieve a similar behaviour.

A few screenshots with my Form fields and the table with actual values are attached here.

Any direction towards the solution would be highly appreciated.