From b2dfea851771b288bea94916a03c8a668b601851 Mon Sep 17 00:00:00 2001 From: Akash Date: Tue, 13 Feb 2024 15:40:23 +0530 Subject: [PATCH] fix(Patient Encounter): set medication in drug prescription if Medication is already created change tests - Medication, Service Request change Patient Encounter drug_prescription filter --- .../test_medication_request.py | 11 +++++++---- .../patient_encounter/patient_encounter.js | 16 ++++++++++++---- .../patient_encounter/patient_encounter.py | 15 +++++++++++---- .../service_request/test_service_request.py | 4 +++- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/healthcare/healthcare/doctype/medication_request/test_medication_request.py b/healthcare/healthcare/doctype/medication_request/test_medication_request.py index 6c1c714d47..12a0db4235 100644 --- a/healthcare/healthcare/doctype/medication_request/test_medication_request.py +++ b/healthcare/healthcare/doctype/medication_request/test_medication_request.py @@ -5,6 +5,8 @@ import frappe +from erpnext.stock.doctype.item.test_item import create_item + from healthcare.healthcare.doctype.patient_appointment.test_patient_appointment import ( create_healthcare_docs, ) @@ -20,7 +22,7 @@ def setup(self): def test_medication_request(self): patient, practitioner = create_healthcare_docs() - medication = create_medcation() + medication = create_medication() encounter = create_encounter(patient, practitioner, "drug_prescription", medication, submit=True) self.assertTrue(frappe.db.exists("Medication Request", {"order_group": encounter.name})) medication_request = frappe.db.get_value( @@ -39,7 +41,7 @@ def test_medication_request(self): ) -def create_medcation(): +def create_medication(): if not frappe.db.exists("Medication", "_Test Medication"): if not frappe.db.exists("Medication Class", "Tablet"): try: @@ -52,6 +54,8 @@ def create_medcation(): except frappe.DuplicateEntryError: pass try: + item_name = "_Test PL Item" + item = create_item(item_code=item_name, is_stock_item=0) medication = frappe.get_doc( { "doctype": "Medication", @@ -60,13 +64,12 @@ def create_medcation(): "abbr": "Test", "strength": 500, "strength_uom": "Unit", - "item_code": "_Test", - "item_group": "Drug", "dosage_form": "Capsule", "default_prescription_dosage": "0-1-0", "default_prescription_duration": "1 Hour", "is_billable": 1, "rate": 800, + "linked_items": [{"item": item.item_code, "item_code": item.item_name, "item_group": "Drug"}], } ).insert(ignore_permissions=True, ignore_mandatory=True) return medication diff --git a/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js b/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js index ffeedf4e62..cb13270654 100644 --- a/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js +++ b/healthcare/healthcare/doctype/patient_encounter/patient_encounter.js @@ -181,10 +181,18 @@ frappe.ui.form.on('Patient Encounter', { if (frappe.meta.get_docfield('Drug Prescription', 'medication').in_list_view === 1) { frm.set_query('drug_code', 'drug_prescription', function(doc, cdt, cdn) { let row = frappe.get_doc(cdt, cdn); - return { - query: 'healthcare.healthcare.doctype.patient_encounter.patient_encounter.get_medications_query', - filters: { name: row.medication } - }; + if (row.medication) { + return { + query: 'healthcare.healthcare.doctype.patient_encounter.patient_encounter.get_medications_query', + filters: { name: row.medication } + }; + } else { + return { + filters: { + is_stock_item: 1 + } + }; + } }); } var table_list = ["drug_prescription", "lab_test_prescription", "procedure_prescription", "therapies"] diff --git a/healthcare/healthcare/doctype/patient_encounter/patient_encounter.py b/healthcare/healthcare/doctype/patient_encounter/patient_encounter.py index 5062305800..b701bd70d8 100644 --- a/healthcare/healthcare/doctype/patient_encounter/patient_encounter.py +++ b/healthcare/healthcare/doctype/patient_encounter/patient_encounter.py @@ -141,10 +141,17 @@ def validate_medications(self): return for item in self.drug_prescription: - if not item.medication and not item.drug_code: - frappe.throw( - _("Row #{0} (Drug Prescription): Medication or Item Code is mandatory").format(item.idx) - ) + if not item.drug_code: + frappe.throw(_("Row #{0} (Drug Prescription): Drug Code is mandatory").format(item.idx)) + else: + if not item.medication: + medication = frappe.db.get_value( + "Medication Linked Item", + {"item": item.drug_code}, + "parent", + ) + if medication: + item.medication = medication def validate_therapies(self): if not self.therapies: diff --git a/healthcare/healthcare/doctype/service_request/test_service_request.py b/healthcare/healthcare/doctype/service_request/test_service_request.py index 5542077761..9049792137 100644 --- a/healthcare/healthcare/doctype/service_request/test_service_request.py +++ b/healthcare/healthcare/doctype/service_request/test_service_request.py @@ -130,7 +130,9 @@ def create_encounter( type, {"lab_test_code": template.item, "lab_test_name": template.lab_test_name} ) elif type == "drug_prescription": - patient_encounter.append(type, {"medication": template.name}) + patient_encounter.append( + type, {"medication": template.name, "drug_code": template.linked_items[0].get("item")} + ) else: patient_encounter.append(type, {"observation_template": template.name})