blob: 17d8f642928aff70c4f9edbeb26e4c0dc62174a0 [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
anoop93d0c782020-04-13 16:42:03 +05306import math
Jamsheerba119722018-07-06 15:58:13 +05307import frappe
Jamsheerba119722018-07-06 15:58:13 +05308from frappe import _
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()
anoop93d0c782020-04-13 16:42:03 +053015def get_healthcare_services_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +053016 patient = frappe.get_doc('Patient', patient)
anoop93d0c782020-04-13 16:42:03 +053017 items_to_invoice = []
Jamsheerba119722018-07-06 15:58:13 +053018 if patient:
Rucha Mahabal27512c82020-03-09 17:29:23 +053019 validate_customer_created(patient)
anoop93d0c782020-04-13 16:42:03 +053020 # Customer validated, build a list of billable services
21 items_to_invoice += get_appointments_to_invoice(patient, company)
22 items_to_invoice += get_encounters_to_invoice(patient, company)
23 items_to_invoice += get_lab_tests_to_invoice(patient, company)
24 items_to_invoice += get_clinical_procedures_to_invoice(patient, company)
25 items_to_invoice += get_inpatient_services_to_invoice(patient, company)
Jamsheerba119722018-07-06 15:58:13 +053026
Rucha Mahabal27512c82020-03-09 17:29:23 +053027 return items_to_invoice
Jamsheerba119722018-07-06 15:58:13 +053028
anoop93d0c782020-04-13 16:42:03 +053029
Rucha Mahabal27512c82020-03-09 17:29:23 +053030def validate_customer_created(patient):
31 if not frappe.db.get_value('Patient', patient.name, 'customer'):
32 msg = _("Please set a Customer linked to the Patient")
33 msg += " <b><a href='#Form/Patient/{0}'>{0}</a></b>".format(patient.name)
34 frappe.throw(msg, title=_('Customer Not Found'))
Jamsheer8da6f4e2018-07-26 21:03:17 +053035
Rucha Mahabalf2574dd2020-03-17 19:28:18 +053036
anoop93d0c782020-04-13 16:42:03 +053037def get_appointments_to_invoice(patient, company):
38 appointments_to_invoice = []
39 patient_appointments = frappe.get_list(
40 'Patient Appointment',
41 fields = '*',
42 filters = {'patient': patient.name, 'company': company, 'invoiced': 0},
43 order_by = 'appointment_date'
44 )
45
Rucha Mahabal27512c82020-03-09 17:29:23 +053046 for appointment in patient_appointments:
anoop93d0c782020-04-13 16:42:03 +053047 # Procedure Appointments
Rucha Mahabal27512c82020-03-09 17:29:23 +053048 if appointment.procedure_template:
49 if frappe.db.get_value('Clinical Procedure Template', appointment.procedure_template, 'is_billable'):
anoop93d0c782020-04-13 16:42:03 +053050 appointments_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +053051 'reference_type': 'Patient Appointment',
52 'reference_name': appointment.name,
53 'service': appointment.procedure_template
54 })
anoop93d0c782020-04-13 16:42:03 +053055 # Consultation Appointments, should check fee validity
Jamsheerba119722018-07-06 15:58:13 +053056 else:
anoop93d0c782020-04-13 16:42:03 +053057 if frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups') and \
58 frappe.db.exists('Fee Validity Reference', {'appointment': appointment.name}):
59 continue # Skip invoicing, fee validty present
60 practitioner_charge = 0
61 income_account = None
62 service_item = None
63 if appointment.practitioner:
64 service_item, practitioner_charge = get_service_item_and_practitioner_charge(appointment)
65 income_account = get_income_account(appointment.practitioner, appointment.company)
66 appointments_to_invoice.append({
67 'reference_type': 'Patient Appointment',
68 'reference_name': appointment.name,
69 'service': service_item,
70 'rate': practitioner_charge,
71 'income_account': income_account
72 })
Rucha Mahabal27512c82020-03-09 17:29:23 +053073
anoop93d0c782020-04-13 16:42:03 +053074 return appointments_to_invoice
Rucha Mahabal27512c82020-03-09 17:29:23 +053075
76
anoop93d0c782020-04-13 16:42:03 +053077def get_encounters_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +053078 encounters_to_invoice = []
79 encounters = frappe.get_list(
80 'Patient Encounter',
81 fields=['*'],
anoop93d0c782020-04-13 16:42:03 +053082 filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1}
Rucha Mahabal27512c82020-03-09 17:29:23 +053083 )
84 if encounters:
85 for encounter in encounters:
86 if not encounter.appointment:
87 practitioner_charge = 0
88 income_account = None
89 service_item = None
90 if encounter.practitioner:
91 service_item, practitioner_charge = get_service_item_and_practitioner_charge(encounter)
92 income_account = get_income_account(encounter.practitioner, encounter.company)
93
94 encounters_to_invoice.append({
95 'reference_type': 'Patient Encounter',
96 'reference_name': encounter.name,
97 'service': service_item,
98 'rate': practitioner_charge,
99 'income_account': income_account
100 })
101
102 return encounters_to_invoice
103
104
anoop93d0c782020-04-13 16:42:03 +0530105def get_lab_tests_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530106 lab_tests_to_invoice = []
107 lab_tests = frappe.get_list(
108 'Lab Test',
109 fields=['name', 'template'],
anoop93d0c782020-04-13 16:42:03 +0530110 filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1}
Rucha Mahabal27512c82020-03-09 17:29:23 +0530111 )
112 for lab_test in lab_tests:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530113 item, is_billable = frappe.get_cached_value('Lab Test Template', lab_test.lab_test_code, ['item', 'is_billable'])
114 if is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530115 lab_tests_to_invoice.append({
116 'reference_type': 'Lab Test',
117 'reference_name': lab_test.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530118 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530119 })
120
Rucha Mahabalced978e2020-04-02 18:45:53 +0530121 lab_prescriptions = frappe.db.sql(
122 '''
123 SELECT
124 lp.name, lp.lab_test_code
125 FROM
126 `tabPatient Encounter` et, `tabLab Prescription` lp
127 WHERE
128 et.patient=%s
129 and lp.parent=et.name
130 and lp.lab_test_created=0
131 and lp.invoiced=0
132 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530133
134 for prescription in lab_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530135 item, is_billable = frappe.get_cached_value('Lab Test Template', prescription.lab_test_code, ['item', 'is_billable'])
136 if prescription.lab_test_code and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530137 lab_tests_to_invoice.append({
138 'reference_type': 'Lab Prescription',
139 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530140 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530141 })
142
143 return lab_tests_to_invoice
144
145
anoop93d0c782020-04-13 16:42:03 +0530146def get_clinical_procedures_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530147 clinical_procedures_to_invoice = []
148 procedures = frappe.get_list(
149 'Clinical Procedure',
150 fields='*',
anoop93d0c782020-04-13 16:42:03 +0530151 filters={'patient': patient.name, 'company': company, 'invoiced': False}
Rucha Mahabal27512c82020-03-09 17:29:23 +0530152 )
153 for procedure in procedures:
154 if not procedure.appointment:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530155 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', procedure.procedure_template, ['item', 'is_billable'])
156 if procedure.procedure_template and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530157 clinical_procedures_to_invoice.append({
158 'reference_type': 'Clinical Procedure',
159 'reference_name': procedure.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530160 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530161 })
162
163 # consumables
164 if procedure.invoice_separately_as_consumables and procedure.consume_stock \
165 and procedure.status == 'Completed' and not procedure.consumption_invoiced:
166
167 service_item = get_healthcare_service_item('clinical_procedure_consumable_item')
168 if not service_item:
169 msg = _('Please Configure Clinical Procedure Consumable Item in ')
170 msg += '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>'''
171 frappe.throw(msg, title=_('Missing Configuration'))
172
173 clinical_procedures_to_invoice.append({
174 'reference_type': 'Clinical Procedure',
175 'reference_name': procedure.name,
176 'service': service_item,
177 'rate': procedure.consumable_total_amount,
178 'description': procedure.consumption_details
179 })
180
Rucha Mahabalced978e2020-04-02 18:45:53 +0530181 procedure_prescriptions = frappe.db.sql(
182 '''
183 SELECT
184 pp.name, pp.procedure
185 FROM
186 `tabPatient Encounter` et, `tabProcedure Prescription` pp
187 WHERE
188 et.patient=%s
189 and pp.parent=et.name
190 and pp.procedure_created=0
191 and pp.invoiced=0
192 and pp.appointment_booked=0
193 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530194
195 for prescription in procedure_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530196 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', prescription.procedure, ['item', 'is_billable'])
197 if is_billable:
Rucha Mahabal197165f2020-03-26 17:29:50 +0530198 clinical_procedures_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +0530199 'reference_type': 'Procedure Prescription',
200 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530201 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530202 })
203
204 return clinical_procedures_to_invoice
205
206
anoop93d0c782020-04-13 16:42:03 +0530207def get_inpatient_services_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530208 services_to_invoice = []
Rucha Mahabalced978e2020-04-02 18:45:53 +0530209 inpatient_services = frappe.db.sql(
210 '''
211 SELECT
212 io.*
213 FROM
214 `tabInpatient Record` ip, `tabInpatient Occupancy` io
215 WHERE
216 ip.patient=%s
anoop93d0c782020-04-13 16:42:03 +0530217 and ip.company=%s
Rucha Mahabalced978e2020-04-02 18:45:53 +0530218 and io.parent=ip.name
219 and io.left=1
220 and io.invoiced=0
anoop93d0c782020-04-13 16:42:03 +0530221 ''', (patient.name, company), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530222
223 for inpatient_occupancy in inpatient_services:
224 service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type')
Rucha Mahabalced978e2020-04-02 18:45:53 +0530225 service_unit_type = frappe.get_cached_doc('Healthcare Service Unit Type', service_unit_type)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530226 if service_unit_type and service_unit_type.is_billable:
227 hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in)
228 qty = 0.5
229 if hours_occupied > 0:
230 actual_qty = hours_occupied / service_unit_type.no_of_hours
231 floor = math.floor(actual_qty)
232 decimal_part = actual_qty - floor
233 if decimal_part > 0.5:
234 qty = rounded(floor + 1, 1)
235 elif decimal_part < 0.5 and decimal_part > 0:
236 qty = rounded(floor + 0.5, 1)
237 if qty <= 0:
238 qty = 0.5
239 services_to_invoice.append({
240 'reference_type': 'Inpatient Occupancy',
241 'reference_name': inpatient_occupancy.name,
242 'service': service_unit_type.item, 'qty': qty
243 })
244
245 return services_to_invoice
246
Jamsheerba119722018-07-06 15:58:13 +0530247
Rucha Mahabal24055e12020-02-24 19:09:50 +0530248def get_service_item_and_practitioner_charge(doc):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530249 is_inpatient = doc.inpatient_record
Rucha Mahabal24055e12020-02-24 19:09:50 +0530250 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530251 service_item = get_practitioner_service_item(doc.practitioner, 'inpatient_visit_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530252 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530253 service_item = get_healthcare_service_item('inpatient_visit_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530254 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530255 service_item = get_practitioner_service_item(doc.practitioner, 'op_consulting_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530256 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530257 service_item = get_healthcare_service_item('op_consulting_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530258 if not service_item:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530259 throw_config_service_item(is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530260
Rucha Mahabal24055e12020-02-24 19:09:50 +0530261 practitioner_charge = get_practitioner_charge(doc.practitioner, is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530262 if not practitioner_charge:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530263 throw_config_practitioner_charge(is_inpatient, doc.practitioner)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530264
265 return service_item, practitioner_charge
266
Jamsheer8da6f4e2018-07-26 21:03:17 +0530267
Rucha Mahabal27512c82020-03-09 17:29:23 +0530268def throw_config_service_item(is_inpatient):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530269 service_item_label = _('Out Patient Consulting Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530270 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530271 service_item_label = _('Inpatient Visit Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530272
Rucha Mahabal4f9a1472020-03-23 10:40:39 +0530273 msg = _(('Please Configure {0} in ').format(service_item_label) \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530274 + '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>''')
275 frappe.throw(msg, title=_('Missing Configuration'))
276
Jamsheer8da6f4e2018-07-26 21:03:17 +0530277
Rucha Mahabal24055e12020-02-24 19:09:50 +0530278def throw_config_practitioner_charge(is_inpatient, practitioner):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530279 charge_name = _('OP Consulting Charge')
Rucha Mahabal24055e12020-02-24 19:09:50 +0530280 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530281 charge_name = _('Inpatient Visit Charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530282
Rucha Mahabal27512c82020-03-09 17:29:23 +0530283 msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \
284 + ''' <b><a href='#Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner))
285 frappe.throw(msg, title=_('Missing Configuration'))
286
Jamsheer8da6f4e2018-07-26 21:03:17 +0530287
Jamsheeree5f9c72018-07-30 12:42:06 +0530288def get_practitioner_service_item(practitioner, service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530289 return frappe.db.get_value('Healthcare Practitioner', practitioner, service_item_field)
290
Jamsheeree5f9c72018-07-30 12:42:06 +0530291
Jamsheer8da6f4e2018-07-26 21:03:17 +0530292def get_healthcare_service_item(service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530293 return frappe.db.get_single_value('Healthcare Settings', service_item_field)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530294
Jamsheer8da6f4e2018-07-26 21:03:17 +0530295
Rucha Mahabal24055e12020-02-24 19:09:50 +0530296def get_practitioner_charge(practitioner, is_inpatient):
297 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530298 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'inpatient_visit_charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530299 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530300 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'op_consulting_charge')
Jamsheerba119722018-07-06 15:58:13 +0530301 if practitioner_charge:
302 return practitioner_charge
Jamsheer8da6f4e2018-07-26 21:03:17 +0530303 return False
Jamsheerba119722018-07-06 15:58:13 +0530304
Rucha Mahabal27512c82020-03-09 17:29:23 +0530305
Jamsheerba119722018-07-06 15:58:13 +0530306def manage_invoice_submit_cancel(doc, method):
307 if doc.items:
308 for item in doc.items:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530309 if item.get('reference_dt') and item.get('reference_dn'):
310 if frappe.get_meta(item.reference_dt).has_field('invoiced'):
Jamsheer146683b2018-07-25 11:30:30 +0530311 set_invoiced(item, method, doc.name)
Jamsheerba119722018-07-06 15:58:13 +0530312
Rucha Mahabal27512c82020-03-09 17:29:23 +0530313 if method=='on_submit' and frappe.db.get_single_value('Healthcare Settings', 'create_lab_test_on_si_submit'):
314 create_multiple('Sales Invoice', doc.name)
315
Jamsheer0ae100b2018-08-01 14:29:43 +0530316
Jamsheer146683b2018-07-25 11:30:30 +0530317def set_invoiced(item, method, ref_invoice=None):
Jamsheerba119722018-07-06 15:58:13 +0530318 invoiced = False
Rucha Mahabal27512c82020-03-09 17:29:23 +0530319 if method=='on_submit':
Jamsheerba119722018-07-06 15:58:13 +0530320 validate_invoiced_on_submit(item)
321 invoiced = True
322
Jamsheer8da6f4e2018-07-26 21:03:17 +0530323 if item.reference_dt == 'Clinical Procedure':
324 if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530325 frappe.db.set_value(item.reference_dt, item.reference_dn, 'consumption_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 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530329 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530330
Jamsheerba119722018-07-06 15:58:13 +0530331 if item.reference_dt == 'Patient Appointment':
332 if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530333 dt_from_appointment = 'Clinical Procedure'
Jamsheerba119722018-07-06 15:58:13 +0530334 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530335 dt_from_appointment = 'Patient Encounter'
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530336 manage_doc_for_appointment(dt_from_appointment, item.reference_dn, invoiced)
Jamsheerba119722018-07-06 15:58:13 +0530337
338 elif item.reference_dt == 'Lab Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530339 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Lab Test', 'lab_test_created')
Jamsheerba119722018-07-06 15:58:13 +0530340
341 elif item.reference_dt == 'Procedure Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530342 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Clinical Procedure', 'procedure_created')
343
Jamsheerba119722018-07-06 15:58:13 +0530344
345def validate_invoiced_on_submit(item):
Jamsheer8da6f4e2018-07-26 21:03:17 +0530346 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 +0530347 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'consumption_invoiced')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530348 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530349 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'invoiced')
350 if is_invoiced:
351 frappe.throw(_('The item referenced by {0} - {1} is already invoiced'\
Jamsheerba119722018-07-06 15:58:13 +0530352 ).format(item.reference_dt, item.reference_dn))
353
Rucha Mahabal27512c82020-03-09 17:29:23 +0530354
Jamsheerba119722018-07-06 15:58:13 +0530355def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
356 created = frappe.db.get_value(ref_dt, ref_dn, created_check_field)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530357 if created:
Jamsheerba119722018-07-06 15:58:13 +0530358 # Fetch the doc created for the prescription
Jamsheereafb0462018-07-25 13:15:12 +0530359 doc_created = frappe.db.get_value(dt, {'prescription': ref_dn})
Jamsheerba119722018-07-06 15:58:13 +0530360 frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
361
Rucha Mahabal27512c82020-03-09 17:29:23 +0530362
Rucha Mahabalcd319962020-03-13 15:39:31 +0530363def check_fee_validity(appointment):
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530364 if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'):
365 return
366
Rucha Mahabalcd319962020-03-13 15:39:31 +0530367 validity = frappe.db.exists('Fee Validity', {
368 'practitioner': appointment.practitioner,
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530369 'patient': appointment.patient,
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530370 'valid_till': ('>=', appointment.appointment_date)
Rucha Mahabalcd319962020-03-13 15:39:31 +0530371 })
372 if not validity:
373 return
374
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530375 validity = frappe.get_doc('Fee Validity', validity)
376 return validity
377
Rucha Mahabal27512c82020-03-09 17:29:23 +0530378
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530379def manage_fee_validity(appointment):
380 fee_validity = check_fee_validity(appointment)
anoop93d0c782020-04-13 16:42:03 +0530381
Rucha Mahabalcd319962020-03-13 15:39:31 +0530382 if fee_validity:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530383 if appointment.status == 'Cancelled' and fee_validity.visited > 0:
384 fee_validity.visited -= 1
385 frappe.db.delete('Fee Validity Reference', {'appointment': appointment.name})
Rucha Mahabal2cec6bd2020-03-26 14:38:12 +0530386 elif fee_validity.status == 'Completed':
387 return
Rucha Mahabalcd319962020-03-13 15:39:31 +0530388 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530389 fee_validity.visited += 1
390 fee_validity.append('ref_appointments', {
391 'appointment': appointment.name
392 })
393 fee_validity.save(ignore_permissions=True)
Jamsheerba119722018-07-06 15:58:13 +0530394 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530395 fee_validity = create_fee_validity(appointment)
396 return fee_validity
Rucha Mahabal27512c82020-03-09 17:29:23 +0530397
Jamsheerba119722018-07-06 15:58:13 +0530398
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530399def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced):
Rucha Mahabalc4b2dce2020-03-09 23:57:00 +0530400 dn_from_appointment = frappe.db.get_value(
401 dt_from_appointment,
Rucha Mahabal27512c82020-03-09 17:29:23 +0530402 filters={'appointment': appointment}
Jamsheerba119722018-07-06 15:58:13 +0530403 )
404 if dn_from_appointment:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530405 frappe.db.set_value(dt_from_appointment, dn_from_appointment, 'invoiced', invoiced)
406
Jamsheere82f27a2018-07-30 11:28:37 +0530407
408@frappe.whitelist()
409def get_drugs_to_invoice(encounter):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530410 encounter = frappe.get_doc('Patient Encounter', encounter)
Jamsheere82f27a2018-07-30 11:28:37 +0530411 if encounter:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530412 patient = frappe.get_doc('Patient', encounter.patient)
413 if patient:
414 if patient.customer:
415 items_to_invoice = []
Jamsheere82f27a2018-07-30 11:28:37 +0530416 for drug_line in encounter.drug_prescription:
417 if drug_line.drug_code:
418 qty = 1
Rucha Mahabal27512c82020-03-09 17:29:23 +0530419 if frappe.db.get_value('Item', drug_line.drug_code, 'stock_uom') == 'Nos':
Jamsheere82f27a2018-07-30 11:28:37 +0530420 qty = drug_line.get_quantity()
Rucha Mahabal27512c82020-03-09 17:29:23 +0530421
422 description = ''
423 if drug_line.dosage and drug_line.period:
424 description = _('{0} for {1}').format(drug_line.dosage, drug_line.period)
425
426 items_to_invoice.append({
427 'drug_code': drug_line.drug_code,
428 'quantity': qty,
429 'description': description
430 })
431 return items_to_invoice
432 else:
433 validate_customer_created(patient)
434
Jamsheer4371c7e2018-08-01 18:40:05 +0530435
436@frappe.whitelist()
437def get_children(doctype, parent, company, is_root=False):
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530438 parent_fieldname = "parent_" + doctype.lower().replace(" ", "_")
Jamsheer4371c7e2018-08-01 18:40:05 +0530439 fields = [
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530440 "name as value",
441 "is_group as expandable",
442 "lft",
443 "rgt"
Jamsheer4371c7e2018-08-01 18:40:05 +0530444 ]
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530445 # fields = [ "name", "is_group", "lft", "rgt" ]
446 filters = [["ifnull(`{0}`,'')".format(parent_fieldname), "=", "" if is_root else parent]]
Jamsheer4371c7e2018-08-01 18:40:05 +0530447
448 if is_root:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530449 fields += ["service_unit_type"] if doctype == "Healthcare Service Unit" else []
450 filters.append(["company", "=", company])
Jamsheer4371c7e2018-08-01 18:40:05 +0530451
452 else:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530453 fields += ["service_unit_type", "allow_appointments", "inpatient_occupancy", "occupancy_status"] if doctype == "Healthcare Service Unit" else []
454 fields += [parent_fieldname + " as parent"]
Jamsheer4371c7e2018-08-01 18:40:05 +0530455
456 hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters)
457
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530458 if doctype == "Healthcare Service Unit":
Jamsheer4371c7e2018-08-01 18:40:05 +0530459 for each in hc_service_units:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530460 occupancy_msg = ""
461 if each["expandable"] == 1:
Jamsheer4371c7e2018-08-01 18:40:05 +0530462 occupied = False
463 vacant = False
Rucha Mahabalced978e2020-04-02 18:45:53 +0530464 child_list = frappe.db.sql(
465 '''
466 SELECT
467 name, occupancy_status
468 FROM
469 `tabHealthcare Service Unit`
470 WHERE
471 inpatient_occupancy = 1
472 and lft > %s and rgt < %s
473 ''', (each['lft'], each['rgt']))
474
Jamsheer4371c7e2018-08-01 18:40:05 +0530475 for child in child_list:
Jamsheer4371c7e2018-08-01 18:40:05 +0530476 if not occupied:
477 occupied = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530478 if child[1] == "Occupied":
Jamsheer4371c7e2018-08-01 18:40:05 +0530479 occupied += 1
480 if not vacant:
481 vacant = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530482 if child[1] == "Vacant":
Jamsheer4371c7e2018-08-01 18:40:05 +0530483 vacant += 1
484 if vacant and occupied:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530485 occupancy_total = vacant + occupied
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530486 occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total)
487 each["occupied_out_of_vacant"] = occupancy_msg
Jamsheer4371c7e2018-08-01 18:40:05 +0530488 return hc_service_units
Jamsheer5073ac42019-07-12 12:28:34 +0530489
Rucha Mahabal27512c82020-03-09 17:29:23 +0530490
Jamsheer5073ac42019-07-12 12:28:34 +0530491@frappe.whitelist()
492def get_patient_vitals(patient, from_date=None, to_date=None):
493 if not patient: return
Rucha Mahabal27512c82020-03-09 17:29:23 +0530494
495 vitals = frappe.db.get_all('Vital Signs', {
496 'docstatus': 1,
497 'patient': patient
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530498 }, order_by='signs_date, signs_time')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530499
500 if len(vitals):
Jamsheer5073ac42019-07-12 12:28:34 +0530501 return vitals
Rucha Mahabal27512c82020-03-09 17:29:23 +0530502 return False
503
Jamsheer5073ac42019-07-12 12:28:34 +0530504
505@frappe.whitelist()
506def render_docs_as_html(docs):
507 # docs key value pair {doctype: docname}
508 docs_html = "<div class='col-md-12 col-sm-12 text-muted'>"
509 for doc in docs:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530510 docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + '<br/>'
Jamsheer5073ac42019-07-12 12:28:34 +0530511 return {'html': docs_html}
512
Rucha Mahabal27512c82020-03-09 17:29:23 +0530513
Jamsheer5073ac42019-07-12 12:28:34 +0530514@frappe.whitelist()
515def render_doc_as_html(doctype, docname, exclude_fields = []):
516 #render document as html, three column layout will break
517 doc = frappe.get_doc(doctype, docname)
518 meta = frappe.get_meta(doctype)
519 doc_html = "<div class='col-md-12 col-sm-12'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530520 section_html = ''
521 section_label = ''
522 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530523 sec_on = False
524 col_on = 0
525 has_data = False
526 for df in meta.fields:
527 #on section break append append previous section and html to doc html
528 if df.fieldtype == "Section Break":
529 if has_data and col_on and sec_on:
530 doc_html += section_html + html + "</div>"
531 elif has_data and not col_on and sec_on:
532 doc_html += "<div class='col-md-12 col-sm-12'\
533 ><div class='col-md-12 col-sm-12'>" \
534 + section_html + html +"</div></div>"
535 while col_on:
536 doc_html += "</div>"
537 col_on -= 1
538 sec_on = True
539 has_data= False
540 col_on = 0
Rucha Mahabal27512c82020-03-09 17:29:23 +0530541 section_html = ''
542 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530543 if df.label:
544 section_label = df.label
545 continue
546 #on column break append html to section html or doc html
547 if df.fieldtype == "Column Break":
548 if sec_on and has_data:
549 section_html += "<div class='col-md-12 col-sm-12'\
550 ><div class='col-md-6 col\
551 -sm-6'><b>" + section_label + "</b>" + html + "</div><div \
552 class='col-md-6 col-sm-6'>"
553 elif has_data:
554 doc_html += "<div class='col-md-12 col-sm-12'><div class='col-m\
555 d-6 col-sm-6'>" + html + "</div><div class='col-md-6 col-sm-6'>"
556 elif sec_on and not col_on:
557 section_html += "<div class='col-md-6 col-sm-6'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530558 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530559 col_on += 1
560 if df.label:
561 html += '<br>' + df.label
562 continue
563 #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 +0530564 if df.fieldtype == 'Table':
Jamsheer5073ac42019-07-12 12:28:34 +0530565 items = doc.get(df.fieldname)
566 if not items: continue
567 child_meta = frappe.get_meta(df.options)
568 if not has_data : has_data = True
Rucha Mahabal27512c82020-03-09 17:29:23 +0530569 table_head = ''
570 table_row = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530571 create_head = True
572 for item in items:
573 table_row += '<tr>'
574 for cdf in child_meta.fields:
575 if cdf.in_list_view:
576 if create_head:
577 table_head += '<th>' + cdf.label + '</th>'
578 if item.get(cdf.fieldname):
579 table_row += '<td>' + str(item.get(cdf.fieldname)) \
580 + '</td>'
581 else:
582 table_row += '<td></td>'
583 create_head = False
584 table_row += '</tr>'
585 if sec_on:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530586 section_html += "<table class='table table-condensed \
587 bordered'>" + table_head + table_row + '</table>'
Jamsheer5073ac42019-07-12 12:28:34 +0530588 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530589 html += "<table class='table table-condensed table-bordered'>" \
590 + table_head + table_row + "</table>"
Jamsheer5073ac42019-07-12 12:28:34 +0530591 continue
592 #on other field types add label and value to html
593 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 +0530594 html += '<br>{0} : {1}'.format(df.label or df.fieldname, \
Jamsheer5073ac42019-07-12 12:28:34 +0530595 doc.get(df.fieldname))
596 if not has_data : has_data = True
597 if sec_on and col_on and has_data:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530598 doc_html += section_html + html + '</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530599 elif sec_on and not col_on and has_data:
600 doc_html += "<div class='col-md-12 col-sm-12'\
601 ><div class='col-md-12 col-sm-12'>" \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530602 + section_html + html +'</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530603 if doc_html:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530604 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 +0530605
606 return {'html': doc_html}