blob: 0987eb5403bd72cef9d65686d8e9327b2cd81072 [file] [log] [blame]
Jamsheerba119722018-07-06 15:58:13 +05301# -*- coding: utf-8 -*-
2# Copyright (c) 2018, earthians and contributors
3# For license information, please see license.txt
4
5from __future__ import unicode_literals
6import frappe
7import datetime
8from frappe import _
Jamsheer25dda3a2018-07-30 14:50:16 +05309import math
Jamsheer363deb62018-08-04 12:56:36 +053010from frappe.utils import time_diff_in_hours, rounded, getdate, add_days
Jamsheerba119722018-07-06 15:58:13 +053011from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_income_account
Jamsheerba119722018-07-06 15:58:13 +053012from erpnext.healthcare.doctype.fee_validity.fee_validity import create_fee_validity, update_fee_validity
Jamsheer0ae100b2018-08-01 14:29:43 +053013from erpnext.healthcare.doctype.lab_test.lab_test import create_multiple
Jamsheerba119722018-07-06 15:58:13 +053014
15@frappe.whitelist()
16def get_healthcare_services_to_invoice(patient):
17 patient = frappe.get_doc("Patient", patient)
18 if patient:
19 if patient.customer:
20 item_to_invoice = []
21 patient_appointments = frappe.get_list("Patient Appointment",{'patient': patient.name, 'invoiced': False},
22 order_by="appointment_date")
23 if patient_appointments:
24 fee_validity_details = []
25 valid_days = frappe.db.get_value("Healthcare Settings", None, "valid_days")
26 max_visit = frappe.db.get_value("Healthcare Settings", None, "max_visit")
27 for patient_appointment in patient_appointments:
28 patient_appointment_obj = frappe.get_doc("Patient Appointment", patient_appointment['name'])
29
30 if patient_appointment_obj.procedure_template:
31 if frappe.db.get_value("Clinical Procedure Template", patient_appointment_obj.procedure_template, "is_billable") == 1:
32 item_to_invoice.append({'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name, 'service': patient_appointment_obj.procedure_template})
33 else:
34 practitioner_exist_in_list = False
35 skip_invoice = False
36 if fee_validity_details:
37 for validity in fee_validity_details:
38 if validity['practitioner'] == patient_appointment_obj.practitioner:
39 practitioner_exist_in_list = True
40 if validity['valid_till'] >= patient_appointment_obj.appointment_date:
41 validity['visits'] = validity['visits']+1
42 if int(max_visit) > validity['visits']:
43 skip_invoice = True
44 if not skip_invoice:
45 validity['visits'] = 1
46 validity['valid_till'] = patient_appointment_obj.appointment_date + datetime.timedelta(days=int(valid_days))
47 if not practitioner_exist_in_list:
48 valid_till = patient_appointment_obj.appointment_date + datetime.timedelta(days=int(valid_days))
49 visits = 0
50 validity_exist = validity_exists(patient_appointment_obj.practitioner, patient_appointment_obj.patient)
51 if validity_exist:
52 fee_validity = frappe.get_doc("Fee Validity", validity_exist[0][0])
53 valid_till = fee_validity.valid_till
54 visits = fee_validity.visited
55 fee_validity_details.append({'practitioner': patient_appointment_obj.practitioner,
56 'valid_till': valid_till, 'visits': visits})
57
58 if not skip_invoice:
59 practitioner_charge = 0
60 income_account = None
Jamsheer8da6f4e2018-07-26 21:03:17 +053061 service_item = None
Jamsheerba119722018-07-06 15:58:13 +053062 if patient_appointment_obj.practitioner:
Jamsheer8da6f4e2018-07-26 21:03:17 +053063 service_item, practitioner_charge = service_item_and_practitioner_charge(patient_appointment_obj)
Jamsheerba119722018-07-06 15:58:13 +053064 income_account = get_income_account(patient_appointment_obj.practitioner, patient_appointment_obj.company)
65 item_to_invoice.append({'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name,
Jamsheer8da6f4e2018-07-26 21:03:17 +053066 'service': service_item, 'rate': practitioner_charge,
Jamsheerba119722018-07-06 15:58:13 +053067 'income_account': income_account})
68
69 encounters = frappe.get_list("Patient Encounter", {'patient': patient.name, 'invoiced': False, 'docstatus': 1})
70 if encounters:
71 for encounter in encounters:
72 encounter_obj = frappe.get_doc("Patient Encounter", encounter['name'])
73 if not encounter_obj.appointment:
74 practitioner_charge = 0
75 income_account = None
Jamsheer8da6f4e2018-07-26 21:03:17 +053076 service_item = None
Jamsheerba119722018-07-06 15:58:13 +053077 if encounter_obj.practitioner:
Jamsheer8da6f4e2018-07-26 21:03:17 +053078 service_item, practitioner_charge = service_item_and_practitioner_charge(encounter_obj)
Jamsheerba119722018-07-06 15:58:13 +053079 income_account = get_income_account(encounter_obj.practitioner, encounter_obj.company)
Jamsheer8da6f4e2018-07-26 21:03:17 +053080
Jamsheerba119722018-07-06 15:58:13 +053081 item_to_invoice.append({'reference_type': 'Patient Encounter', 'reference_name': encounter_obj.name,
Jamsheer8da6f4e2018-07-26 21:03:17 +053082 'service': service_item, 'rate': practitioner_charge,
Jamsheerba119722018-07-06 15:58:13 +053083 'income_account': income_account})
84
85 lab_tests = frappe.get_list("Lab Test", {'patient': patient.name, 'invoiced': False})
86 if lab_tests:
87 for lab_test in lab_tests:
88 lab_test_obj = frappe.get_doc("Lab Test", lab_test['name'])
89 if frappe.db.get_value("Lab Test Template", lab_test_obj.template, "is_billable") == 1:
Jamsheer8da6f4e2018-07-26 21:03:17 +053090 item_to_invoice.append({'reference_type': 'Lab Test', 'reference_name': lab_test_obj.name,
91 'service': frappe.db.get_value("Lab Test Template", lab_test_obj.template, "item")})
Jamsheerba119722018-07-06 15:58:13 +053092
Jamsheerc64880b2018-08-01 14:37:13 +053093 lab_rxs = frappe.db.sql("""select lp.name from `tabPatient Encounter` et, `tabLab Prescription` lp
Jamsheerc07e8e52018-09-18 10:54:03 +053094 where et.patient=%s and lp.parent=et.name and lp.lab_test_created=0 and lp.invoiced=0""", (patient.name))
Jamsheerba119722018-07-06 15:58:13 +053095 if lab_rxs:
96 for lab_rx in lab_rxs:
97 rx_obj = frappe.get_doc("Lab Prescription", lab_rx[0])
Jamsheerc07e8e52018-09-18 10:54:03 +053098 if rx_obj.lab_test_code and (frappe.db.get_value("Lab Test Template", rx_obj.lab_test_code, "is_billable") == 1):
Jamsheer8da6f4e2018-07-26 21:03:17 +053099 item_to_invoice.append({'reference_type': 'Lab Prescription', 'reference_name': rx_obj.name,
Jamsheerc07e8e52018-09-18 10:54:03 +0530100 'service': frappe.db.get_value("Lab Test Template", rx_obj.lab_test_code, "item")})
Jamsheerba119722018-07-06 15:58:13 +0530101
102 procedures = frappe.get_list("Clinical Procedure", {'patient': patient.name, 'invoiced': False})
103 if procedures:
104 for procedure in procedures:
105 procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name'])
106 if not procedure_obj.appointment:
107 if procedure_obj.procedure_template and (frappe.db.get_value("Clinical Procedure Template", procedure_obj.procedure_template, "is_billable") == 1):
Jamsheer8da6f4e2018-07-26 21:03:17 +0530108 item_to_invoice.append({'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name,
109 'service': frappe.db.get_value("Clinical Procedure Template", procedure_obj.procedure_template, "item")})
Jamsheerba119722018-07-06 15:58:13 +0530110
111 procedure_rxs = frappe.db.sql("""select pp.name from `tabPatient Encounter` et,
112 `tabProcedure Prescription` pp where et.patient=%s and pp.parent=et.name and
113 pp.procedure_created=0 and pp.invoiced=0 and pp.appointment_booked=0""", (patient.name))
114 if procedure_rxs:
115 for procedure_rx in procedure_rxs:
116 rx_obj = frappe.get_doc("Procedure Prescription", procedure_rx[0])
117 if frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "is_billable") == 1:
Jamsheer8da6f4e2018-07-26 21:03:17 +0530118 item_to_invoice.append({'reference_type': 'Procedure Prescription', 'reference_name': rx_obj.name,
119 'service': frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "item")})
Jamsheerba119722018-07-06 15:58:13 +0530120
Jamsheer8da6f4e2018-07-26 21:03:17 +0530121 procedures = frappe.get_list("Clinical Procedure",
122 {'patient': patient.name, 'invoice_separately_as_consumables': True, 'consumption_invoiced': False,
123 'consume_stock': True, 'status': 'Completed'})
124 if procedures:
125 service_item = get_healthcare_service_item('clinical_procedure_consumable_item')
126 if not service_item:
127 msg = _(("Please Configure {0} in ").format("Clinical Procedure Consumable Item") \
128 + """<b><a href="#Form/Healthcare Settings">Healthcare Settings</a></b>""")
129 frappe.throw(msg)
130 for procedure in procedures:
131 procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name'])
132 item_to_invoice.append({'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name,
133 'service': service_item, 'rate': procedure_obj.consumable_total_amount, 'description': procedure_obj.consumption_details})
Jamsheerba119722018-07-06 15:58:13 +0530134
Jamsheer54ae74e2018-07-13 21:10:14 +0530135 inpatient_services = frappe.db.sql("""select io.name, io.parent from `tabInpatient Record` ip,
136 `tabInpatient Occupancy` io where ip.patient=%s and io.parent=ip.name and
137 io.left=1 and io.invoiced=0""", (patient.name))
138 if inpatient_services:
139 for inpatient_service in inpatient_services:
140 inpatient_occupancy = frappe.get_doc("Inpatient Occupancy", inpatient_service[0])
141 service_unit_type = frappe.get_doc("Healthcare Service Unit Type", frappe.db.get_value("Healthcare Service Unit", inpatient_occupancy.service_unit, "service_unit_type"))
142 if service_unit_type and service_unit_type.is_billable == 1:
Jamsheer25dda3a2018-07-30 14:50:16 +0530143 hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in)
144 qty = 0.5
145 if hours_occupied > 0:
146 actual_qty = hours_occupied / service_unit_type.no_of_hours
147 floor = math.floor(actual_qty)
148 decimal_part = actual_qty - floor
149 if decimal_part > 0.5:
150 qty = rounded(floor + 1, 1)
151 elif decimal_part < 0.5 and decimal_part > 0:
152 qty = rounded(floor + 0.5, 1)
153 if qty <= 0:
154 qty = 0.5
Jamsheer54ae74e2018-07-13 21:10:14 +0530155 item_to_invoice.append({'reference_type': 'Inpatient Occupancy', 'reference_name': inpatient_occupancy.name,
156 'service': service_unit_type.item, 'qty': qty})
157
Jamsheerba119722018-07-06 15:58:13 +0530158 return item_to_invoice
159 else:
160 frappe.throw(_("The Patient {0} do not have customer refrence to invoice").format(patient.name))
161
Jamsheer8da6f4e2018-07-26 21:03:17 +0530162def service_item_and_practitioner_charge(doc):
163 is_ip = doc_is_ip(doc)
164 if is_ip:
Jamsheeree5f9c72018-07-30 12:42:06 +0530165 service_item = get_practitioner_service_item(doc.practitioner, "inpatient_visit_charge_item")
166 if not service_item:
167 service_item = get_healthcare_service_item("inpatient_visit_charge_item")
Jamsheer8da6f4e2018-07-26 21:03:17 +0530168 else:
Jamsheeree5f9c72018-07-30 12:42:06 +0530169 service_item = get_practitioner_service_item(doc.practitioner, "op_consulting_charge_item")
170 if not service_item:
171 service_item = get_healthcare_service_item("op_consulting_charge_item")
Jamsheer8da6f4e2018-07-26 21:03:17 +0530172 if not service_item:
173 throw_config_service_item(is_ip)
174
175 practitioner_charge = get_practitioner_charge(doc.practitioner, is_ip)
176 if not practitioner_charge:
177 throw_config_practitioner_charge(is_ip, doc.practitioner)
178
179 return service_item, practitioner_charge
180
181def throw_config_service_item(is_ip):
182 service_item_lable = "Out Patient Consulting Charge Item"
183 if is_ip:
184 service_item_lable = "Inpatient Visit Charge Item"
185
186 msg = _(("Please Configure {0} in ").format(service_item_lable) \
187 + """<b><a href="#Form/Healthcare Settings">Healthcare Settings</a></b>""")
188 frappe.throw(msg)
189
190def throw_config_practitioner_charge(is_ip, practitioner):
191 charge_name = "OP Consulting Charge"
192 if is_ip:
193 charge_name = "Inpatient Visit Charge"
194
195 msg = _(("Please Configure {0} for Healthcare Practitioner").format(charge_name) \
196 + """ <b><a href="#Form/Healthcare Practitioner/{0}">{0}</a></b>""".format(practitioner))
197 frappe.throw(msg)
198
Jamsheeree5f9c72018-07-30 12:42:06 +0530199def get_practitioner_service_item(practitioner, service_item_field):
200 return frappe.db.get_value("Healthcare Practitioner", practitioner, service_item_field)
201
Jamsheer8da6f4e2018-07-26 21:03:17 +0530202def get_healthcare_service_item(service_item_field):
203 return frappe.db.get_value("Healthcare Settings", None, service_item_field)
204
205def doc_is_ip(doc):
206 is_ip = False
207 if doc.inpatient_record:
208 is_ip = True
209 return is_ip
210
211def get_practitioner_charge(practitioner, is_ip):
212 if is_ip:
213 practitioner_charge = frappe.db.get_value("Healthcare Practitioner", practitioner, "inpatient_visit_charge")
214 else:
215 practitioner_charge = frappe.db.get_value("Healthcare Practitioner", practitioner, "op_consulting_charge")
Jamsheerba119722018-07-06 15:58:13 +0530216 if practitioner_charge:
217 return practitioner_charge
Jamsheer8da6f4e2018-07-26 21:03:17 +0530218 return False
Jamsheerba119722018-07-06 15:58:13 +0530219
220def manage_invoice_submit_cancel(doc, method):
221 if doc.items:
222 for item in doc.items:
Nabin Hait2ce2d2a2018-09-10 13:16:14 +0530223 if item.get("reference_dt") and item.get("reference_dn"):
Jamsheerba119722018-07-06 15:58:13 +0530224 if frappe.get_meta(item.reference_dt).has_field("invoiced"):
Jamsheer146683b2018-07-25 11:30:30 +0530225 set_invoiced(item, method, doc.name)
Jamsheerba119722018-07-06 15:58:13 +0530226
Jamsheer0ae100b2018-08-01 14:29:43 +0530227 if method=="on_submit" and frappe.db.get_value("Healthcare Settings", None, "create_test_on_si_submit") == '1':
228 create_multiple("Sales Invoice", doc.name)
229
Jamsheer146683b2018-07-25 11:30:30 +0530230def set_invoiced(item, method, ref_invoice=None):
Jamsheerba119722018-07-06 15:58:13 +0530231 invoiced = False
232 if(method=="on_submit"):
233 validate_invoiced_on_submit(item)
234 invoiced = True
235
Jamsheer8da6f4e2018-07-26 21:03:17 +0530236 if item.reference_dt == 'Clinical Procedure':
237 if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
238 frappe.db.set_value(item.reference_dt, item.reference_dn, "consumption_invoiced", invoiced)
239 else:
240 frappe.db.set_value(item.reference_dt, item.reference_dn, "invoiced", invoiced)
241 else:
242 frappe.db.set_value(item.reference_dt, item.reference_dn, "invoiced", invoiced)
243
Jamsheerba119722018-07-06 15:58:13 +0530244 if item.reference_dt == 'Patient Appointment':
245 if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'):
246 dt_from_appointment = "Clinical Procedure"
247 else:
Jamsheer146683b2018-07-25 11:30:30 +0530248 manage_fee_validity(item.reference_dn, method, ref_invoice)
Jamsheerba119722018-07-06 15:58:13 +0530249 dt_from_appointment = "Patient Encounter"
250 manage_doc_for_appoitnment(dt_from_appointment, item.reference_dn, invoiced)
251
252 elif item.reference_dt == 'Lab Prescription':
Jamsheerc07e8e52018-09-18 10:54:03 +0530253 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, "Lab Test", "lab_test_created")
Jamsheerba119722018-07-06 15:58:13 +0530254
255 elif item.reference_dt == 'Procedure Prescription':
256 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, "Clinical Procedure", "procedure_created")
257
258def validate_invoiced_on_submit(item):
Jamsheer8da6f4e2018-07-26 21:03:17 +0530259 if item.reference_dt == 'Clinical Procedure' and get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
260 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, "consumption_invoiced")
261 else:
262 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, "invoiced")
Jamsheerba119722018-07-06 15:58:13 +0530263 if is_invoiced == 1:
264 frappe.throw(_("The item referenced by {0} - {1} is already invoiced"\
265 ).format(item.reference_dt, item.reference_dn))
266
267def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
268 created = frappe.db.get_value(ref_dt, ref_dn, created_check_field)
269 if created == 1:
270 # Fetch the doc created for the prescription
Jamsheereafb0462018-07-25 13:15:12 +0530271 doc_created = frappe.db.get_value(dt, {'prescription': ref_dn})
Jamsheerba119722018-07-06 15:58:13 +0530272 frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
273
Jamsheer363deb62018-08-04 12:56:36 +0530274def validity_exists(practitioner, patient):
275 return frappe.db.exists({
276 "doctype": "Fee Validity",
277 "practitioner": practitioner,
278 "patient": patient})
279
Jamsheer146683b2018-07-25 11:30:30 +0530280def manage_fee_validity(appointment_name, method, ref_invoice=None):
Jamsheerba119722018-07-06 15:58:13 +0530281 appointment_doc = frappe.get_doc("Patient Appointment", appointment_name)
282 validity_exist = validity_exists(appointment_doc.practitioner, appointment_doc.patient)
283 do_not_update = False
284 visited = 0
285 if validity_exist:
286 fee_validity = frappe.get_doc("Fee Validity", validity_exist[0][0])
287 # Check if the validity is valid
288 if (fee_validity.valid_till >= appointment_doc.appointment_date):
289 if (method == "on_cancel" and appointment_doc.status != "Closed"):
Jamsheer363deb62018-08-04 12:56:36 +0530290 if ref_invoice == fee_validity.ref_invoice:
291 visited = fee_validity.visited - 1
292 if visited < 0:
293 visited = 0
294 frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited)
Jamsheerba119722018-07-06 15:58:13 +0530295 do_not_update = True
Jamsheer363deb62018-08-04 12:56:36 +0530296 elif (method == "on_submit" and fee_validity.visited < fee_validity.max_visit):
Jamsheerba119722018-07-06 15:58:13 +0530297 visited = fee_validity.visited + 1
298 frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited)
299 do_not_update = True
300 else:
301 do_not_update = False
302
303 if not do_not_update:
Jamsheer146683b2018-07-25 11:30:30 +0530304 fee_validity = update_fee_validity(fee_validity, appointment_doc.appointment_date, ref_invoice)
Jamsheerba119722018-07-06 15:58:13 +0530305 visited = fee_validity.visited
306 else:
Jamsheer146683b2018-07-25 11:30:30 +0530307 fee_validity = create_fee_validity(appointment_doc.practitioner, appointment_doc.patient, appointment_doc.appointment_date, ref_invoice)
Jamsheerba119722018-07-06 15:58:13 +0530308 visited = fee_validity.visited
309
310 # Mark All Patient Appointment invoiced = True in the validity range do not cross the max visit
311 if (method == "on_cancel"):
312 invoiced = True
313 else:
314 invoiced = False
Jamsheer363deb62018-08-04 12:56:36 +0530315
316 patient_appointments = appointments_valid_in_fee_validity(appointment_doc, invoiced)
Jamsheerba119722018-07-06 15:58:13 +0530317 if patient_appointments and fee_validity:
318 visit = visited
319 for appointment in patient_appointments:
320 if (method == "on_cancel" and appointment.status != "Closed"):
Jamsheer363deb62018-08-04 12:56:36 +0530321 if ref_invoice == fee_validity.ref_invoice:
322 visited = visited - 1
323 if visited < 0:
324 visited = 0
325 frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited)
Jamsheerba119722018-07-06 15:58:13 +0530326 frappe.db.set_value("Patient Appointment", appointment.name, "invoiced", False)
327 manage_doc_for_appoitnment("Patient Encounter", appointment.name, False)
Jamsheer363deb62018-08-04 12:56:36 +0530328 elif method == "on_submit" and int(fee_validity.max_visit) > visit:
329 if ref_invoice == fee_validity.ref_invoice:
330 visited = visited + 1
331 frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited)
Jamsheerba119722018-07-06 15:58:13 +0530332 frappe.db.set_value("Patient Appointment", appointment.name, "invoiced", True)
333 manage_doc_for_appoitnment("Patient Encounter", appointment.name, True)
Jamsheer363deb62018-08-04 12:56:36 +0530334 if ref_invoice == fee_validity.ref_invoice:
335 visit = visit + 1
336
337 if method == "on_cancel":
338 ref_invoice_in_fee_validity = frappe.db.get_value("Fee Validity", fee_validity.name, 'ref_invoice')
339 if ref_invoice_in_fee_validity == ref_invoice:
340 frappe.delete_doc("Fee Validity", fee_validity.name)
341
342def appointments_valid_in_fee_validity(appointment, invoiced):
343 valid_days = frappe.db.get_value("Healthcare Settings", None, "valid_days")
344 max_visit = frappe.db.get_value("Healthcare Settings", None, "max_visit")
Jamsheer14c6ab02018-10-10 14:44:36 +0530345 if int(max_visit) < 1:
346 max_visit = 1
Jamsheer363deb62018-08-04 12:56:36 +0530347 valid_days_date = add_days(getdate(appointment.appointment_date), int(valid_days))
348 return frappe.get_list("Patient Appointment",{'patient': appointment.patient, 'invoiced': invoiced,
349 'appointment_date':("<=", valid_days_date), 'appointment_date':(">=", getdate(appointment.appointment_date)),
350 'practitioner': appointment.practitioner}, order_by="appointment_date", limit=int(max_visit)-1)
Jamsheerba119722018-07-06 15:58:13 +0530351
352def manage_doc_for_appoitnment(dt_from_appointment, appointment, invoiced):
353 dn_from_appointment = frappe.db.exists(
354 dt_from_appointment,
355 {
356 "appointment": appointment
357 }
358 )
359 if dn_from_appointment:
360 frappe.db.set_value(dt_from_appointment, dn_from_appointment, "invoiced", invoiced)
Jamsheere82f27a2018-07-30 11:28:37 +0530361
362@frappe.whitelist()
363def get_drugs_to_invoice(encounter):
364 encounter = frappe.get_doc("Patient Encounter", encounter)
365 if encounter:
366 patient = frappe.get_doc("Patient", encounter.patient)
367 if patient and patient.customer:
368 item_to_invoice = []
369 for drug_line in encounter.drug_prescription:
370 if drug_line.drug_code:
371 qty = 1
372 if frappe.db.get_value("Item", drug_line.drug_code, "stock_uom") == "Nos":
373 qty = drug_line.get_quantity()
Jamsheerc07e8e52018-09-18 10:54:03 +0530374 description = False
375 if drug_line.dosage:
376 description = drug_line.dosage
377 if description and drug_line.period:
378 description += " for "+drug_line.period
379 if not description:
380 description = ""
Jamsheere82f27a2018-07-30 11:28:37 +0530381 item_to_invoice.append({'drug_code': drug_line.drug_code, 'quantity': qty,
Jamsheerc07e8e52018-09-18 10:54:03 +0530382 'description': description})
Jamsheere82f27a2018-07-30 11:28:37 +0530383 return item_to_invoice
Jamsheer4371c7e2018-08-01 18:40:05 +0530384
385@frappe.whitelist()
386def get_children(doctype, parent, company, is_root=False):
387 parent_fieldname = 'parent_' + doctype.lower().replace(' ', '_')
388 fields = [
389 'name as value',
390 'is_group as expandable',
391 'lft',
392 'rgt'
393 ]
394 # fields = [ 'name', 'is_group', 'lft', 'rgt' ]
395 filters = [['ifnull(`{0}`,"")'.format(parent_fieldname), '=', '' if is_root else parent]]
396
397 if is_root:
398 fields += ['service_unit_type'] if doctype == 'Healthcare Service Unit' else []
399 filters.append(['company', '=', company])
400
401 else:
402 fields += ['service_unit_type', 'allow_appointments', 'inpatient_occupancy', 'occupancy_status'] if doctype == 'Healthcare Service Unit' else []
403 fields += [parent_fieldname + ' as parent']
404
405 hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters)
406
407 if doctype == 'Healthcare Service Unit':
408 for each in hc_service_units:
409 occupancy_msg = ""
410 if each['expandable'] == 1:
411 occupied = False
412 vacant = False
413 child_list = frappe.db.sql("""
414 select name, occupancy_status from `tabHealthcare Service Unit`
415 where inpatient_occupancy = 1 and
416 lft > %s and rgt < %s""",
417 (each['lft'], each['rgt']))
418 for child in child_list:
Jamsheer4371c7e2018-08-01 18:40:05 +0530419 if not occupied:
420 occupied = 0
421 if child[1] == "Occupied":
422 occupied += 1
423 if not vacant:
424 vacant = 0
425 if child[1] == "Vacant":
426 vacant += 1
427 if vacant and occupied:
428 occupancy_total = vacant+occupied
429 occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total)
430 each["occupied_out_of_vacant"] = occupancy_msg
431 return hc_service_units