From 037dffe2bf34b893cd4105cd3fb6873a5cfa4491 Mon Sep 17 00:00:00 2001 From: Akash Date: Thu, 14 Dec 2023 14:21:43 +0530 Subject: [PATCH] fix: Sample Collection - move create observation function to queue --- .../sample_collection/sample_collection.js | 10 + .../sample_collection/sample_collection.py | 223 ++++++++++-------- 2 files changed, 132 insertions(+), 101 deletions(-) diff --git a/healthcare/healthcare/doctype/sample_collection/sample_collection.js b/healthcare/healthcare/doctype/sample_collection/sample_collection.js index 51117ba11d..ec5c9d99c9 100644 --- a/healthcare/healthcare/doctype/sample_collection/sample_collection.js +++ b/healthcare/healthcare/doctype/sample_collection/sample_collection.js @@ -2,11 +2,20 @@ // For license information, please see license.txt frappe.ui.form.on('Sample Collection', { + onload: function(frm) { + frappe.realtime.on("observation_creation_progress", (status) => { + if (status == "Completed") { + frm.reload_doc(); + frappe.dom.unfreeze(); + } + }) + }, refresh: function(frm) { frm.fields_dict.observation_sample_collection.grid.add_custom_button(__("Mark Collected"), () => { selected_child = frm.fields_dict.observation_sample_collection.grid.get_selected_children() if (selected_child.length > 0) { frappe.confirm(__("Are you sure you want to mark selected samples as Collected"), function () { + frappe.dom.freeze(__('Creating Observations! Please Wait...')); frappe.call({ "method": "healthcare.healthcare.doctype.sample_collection.sample_collection.create_observation", args: { @@ -224,6 +233,7 @@ frappe.ui.form.on("Observation Sample Collection", { let selected_row = d.fields_dict.items.grid.get_selected_children(); if (selected_row.length > 0) { frappe.confirm(__("Are you sure you want to mark selected samples as Collected"), function () { + frappe.dom.freeze(__('Creating Observations! Please Wait...')); frappe.call({ "method": "healthcare.healthcare.doctype.sample_collection.sample_collection.create_observation", args: { diff --git a/healthcare/healthcare/doctype/sample_collection/sample_collection.py b/healthcare/healthcare/doctype/sample_collection/sample_collection.py index d58f410ca8..8280e2a405 100644 --- a/healthcare/healthcare/doctype/sample_collection/sample_collection.py +++ b/healthcare/healthcare/doctype/sample_collection/sample_collection.py @@ -52,114 +52,135 @@ def validate(self): @frappe.whitelist() def create_observation(selected, sample_collection, component_observations=None, child_name=None): - sample_col_doc = frappe.db.get_value( - "Sample Collection", - sample_collection, - ["reference_name", "patient", "referring_practitioner"], - as_dict=1, + frappe.enqueue( + "healthcare.healthcare.doctype.sample_collection.sample_collection.insert_observation", + selected=selected, + sample_collection=sample_collection, + component_observations=component_observations, + child_name=child_name, ) - selected = json.loads(selected) - if component_observations and len(component_observations) > 0: - component_observations = json.loads(component_observations) - comp_obs_ref = create_specimen(sample_col_doc.get("patient"), selected, component_observations) - for i, obs in enumerate(selected): - parent_observation = obs.get("component_observation_parent") - if child_name: - parent_observation = frappe.db.get_value( - "Observation Sample Collection", child_name, "component_observation_parent" - ) - if obs.get("status") == "Open": - # non has_component templates - if not obs.get("has_component") or obs.get("has_component") == 0: - observation = add_observation( - 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")), - 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 "", - service_request=obs.get("service_request"), +def insert_observation(selected, sample_collection, component_observations=None, child_name=None): + try: + sample_col_doc = frappe.db.get_value( + "Sample Collection", + sample_collection, + ["reference_name", "patient", "referring_practitioner"], + as_dict=1, + ) + selected = json.loads(selected) + if component_observations and len(component_observations) > 0: + component_observations = json.loads(component_observations) + comp_obs_ref = create_specimen(sample_col_doc.get("patient"), selected, component_observations) + for i, obs in enumerate(selected): + parent_observation = obs.get("component_observation_parent") + + if child_name: + parent_observation = frappe.db.get_value( + "Observation Sample Collection", child_name, "component_observation_parent" ) - if observation: - frappe.db.set_value( - "Observation Sample Collection", - obs.get("name"), - { - "status": "Collected", - "collection_date_time": now_datetime(), - "specimen": comp_obs_ref.get(obs.get("name")), - }, + + if obs.get("status") == "Open": + # non has_component templates + if not obs.get("has_component") or obs.get("has_component") == 0: + observation = add_observation( + 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")), + 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 "", + service_request=obs.get("service_request"), ) - else: - # to deal the component template checked from main table and collected - if obs.get("component_observations"): - component_observations = json.loads(obs.get("component_observations")) - for j, comp in enumerate(component_observations): - observation = add_observation( - patient=sample_col_doc.get("patient"), - template=comp.get("observation_template"), - 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 "", - service_request=obs.get("service_request"), + if observation: + frappe.db.set_value( + "Observation Sample Collection", + obs.get("name"), + { + "status": "Collected", + "collection_date_time": now_datetime(), + "specimen": comp_obs_ref.get(obs.get("name")), + }, ) - if observation: - comp["status"] = "Collected" - comp["collection_date_time"] = now_datetime() - comp["specimen"] = comp_obs_ref.get(j + 1) or comp_obs_ref.get(obs.get("name")) - - frappe.db.set_value( - "Observation Sample Collection", - obs.get("name"), - { - "collection_date_time": now_datetime(), - "component_observations": json.dumps(component_observations, default=str), - "status": "Collected", - "specimen": comp_obs_ref.get(j + 1) or comp_obs_ref.get(obs.get("name")), - }, - ) - # to deal individually checked from component dialog - if component_observations: - for j, comp in enumerate(component_observations): - if comp.get("observation_template") == obs.get("observation_template"): - comp["status"] = "Collected" - comp["collection_date_time"] = now_datetime() - comp["specimen"] = comp_obs_ref.get(j + 1) - - child_db_set_dict = {"component_observations": json.dumps(component_observations, default=str)} - # to set child table status Collected if all childs are Collected - if component_observations and not any( - (comp["status"] == "Open") for comp in component_observations - ): - child_db_set_dict["status"] = "Collected" - - if child_name: - frappe.db.set_value( - "Observation Sample Collection", - child_name, - child_db_set_dict, - ) - if sample_collection: - non_collected_samples = frappe.db.get_all( - "Observation Sample Collection", {"parent": sample_collection, "status": ["!=", "Collected"]} - ) - if non_collected_samples and len(non_collected_samples) > 0: - set_status = "Partly Collected" - else: - set_status = "Collected" + else: + # to deal the component template checked from main table and collected + if obs.get("component_observations"): + component_observations = json.loads(obs.get("component_observations")) + for j, comp in enumerate(component_observations): + observation = add_observation( + patient=sample_col_doc.get("patient"), + template=comp.get("observation_template"), + 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 "", + service_request=obs.get("service_request"), + ) + if observation: + comp["status"] = "Collected" + comp["collection_date_time"] = now_datetime() + comp["specimen"] = comp_obs_ref.get(j + 1) or comp_obs_ref.get(obs.get("name")) - frappe.db.set_value("Sample Collection", sample_collection, "status", set_status) + frappe.db.set_value( + "Observation Sample Collection", + obs.get("name"), + { + "collection_date_time": now_datetime(), + "component_observations": json.dumps(component_observations, default=str), + "status": "Collected", + "specimen": comp_obs_ref.get(j + 1) or comp_obs_ref.get(obs.get("name")), + }, + ) + # to deal individually checked from component dialog + if component_observations: + for j, comp in enumerate(component_observations): + if comp.get("observation_template") == obs.get("observation_template"): + comp["status"] = "Collected" + comp["collection_date_time"] = now_datetime() + comp["specimen"] = comp_obs_ref.get(j + 1) + + child_db_set_dict = {"component_observations": json.dumps(component_observations, default=str)} + # to set child table status Collected if all childs are Collected + if component_observations and not any( + (comp["status"] == "Open") for comp in component_observations + ): + child_db_set_dict["status"] = "Collected" + + if child_name: + frappe.db.set_value( + "Observation Sample Collection", + child_name, + child_db_set_dict, + ) + if sample_collection: + non_collected_samples = frappe.db.get_all( + "Observation Sample Collection", {"parent": sample_collection, "status": ["!=", "Collected"]} + ) + if non_collected_samples and len(non_collected_samples) > 0: + set_status = "Partly Collected" + else: + set_status = "Collected" + + frappe.db.set_value("Sample Collection", sample_collection, "status", set_status) + + except Exception as e: + frappe.log_error(message=e, title="Failed to mark Collected!") + + frappe.publish_realtime( + event="observation_creation_progress", + message="Completed", + doctype="Sample Collection", + docname=sample_collection, + ) def create_specimen(patient, selected, component_observations):