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