diff --git a/healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.js b/healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.js index 986d0a4031..f30148c372 100644 --- a/healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.js +++ b/healthcare/healthcare/doctype/diagnostic_report/diagnostic_report.js @@ -5,7 +5,7 @@ frappe.ui.form.on("Diagnostic Report", { refresh: function(frm) { show_diagnostic_report(frm); if (!frm.doc.__islocal) { - frm.add_custom_button(__(`Get PDF1`), function () { + frm.add_custom_button(__(`Get PDF`), function () { generate_pdf_with_print_format(frm) }) } diff --git a/healthcare/healthcare/doctype/observation/observation.py b/healthcare/healthcare/doctype/observation/observation.py index d1795a7a4e..fe5a2358a1 100644 --- a/healthcare/healthcare/doctype/observation/observation.py +++ b/healthcare/healthcare/doctype/observation/observation.py @@ -89,9 +89,8 @@ def validate_input(self): @frappe.whitelist() def get_observation_details(docname): - reference = frappe.get_value( - "Diagnostic Report", docname, ["docname", "ref_doctype"], as_dict=True - ) + reference = frappe.get_value("Diagnostic Report", docname, ["docname", "ref_doctype"], as_dict=True) + if reference.get("ref_doctype") == "Sales Invoice": observation = frappe.get_list( "Observation", @@ -128,69 +127,88 @@ def get_observation_details(docname): order_by="creation", ) + out_data, obs_length = aggregate_and_return_observation_data(observation) + + return out_data, obs_length + + +def aggregate_and_return_observation_data(observations): out_data = [] obs_length = 0 - for obs in observation: - has_result = False - obs_approved = False + + for obs in observations: + if not obs.get("has_component"): if obs.get("permitted_data_type"): obs_length += 1 - observation_data = {} + if obs.get("permitted_data_type") == "Select" and obs.get("options"): obs["options_list"] = obs.get("options").split("\n") - if obs.get("observation_template"): - if obs.get("specimen"): - obs["received_time"] = frappe.get_value("Specimen", obs.get("specimen"), "received_time") - out_data.append(observation_data) - observation_data["observation"] = obs + + if obs.get("observation_template") and obs.get("specimen"): + obs["received_time"] = frappe.get_value("Specimen", obs.get("specimen"), "received_time") + + out_data.append({"observation": obs}) else: - child_observations = frappe.get_list( - "Observation", - fields=["*"], - filters={ - "parent_observation": obs.get("name"), - "status": ["!=", "Cancelled"], - "docstatus": ["!=", 2], - }, - order_by="observation_idx", - ) - obs_list = [] - obs_dict = {} - for child in child_observations: - if child.get("permitted_data_type"): - obs_length += 1 - if child.get("permitted_data_type") == "Select" and child.get("options"): - child["options_list"] = child.get("options").split("\n") - if child.get("specimen"): - child["received_time"] = frappe.get_value("Specimen", child.get("specimen"), "received_time") - observation_data = {} - observation_data["observation"] = child - obs_list.append(observation_data) - if ( - child.get("result_data") - or child.get("result_text") - or child.get("result_select") not in [None, "", "Null"] - ): - has_result = True - if child.get("status") == "Approved": - obs_approved = True - if len(child_observations) > 0: - obs_dict["has_component"] = True - obs_dict["observation"] = obs.get("name") - obs_dict[obs.get("name")] = obs_list - obs_dict["display_name"] = obs.get("observation_template") - obs_dict["practitioner_name"] = obs.get("practitioner_name") - obs_dict["healthcare_practitioner"] = obs.get("healthcare_practitioner") - obs_dict["description"] = obs.get("description") - obs_dict["has_result"] = has_result - obs_dict["obs_approved"] = obs_approved + child_observations = get_child_observations(obs) + obs_dict = return_child_observation_data_as_dict(child_observations, obs, obs_length) + if len(obs_dict) > 0: out_data.append(obs_dict) + return out_data, obs_length +def get_child_observations(obs): + return frappe.get_list( + "Observation", + fields=["*"], + filters={ + "parent_observation": obs.get("name"), + "status": ["!=", "Cancelled"], + "docstatus": ["!=", 2], + }, + order_by="observation_idx", + ) + + +def return_child_observation_data_as_dict(child_observations, obs, obs_length): + obs_list = [] + has_result = False + obs_approved = False + + for child in child_observations: + if child.get("permitted_data_type"): + obs_length += 1 + if child.get("permitted_data_type") == "Select" and child.get("options"): + child["options_list"] = child.get("options").split("\n") + if child.get("specimen"): + child["received_time"] = frappe.get_value("Specimen", child.get("specimen"), "received_time") + observation_data = {"observation": child} + obs_list.append(observation_data) + + if child.get("result_data") or child.get("result_text") or child.get("result_select") not in [None, "", "Null"]: + has_result = True + if child.get("status") == "Approved": + obs_approved = True + + obs_dict = { + "has_component": True, + "observation": obs.get("name"), + obs.get("name"): obs_list, + "display_name": obs.get("observation_template"), + "practitioner_name": obs.get("practitioner_name"), + "healthcare_practitioner": obs.get("healthcare_practitioner"), + "description": obs.get("description"), + "has_result": has_result, + "obs_approved": obs_approved, + } + + return obs_dict + + + def get_observation_reference(doc): template_doc = frappe.get_doc("Observation Template", doc.observation_template) display_reference = "" @@ -254,38 +272,26 @@ def edit_observation(observation, data_type, result): @frappe.whitelist() -def add_observation( - patient, - template, - data_type=None, - result=None, - doc=None, - docname=None, - parent=None, - specimen=None, - invoice="", - practitioner=None, - child=None, -): +def add_observation(**args): observation_doc = frappe.new_doc("Observation") observation_doc.posting_datetime = now_datetime() - observation_doc.patient = patient - observation_doc.observation_template = template - observation_doc.permitted_data_type = data_type - observation_doc.reference_doctype = doc - observation_doc.reference_docname = docname - observation_doc.sales_invoice = invoice - observation_doc.healthcare_practitioner = practitioner - observation_doc.specimen = specimen - if data_type in ["Range", "Ratio", "Quantity", "Numeric"]: - observation_doc.result_data = result + observation_doc.patient = args.get("patient") + observation_doc.observation_template = args.get("template") + observation_doc.permitted_data_type = args.get("data_type") + observation_doc.reference_doctype = args.get("doc") + observation_doc.reference_docname = args.get("docname") + observation_doc.sales_invoice = args.get("invoice") + observation_doc.healthcare_practitioner = args.get("practitioner") + observation_doc.specimen = args.get("specimen") + if args.get("data_type") in ["Range", "Ratio", "Quantity", "Numeric"]: + observation_doc.result_data = args.get("result") # elif data_type in ["Quantity", "Numeric"]: # observation_doc.result_float = result - elif data_type == "Text": - observation_doc.result_text = result - if parent: - observation_doc.parent_observation = parent - observation_doc.sales_invoice_item = child if child else "" + elif args.get("data_type") == "Text": + observation_doc.result_text = args.get("result") + if args.get("parent"): + observation_doc.parent_observation = args.get("parent") + observation_doc.sales_invoice_item = args.get("child") if args.get("child") else "" observation_doc.insert(ignore_permissions=True) return observation_doc.name diff --git a/healthcare/healthcare/doctype/observation/test_observation.py b/healthcare/healthcare/doctype/observation/test_observation.py index 4bc1b5f86a..b6c6553f5a 100644 --- a/healthcare/healthcare/doctype/observation/test_observation.py +++ b/healthcare/healthcare/doctype/observation/test_observation.py @@ -89,7 +89,7 @@ def test_has_component_observation_from_invoice(self): obs_name = "Complete Blood Count (CBC)" obs_template = create_grouped_observation_template(obs_name, idx) sales_invoice = create_sales_invoice(patient, obs_name + str(idx)) - + # parent_observation self.assertTrue( frappe.db.exists( "Observation", @@ -101,6 +101,18 @@ def test_has_component_observation_from_invoice(self): ) ) + # child_observation + self.assertTrue( + frappe.db.exists( + "Observation", + { + "observation_template": obs_name + str(idx+1), + "patient": patient, + "sales_invoice": sales_invoice.name, + }, + ) + ) + self.assertTrue( frappe.db.exists( "Diagnostic Report", diff --git a/healthcare/healthcare/doctype/sample_collection/sample_collection.py b/healthcare/healthcare/doctype/sample_collection/sample_collection.py index bd710ad5a4..e9539bd20e 100644 --- a/healthcare/healthcare/doctype/sample_collection/sample_collection.py +++ b/healthcare/healthcare/doctype/sample_collection/sample_collection.py @@ -92,17 +92,15 @@ def create_observation(selected, sample_collection, component_observations=None, # non has_component templates if not obs.get("has_component") or obs.get("has_component") == 0: observation = add_observation( - sample_col_doc.get("patient"), - obs.get("observation_template"), - "", - "", - "Sample Collection", - sample_collection, - parent_observation, - comp_obs_ref.get(obs.get("name")) + patient=sample_col_doc.get("patient"), + template=obs.get("observation_template"), + doc="Sample Collection", + docname=sample_collection, + parent=parent_observation, + specimen=comp_obs_ref.get(obs.get("name")) or comp_obs_ref.get(i + 1) or comp_obs_ref.get(obs.get("idx")), - sample_col_doc.get("reference_name"), + invoice=sample_col_doc.get("reference_name"), practitioner=sample_col_doc.get("referring_practitioner"), child=obs.get("reference_child") if obs.get("reference_child") else "", ) @@ -124,13 +122,11 @@ def create_observation(selected, sample_collection, component_observations=None, observation = add_observation( sample_col_doc.get("patient"), comp.get("observation_template"), - "", - "", - "Sample Collection", - sample_collection, - obs.get("component_observation_parent"), - comp_obs_ref.get(j + 1) or comp_obs_ref.get(obs.get("name")), - sample_col_doc.get("reference_name"), + doc="Sample Collection", + docname=sample_collection, + parent=obs.get("component_observation_parent"), + specimen=comp_obs_ref.get(j + 1) or comp_obs_ref.get(obs.get("name")), + invoice=sample_col_doc.get("reference_name"), practitioner=sample_col_doc.get("referring_practitioner"), child=obs.get("reference_child") if obs.get("reference_child") else "", ) diff --git a/healthcare/healthcare/doctype/service_request/service_request.py b/healthcare/healthcare/doctype/service_request/service_request.py index dd2ced8862..d7e5195977 100644 --- a/healthcare/healthcare/doctype/service_request/service_request.py +++ b/healthcare/healthcare/doctype/service_request/service_request.py @@ -247,13 +247,11 @@ def make_observation(service_request): if len(non_sample_reqd_component_obs) > 0: for comp in non_sample_reqd_component_obs: add_observation( - service_request.patient, - comp, - "", - "", - "Patient Encounter", - service_request.order_group, - observation.name, + patient=service_request.patient, + template=comp, + doc="Patient Encounter", + docname=service_request.order_group, + parent=observation.name, ) if len(sample_reqd_component_obs) > 0: diff --git a/healthcare/healthcare/utils.py b/healthcare/healthcare/utils.py index 751eecec8d..3593d3412b 100644 --- a/healthcare/healthcare/utils.py +++ b/healthcare/healthcare/utils.py @@ -1177,8 +1177,8 @@ def insert_observation_and_sample_collection(doc, patient, grp, sample_collectio diag_report_required = True # parent observation parent_observation = add_observation( - patient, - grp.get("name"), + patient=patient, + template=grp.get("name"), practitioner=doc.ref_practitioner, invoice=doc.name, child=child if child else "", @@ -1192,8 +1192,8 @@ def insert_observation_and_sample_collection(doc, patient, grp, sample_collectio if len(non_sample_reqd_component_obs) > 0: for comp in non_sample_reqd_component_obs: add_observation( - patient, - comp, + patient=patient, + template=comp, practitioner=doc.ref_practitioner, parent=parent_observation, invoice=doc.name, @@ -1218,8 +1218,8 @@ def insert_observation_and_sample_collection(doc, patient, grp, sample_collectio # create observation for non sample_collection_reqd individual templates if not grp.get("sample_collection_required"): add_observation( - patient, - grp.get("name"), + patient=patient, + template=grp.get("name"), practitioner=doc.ref_practitioner, invoice=doc.name, child=child if child else "",