Merge pull request #17045 from rmehta/fix-sla-auto-update

fix(sla): update agreement status on save'
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index c1ac407..2fac232 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -241,8 +241,7 @@
 		"erpnext.erpnext_integrations.doctype.amazon_mws_settings.amazon_mws_settings.schedule_get_order_details",
 		"erpnext.accounts.doctype.gl_entry.gl_entry.rename_gle_sle_docs",
 		"erpnext.projects.doctype.project.project.hourly_reminder",
-		"erpnext.projects.doctype.project.project.collect_project_status",
-		"erpnext.support.doctype.issue.issue.update_support_timer",
+		"erpnext.projects.doctype.project.project.collect_project_status"
 	],
 	"daily": [
 		"erpnext.stock.reorder_item.reorder_item",
diff --git a/erpnext/support/doctype/issue/issue.py b/erpnext/support/doctype/issue/issue.py
index e690429..2bee844 100644
--- a/erpnext/support/doctype/issue/issue.py
+++ b/erpnext/support/doctype/issue/issue.py
@@ -65,10 +65,20 @@
 			self.first_responded_on = now()
 		if self.status=="Closed" and status !="Closed":
 			self.resolution_date = now()
+			self.update_agreement_status()
 		if self.status=="Open" and status !="Open":
 			# if no date, it should be set as None and not a blank string "", as per mysql strict config
 			self.resolution_date = None
 
+	def update_agreement_status(self):
+		current_time = frappe.flags.current_time or now_datetime()
+		if self.service_level_agreement:
+			if (round(time_diff_in_hours(self.response_by, current_time), 2) < 0
+				or round(time_diff_in_hours(self.resolution_by, current_time), 2) < 0):
+				self.agreement_status = "Failed"
+			else:
+				self.agreement_status = "Fulfilled"
+
 	def create_communication(self):
 		communication = frappe.new_doc("Communication")
 		communication.update({
@@ -265,18 +275,6 @@
 	"""Called when Contact is deleted"""
 	frappe.db.sql("""UPDATE `tabIssue` set contact='' where contact=%s""", contact.name)
 
-def update_support_timer():
-	issues = frappe.get_list("Issue", filters={"status": "Open"}, order_by="creation DESC")
-	for issue in issues:
-		issue = frappe.get_doc("Issue", issue.name)
-
-		if round(time_diff_in_hours(issue.response_by, now_datetime()), 2) < 0 or round(time_diff_in_hours(issue.resolution_by, now_datetime()), 2) < 0:
-			issue.agreement_status = "Failed"
-		else:
-			issue.agreement_status = "Fulfilled"
-		issue.save()
-
-
 def get_holidays(holiday_list_name):
 	holiday_list = frappe.get_cached_doc("Holiday List", holiday_list_name)
 	holidays = [holiday.holiday_date for holiday in holiday_list.holidays]
diff --git a/erpnext/support/doctype/issue/test_issue.py b/erpnext/support/doctype/issue/test_issue.py
index 4675874..2cd7601 100644
--- a/erpnext/support/doctype/issue/test_issue.py
+++ b/erpnext/support/doctype/issue/test_issue.py
@@ -35,6 +35,24 @@
 		self.assertEquals(issue.response_by, datetime.datetime(2019, 3, 4, 18, 0))
 		self.assertEquals(issue.resolution_by, datetime.datetime(2019, 3, 6, 12, 0))
 
+		frappe.flags.current_time = datetime.datetime(2019, 3, 3, 12, 0)
+
+		issue.status = 'Closed'
+		issue.save()
+
+		self.assertEqual(issue.agreement_status, 'Fulfilled')
+
+		issue.status = 'Open'
+		issue.save()
+
+		frappe.flags.current_time = datetime.datetime(2019, 3, 5, 12, 0)
+
+		issue.status = 'Closed'
+		issue.save()
+
+		self.assertEqual(issue.agreement_status, 'Failed')
+
+
 
 def make_issue(creation, customer=None):