blob: 7e319687d8c971f4e1fda302136f5d7683c7018f [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
Jamsheerba119722018-07-06 15:58:13 +05307from frappe import _
Jamsheer25dda3a2018-07-30 14:50:16 +05308import math
Rucha Mahabal197165f2020-03-26 17:29:50 +05309from frappe.utils import time_diff_in_hours, rounded
Jamsheerba119722018-07-06 15:58:13 +053010from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_income_account
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +053011from erpnext.healthcare.doctype.fee_validity.fee_validity import create_fee_validity
Jamsheer0ae100b2018-08-01 14:29:43 +053012from erpnext.healthcare.doctype.lab_test.lab_test import create_multiple
Jamsheerba119722018-07-06 15:58:13 +053013
14@frappe.whitelist()
15def get_healthcare_services_to_invoice(patient):
Rucha Mahabal27512c82020-03-09 17:29:23 +053016 patient = frappe.get_doc('Patient', patient)
Jamsheerba119722018-07-06 15:58:13 +053017 if patient:
Rucha Mahabal27512c82020-03-09 17:29:23 +053018 validate_customer_created(patient)
19 items_to_invoice = []
20 patient_appointments = frappe.get_list(
21 'Patient Appointment',
22 fields='*',
Rucha Mahabal2d785b72020-03-17 20:18:16 +053023 filters={'patient': patient.name, 'invoiced': 0},
Rucha Mahabal27512c82020-03-09 17:29:23 +053024 order_by='appointment_date'
25 )
26 if patient_appointments:
27 items_to_invoice = get_fee_validity(patient_appointments)
Jamsheerba119722018-07-06 15:58:13 +053028
Rucha Mahabal27512c82020-03-09 17:29:23 +053029 encounters = get_encounters_to_invoice(patient)
30 lab_tests = get_lab_tests_to_invoice(patient)
31 clinical_procedures = get_clinical_procedures_to_invoice(patient)
32 inpatient_services = get_inpatient_services_to_invoice(patient)
Jamsheerba119722018-07-06 15:58:13 +053033
Rucha Mahabal2d785b72020-03-17 20:18:16 +053034 items_to_invoice += encounters + lab_tests + clinical_procedures + inpatient_services
Rucha Mahabal27512c82020-03-09 17:29:23 +053035 return items_to_invoice
Jamsheerba119722018-07-06 15:58:13 +053036
Rucha Mahabal27512c82020-03-09 17:29:23 +053037def validate_customer_created(patient):
38 if not frappe.db.get_value('Patient', patient.name, 'customer'):
39 msg = _("Please set a Customer linked to the Patient")
40 msg += " <b><a href='#Form/Patient/{0}'>{0}</a></b>".format(patient.name)
41 frappe.throw(msg, title=_('Customer Not Found'))
Jamsheer8da6f4e2018-07-26 21:03:17 +053042
Rucha Mahabal27512c82020-03-09 17:29:23 +053043def get_fee_validity(patient_appointments):
Rucha Mahabalf2574dd2020-03-17 19:28:18 +053044 if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'):
45 return
46
Rucha Mahabal27512c82020-03-09 17:29:23 +053047 items_to_invoice = []
Rucha Mahabal27512c82020-03-09 17:29:23 +053048 for appointment in patient_appointments:
49 if appointment.procedure_template:
50 if frappe.db.get_value('Clinical Procedure Template', appointment.procedure_template, 'is_billable'):
51 items_to_invoice.append({
52 'reference_type': 'Patient Appointment',
53 'reference_name': appointment.name,
54 'service': appointment.procedure_template
55 })
Jamsheerba119722018-07-06 15:58:13 +053056 else:
Rucha Mahabal2d785b72020-03-17 20:18:16 +053057 fee_validity = frappe.db.exists('Fee Validity Reference', {'appointment': appointment.name})
58 if not fee_validity:
Rucha Mahabal27512c82020-03-09 17:29:23 +053059 practitioner_charge = 0
60 income_account = None
61 service_item = None
62 if appointment.practitioner:
63 service_item, practitioner_charge = get_service_item_and_practitioner_charge(appointment)
64 income_account = get_income_account(appointment.practitioner, appointment.company)
Rucha Mahabal2d785b72020-03-17 20:18:16 +053065 items_to_invoice.append({
66 'reference_type': 'Patient Appointment',
67 'reference_name': appointment.name,
68 'service': service_item,
69 'rate': practitioner_charge,
70 'income_account': income_account
71 })
Rucha Mahabal27512c82020-03-09 17:29:23 +053072
73 return items_to_invoice
74
75
76def get_encounters_to_invoice(patient):
77 encounters_to_invoice = []
78 encounters = frappe.get_list(
79 'Patient Encounter',
80 fields=['*'],
81 filters={'patient': patient.name, 'invoiced': False, 'docstatus': 1}
82 )
83 if encounters:
84 for encounter in encounters:
85 if not encounter.appointment:
86 practitioner_charge = 0
87 income_account = None
88 service_item = None
89 if encounter.practitioner:
90 service_item, practitioner_charge = get_service_item_and_practitioner_charge(encounter)
91 income_account = get_income_account(encounter.practitioner, encounter.company)
92
93 encounters_to_invoice.append({
94 'reference_type': 'Patient Encounter',
95 'reference_name': encounter.name,
96 'service': service_item,
97 'rate': practitioner_charge,
98 'income_account': income_account
99 })
100
101 return encounters_to_invoice
102
103
104def get_lab_tests_to_invoice(patient):
105 lab_tests_to_invoice = []
106 lab_tests = frappe.get_list(
107 'Lab Test',
108 fields=['name', 'template'],
109 filters={'patient': patient.name, 'invoiced': False, 'docstatus': 1}
110 )
111 for lab_test in lab_tests:
112 if frappe.db.get_value('Lab Test Template', lab_test.template, 'is_billable'):
113 lab_tests_to_invoice.append({
114 'reference_type': 'Lab Test',
115 'reference_name': lab_test.name,
116 'service': frappe.db.get_value('Lab Test Template', lab_test.template, 'item')
117 })
118
119 lab_prescriptions = frappe.db.sql('''select lp.name, lp.lab_test_code from `tabPatient Encounter` et, `tabLab Prescription` lp
120 where et.patient=%s and lp.parent=et.name and lp.lab_test_created=0 and lp.invoiced=0''', (patient.name), as_dict=1)
121
122 for prescription in lab_prescriptions:
123 if prescription.lab_test_code and frappe.db.get_value('Lab Test Template', prescription.lab_test_code, 'is_billable'):
124 lab_tests_to_invoice.append({
125 'reference_type': 'Lab Prescription',
126 'reference_name': prescription.name,
127 'service': frappe.db.get_value('Lab Test Template', prescription.lab_test_code, 'item')
128 })
129
130 return lab_tests_to_invoice
131
132
133def get_clinical_procedures_to_invoice(patient):
134 clinical_procedures_to_invoice = []
135 procedures = frappe.get_list(
136 'Clinical Procedure',
137 fields='*',
138 filters={'patient': patient.name, 'invoiced': False}
139 )
140 for procedure in procedures:
141 if not procedure.appointment:
142 if procedure.procedure_template and frappe.db.get_value('Clinical Procedure Template', procedure.procedure_template, 'is_billable'):
143 clinical_procedures_to_invoice.append({
144 'reference_type': 'Clinical Procedure',
145 'reference_name': procedure.name,
146 'service': frappe.db.get_value('Clinical Procedure Template', procedure.procedure_template, 'item')
147 })
148
149 # consumables
150 if procedure.invoice_separately_as_consumables and procedure.consume_stock \
151 and procedure.status == 'Completed' and not procedure.consumption_invoiced:
152
153 service_item = get_healthcare_service_item('clinical_procedure_consumable_item')
154 if not service_item:
155 msg = _('Please Configure Clinical Procedure Consumable Item in ')
156 msg += '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>'''
157 frappe.throw(msg, title=_('Missing Configuration'))
158
159 clinical_procedures_to_invoice.append({
160 'reference_type': 'Clinical Procedure',
161 'reference_name': procedure.name,
162 'service': service_item,
163 'rate': procedure.consumable_total_amount,
164 'description': procedure.consumption_details
165 })
166
167 procedure_prescriptions = frappe.db.sql('''select pp.name, pp.procedure from `tabPatient Encounter` et,
168 `tabProcedure Prescription` pp where et.patient=%s and pp.parent=et.name and
169 pp.procedure_created=0 and pp.invoiced=0 and pp.appointment_booked=0''', (patient.name), as_dict=1)
170
171 for prescription in procedure_prescriptions:
172 if frappe.db.get_value('Clinical Procedure Template', prescription.procedure, 'is_billable'):
Rucha Mahabal197165f2020-03-26 17:29:50 +0530173 clinical_procedures_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +0530174 'reference_type': 'Procedure Prescription',
175 'reference_name': prescription.name,
176 'service': frappe.db.get_value('Clinical Procedure Template', prescription.procedure, 'item')
177 })
178
179 return clinical_procedures_to_invoice
180
181
182def get_inpatient_services_to_invoice(patient):
183 services_to_invoice = []
184 inpatient_services = frappe.db.sql('''select io.* from `tabInpatient Record` ip,
185 `tabInpatient Occupancy` io where ip.patient=%s and io.parent=ip.name and
186 io.left=1 and io.invoiced=0''', (patient.name), as_dict=1)
187
188 for inpatient_occupancy in inpatient_services:
189 service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type')
190 service_unit_type = frappe.get_doc('Healthcare Service Unit Type', service_unit_type)
191 if service_unit_type and service_unit_type.is_billable:
192 hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in)
193 qty = 0.5
194 if hours_occupied > 0:
195 actual_qty = hours_occupied / service_unit_type.no_of_hours
196 floor = math.floor(actual_qty)
197 decimal_part = actual_qty - floor
198 if decimal_part > 0.5:
199 qty = rounded(floor + 1, 1)
200 elif decimal_part < 0.5 and decimal_part > 0:
201 qty = rounded(floor + 0.5, 1)
202 if qty <= 0:
203 qty = 0.5
204 services_to_invoice.append({
205 'reference_type': 'Inpatient Occupancy',
206 'reference_name': inpatient_occupancy.name,
207 'service': service_unit_type.item, 'qty': qty
208 })
209
210 return services_to_invoice
211
Jamsheerba119722018-07-06 15:58:13 +0530212
Rucha Mahabal24055e12020-02-24 19:09:50 +0530213def get_service_item_and_practitioner_charge(doc):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530214 is_inpatient = doc.inpatient_record
Rucha Mahabal24055e12020-02-24 19:09:50 +0530215 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530216 service_item = get_practitioner_service_item(doc.practitioner, 'inpatient_visit_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530217 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530218 service_item = get_healthcare_service_item('inpatient_visit_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530219 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530220 service_item = get_practitioner_service_item(doc.practitioner, 'op_consulting_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530221 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530222 service_item = get_healthcare_service_item('op_consulting_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530223 if not service_item:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530224 throw_config_service_item(is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530225
Rucha Mahabal24055e12020-02-24 19:09:50 +0530226 practitioner_charge = get_practitioner_charge(doc.practitioner, is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530227 if not practitioner_charge:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530228 throw_config_practitioner_charge(is_inpatient, doc.practitioner)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530229
230 return service_item, practitioner_charge
231
Jamsheer8da6f4e2018-07-26 21:03:17 +0530232
Rucha Mahabal27512c82020-03-09 17:29:23 +0530233def throw_config_service_item(is_inpatient):
Rucha Mahabal4f9a1472020-03-23 10:40:39 +0530234 service_item_label = 'Out Patient Consulting Charge Item'
Rucha Mahabal27512c82020-03-09 17:29:23 +0530235 if is_inpatient:
Rucha Mahabal4f9a1472020-03-23 10:40:39 +0530236 service_item_label = 'Inpatient Visit Charge Item'
Rucha Mahabal27512c82020-03-09 17:29:23 +0530237
Rucha Mahabal4f9a1472020-03-23 10:40:39 +0530238 msg = _(('Please Configure {0} in ').format(service_item_label) \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530239 + '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>''')
240 frappe.throw(msg, title=_('Missing Configuration'))
241
Jamsheer8da6f4e2018-07-26 21:03:17 +0530242
Rucha Mahabal24055e12020-02-24 19:09:50 +0530243def throw_config_practitioner_charge(is_inpatient, practitioner):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530244 charge_name = 'OP Consulting Charge'
Rucha Mahabal24055e12020-02-24 19:09:50 +0530245 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530246 charge_name = 'Inpatient Visit Charge'
Jamsheer8da6f4e2018-07-26 21:03:17 +0530247
Rucha Mahabal27512c82020-03-09 17:29:23 +0530248 msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \
249 + ''' <b><a href='#Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner))
250 frappe.throw(msg, title=_('Missing Configuration'))
251
Jamsheer8da6f4e2018-07-26 21:03:17 +0530252
Jamsheeree5f9c72018-07-30 12:42:06 +0530253def get_practitioner_service_item(practitioner, service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530254 return frappe.db.get_value('Healthcare Practitioner', practitioner, service_item_field)
255
Jamsheeree5f9c72018-07-30 12:42:06 +0530256
Jamsheer8da6f4e2018-07-26 21:03:17 +0530257def get_healthcare_service_item(service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530258 return frappe.db.get_single_value('Healthcare Settings', service_item_field)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530259
Jamsheer8da6f4e2018-07-26 21:03:17 +0530260
Rucha Mahabal24055e12020-02-24 19:09:50 +0530261def get_practitioner_charge(practitioner, is_inpatient):
262 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530263 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'inpatient_visit_charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530264 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530265 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'op_consulting_charge')
Jamsheerba119722018-07-06 15:58:13 +0530266 if practitioner_charge:
267 return practitioner_charge
Jamsheer8da6f4e2018-07-26 21:03:17 +0530268 return False
Jamsheerba119722018-07-06 15:58:13 +0530269
Rucha Mahabal27512c82020-03-09 17:29:23 +0530270
Jamsheerba119722018-07-06 15:58:13 +0530271def manage_invoice_submit_cancel(doc, method):
272 if doc.items:
273 for item in doc.items:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530274 if item.get('reference_dt') and item.get('reference_dn'):
275 if frappe.get_meta(item.reference_dt).has_field('invoiced'):
Jamsheer146683b2018-07-25 11:30:30 +0530276 set_invoiced(item, method, doc.name)
Jamsheerba119722018-07-06 15:58:13 +0530277
Rucha Mahabal27512c82020-03-09 17:29:23 +0530278 if method=='on_submit' and frappe.db.get_single_value('Healthcare Settings', 'create_lab_test_on_si_submit'):
279 create_multiple('Sales Invoice', doc.name)
280
Jamsheer0ae100b2018-08-01 14:29:43 +0530281
Jamsheer146683b2018-07-25 11:30:30 +0530282def set_invoiced(item, method, ref_invoice=None):
Jamsheerba119722018-07-06 15:58:13 +0530283 invoiced = False
Rucha Mahabal27512c82020-03-09 17:29:23 +0530284 if method=='on_submit':
Jamsheerba119722018-07-06 15:58:13 +0530285 validate_invoiced_on_submit(item)
286 invoiced = True
287
Jamsheer8da6f4e2018-07-26 21:03:17 +0530288 if item.reference_dt == 'Clinical Procedure':
289 if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530290 frappe.db.set_value(item.reference_dt, item.reference_dn, 'consumption_invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530291 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530292 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530293 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530294 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530295
Jamsheerba119722018-07-06 15:58:13 +0530296 if item.reference_dt == 'Patient Appointment':
297 if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530298 dt_from_appointment = 'Clinical Procedure'
Jamsheerba119722018-07-06 15:58:13 +0530299 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530300 dt_from_appointment = 'Patient Encounter'
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530301 manage_doc_for_appointment(dt_from_appointment, item.reference_dn, invoiced)
Jamsheerba119722018-07-06 15:58:13 +0530302
303 elif item.reference_dt == 'Lab Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530304 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Lab Test', 'lab_test_created')
Jamsheerba119722018-07-06 15:58:13 +0530305
306 elif item.reference_dt == 'Procedure Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530307 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Clinical Procedure', 'procedure_created')
308
Jamsheerba119722018-07-06 15:58:13 +0530309
310def validate_invoiced_on_submit(item):
Jamsheer8da6f4e2018-07-26 21:03:17 +0530311 if item.reference_dt == 'Clinical Procedure' and get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530312 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'consumption_invoiced')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530313 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530314 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'invoiced')
315 if is_invoiced:
316 frappe.throw(_('The item referenced by {0} - {1} is already invoiced'\
Jamsheerba119722018-07-06 15:58:13 +0530317 ).format(item.reference_dt, item.reference_dn))
318
Rucha Mahabal27512c82020-03-09 17:29:23 +0530319
Jamsheerba119722018-07-06 15:58:13 +0530320def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
321 created = frappe.db.get_value(ref_dt, ref_dn, created_check_field)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530322 if created:
Jamsheerba119722018-07-06 15:58:13 +0530323 # Fetch the doc created for the prescription
Jamsheereafb0462018-07-25 13:15:12 +0530324 doc_created = frappe.db.get_value(dt, {'prescription': ref_dn})
Jamsheerba119722018-07-06 15:58:13 +0530325 frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
326
Rucha Mahabal27512c82020-03-09 17:29:23 +0530327
Rucha Mahabalcd319962020-03-13 15:39:31 +0530328def check_fee_validity(appointment):
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530329 if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'):
330 return
331
Rucha Mahabalcd319962020-03-13 15:39:31 +0530332 validity = frappe.db.exists('Fee Validity', {
333 'practitioner': appointment.practitioner,
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530334 'patient': appointment.patient,
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530335 'valid_till': ('>=', appointment.appointment_date)
Rucha Mahabalcd319962020-03-13 15:39:31 +0530336 })
337 if not validity:
338 return
339
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530340 validity = frappe.get_doc('Fee Validity', validity)
341 return validity
342
Rucha Mahabal27512c82020-03-09 17:29:23 +0530343
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530344def manage_fee_validity(appointment):
345 fee_validity = check_fee_validity(appointment)
Rucha Mahabalcd319962020-03-13 15:39:31 +0530346 if fee_validity:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530347 if appointment.status == 'Cancelled' and fee_validity.visited > 0:
348 fee_validity.visited -= 1
349 frappe.db.delete('Fee Validity Reference', {'appointment': appointment.name})
Rucha Mahabal2cec6bd2020-03-26 14:38:12 +0530350 elif fee_validity.status == 'Completed':
351 return
Rucha Mahabalcd319962020-03-13 15:39:31 +0530352 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530353 fee_validity.visited += 1
354 fee_validity.append('ref_appointments', {
355 'appointment': appointment.name
356 })
357 fee_validity.save(ignore_permissions=True)
Jamsheerba119722018-07-06 15:58:13 +0530358 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530359 fee_validity = create_fee_validity(appointment)
360 return fee_validity
Rucha Mahabal27512c82020-03-09 17:29:23 +0530361
Jamsheerba119722018-07-06 15:58:13 +0530362
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530363def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced):
Rucha Mahabalc4b2dce2020-03-09 23:57:00 +0530364 dn_from_appointment = frappe.db.get_value(
365 dt_from_appointment,
Rucha Mahabal27512c82020-03-09 17:29:23 +0530366 filters={'appointment': appointment}
Jamsheerba119722018-07-06 15:58:13 +0530367 )
368 if dn_from_appointment:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530369 frappe.db.set_value(dt_from_appointment, dn_from_appointment, 'invoiced', invoiced)
370
Jamsheere82f27a2018-07-30 11:28:37 +0530371
372@frappe.whitelist()
373def get_drugs_to_invoice(encounter):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530374 encounter = frappe.get_doc('Patient Encounter', encounter)
Jamsheere82f27a2018-07-30 11:28:37 +0530375 if encounter:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530376 patient = frappe.get_doc('Patient', encounter.patient)
377 if patient:
378 if patient.customer:
379 items_to_invoice = []
Jamsheere82f27a2018-07-30 11:28:37 +0530380 for drug_line in encounter.drug_prescription:
381 if drug_line.drug_code:
382 qty = 1
Rucha Mahabal27512c82020-03-09 17:29:23 +0530383 if frappe.db.get_value('Item', drug_line.drug_code, 'stock_uom') == 'Nos':
Jamsheere82f27a2018-07-30 11:28:37 +0530384 qty = drug_line.get_quantity()
Rucha Mahabal27512c82020-03-09 17:29:23 +0530385
386 description = ''
387 if drug_line.dosage and drug_line.period:
388 description = _('{0} for {1}').format(drug_line.dosage, drug_line.period)
389
390 items_to_invoice.append({
391 'drug_code': drug_line.drug_code,
392 'quantity': qty,
393 'description': description
394 })
395 return items_to_invoice
396 else:
397 validate_customer_created(patient)
398
Jamsheer4371c7e2018-08-01 18:40:05 +0530399
400@frappe.whitelist()
401def get_children(doctype, parent, company, is_root=False):
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530402 parent_fieldname = "parent_" + doctype.lower().replace(" ", "_")
Jamsheer4371c7e2018-08-01 18:40:05 +0530403 fields = [
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530404 "name as value",
405 "is_group as expandable",
406 "lft",
407 "rgt"
Jamsheer4371c7e2018-08-01 18:40:05 +0530408 ]
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530409 # fields = [ "name", "is_group", "lft", "rgt" ]
410 filters = [["ifnull(`{0}`,'')".format(parent_fieldname), "=", "" if is_root else parent]]
Jamsheer4371c7e2018-08-01 18:40:05 +0530411
412 if is_root:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530413 fields += ["service_unit_type"] if doctype == "Healthcare Service Unit" else []
414 filters.append(["company", "=", company])
Jamsheer4371c7e2018-08-01 18:40:05 +0530415
416 else:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530417 fields += ["service_unit_type", "allow_appointments", "inpatient_occupancy", "occupancy_status"] if doctype == "Healthcare Service Unit" else []
418 fields += [parent_fieldname + " as parent"]
Jamsheer4371c7e2018-08-01 18:40:05 +0530419
420 hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters)
421
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530422 if doctype == "Healthcare Service Unit":
Jamsheer4371c7e2018-08-01 18:40:05 +0530423 for each in hc_service_units:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530424 occupancy_msg = ""
425 if each["expandable"] == 1:
Jamsheer4371c7e2018-08-01 18:40:05 +0530426 occupied = False
427 vacant = False
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530428 child_list = frappe.db.sql("""
Jamsheer4371c7e2018-08-01 18:40:05 +0530429 select name, occupancy_status from `tabHealthcare Service Unit`
430 where inpatient_occupancy = 1 and
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530431 lft > %s and rgt < %s""",
432 (each["lft"], each["rgt"]))
Jamsheer4371c7e2018-08-01 18:40:05 +0530433 for child in child_list:
Jamsheer4371c7e2018-08-01 18:40:05 +0530434 if not occupied:
435 occupied = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530436 if child[1] == "Occupied":
Jamsheer4371c7e2018-08-01 18:40:05 +0530437 occupied += 1
438 if not vacant:
439 vacant = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530440 if child[1] == "Vacant":
Jamsheer4371c7e2018-08-01 18:40:05 +0530441 vacant += 1
442 if vacant and occupied:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530443 occupancy_total = vacant + occupied
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530444 occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total)
445 each["occupied_out_of_vacant"] = occupancy_msg
Jamsheer4371c7e2018-08-01 18:40:05 +0530446 return hc_service_units
Jamsheer5073ac42019-07-12 12:28:34 +0530447
Rucha Mahabal27512c82020-03-09 17:29:23 +0530448
Jamsheer5073ac42019-07-12 12:28:34 +0530449@frappe.whitelist()
450def get_patient_vitals(patient, from_date=None, to_date=None):
451 if not patient: return
Rucha Mahabal27512c82020-03-09 17:29:23 +0530452
453 vitals = frappe.db.get_all('Vital Signs', {
454 'docstatus': 1,
455 'patient': patient
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530456 }, order_by='signs_date, signs_time')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530457
458 if len(vitals):
Jamsheer5073ac42019-07-12 12:28:34 +0530459 return vitals
Rucha Mahabal27512c82020-03-09 17:29:23 +0530460 return False
461
Jamsheer5073ac42019-07-12 12:28:34 +0530462
463@frappe.whitelist()
464def render_docs_as_html(docs):
465 # docs key value pair {doctype: docname}
466 docs_html = "<div class='col-md-12 col-sm-12 text-muted'>"
467 for doc in docs:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530468 docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + '<br/>'
Jamsheer5073ac42019-07-12 12:28:34 +0530469 return {'html': docs_html}
470
Rucha Mahabal27512c82020-03-09 17:29:23 +0530471
Jamsheer5073ac42019-07-12 12:28:34 +0530472@frappe.whitelist()
473def render_doc_as_html(doctype, docname, exclude_fields = []):
474 #render document as html, three column layout will break
475 doc = frappe.get_doc(doctype, docname)
476 meta = frappe.get_meta(doctype)
477 doc_html = "<div class='col-md-12 col-sm-12'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530478 section_html = ''
479 section_label = ''
480 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530481 sec_on = False
482 col_on = 0
483 has_data = False
484 for df in meta.fields:
485 #on section break append append previous section and html to doc html
486 if df.fieldtype == "Section Break":
487 if has_data and col_on and sec_on:
488 doc_html += section_html + html + "</div>"
489 elif has_data and not col_on and sec_on:
490 doc_html += "<div class='col-md-12 col-sm-12'\
491 ><div class='col-md-12 col-sm-12'>" \
492 + section_html + html +"</div></div>"
493 while col_on:
494 doc_html += "</div>"
495 col_on -= 1
496 sec_on = True
497 has_data= False
498 col_on = 0
Rucha Mahabal27512c82020-03-09 17:29:23 +0530499 section_html = ''
500 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530501 if df.label:
502 section_label = df.label
503 continue
504 #on column break append html to section html or doc html
505 if df.fieldtype == "Column Break":
506 if sec_on and has_data:
507 section_html += "<div class='col-md-12 col-sm-12'\
508 ><div class='col-md-6 col\
509 -sm-6'><b>" + section_label + "</b>" + html + "</div><div \
510 class='col-md-6 col-sm-6'>"
511 elif has_data:
512 doc_html += "<div class='col-md-12 col-sm-12'><div class='col-m\
513 d-6 col-sm-6'>" + html + "</div><div class='col-md-6 col-sm-6'>"
514 elif sec_on and not col_on:
515 section_html += "<div class='col-md-6 col-sm-6'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530516 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530517 col_on += 1
518 if df.label:
519 html += '<br>' + df.label
520 continue
521 #on table iterate in items and create table based on in_list_view, append to section html or doc html
Rucha Mahabal27512c82020-03-09 17:29:23 +0530522 if df.fieldtype == 'Table':
Jamsheer5073ac42019-07-12 12:28:34 +0530523 items = doc.get(df.fieldname)
524 if not items: continue
525 child_meta = frappe.get_meta(df.options)
526 if not has_data : has_data = True
Rucha Mahabal27512c82020-03-09 17:29:23 +0530527 table_head = ''
528 table_row = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530529 create_head = True
530 for item in items:
531 table_row += '<tr>'
532 for cdf in child_meta.fields:
533 if cdf.in_list_view:
534 if create_head:
535 table_head += '<th>' + cdf.label + '</th>'
536 if item.get(cdf.fieldname):
537 table_row += '<td>' + str(item.get(cdf.fieldname)) \
538 + '</td>'
539 else:
540 table_row += '<td></td>'
541 create_head = False
542 table_row += '</tr>'
543 if sec_on:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530544 section_html += "<table class='table table-condensed \
545 bordered'>" + table_head + table_row + '</table>'
Jamsheer5073ac42019-07-12 12:28:34 +0530546 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530547 html += "<table class='table table-condensed table-bordered'>" \
548 + table_head + table_row + "</table>"
Jamsheer5073ac42019-07-12 12:28:34 +0530549 continue
550 #on other field types add label and value to html
551 if not df.hidden and not df.print_hide and doc.get(df.fieldname) and df.fieldname not in exclude_fields:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530552 html += '<br>{0} : {1}'.format(df.label or df.fieldname, \
Jamsheer5073ac42019-07-12 12:28:34 +0530553 doc.get(df.fieldname))
554 if not has_data : has_data = True
555 if sec_on and col_on and has_data:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530556 doc_html += section_html + html + '</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530557 elif sec_on and not col_on and has_data:
558 doc_html += "<div class='col-md-12 col-sm-12'\
559 ><div class='col-md-12 col-sm-12'>" \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530560 + section_html + html +'</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530561 if doc_html:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530562 doc_html = "<div class='small'><div class='col-md-12 text-right'><a class='btn btn-default btn-xs' href='#Form/%s/%s'></a></div>" %(doctype, docname) + doc_html + '</div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530563
564 return {'html': doc_html}