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