blob: d645e309f872ee640f0d037101c102a48dccbbaa [file] [log] [blame]
Rushabh Mehtaf0569742017-09-13 12:52:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4import frappe, json
5from frappe.utils.make_random import get_random
6import datetime
7from erpnext.demo.setup.setup_data import import_json
8from frappe.utils import getdate
9from erpnext.healthcare.doctype.lab_test.lab_test import create_test_from_template
10
11def 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()
19 lab_test_on_consultation()
20 frappe.db.commit()
21 frappe.clear_cache()
22
23def make_masters():
24 import_json("Physician")
25 import_drug()
26 frappe.db.commit()
27
28def 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
46def make_appointment():
47 i = 1
48 while i <= 4:
49 physician = get_random("Physician")
50 department = frappe.get_value("Physician", physician, "department")
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
62 appointment.physician = physician
63 appointment.department = department
64 appointment.save(ignore_permissions = True)
65 i += 1
66
67def make_consulation():
68 for i in xrange(3):
69 physician = get_random("Physician")
70 department = frappe.get_value("Physician", physician, "department")
71 patient = get_random("Patient")
72 patient_sex = frappe.get_value("Patient", patient, "sex")
73 consultation = set_consultation(patient, patient_sex, physician, department, getdate(), i)
74 consultation.save(ignore_permissions=True)
75
76def consulation_on_appointment():
77 for i in xrange(3):
78 appointment = get_random("Patient Appointment")
79 appointment = frappe.get_doc("Patient Appointment",appointment)
80 consultation = set_consultation(appointment.patient, appointment.patient_sex, appointment.physician, appointment.department, appointment.appointment_date, i)
81 consultation.appointment = appointment.name
82 consultation.save(ignore_permissions=True)
83
84def set_consultation(patient, patient_sex, physician, department, consultation_date, i):
85 consultation = frappe.new_doc("Consultation")
86 consultation.patient = patient
87 consultation.patient_sex = patient_sex
88 consultation.physician = physician
89 consultation.visit_department = department
90 consultation.consultation_date = consultation_date
91 if i > 2 and patient_sex=='Female':
92 consultation.symptoms = "Having chest pains for the last week."
93 consultation.diagnosis = """This patient's description of dull, aching,
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:
99 consultation = append_drug_rx(consultation)
100 consultation = append_test_rx(consultation)
101 return consultation
102
103def make_lab_test():
104 physician = get_random("Physician")
105 patient = get_random("Patient")
106 patient_sex = frappe.get_value("Patient", patient, "sex")
107 template = get_random("Lab Test Template")
108 set_lab_test(patient, patient_sex, physician, template)
109
110def lab_test_on_consultation():
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)
115 consultation = frappe.get_doc("Consultation", test_rx.parent)
116 set_lab_test(consultation.patient, consultation.patient_sex, consultation.physician, test_rx.test_code, test_rx.name)
117 i += 1
118
119def set_lab_test(patient, patient_sex, physician, template, rx=None):
120 lab_test = frappe.new_doc("Lab Test")
121 lab_test.physician = physician
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
128def append_test_rx(consultation):
129 i = 1
130 while i <= 2:
131 test_rx = consultation.append("test_prescription")
132 test_rx.test_code = get_random("Lab Test Template")
133 i += 1
134 return consultation
135
136def append_drug_rx(consultation):
137 i = 1
138 while i <= 3:
139 drug = get_random("Item", filters={"item_group":"Drug"})
140 drug = frappe.get_doc("Item", drug)
141 drug_rx = consultation.append("drug_prescription")
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
147 return consultation
148
149def 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
156def 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
165def get_json_path(doctype):
166 return frappe.get_app_path('erpnext', 'demo', 'data', frappe.scrub(doctype) + '.json')