feat: Create Patient Medical Record on configured doctype submission
diff --git a/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.js b/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.js
index ca2707f..60926ee 100644
--- a/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.js
+++ b/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.js
@@ -40,7 +40,8 @@
 				if (field) {
 					selected_fields.push({
 						label: field.label,
-						fieldname: field.fieldname
+						fieldname: field.fieldname,
+						fieldtype: field.fieldtype
 					});
 				}
 			}
@@ -58,7 +59,8 @@
 		frappe.model.with_doctype(document_type, () => {
 			// get doctype fields
 			frappe.get_doc('DocType', document_type).fields.forEach(field => {
-				if (!in_list(frappe.model.no_value_type, field.fieldtype) && !field.hidden) {
+				if (!in_list(frappe.model.no_value_type, field.fieldtype) ||
+					in_list(frappe.model.table_fields, field.fieldtype) && !field.hidden) {
 					multiselect_fields.push({
 						label: field.label,
 						value: field.fieldname,
diff --git a/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.py b/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.py
index 9e876e8..af8c6f4 100644
--- a/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.py
+++ b/erpnext/healthcare/doctype/patient_history_settings/patient_history_settings.py
@@ -5,6 +5,7 @@
 from __future__ import unicode_literals
 import frappe
 from frappe import _
+from frappe.utils import cstr
 from frappe.model.document import Document
 
 class PatientHistorySettings(Document):
@@ -20,4 +21,54 @@
 
 			if field.fieldtype not in ['Date', 'Datetime']:
 				frappe.throw(_('Row #{0}: Field {1} in Document Type {2} is not a Date / Datetime field.').format(
-					entry.idx, frappe.bold(entry.date_fieldname), frappe.bold(entry.document_type)))
\ No newline at end of file
+					entry.idx, frappe.bold(entry.date_fieldname), frappe.bold(entry.document_type)))
+
+
+def create_medical_record(doc, method=None):
+	if frappe.flags.in_patch or frappe.flags.in_install or frappe.flags.in_setup_wizard or \
+		frappe.db.get_value('Doctype', doc.doctype, 'module') != 'Healthcare':
+		return
+
+	subject = set_subject_field(doc)
+	date_field = get_date_field(doc.doctype)
+	medical_record = frappe.new_doc('Patient Medical Record')
+	medical_record.patient = doc.patient
+	medical_record.subject = subject
+	medical_record.status = 'Open'
+	medical_record.communication_date = doc.get(date_field)
+	medical_record.reference_doctype = doc.doctype
+	medical_record.reference_name = doc.name
+	medical_record.reference_owner = doc.owner
+	medical_record.save(ignore_permissions=True)
+
+
+def set_subject_field(doc):
+	from frappe.utils.formatters import format_value
+
+	meta = frappe.get_meta(doc.doctype)
+	subject = ''
+	patient_history_fields = get_patient_history_fields(doc)
+
+	for entry in patient_history_fields:
+		fieldname = entry.get('fieldname')
+		if doc.get(fieldname):
+			formated_value = format_value(doc.get(fieldname), meta.get_field(fieldname), doc)
+			subject += frappe.bold(_(entry.get('label')) + ': ') + cstr(formated_value)
+			subject += '<br>'
+
+	return subject
+
+
+def get_date_field(doctype):
+	return frappe.db.get_value('Patient History Custom Document Type',
+		{ 'document_type': doctype }, 'date_fieldname')
+
+
+def get_patient_history_fields(doc):
+	import json
+	patient_history_fields = frappe.db.get_value('Patient History Custom Document Type',
+		{ 'document_type': doc.doctype }, 'selected_fields')
+
+	if patient_history_fields:
+		return json.loads(patient_history_fields)
+
diff --git a/erpnext/hooks.py b/erpnext/hooks.py
index 741176f..4ee42c7 100644
--- a/erpnext/hooks.py
+++ b/erpnext/hooks.py
@@ -221,6 +221,10 @@
 }
 
 doc_events = {
+	"*": {
+		"on_submit": "erpnext.healthcare.doctype.patient_history_settings.patient_history_settings.create_medical_record",
+		"on_cancel": "erpnext.healthcare.doctype.patient_history_settings.patient_history_settings.delete_medical_record"
+	},
 	"Stock Entry": {
 		"on_submit": "erpnext.stock.doctype.material_request.material_request.update_completed_and_requested_qty",
 		"on_cancel": "erpnext.stock.doctype.material_request.material_request.update_completed_and_requested_qty"