blob: 246242ad84cdd82f4b16942f2387d3b7b022acc0 [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:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530112 item, is_billable = frappe.get_cached_value('Lab Test Template', lab_test.lab_test_code, ['item', 'is_billable'])
113 if is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530114 lab_tests_to_invoice.append({
115 'reference_type': 'Lab Test',
116 'reference_name': lab_test.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530117 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530118 })
119
Rucha Mahabalced978e2020-04-02 18:45:53 +0530120 lab_prescriptions = frappe.db.sql(
121 '''
122 SELECT
123 lp.name, lp.lab_test_code
124 FROM
125 `tabPatient Encounter` et, `tabLab Prescription` lp
126 WHERE
127 et.patient=%s
128 and lp.parent=et.name
129 and lp.lab_test_created=0
130 and lp.invoiced=0
131 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530132
133 for prescription in lab_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530134 item, is_billable = frappe.get_cached_value('Lab Test Template', prescription.lab_test_code, ['item', 'is_billable'])
135 if prescription.lab_test_code and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530136 lab_tests_to_invoice.append({
137 'reference_type': 'Lab Prescription',
138 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530139 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530140 })
141
142 return lab_tests_to_invoice
143
144
145def get_clinical_procedures_to_invoice(patient):
146 clinical_procedures_to_invoice = []
147 procedures = frappe.get_list(
148 'Clinical Procedure',
149 fields='*',
150 filters={'patient': patient.name, 'invoiced': False}
151 )
152 for procedure in procedures:
153 if not procedure.appointment:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530154 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', procedure.procedure_template, ['item', 'is_billable'])
155 if procedure.procedure_template and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530156 clinical_procedures_to_invoice.append({
157 'reference_type': 'Clinical Procedure',
158 'reference_name': procedure.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530159 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530160 })
161
162 # consumables
163 if procedure.invoice_separately_as_consumables and procedure.consume_stock \
164 and procedure.status == 'Completed' and not procedure.consumption_invoiced:
165
166 service_item = get_healthcare_service_item('clinical_procedure_consumable_item')
167 if not service_item:
168 msg = _('Please Configure Clinical Procedure Consumable Item in ')
169 msg += '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>'''
170 frappe.throw(msg, title=_('Missing Configuration'))
171
172 clinical_procedures_to_invoice.append({
173 'reference_type': 'Clinical Procedure',
174 'reference_name': procedure.name,
175 'service': service_item,
176 'rate': procedure.consumable_total_amount,
177 'description': procedure.consumption_details
178 })
179
Rucha Mahabalced978e2020-04-02 18:45:53 +0530180 procedure_prescriptions = frappe.db.sql(
181 '''
182 SELECT
183 pp.name, pp.procedure
184 FROM
185 `tabPatient Encounter` et, `tabProcedure Prescription` pp
186 WHERE
187 et.patient=%s
188 and pp.parent=et.name
189 and pp.procedure_created=0
190 and pp.invoiced=0
191 and pp.appointment_booked=0
192 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530193
194 for prescription in procedure_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530195 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', prescription.procedure, ['item', 'is_billable'])
196 if is_billable:
Rucha Mahabal197165f2020-03-26 17:29:50 +0530197 clinical_procedures_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +0530198 'reference_type': 'Procedure Prescription',
199 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530200 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530201 })
202
203 return clinical_procedures_to_invoice
204
205
206def get_inpatient_services_to_invoice(patient):
207 services_to_invoice = []
Rucha Mahabalced978e2020-04-02 18:45:53 +0530208 inpatient_services = frappe.db.sql(
209 '''
210 SELECT
211 io.*
212 FROM
213 `tabInpatient Record` ip, `tabInpatient Occupancy` io
214 WHERE
215 ip.patient=%s
216 and io.parent=ip.name
217 and io.left=1
218 and io.invoiced=0
219 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530220
221 for inpatient_occupancy in inpatient_services:
222 service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type')
Rucha Mahabalced978e2020-04-02 18:45:53 +0530223 service_unit_type = frappe.get_cached_doc('Healthcare Service Unit Type', service_unit_type)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530224 if service_unit_type and service_unit_type.is_billable:
225 hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in)
226 qty = 0.5
227 if hours_occupied > 0:
228 actual_qty = hours_occupied / service_unit_type.no_of_hours
229 floor = math.floor(actual_qty)
230 decimal_part = actual_qty - floor
231 if decimal_part > 0.5:
232 qty = rounded(floor + 1, 1)
233 elif decimal_part < 0.5 and decimal_part > 0:
234 qty = rounded(floor + 0.5, 1)
235 if qty <= 0:
236 qty = 0.5
237 services_to_invoice.append({
238 'reference_type': 'Inpatient Occupancy',
239 'reference_name': inpatient_occupancy.name,
240 'service': service_unit_type.item, 'qty': qty
241 })
242
243 return services_to_invoice
244
Jamsheerba119722018-07-06 15:58:13 +0530245
Rucha Mahabal24055e12020-02-24 19:09:50 +0530246def get_service_item_and_practitioner_charge(doc):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530247 is_inpatient = doc.inpatient_record
Rucha Mahabal24055e12020-02-24 19:09:50 +0530248 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530249 service_item = get_practitioner_service_item(doc.practitioner, 'inpatient_visit_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530250 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530251 service_item = get_healthcare_service_item('inpatient_visit_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530252 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530253 service_item = get_practitioner_service_item(doc.practitioner, 'op_consulting_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530254 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530255 service_item = get_healthcare_service_item('op_consulting_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530256 if not service_item:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530257 throw_config_service_item(is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530258
Rucha Mahabal24055e12020-02-24 19:09:50 +0530259 practitioner_charge = get_practitioner_charge(doc.practitioner, is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530260 if not practitioner_charge:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530261 throw_config_practitioner_charge(is_inpatient, doc.practitioner)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530262
263 return service_item, practitioner_charge
264
Jamsheer8da6f4e2018-07-26 21:03:17 +0530265
Rucha Mahabal27512c82020-03-09 17:29:23 +0530266def throw_config_service_item(is_inpatient):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530267 service_item_label = _('Out Patient Consulting Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530268 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530269 service_item_label = _('Inpatient Visit Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530270
Rucha Mahabal4f9a1472020-03-23 10:40:39 +0530271 msg = _(('Please Configure {0} in ').format(service_item_label) \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530272 + '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>''')
273 frappe.throw(msg, title=_('Missing Configuration'))
274
Jamsheer8da6f4e2018-07-26 21:03:17 +0530275
Rucha Mahabal24055e12020-02-24 19:09:50 +0530276def throw_config_practitioner_charge(is_inpatient, practitioner):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530277 charge_name = _('OP Consulting Charge')
Rucha Mahabal24055e12020-02-24 19:09:50 +0530278 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530279 charge_name = _('Inpatient Visit Charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530280
Rucha Mahabal27512c82020-03-09 17:29:23 +0530281 msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \
282 + ''' <b><a href='#Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner))
283 frappe.throw(msg, title=_('Missing Configuration'))
284
Jamsheer8da6f4e2018-07-26 21:03:17 +0530285
Jamsheeree5f9c72018-07-30 12:42:06 +0530286def get_practitioner_service_item(practitioner, service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530287 return frappe.db.get_value('Healthcare Practitioner', practitioner, service_item_field)
288
Jamsheeree5f9c72018-07-30 12:42:06 +0530289
Jamsheer8da6f4e2018-07-26 21:03:17 +0530290def get_healthcare_service_item(service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530291 return frappe.db.get_single_value('Healthcare Settings', service_item_field)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530292
Jamsheer8da6f4e2018-07-26 21:03:17 +0530293
Rucha Mahabal24055e12020-02-24 19:09:50 +0530294def get_practitioner_charge(practitioner, is_inpatient):
295 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530296 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'inpatient_visit_charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530297 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530298 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'op_consulting_charge')
Jamsheerba119722018-07-06 15:58:13 +0530299 if practitioner_charge:
300 return practitioner_charge
Jamsheer8da6f4e2018-07-26 21:03:17 +0530301 return False
Jamsheerba119722018-07-06 15:58:13 +0530302
Rucha Mahabal27512c82020-03-09 17:29:23 +0530303
Jamsheerba119722018-07-06 15:58:13 +0530304def manage_invoice_submit_cancel(doc, method):
305 if doc.items:
306 for item in doc.items:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530307 if item.get('reference_dt') and item.get('reference_dn'):
308 if frappe.get_meta(item.reference_dt).has_field('invoiced'):
Jamsheer146683b2018-07-25 11:30:30 +0530309 set_invoiced(item, method, doc.name)
Jamsheerba119722018-07-06 15:58:13 +0530310
Rucha Mahabal27512c82020-03-09 17:29:23 +0530311 if method=='on_submit' and frappe.db.get_single_value('Healthcare Settings', 'create_lab_test_on_si_submit'):
312 create_multiple('Sales Invoice', doc.name)
313
Jamsheer0ae100b2018-08-01 14:29:43 +0530314
Jamsheer146683b2018-07-25 11:30:30 +0530315def set_invoiced(item, method, ref_invoice=None):
Jamsheerba119722018-07-06 15:58:13 +0530316 invoiced = False
Rucha Mahabal27512c82020-03-09 17:29:23 +0530317 if method=='on_submit':
Jamsheerba119722018-07-06 15:58:13 +0530318 validate_invoiced_on_submit(item)
319 invoiced = True
320
Jamsheer8da6f4e2018-07-26 21:03:17 +0530321 if item.reference_dt == 'Clinical Procedure':
322 if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530323 frappe.db.set_value(item.reference_dt, item.reference_dn, 'consumption_invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530324 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530325 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530326 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530327 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530328
Jamsheerba119722018-07-06 15:58:13 +0530329 if item.reference_dt == 'Patient Appointment':
330 if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530331 dt_from_appointment = 'Clinical Procedure'
Jamsheerba119722018-07-06 15:58:13 +0530332 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530333 dt_from_appointment = 'Patient Encounter'
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530334 manage_doc_for_appointment(dt_from_appointment, item.reference_dn, invoiced)
Jamsheerba119722018-07-06 15:58:13 +0530335
336 elif item.reference_dt == 'Lab Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530337 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Lab Test', 'lab_test_created')
Jamsheerba119722018-07-06 15:58:13 +0530338
339 elif item.reference_dt == 'Procedure Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530340 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Clinical Procedure', 'procedure_created')
341
Jamsheerba119722018-07-06 15:58:13 +0530342
343def validate_invoiced_on_submit(item):
Jamsheer8da6f4e2018-07-26 21:03:17 +0530344 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 +0530345 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'consumption_invoiced')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530346 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530347 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'invoiced')
348 if is_invoiced:
349 frappe.throw(_('The item referenced by {0} - {1} is already invoiced'\
Jamsheerba119722018-07-06 15:58:13 +0530350 ).format(item.reference_dt, item.reference_dn))
351
Rucha Mahabal27512c82020-03-09 17:29:23 +0530352
Jamsheerba119722018-07-06 15:58:13 +0530353def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
354 created = frappe.db.get_value(ref_dt, ref_dn, created_check_field)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530355 if created:
Jamsheerba119722018-07-06 15:58:13 +0530356 # Fetch the doc created for the prescription
Jamsheereafb0462018-07-25 13:15:12 +0530357 doc_created = frappe.db.get_value(dt, {'prescription': ref_dn})
Jamsheerba119722018-07-06 15:58:13 +0530358 frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
359
Rucha Mahabal27512c82020-03-09 17:29:23 +0530360
Rucha Mahabalcd319962020-03-13 15:39:31 +0530361def check_fee_validity(appointment):
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530362 if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'):
363 return
364
Rucha Mahabalcd319962020-03-13 15:39:31 +0530365 validity = frappe.db.exists('Fee Validity', {
366 'practitioner': appointment.practitioner,
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530367 'patient': appointment.patient,
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530368 'valid_till': ('>=', appointment.appointment_date)
Rucha Mahabalcd319962020-03-13 15:39:31 +0530369 })
370 if not validity:
371 return
372
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530373 validity = frappe.get_doc('Fee Validity', validity)
374 return validity
375
Rucha Mahabal27512c82020-03-09 17:29:23 +0530376
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530377def manage_fee_validity(appointment):
378 fee_validity = check_fee_validity(appointment)
Rucha Mahabalcd319962020-03-13 15:39:31 +0530379 if fee_validity:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530380 if appointment.status == 'Cancelled' and fee_validity.visited > 0:
381 fee_validity.visited -= 1
382 frappe.db.delete('Fee Validity Reference', {'appointment': appointment.name})
Rucha Mahabal2cec6bd2020-03-26 14:38:12 +0530383 elif fee_validity.status == 'Completed':
384 return
Rucha Mahabalcd319962020-03-13 15:39:31 +0530385 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530386 fee_validity.visited += 1
387 fee_validity.append('ref_appointments', {
388 'appointment': appointment.name
389 })
390 fee_validity.save(ignore_permissions=True)
Jamsheerba119722018-07-06 15:58:13 +0530391 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530392 fee_validity = create_fee_validity(appointment)
393 return fee_validity
Rucha Mahabal27512c82020-03-09 17:29:23 +0530394
Jamsheerba119722018-07-06 15:58:13 +0530395
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530396def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced):
Rucha Mahabalc4b2dce2020-03-09 23:57:00 +0530397 dn_from_appointment = frappe.db.get_value(
398 dt_from_appointment,
Rucha Mahabal27512c82020-03-09 17:29:23 +0530399 filters={'appointment': appointment}
Jamsheerba119722018-07-06 15:58:13 +0530400 )
401 if dn_from_appointment:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530402 frappe.db.set_value(dt_from_appointment, dn_from_appointment, 'invoiced', invoiced)
403
Jamsheere82f27a2018-07-30 11:28:37 +0530404
405@frappe.whitelist()
406def get_drugs_to_invoice(encounter):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530407 encounter = frappe.get_doc('Patient Encounter', encounter)
Jamsheere82f27a2018-07-30 11:28:37 +0530408 if encounter:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530409 patient = frappe.get_doc('Patient', encounter.patient)
410 if patient:
411 if patient.customer:
412 items_to_invoice = []
Jamsheere82f27a2018-07-30 11:28:37 +0530413 for drug_line in encounter.drug_prescription:
414 if drug_line.drug_code:
415 qty = 1
Rucha Mahabal27512c82020-03-09 17:29:23 +0530416 if frappe.db.get_value('Item', drug_line.drug_code, 'stock_uom') == 'Nos':
Jamsheere82f27a2018-07-30 11:28:37 +0530417 qty = drug_line.get_quantity()
Rucha Mahabal27512c82020-03-09 17:29:23 +0530418
419 description = ''
420 if drug_line.dosage and drug_line.period:
421 description = _('{0} for {1}').format(drug_line.dosage, drug_line.period)
422
423 items_to_invoice.append({
424 'drug_code': drug_line.drug_code,
425 'quantity': qty,
426 'description': description
427 })
428 return items_to_invoice
429 else:
430 validate_customer_created(patient)
431
Jamsheer4371c7e2018-08-01 18:40:05 +0530432
433@frappe.whitelist()
434def get_children(doctype, parent, company, is_root=False):
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530435 parent_fieldname = "parent_" + doctype.lower().replace(" ", "_")
Jamsheer4371c7e2018-08-01 18:40:05 +0530436 fields = [
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530437 "name as value",
438 "is_group as expandable",
439 "lft",
440 "rgt"
Jamsheer4371c7e2018-08-01 18:40:05 +0530441 ]
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530442 # fields = [ "name", "is_group", "lft", "rgt" ]
443 filters = [["ifnull(`{0}`,'')".format(parent_fieldname), "=", "" if is_root else parent]]
Jamsheer4371c7e2018-08-01 18:40:05 +0530444
445 if is_root:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530446 fields += ["service_unit_type"] if doctype == "Healthcare Service Unit" else []
447 filters.append(["company", "=", company])
Jamsheer4371c7e2018-08-01 18:40:05 +0530448
449 else:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530450 fields += ["service_unit_type", "allow_appointments", "inpatient_occupancy", "occupancy_status"] if doctype == "Healthcare Service Unit" else []
451 fields += [parent_fieldname + " as parent"]
Jamsheer4371c7e2018-08-01 18:40:05 +0530452
453 hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters)
454
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530455 if doctype == "Healthcare Service Unit":
Jamsheer4371c7e2018-08-01 18:40:05 +0530456 for each in hc_service_units:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530457 occupancy_msg = ""
458 if each["expandable"] == 1:
Jamsheer4371c7e2018-08-01 18:40:05 +0530459 occupied = False
460 vacant = False
Rucha Mahabalced978e2020-04-02 18:45:53 +0530461 child_list = frappe.db.sql(
462 '''
463 SELECT
464 name, occupancy_status
465 FROM
466 `tabHealthcare Service Unit`
467 WHERE
468 inpatient_occupancy = 1
469 and lft > %s and rgt < %s
470 ''', (each['lft'], each['rgt']))
471
Jamsheer4371c7e2018-08-01 18:40:05 +0530472 for child in child_list:
Jamsheer4371c7e2018-08-01 18:40:05 +0530473 if not occupied:
474 occupied = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530475 if child[1] == "Occupied":
Jamsheer4371c7e2018-08-01 18:40:05 +0530476 occupied += 1
477 if not vacant:
478 vacant = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530479 if child[1] == "Vacant":
Jamsheer4371c7e2018-08-01 18:40:05 +0530480 vacant += 1
481 if vacant and occupied:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530482 occupancy_total = vacant + occupied
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530483 occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total)
484 each["occupied_out_of_vacant"] = occupancy_msg
Jamsheer4371c7e2018-08-01 18:40:05 +0530485 return hc_service_units
Jamsheer5073ac42019-07-12 12:28:34 +0530486
Rucha Mahabal27512c82020-03-09 17:29:23 +0530487
Jamsheer5073ac42019-07-12 12:28:34 +0530488@frappe.whitelist()
489def get_patient_vitals(patient, from_date=None, to_date=None):
490 if not patient: return
Rucha Mahabal27512c82020-03-09 17:29:23 +0530491
492 vitals = frappe.db.get_all('Vital Signs', {
493 'docstatus': 1,
494 'patient': patient
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530495 }, order_by='signs_date, signs_time')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530496
497 if len(vitals):
Jamsheer5073ac42019-07-12 12:28:34 +0530498 return vitals
Rucha Mahabal27512c82020-03-09 17:29:23 +0530499 return False
500
Jamsheer5073ac42019-07-12 12:28:34 +0530501
502@frappe.whitelist()
503def render_docs_as_html(docs):
504 # docs key value pair {doctype: docname}
505 docs_html = "<div class='col-md-12 col-sm-12 text-muted'>"
506 for doc in docs:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530507 docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + '<br/>'
Jamsheer5073ac42019-07-12 12:28:34 +0530508 return {'html': docs_html}
509
Rucha Mahabal27512c82020-03-09 17:29:23 +0530510
Jamsheer5073ac42019-07-12 12:28:34 +0530511@frappe.whitelist()
512def render_doc_as_html(doctype, docname, exclude_fields = []):
513 #render document as html, three column layout will break
514 doc = frappe.get_doc(doctype, docname)
515 meta = frappe.get_meta(doctype)
516 doc_html = "<div class='col-md-12 col-sm-12'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530517 section_html = ''
518 section_label = ''
519 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530520 sec_on = False
521 col_on = 0
522 has_data = False
523 for df in meta.fields:
524 #on section break append append previous section and html to doc html
525 if df.fieldtype == "Section Break":
526 if has_data and col_on and sec_on:
527 doc_html += section_html + html + "</div>"
528 elif has_data and not col_on and sec_on:
529 doc_html += "<div class='col-md-12 col-sm-12'\
530 ><div class='col-md-12 col-sm-12'>" \
531 + section_html + html +"</div></div>"
532 while col_on:
533 doc_html += "</div>"
534 col_on -= 1
535 sec_on = True
536 has_data= False
537 col_on = 0
Rucha Mahabal27512c82020-03-09 17:29:23 +0530538 section_html = ''
539 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530540 if df.label:
541 section_label = df.label
542 continue
543 #on column break append html to section html or doc html
544 if df.fieldtype == "Column Break":
545 if sec_on and has_data:
546 section_html += "<div class='col-md-12 col-sm-12'\
547 ><div class='col-md-6 col\
548 -sm-6'><b>" + section_label + "</b>" + html + "</div><div \
549 class='col-md-6 col-sm-6'>"
550 elif has_data:
551 doc_html += "<div class='col-md-12 col-sm-12'><div class='col-m\
552 d-6 col-sm-6'>" + html + "</div><div class='col-md-6 col-sm-6'>"
553 elif sec_on and not col_on:
554 section_html += "<div class='col-md-6 col-sm-6'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530555 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530556 col_on += 1
557 if df.label:
558 html += '<br>' + df.label
559 continue
560 #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 +0530561 if df.fieldtype == 'Table':
Jamsheer5073ac42019-07-12 12:28:34 +0530562 items = doc.get(df.fieldname)
563 if not items: continue
564 child_meta = frappe.get_meta(df.options)
565 if not has_data : has_data = True
Rucha Mahabal27512c82020-03-09 17:29:23 +0530566 table_head = ''
567 table_row = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530568 create_head = True
569 for item in items:
570 table_row += '<tr>'
571 for cdf in child_meta.fields:
572 if cdf.in_list_view:
573 if create_head:
574 table_head += '<th>' + cdf.label + '</th>'
575 if item.get(cdf.fieldname):
576 table_row += '<td>' + str(item.get(cdf.fieldname)) \
577 + '</td>'
578 else:
579 table_row += '<td></td>'
580 create_head = False
581 table_row += '</tr>'
582 if sec_on:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530583 section_html += "<table class='table table-condensed \
584 bordered'>" + table_head + table_row + '</table>'
Jamsheer5073ac42019-07-12 12:28:34 +0530585 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530586 html += "<table class='table table-condensed table-bordered'>" \
587 + table_head + table_row + "</table>"
Jamsheer5073ac42019-07-12 12:28:34 +0530588 continue
589 #on other field types add label and value to html
590 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 +0530591 html += '<br>{0} : {1}'.format(df.label or df.fieldname, \
Jamsheer5073ac42019-07-12 12:28:34 +0530592 doc.get(df.fieldname))
593 if not has_data : has_data = True
594 if sec_on and col_on and has_data:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530595 doc_html += section_html + html + '</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530596 elif sec_on and not col_on and has_data:
597 doc_html += "<div class='col-md-12 col-sm-12'\
598 ><div class='col-md-12 col-sm-12'>" \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530599 + section_html + html +'</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530600 if doc_html:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530601 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 +0530602
603 return {'html': doc_html}