refactor: Patient Appointment status
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js
index 7c6b011..133fee1 100644
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.js
@@ -108,15 +108,6 @@
 			}, __('Create'));
 		}
 
-		if (frm.doc.status == 'Pending') {
-			frm.add_custom_button(__('Set Open'), function() {
-				update_status(frm, 'Open');
-			});
-			frm.add_custom_button(__('Cancel'), function() {
-				update_status(frm, 'Cancelled');
-			});
-		}
-
 		frappe.db.get_value('Healthcare Settings', {name: 'Healthcare Settings'}, 'automate_appointment_invoicing', (settings) => {
 			if (settings.automate_appointment_invoicing) {
 				frm.set_df_property('mode_of_payment', 'hidden', 0);
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.json b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.json
index f9dc892..c0119fe 100644
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.json
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.json
@@ -89,7 +89,7 @@
    "in_filter": 1,
    "in_list_view": 1,
    "label": "Status",
-   "options": "\nScheduled\nOpen\nClosed\nPending\nCancelled",
+   "options": "\nScheduled\nOpen\nClosed\nCancelled",
    "read_only": 1,
    "search_index": 1
   },
@@ -257,8 +257,7 @@
    "fieldtype": "Link",
    "ignore_user_permissions": 1,
    "label": "Referring Practitioner",
-   "options": "Healthcare Practitioner",
-   "set_only_once": 1
+   "options": "Healthcare Practitioner"
   },
   {
    "default": "0",
@@ -278,7 +277,7 @@
   }
  ],
  "links": [],
- "modified": "2020-02-25 11:29:26.988952",
+ "modified": "2020-02-25 13:09:50.055119",
  "modified_by": "Administrator",
  "module": "Healthcare",
  "name": "Patient Appointment",
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
index ecabbe3..888ecff 100755
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment.py
@@ -20,6 +20,7 @@
 		self.validate_overlaps()
 		self.set_appointment_datetime()
 		self.validate_customer_created()
+		self.set_status()
 
 	def after_insert(self):
 		invoice_appointment(self)
@@ -27,14 +28,17 @@
 		self.check_fee_validity()
 		send_confirmation_msg(self)
 
-	def on_update(self):
+	def set_status(self):
 		today = getdate()
 		appointment_date = getdate(self.appointment_date)
 
-		# If appointment is created for today set status as Open
-		if today == appointment_date:
-			frappe.db.set_value('Patient Appointment', self.name, 'status', 'Open')
-			self.reload()
+		# If appointment is created for today set status as Open else Scheduled
+		if appointment_date == today:
+			self.status = 'Open'
+		elif appointment_date > today:
+			self.status = 'Scheduled'
+		elif appointment_date < today:
+			self.status = 'Expired'
 
 	def validate_overlaps(self):
 		end_time = datetime.datetime.combine(getdate(self.appointment_date), get_time(self.appointment_time)) \
@@ -427,3 +431,12 @@
 	from `tabPatient Encounter` ct, `tabProcedure Prescription` pp
 	where ct.patient='{0}' and pp.parent=ct.name and pp.appointment_booked=0
 	order by ct.creation desc""".format(patient))
+
+def update_appointment_status():
+	# update the status of appointments daily
+
+	frappe.db.sql("""update `tabPatient Appointment` set status = 'Open'
+		where appointment_date = CURDATE() and status = 'Scheduled'""")
+
+	frappe.db.sql("""update `tabPatient Appointment` set status = 'Expired'
+		where appointment_date < CURDATE() and status NOT IN ('Closed', 'Cancelled')""")
\ No newline at end of file
diff --git a/erpnext/healthcare/doctype/patient_appointment/patient_appointment_list.js b/erpnext/healthcare/doctype/patient_appointment/patient_appointment_list.js
index 701cb69..721887b 100644
--- a/erpnext/healthcare/doctype/patient_appointment/patient_appointment_list.js
+++ b/erpnext/healthcare/doctype/patient_appointment/patient_appointment_list.js
@@ -3,4 +3,14 @@
 */
 frappe.listview_settings['Patient Appointment'] = {
 	filters: [["status", "=", "Open"]],
+	get_indicator: function(doc) {
+		var colors = {
+			"Open": "orange",
+			"Scheduled": "yellow",
+			"Closed": "green",
+			"Cancelled": "red",
+			"Expired": "grey"
+		};
+		return [__(doc.status), colors[doc.status], "status,=," + doc.status];
+	}
 };
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index bc3dffa..1f8eb09 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -309,7 +309,8 @@
 		"erpnext.support.doctype.service_level_agreement.service_level_agreement.check_agreement_status",
 		"erpnext.crm.doctype.email_campaign.email_campaign.send_email_to_leads_or_contacts",
 		"erpnext.crm.doctype.email_campaign.email_campaign.set_email_campaign_status",
-		"erpnext.selling.doctype.quotation.quotation.set_expired_status"
+		"erpnext.selling.doctype.quotation.quotation.set_expired_status",
+		"erpnext.healthcare_healthcare.doctype.patient_appointment.patient_appointment.update_appointment_status"
 	],
 	"daily_long": [
 		"erpnext.setup.doctype.email_digest.email_digest.send",