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 -
-
-
-
- - UNIT - -
-
-
-
- - 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 +
+
+
+
+ + UNIT + +
+
+
+
+ + 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 8fdd9b030f..a31c9d641d 100644 --- a/healthcare/healthcare/doctype/observation/observation.py +++ b/healthcare/healthcare/doctype/observation/observation.py @@ -483,3 +483,133 @@ def set_diagnostic_report_status(doc): frappe.db.set_value( "Diagnostic Report", diagnostic_report.get("name"), set_value_dict, update_modified=False ) +<<<<<<< HEAD +======= + + +def set_calculated_result(doc): + if doc.parent_observation: + parent_template = frappe.db.get_value( + "Observation", doc.parent_observation, "observation_template" + ) + parent_template_doc = frappe.get_cached_doc("Observation Template", parent_template) + + data = frappe._dict() + patient_doc = frappe.get_cached_doc("Patient", doc.patient).as_dict() + settings = frappe.get_cached_doc("Healthcare Settings").as_dict() + + data.update(doc.as_dict()) + data.update(parent_template_doc.as_dict()) + data.update(patient_doc) + data.update(settings) + + for component in parent_template_doc.observation_component: + """ + Data retrieval from observations has been moved into the loop + to accommodate component observations, which may contain formulas + utilizing results from previous iterations. + + """ + if component.based_on_formula and component.formula: + obs_data = get_data(doc, parent_template_doc) + else: + continue + + if obs_data and len(obs_data) > 0: + data.update(obs_data) + result = eval_condition_and_formula(component, data) + if not result: + continue + + result_observation_name, result_data = frappe.db.get_value( + "Observation", + { + "parent_observation": doc.parent_observation, + "observation_template": component.get("observation_template"), + }, + ["name", "result_data"], + ) + if result_observation_name and result_data != str(result): + frappe.db.set_value( + "Observation", + result_observation_name, + "result_data", + str(result), + ) + + +def get_data(doc, parent_template_doc): + data = frappe._dict() + observation_details = frappe.get_all( + "Observation", + {"parent_observation": doc.parent_observation}, + ["observation_template", "result_data"], + ) + + # to get all result_data to map against abbs of all table rows + for component in parent_template_doc.observation_component: + result = [ + d["result_data"] + for d in observation_details + if (d["observation_template"] == component.get("observation_template") and d["result_data"]) + ] + data[component.get("abbr")] = flt(result[0]) if (result and len(result) > 0 and result[0]) else 0 + return data + + +def eval_condition_and_formula(d, data): + try: + if d.get("condition"): + cond = d.get("condition") + parts = cond.strip().splitlines() + condition = " ".join(parts) + if condition: + if not frappe.safe_eval(condition, data): + return None + + if d.based_on_formula: + amount = None + formula = d.formula.strip().replace("\n", " ") if d.formula else None + operands = re.split(r"\W+", formula) + abbrs = [operand for operand in operands if re.search(r"[a-zA-Z]", operand)] + if "age" in abbrs and data.get("dob"): + age = ( + getdate(nowdate()).year + - data.get("dob").year + - ( + (getdate(nowdate()).month, getdate(nowdate()).day) + < (data.get("dob").month, data.get("dob").day) + ) + ) + if age > 0: + data["age"] = age + + # check the formula abbrs has result value + abbrs_present = all(abbr in data and data[abbr] != 0 for abbr in abbrs) + if formula and abbrs_present: + amount = flt(frappe.safe_eval(formula, {}, data)) + + return amount + + except Exception as err: + description = _("This error can be due to invalid formula.") + message = _( + """Error while evaluating the {0} {1} at row {2}.

Error: {3} +

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 +>>>>>>> ec52762 (fix(Observation): change reference of diagnostic report to observation and update patch) 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 904787ab5e..90d847855c 100644 --- a/healthcare/hooks.py +++ b/healthcare/hooks.py @@ -62,6 +62,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)