Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | import frappe, json |
| 5 | from frappe.utils.make_random import get_random |
| 6 | import datetime |
| 7 | from erpnext.demo.setup.setup_data import import_json |
| 8 | from frappe.utils import getdate |
| 9 | from erpnext.healthcare.doctype.lab_test.lab_test import create_test_from_template |
| 10 | |
| 11 | def setup_data(): |
| 12 | frappe.flags.mute_emails = True |
| 13 | make_masters() |
| 14 | make_patient() |
| 15 | make_lab_test() |
| 16 | make_consulation() |
| 17 | make_appointment() |
| 18 | consulation_on_appointment() |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 19 | lab_test_on_encounter() |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 20 | frappe.db.commit() |
| 21 | frappe.clear_cache() |
| 22 | |
| 23 | def make_masters(): |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 24 | import_json("Healthcare Practitioner") |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 25 | import_drug() |
| 26 | frappe.db.commit() |
| 27 | |
| 28 | def make_patient(): |
| 29 | file_path = get_json_path("Patient") |
| 30 | with open(file_path, "r") as open_file: |
| 31 | patient_data = json.loads(open_file.read()) |
| 32 | count = 1 |
| 33 | |
| 34 | for d in enumerate(patient_data): |
| 35 | patient = frappe.new_doc("Patient") |
| 36 | patient.patient_name = d[1]['patient_name'].title() |
| 37 | patient.sex = d[1]['gender'] |
| 38 | patient.blood_group = "A Positive" |
| 39 | patient.date_of_birth = datetime.datetime(1990, 3, 25) |
| 40 | patient.email_id = d[1]['patient_name'] + "_" + patient.date_of_birth.strftime('%m/%d/%Y') + "@example.com" |
| 41 | if count <5: |
| 42 | patient.insert() |
| 43 | frappe.db.commit() |
| 44 | count+=1 |
| 45 | |
| 46 | def make_appointment(): |
| 47 | i = 1 |
| 48 | while i <= 4: |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 49 | practitioner = get_random("Healthcare Practitioner") |
| 50 | department = frappe.get_value("Healthcare Practitioner", practitioner, "department") |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 51 | patient = get_random("Patient") |
| 52 | patient_sex = frappe.get_value("Patient", patient, "sex") |
| 53 | appointment = frappe.new_doc("Patient Appointment") |
| 54 | startDate = datetime.datetime.now() |
| 55 | for x in random_date(startDate,0): |
| 56 | appointment_datetime = x |
| 57 | appointment.appointment_datetime = appointment_datetime |
| 58 | appointment.appointment_time = appointment_datetime |
| 59 | appointment.appointment_date = appointment_datetime |
| 60 | appointment.patient = patient |
| 61 | appointment.patient_sex = patient_sex |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 62 | appointment.practitioner = practitioner |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 63 | appointment.department = department |
| 64 | appointment.save(ignore_permissions = True) |
| 65 | i += 1 |
| 66 | |
| 67 | def make_consulation(): |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 68 | for i in range(3): |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 69 | practitioner = get_random("Healthcare Practitioner") |
| 70 | department = frappe.get_value("Healthcare Practitioner", practitioner, "department") |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 71 | patient = get_random("Patient") |
| 72 | patient_sex = frappe.get_value("Patient", patient, "sex") |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 73 | encounter = set_encounter(patient, patient_sex, practitioner, department, getdate(), i) |
| 74 | encounter.save(ignore_permissions=True) |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 75 | |
| 76 | def consulation_on_appointment(): |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 77 | for i in range(3): |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 78 | appointment = get_random("Patient Appointment") |
| 79 | appointment = frappe.get_doc("Patient Appointment",appointment) |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 80 | encounter = set_encounter(appointment.patient, appointment.patient_sex, appointment.practitioner, appointment.department, appointment.appointment_date, i) |
| 81 | encounter.appointment = appointment.name |
| 82 | encounter.save(ignore_permissions=True) |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 83 | |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 84 | def set_encounter(patient, patient_sex, practitioner, department, encounter_date, i): |
| 85 | encounter = frappe.new_doc("Patient Encounter") |
| 86 | encounter.patient = patient |
| 87 | encounter.patient_sex = patient_sex |
| 88 | encounter.practitioner = practitioner |
| 89 | encounter.visit_department = department |
| 90 | encounter.encounter_date = encounter_date |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 91 | if i > 2 and patient_sex=='Female': |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 92 | encounter.symptoms = "Having chest pains for the last week." |
| 93 | encounter.diagnosis = """This patient's description of dull, aching, |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 94 | exertion related substernal chest pain is suggestive of ischemic |
| 95 | cardiac origin. Her findings of a FH of early ASCVD, hypertension, |
| 96 | and early surgical menopause are pertinent risk factors for development |
| 97 | of coronary artery disease. """ |
| 98 | else: |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 99 | encounter = append_drug_rx(encounter) |
| 100 | encounter = append_test_rx(encounter) |
| 101 | return encounter |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 102 | |
| 103 | def make_lab_test(): |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 104 | practitioner = get_random("Healthcare Practitioner") |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 105 | patient = get_random("Patient") |
| 106 | patient_sex = frappe.get_value("Patient", patient, "sex") |
| 107 | template = get_random("Lab Test Template") |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 108 | set_lab_test(patient, patient_sex, practitioner, template) |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 109 | |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 110 | def lab_test_on_encounter(): |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 111 | i = 1 |
| 112 | while i <= 2: |
| 113 | test_rx = get_random("Lab Prescription", filters={'test_created': 0}) |
| 114 | test_rx = frappe.get_doc("Lab Prescription", test_rx) |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 115 | encounter = frappe.get_doc("Patient Encounter", test_rx.parent) |
| 116 | set_lab_test(encounter.patient, encounter.patient_sex, encounter.practitioner, test_rx.test_code, test_rx.name) |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 117 | i += 1 |
| 118 | |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 119 | def set_lab_test(patient, patient_sex, practitioner, template, rx=None): |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 120 | lab_test = frappe.new_doc("Lab Test") |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 121 | lab_test.practitioner = practitioner |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 122 | lab_test.patient = patient |
| 123 | lab_test.patient_sex = patient_sex |
| 124 | lab_test.template = template |
| 125 | lab_test.prescription = rx |
| 126 | create_test_from_template(lab_test) |
| 127 | |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 128 | def append_test_rx(encounter): |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 129 | i = 1 |
| 130 | while i <= 2: |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 131 | test_rx = encounter.append("test_prescription") |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 132 | test_rx.test_code = get_random("Lab Test Template") |
| 133 | i += 1 |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 134 | return encounter |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 135 | |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 136 | def append_drug_rx(encounter): |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 137 | i = 1 |
| 138 | while i <= 3: |
| 139 | drug = get_random("Item", filters={"item_group":"Drug"}) |
| 140 | drug = frappe.get_doc("Item", drug) |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 141 | drug_rx = encounter.append("drug_prescription") |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 142 | drug_rx.drug_code = drug.item_code |
| 143 | drug_rx.drug_name = drug.item_name |
| 144 | drug_rx.dosage = get_random("Prescription Dosage") |
| 145 | drug_rx.period = get_random("Prescription Duration") |
| 146 | i += 1 |
Jamsheer | f926911 | 2018-07-16 18:08:53 +0530 | [diff] [blame] | 147 | return encounter |
Rushabh Mehta | f056974 | 2017-09-13 12:52:30 +0530 | [diff] [blame] | 148 | |
| 149 | def random_date(start,l): |
| 150 | current = start |
| 151 | while l >= 0: |
| 152 | curr = current + datetime.timedelta(minutes=60) |
| 153 | yield curr |
| 154 | l-=1 |
| 155 | |
| 156 | def import_drug(): |
| 157 | frappe.flags.in_import = True |
| 158 | data = json.loads(open(frappe.get_app_path('erpnext', 'demo', 'data', 'drug_list.json')).read()) |
| 159 | for d in data: |
| 160 | doc = frappe.new_doc("Item") |
| 161 | doc.update(d) |
| 162 | doc.insert() |
| 163 | frappe.flags.in_import = False |
| 164 | |
| 165 | def get_json_path(doctype): |
| 166 | return frappe.get_app_path('erpnext', 'demo', 'data', frappe.scrub(doctype) + '.json') |