Salary Slip not updating when adding/removing Timesheet links (#11982)

* timesheet on salary slip

* changes

* cal total wages

* calculate gross_pay and net_pay

* codacy fix
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.js b/erpnext/hr/doctype/salary_slip/salary_slip.js
index 840387c..a99a919 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.js
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.js
@@ -98,6 +98,15 @@
 	}
 })
 
+frappe.ui.form.on('Salary Slip Timesheet', {
+	time_sheet: function(frm, dt, dn) {
+		total_work_hours(frm, dt, dn);
+	},
+	timesheets_remove: function(frm, dt, dn) {
+		total_work_hours(frm, dt, dn);
+	}
+});
+
 // Get leave details
 //---------------------------------------------------------------------
 cur_frm.cscript.start_date = function(doc, dt, dn){
@@ -154,11 +163,11 @@
 // Calculate earning total
 // ------------------------------------------------------------------------
 var calculate_earning_total = function(doc, dt, dn, reset_amount) {
+
 	var tbl = doc.earnings || [];
 	var total_earn = 0;
 	for(var i = 0; i < tbl.length; i++){
 		if(cint(tbl[i].depends_on_lwp) == 1) {
-
 			tbl[i].amount =  Math.round(tbl[i].default_amount)*(flt(doc.payment_days) /
 				cint(doc.total_working_days)*100)/100;
 		} else if(reset_amount) {
@@ -166,10 +175,12 @@
 		}
 		if(!tbl[i].do_not_include_in_total) {
 			total_earn += flt(tbl[i].amount);
+
 		}
 	}
 	doc.gross_pay = total_earn;
 	refresh_many(['earnings', 'amount','gross_pay']);
+
 }
 
 // Calculate deduction total
@@ -210,3 +221,31 @@
 		query: "erpnext.controllers.queries.employee_query"
 	}
 }
+
+// calculate total working hours, earnings based on hourly wages and totals
+// ------------------------------------------------------------------------
+var total_work_hours = function(frm, dt, dn) {
+	frm.set_value('total_working_hours', 0);
+
+	$.each(frm.doc["timesheets"] || [], function(i, timesheet) {
+		frm.doc.total_working_hours += timesheet.working_hours;
+	});
+	frm.refresh_field('total_working_hours');
+
+	var wages_amount = frm.doc.total_working_hours * frm.doc.hour_rate;
+
+	frappe.db.get_value('Salary Structure', {'name': frm.doc.salary_structure}, 'salary_component', (r) => {
+		frm.set_value('gross_pay', 0);
+
+		$.each(frm.doc["earnings"], function(i, earning) {
+			if (earning.salary_component == r.salary_component) {
+				earning.amount = wages_amount;
+				frm.refresh_fields('earnings');
+			}
+			frm.doc.gross_pay += earning.amount;
+		});
+
+		frm.refresh_field('gross_pay');
+		calculate_net_pay(frm.doc, dt, dn);
+	});
+}
\ No newline at end of file
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.py b/erpnext/hr/doctype/salary_slip/salary_slip.py
index 656a4ac..a474569 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.py
@@ -463,4 +463,4 @@
 	if linked_ss:
 		for ss in linked_ss:
 			ss_doc = frappe.get_doc("Salary Slip", ss)
-			frappe.db.set_value("Salary Slip", ss_doc.name, "journal_entry", "")
+			frappe.db.set_value("Salary Slip", ss_doc.name, "journal_entry", "")
\ No newline at end of file