Custom script of deduction calculation in sales invoice item table

Hello Community,

I have stuck with custom script of deduction calculation in sales invoice item table.

Scenario: I have 1 deduction field in sales invoice item table. And it works as
(Rate * Qty ) = Amount
Net Amount = Deduction amount - amount

Now every time it overwrite final amount with ( Rate * Qty ) when I save sales invoice.

If I add new Field " Final Amount = Amount - Deduction Amount " then how can I update all parent table Fields such as Total, Grand total, net total , rounded total etc with value of deduction amount Field in child table.

Custom Script code, Actual Invoice in MS Excel and Work Done in ERP Next is attatched below.
Your help is highly appreciated, Thanks.

Custom Script

cur_frm.add_fetch("item_code","cost_of_one_inch_of_excavation","cost_of_one_inch_of_excavation")

frappe.ui.form.on("Sales Invoice Item", "cost_of_one_inch_of_excavation", function(frm, cdt, cdn){
	var d = locals[cdt][cdn];
	frappe.model.set_value(d.doctype, d.name, "inches_not_excavated", (39 - d.actual_depth_excavated ));
	frappe.model.set_value(d.doctype, d.name, "cost_of_inches_not_excavated",(d.inches_not_excavated * d.cost_of_one_inch_of_excavation));
	frappe.model.set_value(d.doctype, d.name, "margin_rate_or_amount",(d.qty * d.cost_of_inches_not_excavated));
});

frappe.ui.form.on("Sales Invoice Item", "actual_depth_excavated", function(frm, cdt, cdn){
	var d = locals[cdt][cdn];
	frappe.model.set_value(d.doctype, d.name, "inches_not_excavated", (39 - d.actual_depth_excavated ));
	frappe.model.set_value(d.doctype, d.name, "cost_of_inches_not_excavated",(d.inches_not_excavated * d.cost_of_one_inch_of_excavation));
	frappe.model.set_value(d.doctype, d.name, "margin_rate_or_amount",(d.qty * d.cost_of_inches_not_excavated));	
});

frappe.ui.form.on("Sales Invoice Item", "qty", function(frm, cdt, cdn){
	var d = locals[cdt][cdn];
	frappe.model.set_value(d.doctype, d.name, "inches_not_excavated", (39 - d.actual_depth_excavated ));
	frappe.model.set_value(d.doctype, d.name, "cost_of_inches_not_excavated",(d.inches_not_excavated * d.cost_of_one_inch_of_excavation));
	frappe.model.set_value(d.doctype, d.name, "margin_rate_or_amount",(d.qty * d.cost_of_inches_not_excavated));
});

frappe.ui.form.on("Sales Invoice Item", "margin_type", function(frm, cdt, cdn){
	var d = locals[cdt][cdn];
	frappe.model.set_value(d.doctype, d.name, "inches_not_excavated", (39 - d.actual_depth_excavated ));
	frappe.model.set_value(d.doctype, d.name, "cost_of_inches_not_excavated",(d.inches_not_excavated * d.cost_of_one_inch_of_excavation));
	frappe.model.set_value(d.doctype, d.name, "margin_rate_or_amount",(d.qty * d.cost_of_inches_not_excavated));
});