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 3e33a42bed..61b5fd9f6b 100644 --- a/healthcare/healthcare/doctype/patient_history_settings/patient_history_settings.py +++ b/healthcare/healthcare/doctype/patient_history_settings/patient_history_settings.py @@ -9,7 +9,9 @@ from frappe import _ from frappe.model.document import Document from frappe.utils import cint, cstr +from frappe.utils.formatters import format_value +from healthcare.healthcare.doctype.observation.observation import get_observation_details from healthcare.healthcare.page.patient_history.patient_history import get_patient_history_doctypes @@ -79,31 +81,47 @@ def create_medical_record(doc, method=None): if not medical_record_required: return - if frappe.db.exists("Patient Medical Record", {"reference_name": doc.name}): + 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 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) + subject = set_subject_field(doc, reference) 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_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_owner = doc.owner medical_record.save(ignore_permissions=True) -def update_medical_record(doc, method=None): +def update_medical_record(doc, method=None, reference=None): medical_record_required = validate_medical_record_required(doc) if not medical_record_required: return - medical_record_id = frappe.db.exists("Patient Medical Record", {"reference_name": doc.name}) + medical_record_id = frappe.db.exists( + "Patient Medical Record", + {"reference_name": reference if doc.doctype == "Observation" else doc.name}, + ) if medical_record_id: - subject = set_subject_field(doc) + subject = set_subject_field(doc, reference) frappe.db.set_value("Patient Medical Record", medical_record_id, "subject", subject) else: create_medical_record(doc) @@ -119,25 +137,27 @@ def delete_medical_record(doc, method=None): frappe.delete_doc("Patient Medical Record", record, force=1) -def set_subject_field(doc): - from frappe.utils.formatters import format_value - +def set_subject_field(doc, reference=None): 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 entry.get("fieldtype") == "Table" and doc.get(fieldname): - formatted_value = get_formatted_value_for_table_field( - doc.get(fieldname), meta.get_field(fieldname) - ) - subject += frappe.bold(_(entry.get("label")) + ":") + "
" + cstr(formatted_value) + "
" - + if doc.doctype == "Observation": + if doc.reference_docname or doc.sales_invoice: + subject = get_observation_subject_and_ref(patient_history_fields, reference, meta) else: - if doc.get(fieldname): - formatted_value = format_value(doc.get(fieldname), meta.get_field(fieldname), doc) - subject += frappe.bold(_(entry.get("label")) + ":") + cstr(formatted_value) + "
" + return + else: + for entry in patient_history_fields: + fieldname = entry.get("fieldname") + if entry.get("fieldtype") == "Table" and doc.get(fieldname): + formatted_value = get_formatted_value_for_table_field( + doc.get(fieldname), meta.get_field(fieldname) + ) + subject += frappe.bold(_(entry.get("label")) + ":") + "
" + cstr(formatted_value) + "
" + else: + if doc.get(fieldname): + formatted_value = format_value(doc.get(fieldname), meta.get_field(fieldname), doc) + subject += frappe.bold(_(entry.get("label")) + ":") + cstr(formatted_value) + "
" return subject @@ -213,3 +233,74 @@ def get_module(doc): module = frappe.db.get_value("DocType", doc.doctype, "module") return module + + +def get_observation_subject_and_ref(patient_history_fields, reference, meta): + if not reference: + return + + obs_data = get_observation_details(reference) + + subject = "" + for entry in obs_data[0]: + parent_obs = entry.get("observation") + + if entry.get("has_component"): + formatted_value = get_formatted_value_for_observations( + entry.get(parent_obs), patient_history_fields, meta + ) + subject += f"Observation: {parent_obs}
{cstr(formatted_value)}
" + else: + formatted_value = get_formatted_value_for_observations([entry], patient_history_fields, meta) + subject += f"Observation: {parent_obs.get('name')}
{cstr(formatted_value)}
" + + return subject + + +def get_formatted_value_for_observations(items, patient_history_fields, meta): + result_fields = [ + "result_boolean", + "result_data", + "result_text", + "result_float", + "result_select", + ] + table_head = "" + table_row = "" + html = "" + create_head = True + for item in items: + obs_details = item.get("observation") + table_row += "" + for key in patient_history_fields: + label, fieldname = key.get("label"), key.get("fieldname") + if create_head: + table_head += f"{label} " + if obs_details.get(fieldname): + if key.get("fieldtype") == "Datetime": + formatted_value = format_value(obs_details.get(fieldname), meta.get_field(fieldname)) + table_row += f"{str(formatted_value)}" + else: + table_row += f"{str(obs_details.get(fieldname))}" + else: + table_row += "" + + result = None + for field in result_fields: + if obs_details.get(field): + result = obs_details.get(field) + break + if create_head: + table_head += "Result" + if result: + table_row += f"{str(result)}" + + create_head = False + table_row += "" + + html += f""" + + {table_head} {table_row} +
""" + + return html diff --git a/healthcare/patches.txt b/healthcare/patches.txt index bda432c601..6a21502838 100644 --- a/healthcare/patches.txt +++ b/healthcare/patches.txt @@ -8,6 +8,7 @@ healthcare.patches.v15_0.rename_automate_appointment_invoicing healthcare.patches.v15_0.rename_medical_code_standard_and_medical_code healthcare.patches.v15_0.setup_service_request healthcare.patches.v15_0.create_custom_field_in_payment_entry +healthcare.patches.v15_0.add_observation_to_patient_history [post_model_sync] healthcare.patches.v15_0.rename_field_medical_department_in_appoitment_type_service_item diff --git a/healthcare/patches/v15_0/add_observation_to_patient_history.py b/healthcare/patches/v15_0/add_observation_to_patient_history.py new file mode 100644 index 0000000000..a7802e0144 --- /dev/null +++ b/healthcare/patches/v15_0/add_observation_to_patient_history.py @@ -0,0 +1,23 @@ +import json + +import frappe + + +def execute(): + settings = frappe.get_single("Patient History Settings") + selected_fields = [ + {"label": "Observation Template", "fieldname": "observation_template", "fieldtype": "Link"}, + {"label": "Posting Date", "fieldname": "posting_date", "fieldtype": "Date"}, + {"label": "Status", "fieldname": "status", "fieldtype": "Select"}, + {"label": "Time of Result", "fieldname": "time_of_result", "fieldtype": "Datetime"}, + ] + + settings.append( + "standard_doctypes", + { + "document_type": "Observation", + "date_fieldname": "posting_date", + "selected_fields": json.dumps(selected_fields), + }, + ) + settings.save() diff --git a/healthcare/setup.py b/healthcare/setup.py index 156d0ad029..45cc76db3e 100644 --- a/healthcare/setup.py +++ b/healthcare/setup.py @@ -984,6 +984,15 @@ def get_patient_history_config(): {"label": "Total Orders", "fieldname": "total_orders", "fieldtype": "Float"}, ], ), + "Observation": ( + "posting_date", + [ + {"label": "Observation Template", "fieldname": "observation_template", "fieldtype": "Link"}, + {"label": "Posting Date", "fieldname": "posting_date", "fieldtype": "Date"}, + {"label": "Status", "fieldname": "status", "fieldtype": "Select"}, + {"label": "Time of Result", "fieldname": "time_of_result", "fieldtype": "Datetime"}, + ], + ), }