diff --git a/healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.html b/healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.html
deleted file mode 100644
index 63a9f5af4e..0000000000
--- a/healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.html
+++ /dev/null
@@ -1,411 +0,0 @@
-
-
-
-{% set dob = frappe.db.get_value("Patient", doc.patient, "dob") %}
-{% set years = 0 %}
-{% set months = 0 %}
-{% set days = 0 %}
-{% if dob %}
- {% set now = frappe.utils.nowdate() %}
- {% if doc.ref_doctype=="Sales Invoice" and doc.docname %}
- {% set now = frappe.db.get_value("Sales Invoice", doc.docname, "posting_date") %}
- {% endif %}
- {% set diff = frappe.utils.date_diff(now, dob) %}
- {% set years = diff//365 %}
- {% set months = (diff - (years * 365))//30 %}
- {% set days = ( (diff - (years * 365)) - (months * 30) ) %}
-{% endif %}
-
-{% set sex = frappe.db.get_value("Patient", doc.patient, "sex") %}
-
-{% set salutation = "Ms" %}
-
-{%- if years <= 1 -%}
- {% set salutation = "Baby" %}
-{% elif sex == "Male" and years <= 13%}
- {% set salutation = "Master" %}
-{% elif sex == "Female" and years <= 18 %}
- {% set salutation = "Miss" %}
-{% elif sex == "Male" %}
- {% set salutation = "Mr" %}
-{% endif %}
-
DEPARTMENT OF LABORATORY MEDICINE
-
-
-
-
Patient name:
-
{{ salutation }} {{ doc.patient_name.upper() }}
-
-
-
Patient:
-
{{ doc.patient }}
-
-
-
Age/Gender:
-
{{ years }}Y {{ months }}M {{ days }}D / {{ sex }}
-
-
-
-
-
Referred By:
- {%- if doc.practitioner_name -%}
-
{{ doc.practitioner_name.upper() }}
- {% else %}
-
Self
- {% endif %}
-
-
-
Invoice No.:
-
{{ doc.docname }}
-
-
-
Invoiced On:
-
{{ frappe.utils.format_date(now) }}
-
-
-
-
-
-
-
-
- SAMPLE
-
-
-
- Collected on
-
-
-
-
-
- INVESTIGATION
-
-
-
- Method
-
-
-
-
-
- RESULT
-
-
-
- Reported on
-
-
-
-
-
-
- REFERENCE INTERVAL
-
-
-
-
-
-{% if doc.get("docname") %}
- {% if doc.get("docname") %}
- {% set full_data = diagnostic_report_print(doc.name) %}
-
- {% for data in full_data[0] %}
- {% if not data.get("has_component") %}
- {% if data.get("observation").get("preferred_display_name") %}
- {% set observation_name = data.get("observation").get("preferred_display_name") %}
- {% else %}
- {% set observation_name = data.get("observation").get("observation_template") %}
- {% endif %}
-
- {% if data.get("observation") or data.get("observation") %}
- {% if data.get("observation").get("status")=="Approved" and (data.get("observation").get("result_data") or data.get("observation").get("result_text") or data.get("observation").get("result_select") not in [None, "", "Null"]) %}
-
-
-
- {{observation_name}}
-
-
-
-
-
- {% if data.get("observation").get("sample") %}
-
- {{data.get("observation").get("sample")}}
-
- {% else %}
-
- {{ frappe.db.get_value("Observation Template", data.get("observation").get("observation_template"), "sample") or "" }}
-
- {% endif %}
- {% if data.get("observation").get("received_time") %}
-
- {{frappe.utils.format_datetime(data.get("observation").get("received_time"))[:-3]}}
-
- {% endif %}
-
-
-
- {{observation_name}}
-
- {% if data.get("observation").get("method") %}
-
- {{data.get("observation").get("method")}}
-
- {% endif %}
-
-
-
- {% if data.get("observation").get("result_data") or data.get("observation").get("result_select") %}
- {{data.get("observation").get("result_data")
- or data.get("observation").get("result_select")}}
- {% elif data.get("observation").get("result_text") %}
- {% if '
' in data.get("observation").get("result_text") %}
- {% if data.get("observation").get("result_text")|length <= 60 %}
- {{data.get("observation").get("result_text")}}
- {% endif %}
- {% elif data.get("observation").get("result_text")|length <= 24 %}
- {{data.get("observation").get("result_text")}}
- {% endif %}
- {% endif %}
-
- {% if data.get("observation").get("time_of_result") %}
-
- {{frappe.utils.format_datetime(data.get("observation").get("time_of_result"))[:-3]}}
-
- {% endif %}
-
-
- {% if data.get("observation").get("permitted_unit") %}
- {{data.get("observation").get("permitted_unit")}}
- {% endif %}
-
-
- {% if data.get("observation").get("reference") %}
- {{data.get("observation").get("reference")}}
- {% endif %}
-
-
- {% if data.get("observation").get("result_text") %}
- {% if '
' in data.get("observation").get("result_text") %}
- {% if data.get("observation").get("result_text")|length > 60 %}
-
- {{data.get("observation").get("result_text")}}
-
- {% endif %}
- {% elif data.get("observation").get("result_text")|length > 24 %}
-
- {{data.get("observation").get("result_text")}}
-
- {% endif %}
- {% endif %}
- {% if data.get("observation").get("result_interpretation") %}
-
- {{data.get("observation").get("result_interpretation")}}
-
- {% endif %}
- {% if data.get("observation").get("note") %}
-
- {{data.get("observation").get("note")}}
-
- {% endif %}
- {% if data.get("observation").get("description") %}
-
- {{data.get("observation").get("description")}}
-
- {% endif %}
-
-
- {% endif %}
- {% endif %}
- {% else %}
- {% if data["obs_approved"] and data[data.get("observation")] and data["has_result"] %}
-
-
-
-
-
-
- {{data.get("display_name")}}
-
-
- {% for comps in data[data.get("observation")] %}
- {% if comps.get("observation").get("preferred_display_name") %}
- {% set observation_name = comps.get("observation").get("preferred_display_name") %}
- {% else %}
- {% set observation_name = comps.get("observation").get("observation_template") %}
- {% endif %}
- {% if comps.get("observation").get("status")=="Approved" and comps.get("observation") %}
- {% if comps.get("observation").get("result_data") or comps.get("observation").get("result_text") or comps.get("observation").get("result_select") not in [None, "", "Null"] %}
-
-
-
-
- {% if comps.get("observation").get("sample") %}
-
- {{comps.get("observation").get("sample")}}
-
- {% else %}
-
- {{ frappe.db.get_value("Observation Template", comps.get("observation").get("observation_template"), "sample") or ""}}
-
- {% endif %}
- {% if comps.get("observation").get("received_time") %}
-
- {{frappe.utils.format_datetime(comps.get("observation").get("received_time"))[:-3]}}
-
- {% endif %}
-
-
-
- {{observation_name}}
-
- {% if comps.get("observation").get("method") %}
-
- {{comps.get("observation").get("method")}}
-
- {% endif %}
-
-
-
- {% if comps.get("observation").get("result_data") or comps.get("observation").get("result_select") %}
- {{comps.get("observation").get("result_data")
- or comps.get("observation").get("result_select")}}
- {% elif comps.get("observation").get("result_text") %}
- {% if '
' in comps.get("observation").get("result_text") %}
- {% if comps.get("observation").get("result_text")|length <= 60 %}
- {{comps.get("observation").get("result_text")}}
- {% endif %}
- {% elif comps.get("observation").get("result_text")|length <= 24 %}
- {{comps.get("observation").get("result_text")}}
- {% endif %}
- {% endif %}
-
- {% if comps.get("observation").get("time_of_result") %}
-
- {{frappe.utils.format_datetime(comps.get("observation").get("time_of_result"))[:-3]}}
-
- {% endif %}
-
-
- {% if comps.get("observation").get("permitted_unit") %}
- {{comps.get("observation").get("permitted_unit")}}
- {% endif %}
-
-
- {% if comps.get("observation").get("reference") %}
- {{comps.get("observation").get("reference")}}
- {% endif %}
-
-
- {% if comps.get("observation").get("result_text") %}
- {% if '
' in comps.get("observation").get("result_text") %}
- {% if comps.get("observation").get("result_text")|length > 60 %}
-
- {{comps.get("observation").get("result_text")}}
-
- {% endif %}
- {% elif comps.get("observation").get("result_text")|length > 24 %}
-
- {{comps.get("observation").get("result_text")}}
-
- {% endif %}
- {% endif %}
- {% if comps.get("observation").get("result_interpretation") %}
-
- {{comps.get("observation").get("result_interpretation")}}
-
- {% endif %}
- {% if comps.get("observation").get("note") %}
-
- {{comps.get("observation").get("note")}}
-
- {% endif %}
- {% if comps.get("observation").get("description") and not data.get("description") %}
-
- {{comps.get("observation").get("description")}}
-
- {% endif %}
-
-
- {% endif %}
- {% endif %}
- {% endfor %}
-
- {{data.get("description") or ""}}
-
-
-
-
-
- {% endif %}
- {% endif %}
- {% endfor %}
- {% if full_data[0]|length > 0 %}
- * End of Report *
- {% endif %}
-
- {% endif %}
-{% endif %}
\ No newline at end of file
diff --git a/healthcare/healthcare/doctype/observation/observation.html b/healthcare/healthcare/doctype/observation/observation.html
new file mode 100644
index 0000000000..178a767a94
--- /dev/null
+++ b/healthcare/healthcare/doctype/observation/observation.html
@@ -0,0 +1,360 @@
+
+
+
+
+
+
+
Referred By:
+ {%- if doc.practitioner_name -%}
+
{{ doc.practitioner_name.upper() }}
+ {% else %}
+
Self
+ {% endif %}
+
+ {%- if doc.sales_invoice -%}
+
+
Invoice No.:
+
{{ doc.sales_invoice }}
+
+ {% endif %}
+
+
+
+
+
+
+ SAMPLE
+
+
+
+ Collected on
+
+
+
+
+
+ INVESTIGATION
+
+
+
+ Method
+
+
+
+
+
+ RESULT
+
+
+
+ Reported on
+
+
+
+
+
+
+ REFERENCE INTERVAL
+
+
+
+
+
+{% if doc.get("name") %}
+ {% set full_data = get_observations_for_medical_record(doc.name, doc.parent_observation) %}
+
+ {% for data in full_data[0] %}
+ {% if not data.get("has_component") %}
+ {% if data.get("observation").get("preferred_display_name") %}
+ {% set observation_name = data.get("observation").get("preferred_display_name") %}
+ {% else %}
+ {% set observation_name = data.get("observation").get("observation_template") %}
+ {% endif %}
+
+ {% if data.get("observation") or data.get("observation") %}
+ {% if data.get("observation").get("status")=="Approved" and (data.get("observation").get("result_data") or data.get("observation").get("result_text") or data.get("observation").get("result_select") not in [None, "", "Null"]) %}
+
+
+
+ {{observation_name}}
+
+
+
+
+
+ {% if data.get("observation").get("sample") %}
+
+ {{data.get("observation").get("sample")}}
+
+ {% else %}
+
+ {{ frappe.db.get_value("Observation Template", data.get("observation").get("observation_template"), "sample") or "" }}
+
+ {% endif %}
+ {% if data.get("observation").get("received_time") %}
+
+ {{frappe.utils.format_datetime(data.get("observation").get("received_time"))[:-3]}}
+
+ {% endif %}
+
+
+
+ {{observation_name}}
+
+ {% if data.get("observation").get("method") %}
+
+ {{data.get("observation").get("method")}}
+
+ {% endif %}
+
+
+
+ {% if data.get("observation").get("result_data") or data.get("observation").get("result_select") %}
+ {{data.get("observation").get("result_data")
+ or data.get("observation").get("result_select")}}
+ {% elif data.get("observation").get("result_text") %}
+ {% if '
' in data.get("observation").get("result_text") %}
+ {% if data.get("observation").get("result_text")|length <= 60 %}
+ {{data.get("observation").get("result_text")}}
+ {% endif %}
+ {% elif data.get("observation").get("result_text")|length <= 24 %}
+ {{data.get("observation").get("result_text")}}
+ {% endif %}
+ {% endif %}
+
+ {% if data.get("observation").get("time_of_result") %}
+
+ {{frappe.utils.format_datetime(data.get("observation").get("time_of_result"))[:-3]}}
+
+ {% endif %}
+
+
+ {% if data.get("observation").get("permitted_unit") %}
+ {{data.get("observation").get("permitted_unit")}}
+ {% endif %}
+
+
+ {% if data.get("observation").get("reference") %}
+ {{data.get("observation").get("reference")}}
+ {% endif %}
+
+
+ {% if data.get("observation").get("result_text") %}
+ {% if '
' in data.get("observation").get("result_text") %}
+ {% if data.get("observation").get("result_text")|length > 60 %}
+
+ {{data.get("observation").get("result_text")}}
+
+ {% endif %}
+ {% elif data.get("observation").get("result_text")|length > 24 %}
+
+ {{data.get("observation").get("result_text")}}
+
+ {% endif %}
+ {% endif %}
+ {% if data.get("observation").get("result_interpretation") %}
+
+ {{data.get("observation").get("result_interpretation")}}
+
+ {% endif %}
+ {% if data.get("observation").get("note") %}
+
+ {{data.get("observation").get("note")}}
+
+ {% endif %}
+ {% if data.get("observation").get("description") %}
+
+ {{data.get("observation").get("description")}}
+
+ {% endif %}
+
+
+ {% endif %}
+ {% endif %}
+ {% else %}
+ {% if data["obs_approved"] and data[data.get("observation")] and data["has_result"] %}
+
+
+
+
+
+
+ {{data.get("display_name")}}
+
+
+ {% for comps in data[data.get("observation")] %}
+ {% if comps.get("observation").get("preferred_display_name") %}
+ {% set observation_name = comps.get("observation").get("preferred_display_name") %}
+ {% else %}
+ {% set observation_name = comps.get("observation").get("observation_template") %}
+ {% endif %}
+ {% if comps.get("observation").get("status")=="Approved" and comps.get("observation") %}
+ {% if comps.get("observation").get("result_data") or comps.get("observation").get("result_text") or comps.get("observation").get("result_select") not in [None, "", "Null"] %}
+
+
+
+
+ {% if comps.get("observation").get("sample") %}
+
+ {{comps.get("observation").get("sample")}}
+
+ {% else %}
+
+ {{ frappe.db.get_value("Observation Template", comps.get("observation").get("observation_template"), "sample") or ""}}
+
+ {% endif %}
+ {% if comps.get("observation").get("received_time") %}
+
+ {{frappe.utils.format_datetime(comps.get("observation").get("received_time"))[:-3]}}
+
+ {% endif %}
+
+
+
+ {{observation_name}}
+
+ {% if comps.get("observation").get("method") %}
+
+ {{comps.get("observation").get("method")}}
+
+ {% endif %}
+
+
+
+ {% if comps.get("observation").get("result_data") or comps.get("observation").get("result_select") %}
+ {{comps.get("observation").get("result_data")
+ or comps.get("observation").get("result_select")}}
+ {% elif comps.get("observation").get("result_text") %}
+ {% if '
' in comps.get("observation").get("result_text") %}
+ {% if comps.get("observation").get("result_text")|length <= 60 %}
+ {{comps.get("observation").get("result_text")}}
+ {% endif %}
+ {% elif comps.get("observation").get("result_text")|length <= 24 %}
+ {{comps.get("observation").get("result_text")}}
+ {% endif %}
+ {% endif %}
+
+ {% if comps.get("observation").get("time_of_result") %}
+
+ {{frappe.utils.format_datetime(comps.get("observation").get("time_of_result"))[:-3]}}
+
+ {% endif %}
+
+
+ {% if comps.get("observation").get("permitted_unit") %}
+ {{comps.get("observation").get("permitted_unit")}}
+ {% endif %}
+
+
+ {% if comps.get("observation").get("reference") %}
+ {{comps.get("observation").get("reference")}}
+ {% endif %}
+
+
+ {% if comps.get("observation").get("result_text") %}
+ {% if '
' in comps.get("observation").get("result_text") %}
+ {% if comps.get("observation").get("result_text")|length > 60 %}
+
+ {{comps.get("observation").get("result_text")}}
+
+ {% endif %}
+ {% elif comps.get("observation").get("result_text")|length > 24 %}
+
+ {{comps.get("observation").get("result_text")}}
+
+ {% endif %}
+ {% endif %}
+ {% if comps.get("observation").get("result_interpretation") %}
+
+ {{comps.get("observation").get("result_interpretation")}}
+
+ {% endif %}
+ {% if comps.get("observation").get("note") %}
+
+ {{comps.get("observation").get("note")}}
+
+ {% endif %}
+ {% if comps.get("observation").get("description") and not data.get("description") %}
+
+ {{comps.get("observation").get("description")}}
+
+ {% endif %}
+
+
+ {% endif %}
+ {% endif %}
+ {% endfor %}
+
+ {{data.get("description") or ""}}
+
+
+
+
+
+ {% endif %}
+ {% endif %}
+ {% endfor %}
+
+{% endif %}
\ No newline at end of file
diff --git a/healthcare/healthcare/doctype/observation/observation.py b/healthcare/healthcare/doctype/observation/observation.py
index ed8782c53d..ed5611a538 100644
--- a/healthcare/healthcare/doctype/observation/observation.py
+++ b/healthcare/healthcare/doctype/observation/observation.py
@@ -602,3 +602,17 @@ def eval_condition_and_formula(d, data):
Hint: {4}"""
).format(d.parenttype, get_link_to_form(d.parenttype, d.parent), d.idx, err, description)
frappe.throw(message, title=_("Error in formula"))
+
+
+def get_observations_for_medical_record(observation, parent_observation=None):
+ if not observation:
+ return
+
+ if parent_observation:
+ obs_doc = frappe.get_doc("Observation", parent_observation)
+ else:
+ obs_doc = frappe.get_doc("Observation", observation)
+
+ out_data, obs_length = aggregate_and_return_observation_data([obs_doc])
+
+ return out_data, obs_length
diff --git a/healthcare/healthcare/doctype/patient_history_settings/patient_history_settings.py b/healthcare/healthcare/doctype/patient_history_settings/patient_history_settings.py
index 79ee6b41aa..ad31492780 100644
--- a/healthcare/healthcare/doctype/patient_history_settings/patient_history_settings.py
+++ b/healthcare/healthcare/doctype/patient_history_settings/patient_history_settings.py
@@ -82,29 +82,23 @@ def create_medical_record(doc, method=None):
reference = doc.name
if doc.doctype == "Observation":
- if doc.reference_docname or doc.sales_invoice:
- ref_docname = doc.reference_docname
- if doc.sales_invoice:
- ref_docname = doc.sales_invoice
-
- reference = frappe.db.exists("Diagnostic Report", {"docname": ref_docname})
+ if doc.parent_observation:
+ reference = doc.parent_observation
if frappe.db.exists("Patient Medical Record", {"reference_name": reference}):
if doc.doctype == "Observation" and reference:
update_medical_record(doc, reference=reference)
return
- subject = set_subject_field(doc, reference)
+ 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 if doc.doctype != "Observation" else "Diagnostic Report"
- )
- medical_record.reference_name = doc.name if doc.doctype != "Observation" else reference
+ medical_record.reference_doctype = doc.doctype
+ medical_record.reference_name = reference
medical_record.reference_owner = doc.owner
medical_record.save(ignore_permissions=True)
@@ -120,7 +114,7 @@ def update_medical_record(doc, method=None, reference=None):
)
if medical_record_id:
- subject = set_subject_field(doc, reference)
+ subject = set_subject_field(doc)
frappe.db.set_value("Patient Medical Record", medical_record_id, "subject", subject)
else:
create_medical_record(doc)
@@ -136,18 +130,14 @@ def delete_medical_record(doc, method=None):
frappe.delete_doc("Patient Medical Record", record, force=1)
-def set_subject_field(doc, reference=None):
+def set_subject_field(doc):
meta = frappe.get_meta(doc.doctype)
subject = ""
patient_history_fields = get_patient_history_fields(doc)
if doc.doctype == "Observation":
- if doc.reference_docname or doc.sales_invoice:
- doc = frappe.get_doc("Diagnostic Report", reference)
- subject = frappe.render_template(
- "healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.html", dict(doc=doc)
- )
- else:
- return
+ subject = frappe.render_template(
+ "healthcare/healthcare/doctype/observation/observation.html", dict(doc=doc)
+ )
else:
for entry in patient_history_fields:
fieldname = entry.get("fieldname")
diff --git a/healthcare/hooks.py b/healthcare/hooks.py
index 7d8909318b..88d8282d27 100644
--- a/healthcare/hooks.py
+++ b/healthcare/hooks.py
@@ -72,6 +72,7 @@
"methods": [
"healthcare.healthcare.doctype.diagnostic_report.diagnostic_report.diagnostic_report_print",
"healthcare.healthcare.utils.generate_barcodes",
+ "healthcare.healthcare.doctype.observation.observation.get_observations_for_medical_record",
]
}
diff --git a/healthcare/patches/v15_0/create_patient_medical_records_for_observations.py b/healthcare/patches/v15_0/create_patient_medical_records_for_observations.py
index e5691d40c0..2f4bf952b8 100644
--- a/healthcare/patches/v15_0/create_patient_medical_records_for_observations.py
+++ b/healthcare/patches/v15_0/create_patient_medical_records_for_observations.py
@@ -1,21 +1,34 @@
import frappe
+from frappe.utils import getdate
def execute():
- diagnostic_report = frappe.db.get_all("Diagnostic Report", pluck="name")
+ observations = frappe.db.get_all("Observation", filters={"docstatus": 1}, pluck="name")
- for diag in diagnostic_report:
- diag_doc = frappe.get_doc("Diagnostic Report", diag)
+ for obs in observations:
+ obs_doc = frappe.get_doc("Observation", obs)
subject = frappe.render_template(
- "healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.html", dict(doc=diag_doc)
+ "healthcare/healthcare/doctype/observation/observation.html", dict(doc=obs_doc)
)
- medical_record = frappe.new_doc("Patient Medical Record")
- medical_record.patient = diag_doc.patient
- medical_record.subject = subject
- medical_record.status = "Open"
- medical_record.communication_date = diag_doc.reference_posting_date
- medical_record.reference_doctype = "Diagnostic Report"
- medical_record.reference_name = diag_doc.name
- medical_record.reference_owner = diag_doc.owner
- medical_record.save(ignore_permissions=True)
+
+ reference = obs
+ if obs_doc.parent_observation:
+ reference = obs_doc.parent_observation
+
+ exists = frappe.db.exists(
+ "Patient Medical Record", {"reference_doctype": "Observation", "reference_name": reference}
+ )
+
+ if exists:
+ frappe.db.set_value("Patient Medical Record", exists, "subject", subject)
+ else:
+ medical_record = frappe.new_doc("Patient Medical Record")
+ medical_record.patient = obs_doc.patient
+ medical_record.subject = subject
+ medical_record.status = "Open"
+ medical_record.communication_date = getdate(obs_doc.modified)
+ medical_record.reference_doctype = "Observation"
+ medical_record.reference_name = reference
+ medical_record.reference_owner = obs_doc.owner
+ medical_record.save(ignore_permissions=True)