feat: create medical record for therapy sessions
diff --git a/erpnext/healthcare/doctype/clinical_procedure/clinical_procedure.py b/erpnext/healthcare/doctype/clinical_procedure/clinical_procedure.py
index 297f1b9..b7d7a62 100644
--- a/erpnext/healthcare/doctype/clinical_procedure/clinical_procedure.py
+++ b/erpnext/healthcare/doctype/clinical_procedure/clinical_procedure.py
@@ -246,7 +246,7 @@
 
 
 def insert_clinical_procedure_to_medical_record(doc):
-	subject = cstr(doc.procedure_template)
+	subject = frappe.bold(_("Clinical Procedure conducted: ")) + cstr(doc.procedure_template) + "<br>"
 	if doc.practitioner:
 		subject += frappe.bold(_('Healthcare Practitioner: ')) + doc.practitioner
 	if subject and doc.notes:
diff --git a/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py b/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py
index 201264f..c19be17 100644
--- a/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py
+++ b/erpnext/healthcare/doctype/therapy_plan/therapy_plan.py
@@ -21,8 +21,14 @@
 				self.status = 'Completed'
 
 	def set_totals(self):
-		total_sessions = sum([int(d.no_of_sessions) for d in self.get('therapy_plan_details')])
-		total_sessions_completed = sum([int(d.sessions_completed) for d in self.get('therapy_plan_details')])
+		total_sessions = 0
+		total_sessions_completed = 0
+		for entry in self.therapy_plan_details:
+			if entry.no_of_sessions:
+				total_sessions += entry.no_of_sessions
+			if entry.sessions_completed:
+				total_sessions_completed += entry.sessions_completed
+
 		self.db_set('total_sessions', total_sessions)
 		self.db_set('total_sessions_completed', total_sessions_completed)
 
diff --git a/erpnext/healthcare/doctype/therapy_session/therapy_session.js b/erpnext/healthcare/doctype/therapy_session/therapy_session.js
index bb67575..80fca39 100644
--- a/erpnext/healthcare/doctype/therapy_session/therapy_session.js
+++ b/erpnext/healthcare/doctype/therapy_session/therapy_session.js
@@ -13,14 +13,9 @@
 
 	refresh: function(frm) {
 		if (!frm.doc.__islocal) {
-			let target = 0;
-			let completed = 0;
-			$.each(frm.doc.exercises, function(_i, e) {
-				target += e.counts_target;
-				completed += e.counts_completed;
-			});
-			frm.dashboard.add_indicator(__('Counts Targetted: {0}', [target]), 'blue');
-			frm.dashboard.add_indicator(__('Counts Completed: {0}', [completed]), (completed < target) ? 'orange' : 'green');
+			frm.dashboard.add_indicator(__('Counts Targeted: {0}', [frm.doc.total_counts_targeted]), 'blue');
+			frm.dashboard.add_indicator(__('Counts Completed: {0}', [frm.doc.total_counts_completed]),
+				(frm.doc.total_counts_completed < frm.doc.total_counts_targeted) ? 'orange' : 'green');
 		}
 
 		if (frm.doc.docstatus === 1) {
diff --git a/erpnext/healthcare/doctype/therapy_session/therapy_session.json b/erpnext/healthcare/doctype/therapy_session/therapy_session.json
index 5ff7196..d8c4b99 100644
--- a/erpnext/healthcare/doctype/therapy_session/therapy_session.json
+++ b/erpnext/healthcare/doctype/therapy_session/therapy_session.json
@@ -28,6 +28,10 @@
   "invoiced",
   "exercises_section",
   "exercises",
+  "section_break_23",
+  "total_counts_targeted",
+  "column_break_25",
+  "total_counts_completed",
   "amended_from"
  ],
  "fields": [
@@ -173,11 +177,31 @@
    "fieldtype": "Data",
    "label": "Patient Age",
    "read_only": 1
+  },
+  {
+   "fieldname": "total_counts_targeted",
+   "fieldtype": "Int",
+   "label": "Total Counts Targeted",
+   "read_only": 1
+  },
+  {
+   "fieldname": "total_counts_completed",
+   "fieldtype": "Int",
+   "label": "Total Counts Completed",
+   "read_only": 1
+  },
+  {
+   "fieldname": "section_break_23",
+   "fieldtype": "Section Break"
+  },
+  {
+   "fieldname": "column_break_25",
+   "fieldtype": "Column Break"
   }
  ],
  "is_submittable": 1,
  "links": [],
- "modified": "2020-04-21 13:16:46.378798",
+ "modified": "2020-04-29 13:22:13.190353",
  "modified_by": "Administrator",
  "module": "Healthcare",
  "name": "Therapy Session",
diff --git a/erpnext/healthcare/doctype/therapy_session/therapy_session.py b/erpnext/healthcare/doctype/therapy_session/therapy_session.py
index 45d2ee6..7e24095 100644
--- a/erpnext/healthcare/doctype/therapy_session/therapy_session.py
+++ b/erpnext/healthcare/doctype/therapy_session/therapy_session.py
@@ -6,10 +6,16 @@
 import frappe
 from frappe.model.document import Document
 from frappe.model.mapper import get_mapped_doc
+from frappe import _
+from frappe.utils import cstr
 
 class TherapySession(Document):
+	def validate(self):
+		self.set_total_counts()
+
 	def on_submit(self):
 		self.update_sessions_count_in_therapy_plan()
+		insert_session_medical_record(self)
 
 	def on_cancel(self):
 		self.update_sessions_count_in_therapy_plan(on_cancel=True)
@@ -24,6 +30,18 @@
 					entry.sessions_completed += 1
 		therapy_plan.save()
 
+	def set_total_counts(self):
+		target_total = 0
+		counts_completed = 0
+		for entry in self.exercises:
+			if entry.counts_target:
+				target_total += entry.counts_target
+			if entry.counts_completed:
+				counts_completed += entry.counts_completed
+
+		self.db_set('total_counts_targeted', target_total)
+		self.db_set('total_counts_completed', counts_completed)
+
 
 @frappe.whitelist()
 def create_therapy_session(source_name, target_doc=None):
@@ -52,4 +70,24 @@
 			}
 		}, target_doc, set_missing_values)
 
-	return doc
\ No newline at end of file
+	return doc
+
+
+def insert_session_medical_record(doc):
+	subject = frappe.bold(_('Therapy: ')) + cstr(doc.therapy_type) + '<br>'
+	if doc.therapy_plan:
+		subject += frappe.bold(_('Therapy Plan: ')) + cstr(doc.therapy_plan) + '<br>'
+	if doc.practitioner:
+		subject += frappe.bold(_('Healthcare Practitioner: ')) + doc.practitioner
+	subject += frappe.bold(_('Total Counts Targeted: ')) + cstr(doc.total_counts_targeted) + '<br>'
+	subject += frappe.bold(_('Total Counts Completed: ')) + cstr(doc.total_counts_completed) + '<br>'
+
+	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.start_date
+	medical_record.reference_doctype = 'Therapy Session'
+	medical_record.reference_name = doc.name
+	medical_record.reference_owner = doc.owner
+	medical_record.save(ignore_permissions=True)
\ No newline at end of file