Skip to content

Commit

Permalink
fix: Sample Collection - move create observation function to queue
Browse files Browse the repository at this point in the history
  • Loading branch information
akashkrishna619 committed Dec 19, 2023
1 parent 5c55b9d commit 037dffe
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
223 changes: 122 additions & 101 deletions healthcare/healthcare/doctype/sample_collection/sample_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 037dffe

Please sign in to comment.