fix: fields fetched incosistently from appointment and patient
diff --git a/erpnext/healthcare/doctype/patient_encounter/patient_encounter.js b/erpnext/healthcare/doctype/patient_encounter/patient_encounter.js
index 0509259..dd2a55f 100644
--- a/erpnext/healthcare/doctype/patient_encounter/patient_encounter.js
+++ b/erpnext/healthcare/doctype/patient_encounter/patient_encounter.js
@@ -19,7 +19,8 @@
 	refresh: function(frm) {
 		refresh_field('drug_prescription');
 		refresh_field('lab_test_prescription');
-		if (!frm.doc.__islocal){
+
+		if (!frm.doc.__islocal) {
 			frappe.call({
 				method: 'frappe.client.get_value',
 				args: {
@@ -28,239 +29,249 @@
 					filters: {name: frm.doc.patient}
 				},
 				callback: function(data) {
-					if(data.message && data.message.inpatient_status == "Admission Scheduled" || data.message.inpatient_status == "Admitted"){
+					if (data.message && data.message.inpatient_status == 'Admission Scheduled' || data.message.inpatient_status == 'Admitted') {
 						frm.add_custom_button(__('Schedule Discharge'), function() {
 							schedule_discharge(frm);
 						});
 					}
-					else if(data.message.inpatient_status != "Discharge Scheduled"){
+					else if (data.message.inpatient_status != 'Discharge Scheduled') {
 						frm.add_custom_button(__('Schedule Admission'), function() {
 							schedule_inpatient(frm);
 						});
 					}
 				}
 			});
+
+			frm.add_custom_button(__('Patient History'), function() {
+				if (frm.doc.patient) {
+					frappe.route_options = {'patient': frm.doc.patient};
+					frappe.set_route('patient_history');
+				} else {
+					frappe.msgprint(__('Please select Patient'));
+				}
+			},'View');
+
+			frm.add_custom_button(__('Vital Signs'), function() {
+				btn_create_vital_signs(frm);
+			},'Create');
+
+			frm.add_custom_button(__('Medical Record'), function() {
+				create_medical_record(frm);
+			},'Create');
+
+			frm.add_custom_button(__('Procedure'), function() {
+				btn_create_procedure(frm);
+			},'Create');
+
 		}
-		frm.add_custom_button(__('Patient History'), function() {
-			if (frm.doc.patient) {
-				frappe.route_options = {"patient": frm.doc.patient};
-				frappe.set_route("patient_history");
-			} else {
-				frappe.msgprint(__("Please select Patient"));
-			}
-		},"View");
-		frm.add_custom_button(__('Vital Signs'), function() {
-			btn_create_vital_signs(frm);
-		},"Create");
-		frm.add_custom_button(__('Medical Record'), function() {
-			create_medical_record(frm);
-		},"Create");
 
-		frm.add_custom_button(__("Procedure"),function(){
-			btn_create_procedure(frm);
-		},"Create");
-
-		frm.set_query("patient", function () {
+		frm.set_query('patient', function() {
 			return {
-				filters: {"status": "Disabled"}
+				filters: {'status': 'Active'}
 			};
 		});
-		frm.set_query("drug_code", "drug_prescription", function() {
+		frm.set_query('drug_code', 'drug_prescription', function() {
 			return {
 				filters: {
-					is_stock_item:'1'
+					is_stock_item: '1'
 				}
 			};
 		});
-		frm.set_query("lab_test_code", "lab_test_prescription", function() {
+		frm.set_query('lab_test_code', 'lab_test_prescription', function() {
 			return {
 				filters: {
 					is_billable:'1'
 				}
 			};
 		});
-		frm.set_query("medical_code", "codification_table", function() {
+		frm.set_query('medical_code', 'codification_table', function() {
 			return {
 				filters: {
-					medical_code_standard: frappe.defaults.get_default("default_medical_code_standard")
+					medical_code_standard: frappe.defaults.get_default('default_medical_code_standard')
 				}
 			};
 		});
-		frm.set_query("appointment", function() {
+		frm.set_query('appointment', function() {
 			return {
 				filters: {
 					//	Scheduled filter for demo ...
-					status:['in',["Open","Scheduled"]]
+					status:['in',['Open','Scheduled']]
 				}
 			};
 		});
-		frm.set_df_property("appointment", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("patient", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("patient_age", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("patient_sex", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("type", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("practitioner", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("visit_department", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("encounter_date", "read_only", frm.doc.__islocal ? 0:1);
-		frm.set_df_property("encounter_time", "read_only", frm.doc.__islocal ? 0:1);
+
+		frm.set_df_property('patient', 'read_only', frm.doc.appointment ? 1 : 0);
+	},
+
+	appointment: function(frm) {
+		frm.events.set_appointment_fields(frm);
+	},
+
+	patient: function(frm) {
+		frm.events.set_patient_info(frm);
+	},
+
+	set_appointment_fields: function(frm) {
+		if (frm.doc.appointment) {
+			frappe.call({
+				method: 'frappe.client.get',
+				args: {
+					doctype: 'Patient Appointment',
+					name: frm.doc.appointment
+				},
+				callback: function(data) {
+					frappe.model.set_value(frm.doctype, frm.docname, 'patient', data.message.patient);
+					frappe.model.set_value(frm.doctype, frm.docname, 'type', data.message.appointment_type);
+					frappe.model.set_value(frm.doctype, frm.docname, 'practitioner', data.message.practitioner);
+					frappe.model.set_value(frm.doctype, frm.docname, 'invoiced', data.message.invoiced);
+				}
+			});
+		}
+		else {
+			frappe.model.set_value(frm.doctype, frm.docname, 'patient', '');
+			frappe.model.set_value(frm.doctype, frm.docname, 'type', '');
+			frappe.model.set_value(frm.doctype, frm.docname, 'practitioner', '');
+			frappe.model.set_value(frm.doctype, frm.docname, 'invoiced', 0);
+			frappe.model.set_value(frm.doctype, frm.docname, 'patient_sex', '');
+			frappe.model.set_value(frm.doctype, frm.docname, 'patient_age', '');
+		}
+	},
+
+	set_patient_info: function(frm) {
+		if (frm.doc.patient) {
+			frappe.call({
+				method: 'erpnext.healthcare.doctype.patient.patient.get_patient_detail',
+				args: {
+					patient: frm.doc.patient
+				},
+				callback: function(data) {
+					var age = '';
+					if (data.message.dob) {
+						age = calculate_age(data.message.dob);
+					}
+					frappe.model.set_value(frm.doctype, frm.docname, 'patient_age', age);
+					frappe.model.set_value(frm.doctype, frm.docname, 'patient_sex', data.message.sex);
+				}
+			});
+		} else {
+			frappe.model.set_value(frm.doctype, frm.docname, 'patient_sex', '');
+			frappe.model.set_value(frm.doctype, frm.docname, 'patient_age', '');
+		}
 	}
 });
 
 var schedule_inpatient = function(frm) {
 	frappe.call({
-		method: "erpnext.healthcare.doctype.inpatient_record.inpatient_record.schedule_inpatient",
+		method: 'erpnext.healthcare.doctype.inpatient_record.inpatient_record.schedule_inpatient',
 		args: {patient: frm.doc.patient, encounter_id: frm.doc.name, practitioner: frm.doc.practitioner},
 		callback: function(data) {
-			if(!data.exc){
+			if (!data.exc) {
 				frm.reload_doc();
 			}
 		},
 		freeze: true,
-		freeze_message: "Process Inpatient Scheduling"
+		freeze_message: 'Process Inpatient Scheduling'
 	});
 };
 
 var schedule_discharge = function(frm) {
 	frappe.call({
-		method: "erpnext.healthcare.doctype.inpatient_record.inpatient_record.schedule_discharge",
+		method: 'erpnext.healthcare.doctype.inpatient_record.inpatient_record.schedule_discharge',
 		args: {patient: frm.doc.patient, encounter_id: frm.doc.name, practitioner: frm.doc.practitioner},
 		callback: function(data) {
-			if(!data.exc){
+			if (!data.exc) {
 				frm.reload_doc();
 			}
 		},
 		freeze: true,
-		freeze_message: "Process Discharge"
+		freeze_message: 'Process Discharge'
 	});
 };
 
 var create_medical_record = function (frm) {
-	if(!frm.doc.patient){
-		frappe.throw(__("Please select patient"));
+	if (!frm.doc.patient) {
+		frappe.throw(__('Please select patient'));
 	}
 	frappe.route_options = {
-		"patient": frm.doc.patient,
-		"status": "Open",
-		"reference_doctype": "Patient Medical Record",
-		"reference_owner": frm.doc.owner
+		'patient': frm.doc.patient,
+		'status': 'Open',
+		'reference_doctype': 'Patient Medical Record',
+		'reference_owner': frm.doc.owner
 	};
-	frappe.new_doc("Patient Medical Record");
+	frappe.new_doc('Patient Medical Record');
 };
 
 var btn_create_vital_signs = function (frm) {
-	if(!frm.doc.patient){
-		frappe.throw(__("Please select patient"));
+	if (!frm.doc.patient) {
+		frappe.throw(__('Please select patient'));
 	}
 	frappe.route_options = {
-		"patient": frm.doc.patient,
-		"appointment": frm.doc.appointment
+		'patient': frm.doc.patient,
+		'appointment': frm.doc.appointment
 	};
-	frappe.new_doc("Vital Signs");
+	frappe.new_doc('Vital Signs');
 };
 
 var btn_create_procedure = function (frm) {
-	if(!frm.doc.patient){
-		frappe.throw(__("Please select patient"));
+	if (!frm.doc.patient) {
+		frappe.throw(__('Please select patient'));
 	}
 	frappe.route_options = {
-		"patient": frm.doc.patient,
-		"medical_department": frm.doc.visit_department
+		'patient': frm.doc.patient,
+		'medical_department': frm.doc.medical_department
 	};
-	frappe.new_doc("Clinical Procedure");
+	frappe.new_doc('Clinical Procedure');
 };
 
-frappe.ui.form.on("Patient Encounter", "appointment", function(frm){
-	if(frm.doc.appointment){
+frappe.ui.form.on('Patient Encounter', 'practitioner', function(frm) {
+	if (frm.doc.practitioner) {
 		frappe.call({
-			"method": "frappe.client.get",
+			'method': 'frappe.client.get',
 			args: {
-				doctype: "Patient Appointment",
-				name: frm.doc.appointment
-			},
-			callback: function (data) {
-				frappe.model.set_value(frm.doctype,frm.docname, "patient", data.message.patient);
-				frappe.model.set_value(frm.doctype,frm.docname, "type", data.message.appointment_type);
-				frappe.model.set_value(frm.doctype,frm.docname, "practitioner", data.message.practitioner);
-				frappe.model.set_value(frm.doctype,frm.docname, "invoiced", data.message.invoiced);
-			}
-		});
-	}
-	else{
-		frappe.model.set_value(frm.doctype,frm.docname, "patient", "");
-		frappe.model.set_value(frm.doctype,frm.docname, "type", "");
-		frappe.model.set_value(frm.doctype,frm.docname, "practitioner", "");
-		frappe.model.set_value(frm.doctype,frm.docname, "invoiced", 0);
-	}
-});
-
-frappe.ui.form.on("Patient Encounter", "practitioner", function(frm) {
-	if(frm.doc.practitioner){
-		frappe.call({
-			"method": "frappe.client.get",
-			args: {
-				doctype: "Healthcare Practitioner",
+				doctype: 'Healthcare Practitioner',
 				name: frm.doc.practitioner
 			},
 			callback: function (data) {
-				frappe.model.set_value(frm.doctype,frm.docname, "visit_department",data.message.department);
+				frappe.model.set_value(frm.doctype,frm.docname, 'medical_department',data.message.department);
 			}
 		});
 	}
 });
 
-frappe.ui.form.on("Patient Encounter", "symptoms_select", function(frm) {
-	if(frm.doc.symptoms_select){
+frappe.ui.form.on('Patient Encounter', 'symptoms_select', function(frm) {
+	if (frm.doc.symptoms_select) {
 		var symptoms = null;
 		if(frm.doc.symptoms)
-			symptoms = frm.doc.symptoms + "\n" +frm.doc.symptoms_select;
+			symptoms = frm.doc.symptoms + '\n' +frm.doc.symptoms_select;
 		else
 			symptoms = frm.doc.symptoms_select;
-		frappe.model.set_value(frm.doctype,frm.docname, "symptoms", symptoms);
-		frappe.model.set_value(frm.doctype,frm.docname, "symptoms_select", null);
+		frappe.model.set_value(frm.doctype,frm.docname, 'symptoms', symptoms);
+		frappe.model.set_value(frm.doctype,frm.docname, 'symptoms_select', null);
 	}
 });
-frappe.ui.form.on("Patient Encounter", "diagnosis_select", function(frm) {
-	if(frm.doc.diagnosis_select){
+frappe.ui.form.on('Patient Encounter', 'diagnosis_select', function(frm) {
+	if (frm.doc.diagnosis_select) {
 		var diagnosis = null;
 		if(frm.doc.diagnosis)
-			diagnosis = frm.doc.diagnosis + "\n" +frm.doc.diagnosis_select;
+			diagnosis = frm.doc.diagnosis + '\n' +frm.doc.diagnosis_select;
 		else
 			diagnosis = frm.doc.diagnosis_select;
-		frappe.model.set_value(frm.doctype,frm.docname, "diagnosis", diagnosis);
-		frappe.model.set_value(frm.doctype,frm.docname, "diagnosis_select", null);
+		frappe.model.set_value(frm.doctype,frm.docname, 'diagnosis', diagnosis);
+		frappe.model.set_value(frm.doctype,frm.docname, 'diagnosis_select', null);
 	}
 });
 
-frappe.ui.form.on("Patient Encounter", "patient", function(frm) {
-	if(frm.doc.patient){
-		frappe.call({
-			"method": "erpnext.healthcare.doctype.patient.patient.get_patient_detail",
-			args: {
-				patient: frm.doc.patient
-			},
-			callback: function (data) {
-				var age = "";
-				if(data.message.dob){
-					age = calculate_age(data.message.dob);
-				}
-				frappe.model.set_value(frm.doctype,frm.docname, "patient_age", age);
-				frappe.model.set_value(frm.doctype,frm.docname, "patient_sex", data.message.sex);
-			}
-		});
-	}
-});
-
-frappe.ui.form.on("Drug Prescription", {
+frappe.ui.form.on('Drug Prescription', {
 	drug_code:  function(frm, cdt, cdn) {
 		var child = locals[cdt][cdn];
-		if(child.drug_code){
+		if (child.drug_code) {
 			frappe.call({
-				"method": "frappe.client.get",
+				'method': 'frappe.client.get',
 				args: {
-					doctype: "Item",
+					doctype: 'Item',
 					name: child.drug_code,
 				},
-				callback: function (data) {
+				callback: function(data) {
 					frappe.model.set_value(cdt, cdn, 'drug_name',data.message.item_name);
 				}
 			});
@@ -269,35 +280,35 @@
 	dosage: function(frm, cdt, cdn){
 		frappe.model.set_value(cdt, cdn, 'update_schedule', 1);
 		var child = locals[cdt][cdn];
-		if(child.dosage){
+		if (child.dosage) {
 			frappe.model.set_value(cdt, cdn, 'in_every', 'Day');
 			frappe.model.set_value(cdt, cdn, 'interval', 1);
 		}
 	},
-	period: function(frm, cdt, cdn){
+	period: function(frm, cdt, cdn) {
 		frappe.model.set_value(cdt, cdn, 'update_schedule', 1);
 	},
-	in_every: function(frm, cdt, cdn){
+	in_every: function(frm, cdt, cdn) {
 		frappe.model.set_value(cdt, cdn, 'update_schedule', 1);
 		var child = locals[cdt][cdn];
-		if(child.in_every == "Hour"){
+		if (child.in_every == 'Hour') {
 			frappe.model.set_value(cdt, cdn, 'dosage', null);
 		}
 	}
 });
 
-frappe.ui.form.on("Procedure Prescription", {
+frappe.ui.form.on('Procedure Prescription', {
 	procedure:  function(frm, cdt, cdn) {
 		var child = locals[cdt][cdn];
-		if(child.procedure){
+		if (child.procedure) {
 			frappe.call({
-				"method": "frappe.client.get_value",
+				'method': 'frappe.client.get_value',
 				args: {
-					doctype: "Clinical Procedure Template",
-					fieldname: "medical_department",
+					doctype: 'Clinical Procedure Template',
+					fieldname: 'medical_department',
 					filters: {name: child.procedure}
 				},
-				callback: function (data) {
+				callback: function(data) {
 					frappe.model.set_value(cdt, cdn, 'department',data.message.medical_department);
 				}
 			});
@@ -311,5 +322,5 @@
 	var age = new Date();
 	age.setTime(ageMS);
 	var years =  age.getFullYear() - 1970;
-	return  years + " Year(s) " + age.getMonth() + " Month(s) " + age.getDate() + " Day(s)";
+	return  years + ' Year(s) ' + age.getMonth() + ' Month(s) ' + age.getDate() + ' Day(s)';
 };