blob: e9cc597e813e9bbed6d84fea495a20c3b216f27e [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
7import json
Jamsheerba119722018-07-06 15:58:13 +05308import frappe
Jamsheerba119722018-07-06 15:58:13 +05309from frappe import _
Rucha Mahabal197165f2020-03-26 17:29:50 +053010from frappe.utils import time_diff_in_hours, rounded
Jamsheerba119722018-07-06 15:58:13 +053011from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_income_account
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +053012from erpnext.healthcare.doctype.fee_validity.fee_validity import create_fee_validity
Jamsheer0ae100b2018-08-01 14:29:43 +053013from erpnext.healthcare.doctype.lab_test.lab_test import create_multiple
Jamsheerba119722018-07-06 15:58:13 +053014
15@frappe.whitelist()
anoop93d0c782020-04-13 16:42:03 +053016def get_healthcare_services_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +053017 patient = frappe.get_doc('Patient', patient)
anoop93d0c782020-04-13 16:42:03 +053018 items_to_invoice = []
Jamsheerba119722018-07-06 15:58:13 +053019 if patient:
Rucha Mahabal27512c82020-03-09 17:29:23 +053020 validate_customer_created(patient)
anoop93d0c782020-04-13 16:42:03 +053021 # Customer validated, build a list of billable services
22 items_to_invoice += get_appointments_to_invoice(patient, company)
23 items_to_invoice += get_encounters_to_invoice(patient, company)
24 items_to_invoice += get_lab_tests_to_invoice(patient, company)
25 items_to_invoice += get_clinical_procedures_to_invoice(patient, company)
26 items_to_invoice += get_inpatient_services_to_invoice(patient, company)
Jamsheerba119722018-07-06 15:58:13 +053027
Rucha Mahabal27512c82020-03-09 17:29:23 +053028 return items_to_invoice
Jamsheerba119722018-07-06 15:58:13 +053029
anoop93d0c782020-04-13 16:42:03 +053030
Rucha Mahabal27512c82020-03-09 17:29:23 +053031def validate_customer_created(patient):
32 if not frappe.db.get_value('Patient', patient.name, 'customer'):
33 msg = _("Please set a Customer linked to the Patient")
34 msg += " <b><a href='#Form/Patient/{0}'>{0}</a></b>".format(patient.name)
35 frappe.throw(msg, title=_('Customer Not Found'))
Jamsheer8da6f4e2018-07-26 21:03:17 +053036
Rucha Mahabalf2574dd2020-03-17 19:28:18 +053037
anoop93d0c782020-04-13 16:42:03 +053038def get_appointments_to_invoice(patient, company):
39 appointments_to_invoice = []
40 patient_appointments = frappe.get_list(
41 'Patient Appointment',
42 fields = '*',
43 filters = {'patient': patient.name, 'company': company, 'invoiced': 0},
44 order_by = 'appointment_date'
45 )
46
Rucha Mahabal27512c82020-03-09 17:29:23 +053047 for appointment in patient_appointments:
anoop93d0c782020-04-13 16:42:03 +053048 # Procedure Appointments
Rucha Mahabal27512c82020-03-09 17:29:23 +053049 if appointment.procedure_template:
50 if frappe.db.get_value('Clinical Procedure Template', appointment.procedure_template, 'is_billable'):
anoop93d0c782020-04-13 16:42:03 +053051 appointments_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +053052 'reference_type': 'Patient Appointment',
53 'reference_name': appointment.name,
54 'service': appointment.procedure_template
55 })
anoop93d0c782020-04-13 16:42:03 +053056 # Consultation Appointments, should check fee validity
Jamsheerba119722018-07-06 15:58:13 +053057 else:
anoop93d0c782020-04-13 16:42:03 +053058 if frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups') and \
59 frappe.db.exists('Fee Validity Reference', {'appointment': appointment.name}):
60 continue # Skip invoicing, fee validty present
61 practitioner_charge = 0
62 income_account = None
63 service_item = None
64 if appointment.practitioner:
65 service_item, practitioner_charge = get_service_item_and_practitioner_charge(appointment)
66 income_account = get_income_account(appointment.practitioner, appointment.company)
67 appointments_to_invoice.append({
68 'reference_type': 'Patient Appointment',
69 'reference_name': appointment.name,
70 'service': service_item,
71 'rate': practitioner_charge,
72 'income_account': income_account
73 })
Rucha Mahabal27512c82020-03-09 17:29:23 +053074
anoop93d0c782020-04-13 16:42:03 +053075 return appointments_to_invoice
Rucha Mahabal27512c82020-03-09 17:29:23 +053076
77
anoop93d0c782020-04-13 16:42:03 +053078def get_encounters_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +053079 encounters_to_invoice = []
80 encounters = frappe.get_list(
81 'Patient Encounter',
82 fields=['*'],
anoop93d0c782020-04-13 16:42:03 +053083 filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1}
Rucha Mahabal27512c82020-03-09 17:29:23 +053084 )
85 if encounters:
86 for encounter in encounters:
87 if not encounter.appointment:
88 practitioner_charge = 0
89 income_account = None
90 service_item = None
91 if encounter.practitioner:
92 service_item, practitioner_charge = get_service_item_and_practitioner_charge(encounter)
93 income_account = get_income_account(encounter.practitioner, encounter.company)
94
95 encounters_to_invoice.append({
96 'reference_type': 'Patient Encounter',
97 'reference_name': encounter.name,
98 'service': service_item,
99 'rate': practitioner_charge,
100 'income_account': income_account
101 })
102
103 return encounters_to_invoice
104
105
anoop93d0c782020-04-13 16:42:03 +0530106def get_lab_tests_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530107 lab_tests_to_invoice = []
108 lab_tests = frappe.get_list(
109 'Lab Test',
110 fields=['name', 'template'],
anoop93d0c782020-04-13 16:42:03 +0530111 filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1}
Rucha Mahabal27512c82020-03-09 17:29:23 +0530112 )
113 for lab_test in lab_tests:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530114 item, is_billable = frappe.get_cached_value('Lab Test Template', lab_test.lab_test_code, ['item', 'is_billable'])
115 if is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530116 lab_tests_to_invoice.append({
117 'reference_type': 'Lab Test',
118 'reference_name': lab_test.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530119 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530120 })
121
Rucha Mahabalced978e2020-04-02 18:45:53 +0530122 lab_prescriptions = frappe.db.sql(
123 '''
124 SELECT
125 lp.name, lp.lab_test_code
126 FROM
127 `tabPatient Encounter` et, `tabLab Prescription` lp
128 WHERE
129 et.patient=%s
130 and lp.parent=et.name
131 and lp.lab_test_created=0
132 and lp.invoiced=0
133 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530134
135 for prescription in lab_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530136 item, is_billable = frappe.get_cached_value('Lab Test Template', prescription.lab_test_code, ['item', 'is_billable'])
137 if prescription.lab_test_code and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530138 lab_tests_to_invoice.append({
139 'reference_type': 'Lab Prescription',
140 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530141 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530142 })
143
144 return lab_tests_to_invoice
145
146
anoop93d0c782020-04-13 16:42:03 +0530147def get_clinical_procedures_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530148 clinical_procedures_to_invoice = []
149 procedures = frappe.get_list(
150 'Clinical Procedure',
151 fields='*',
anoop93d0c782020-04-13 16:42:03 +0530152 filters={'patient': patient.name, 'company': company, 'invoiced': False}
Rucha Mahabal27512c82020-03-09 17:29:23 +0530153 )
154 for procedure in procedures:
155 if not procedure.appointment:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530156 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', procedure.procedure_template, ['item', 'is_billable'])
157 if procedure.procedure_template and is_billable:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530158 clinical_procedures_to_invoice.append({
159 'reference_type': 'Clinical Procedure',
160 'reference_name': procedure.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530161 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530162 })
163
164 # consumables
165 if procedure.invoice_separately_as_consumables and procedure.consume_stock \
166 and procedure.status == 'Completed' and not procedure.consumption_invoiced:
167
168 service_item = get_healthcare_service_item('clinical_procedure_consumable_item')
169 if not service_item:
170 msg = _('Please Configure Clinical Procedure Consumable Item in ')
171 msg += '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>'''
172 frappe.throw(msg, title=_('Missing Configuration'))
173
174 clinical_procedures_to_invoice.append({
175 'reference_type': 'Clinical Procedure',
176 'reference_name': procedure.name,
177 'service': service_item,
178 'rate': procedure.consumable_total_amount,
179 'description': procedure.consumption_details
180 })
181
Rucha Mahabalced978e2020-04-02 18:45:53 +0530182 procedure_prescriptions = frappe.db.sql(
183 '''
184 SELECT
185 pp.name, pp.procedure
186 FROM
187 `tabPatient Encounter` et, `tabProcedure Prescription` pp
188 WHERE
189 et.patient=%s
190 and pp.parent=et.name
191 and pp.procedure_created=0
192 and pp.invoiced=0
193 and pp.appointment_booked=0
194 ''', (patient.name), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530195
196 for prescription in procedure_prescriptions:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530197 item, is_billable = frappe.get_cached_value('Clinical Procedure Template', prescription.procedure, ['item', 'is_billable'])
198 if is_billable:
Rucha Mahabal197165f2020-03-26 17:29:50 +0530199 clinical_procedures_to_invoice.append({
Rucha Mahabal27512c82020-03-09 17:29:23 +0530200 'reference_type': 'Procedure Prescription',
201 'reference_name': prescription.name,
Rucha Mahabalced978e2020-04-02 18:45:53 +0530202 'service': item
Rucha Mahabal27512c82020-03-09 17:29:23 +0530203 })
204
205 return clinical_procedures_to_invoice
206
207
anoop93d0c782020-04-13 16:42:03 +0530208def get_inpatient_services_to_invoice(patient, company):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530209 services_to_invoice = []
Rucha Mahabalced978e2020-04-02 18:45:53 +0530210 inpatient_services = frappe.db.sql(
211 '''
212 SELECT
213 io.*
214 FROM
215 `tabInpatient Record` ip, `tabInpatient Occupancy` io
216 WHERE
217 ip.patient=%s
anoop93d0c782020-04-13 16:42:03 +0530218 and ip.company=%s
Rucha Mahabalced978e2020-04-02 18:45:53 +0530219 and io.parent=ip.name
220 and io.left=1
221 and io.invoiced=0
anoop93d0c782020-04-13 16:42:03 +0530222 ''', (patient.name, company), as_dict=1)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530223
224 for inpatient_occupancy in inpatient_services:
225 service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type')
Rucha Mahabalced978e2020-04-02 18:45:53 +0530226 service_unit_type = frappe.get_cached_doc('Healthcare Service Unit Type', service_unit_type)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530227 if service_unit_type and service_unit_type.is_billable:
228 hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in)
229 qty = 0.5
230 if hours_occupied > 0:
231 actual_qty = hours_occupied / service_unit_type.no_of_hours
232 floor = math.floor(actual_qty)
233 decimal_part = actual_qty - floor
234 if decimal_part > 0.5:
235 qty = rounded(floor + 1, 1)
236 elif decimal_part < 0.5 and decimal_part > 0:
237 qty = rounded(floor + 0.5, 1)
238 if qty <= 0:
239 qty = 0.5
240 services_to_invoice.append({
241 'reference_type': 'Inpatient Occupancy',
242 'reference_name': inpatient_occupancy.name,
243 'service': service_unit_type.item, 'qty': qty
244 })
245
246 return services_to_invoice
247
Jamsheerba119722018-07-06 15:58:13 +0530248
Rucha Mahabal24055e12020-02-24 19:09:50 +0530249def get_service_item_and_practitioner_charge(doc):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530250 is_inpatient = doc.inpatient_record
Rucha Mahabal24055e12020-02-24 19:09:50 +0530251 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530252 service_item = get_practitioner_service_item(doc.practitioner, 'inpatient_visit_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530253 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530254 service_item = get_healthcare_service_item('inpatient_visit_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530255 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530256 service_item = get_practitioner_service_item(doc.practitioner, 'op_consulting_charge_item')
Jamsheeree5f9c72018-07-30 12:42:06 +0530257 if not service_item:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530258 service_item = get_healthcare_service_item('op_consulting_charge_item')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530259 if not service_item:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530260 throw_config_service_item(is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530261
Rucha Mahabal24055e12020-02-24 19:09:50 +0530262 practitioner_charge = get_practitioner_charge(doc.practitioner, is_inpatient)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530263 if not practitioner_charge:
Rucha Mahabal24055e12020-02-24 19:09:50 +0530264 throw_config_practitioner_charge(is_inpatient, doc.practitioner)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530265
266 return service_item, practitioner_charge
267
Jamsheer8da6f4e2018-07-26 21:03:17 +0530268
Rucha Mahabal27512c82020-03-09 17:29:23 +0530269def throw_config_service_item(is_inpatient):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530270 service_item_label = _('Out Patient Consulting Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530271 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530272 service_item_label = _('Inpatient Visit Charge Item')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530273
Rucha Mahabal4f9a1472020-03-23 10:40:39 +0530274 msg = _(('Please Configure {0} in ').format(service_item_label) \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530275 + '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>''')
276 frappe.throw(msg, title=_('Missing Configuration'))
277
Jamsheer8da6f4e2018-07-26 21:03:17 +0530278
Rucha Mahabal24055e12020-02-24 19:09:50 +0530279def throw_config_practitioner_charge(is_inpatient, practitioner):
Rucha Mahabalced978e2020-04-02 18:45:53 +0530280 charge_name = _('OP Consulting Charge')
Rucha Mahabal24055e12020-02-24 19:09:50 +0530281 if is_inpatient:
Rucha Mahabalced978e2020-04-02 18:45:53 +0530282 charge_name = _('Inpatient Visit Charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530283
Rucha Mahabal27512c82020-03-09 17:29:23 +0530284 msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \
285 + ''' <b><a href='#Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner))
286 frappe.throw(msg, title=_('Missing Configuration'))
287
Jamsheer8da6f4e2018-07-26 21:03:17 +0530288
Jamsheeree5f9c72018-07-30 12:42:06 +0530289def get_practitioner_service_item(practitioner, service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530290 return frappe.db.get_value('Healthcare Practitioner', practitioner, service_item_field)
291
Jamsheeree5f9c72018-07-30 12:42:06 +0530292
Jamsheer8da6f4e2018-07-26 21:03:17 +0530293def get_healthcare_service_item(service_item_field):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530294 return frappe.db.get_single_value('Healthcare Settings', service_item_field)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530295
Jamsheer8da6f4e2018-07-26 21:03:17 +0530296
Rucha Mahabal24055e12020-02-24 19:09:50 +0530297def get_practitioner_charge(practitioner, is_inpatient):
298 if is_inpatient:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530299 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'inpatient_visit_charge')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530300 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530301 practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'op_consulting_charge')
Jamsheerba119722018-07-06 15:58:13 +0530302 if practitioner_charge:
303 return practitioner_charge
Jamsheer8da6f4e2018-07-26 21:03:17 +0530304 return False
Jamsheerba119722018-07-06 15:58:13 +0530305
Rucha Mahabal27512c82020-03-09 17:29:23 +0530306
Jamsheerba119722018-07-06 15:58:13 +0530307def manage_invoice_submit_cancel(doc, method):
308 if doc.items:
309 for item in doc.items:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530310 if item.get('reference_dt') and item.get('reference_dn'):
311 if frappe.get_meta(item.reference_dt).has_field('invoiced'):
Jamsheer146683b2018-07-25 11:30:30 +0530312 set_invoiced(item, method, doc.name)
Jamsheerba119722018-07-06 15:58:13 +0530313
Rucha Mahabal27512c82020-03-09 17:29:23 +0530314 if method=='on_submit' and frappe.db.get_single_value('Healthcare Settings', 'create_lab_test_on_si_submit'):
315 create_multiple('Sales Invoice', doc.name)
316
Jamsheer0ae100b2018-08-01 14:29:43 +0530317
Jamsheer146683b2018-07-25 11:30:30 +0530318def set_invoiced(item, method, ref_invoice=None):
Jamsheerba119722018-07-06 15:58:13 +0530319 invoiced = False
Rucha Mahabal27512c82020-03-09 17:29:23 +0530320 if method=='on_submit':
Jamsheerba119722018-07-06 15:58:13 +0530321 validate_invoiced_on_submit(item)
322 invoiced = True
323
Jamsheer8da6f4e2018-07-26 21:03:17 +0530324 if item.reference_dt == 'Clinical Procedure':
325 if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530326 frappe.db.set_value(item.reference_dt, item.reference_dn, 'consumption_invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530327 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530328 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530329 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530330 frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
Jamsheer8da6f4e2018-07-26 21:03:17 +0530331
Jamsheerba119722018-07-06 15:58:13 +0530332 if item.reference_dt == 'Patient Appointment':
333 if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530334 dt_from_appointment = 'Clinical Procedure'
Jamsheerba119722018-07-06 15:58:13 +0530335 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530336 dt_from_appointment = 'Patient Encounter'
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530337 manage_doc_for_appointment(dt_from_appointment, item.reference_dn, invoiced)
Jamsheerba119722018-07-06 15:58:13 +0530338
339 elif item.reference_dt == 'Lab Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530340 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Lab Test', 'lab_test_created')
Jamsheerba119722018-07-06 15:58:13 +0530341
342 elif item.reference_dt == 'Procedure Prescription':
Rucha Mahabal27512c82020-03-09 17:29:23 +0530343 manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Clinical Procedure', 'procedure_created')
344
Jamsheerba119722018-07-06 15:58:13 +0530345
346def validate_invoiced_on_submit(item):
Jamsheer8da6f4e2018-07-26 21:03:17 +0530347 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 +0530348 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'consumption_invoiced')
Jamsheer8da6f4e2018-07-26 21:03:17 +0530349 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530350 is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'invoiced')
351 if is_invoiced:
352 frappe.throw(_('The item referenced by {0} - {1} is already invoiced'\
Jamsheerba119722018-07-06 15:58:13 +0530353 ).format(item.reference_dt, item.reference_dn))
354
Rucha Mahabal27512c82020-03-09 17:29:23 +0530355
Jamsheerba119722018-07-06 15:58:13 +0530356def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
357 created = frappe.db.get_value(ref_dt, ref_dn, created_check_field)
Rucha Mahabal27512c82020-03-09 17:29:23 +0530358 if created:
Jamsheerba119722018-07-06 15:58:13 +0530359 # Fetch the doc created for the prescription
Jamsheereafb0462018-07-25 13:15:12 +0530360 doc_created = frappe.db.get_value(dt, {'prescription': ref_dn})
Jamsheerba119722018-07-06 15:58:13 +0530361 frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
362
Rucha Mahabal27512c82020-03-09 17:29:23 +0530363
Rucha Mahabalcd319962020-03-13 15:39:31 +0530364def check_fee_validity(appointment):
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530365 if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'):
366 return
367
Rucha Mahabalcd319962020-03-13 15:39:31 +0530368 validity = frappe.db.exists('Fee Validity', {
369 'practitioner': appointment.practitioner,
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530370 'patient': appointment.patient,
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530371 'valid_till': ('>=', appointment.appointment_date)
Rucha Mahabalcd319962020-03-13 15:39:31 +0530372 })
373 if not validity:
374 return
375
Rucha Mahabalf2574dd2020-03-17 19:28:18 +0530376 validity = frappe.get_doc('Fee Validity', validity)
377 return validity
378
Rucha Mahabal27512c82020-03-09 17:29:23 +0530379
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530380def manage_fee_validity(appointment):
381 fee_validity = check_fee_validity(appointment)
anoop93d0c782020-04-13 16:42:03 +0530382
Rucha Mahabalcd319962020-03-13 15:39:31 +0530383 if fee_validity:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530384 if appointment.status == 'Cancelled' and fee_validity.visited > 0:
385 fee_validity.visited -= 1
386 frappe.db.delete('Fee Validity Reference', {'appointment': appointment.name})
Rucha Mahabal2cec6bd2020-03-26 14:38:12 +0530387 elif fee_validity.status == 'Completed':
388 return
Rucha Mahabalcd319962020-03-13 15:39:31 +0530389 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530390 fee_validity.visited += 1
391 fee_validity.append('ref_appointments', {
392 'appointment': appointment.name
393 })
394 fee_validity.save(ignore_permissions=True)
Jamsheerba119722018-07-06 15:58:13 +0530395 else:
Rucha Mahabal2f2c09b2020-03-17 18:10:39 +0530396 fee_validity = create_fee_validity(appointment)
397 return fee_validity
Rucha Mahabal27512c82020-03-09 17:29:23 +0530398
Jamsheerba119722018-07-06 15:58:13 +0530399
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530400def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced):
Rucha Mahabalc4b2dce2020-03-09 23:57:00 +0530401 dn_from_appointment = frappe.db.get_value(
402 dt_from_appointment,
Rucha Mahabal27512c82020-03-09 17:29:23 +0530403 filters={'appointment': appointment}
Jamsheerba119722018-07-06 15:58:13 +0530404 )
405 if dn_from_appointment:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530406 frappe.db.set_value(dt_from_appointment, dn_from_appointment, 'invoiced', invoiced)
407
Jamsheere82f27a2018-07-30 11:28:37 +0530408
409@frappe.whitelist()
410def get_drugs_to_invoice(encounter):
Rucha Mahabal27512c82020-03-09 17:29:23 +0530411 encounter = frappe.get_doc('Patient Encounter', encounter)
Jamsheere82f27a2018-07-30 11:28:37 +0530412 if encounter:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530413 patient = frappe.get_doc('Patient', encounter.patient)
414 if patient:
415 if patient.customer:
416 items_to_invoice = []
Jamsheere82f27a2018-07-30 11:28:37 +0530417 for drug_line in encounter.drug_prescription:
418 if drug_line.drug_code:
419 qty = 1
Rucha Mahabal27512c82020-03-09 17:29:23 +0530420 if frappe.db.get_value('Item', drug_line.drug_code, 'stock_uom') == 'Nos':
Jamsheere82f27a2018-07-30 11:28:37 +0530421 qty = drug_line.get_quantity()
Rucha Mahabal27512c82020-03-09 17:29:23 +0530422
423 description = ''
424 if drug_line.dosage and drug_line.period:
425 description = _('{0} for {1}').format(drug_line.dosage, drug_line.period)
426
427 items_to_invoice.append({
428 'drug_code': drug_line.drug_code,
429 'quantity': qty,
430 'description': description
431 })
432 return items_to_invoice
433 else:
434 validate_customer_created(patient)
435
Jamsheer4371c7e2018-08-01 18:40:05 +0530436
437@frappe.whitelist()
438def get_children(doctype, parent, company, is_root=False):
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530439 parent_fieldname = "parent_" + doctype.lower().replace(" ", "_")
Jamsheer4371c7e2018-08-01 18:40:05 +0530440 fields = [
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530441 "name as value",
442 "is_group as expandable",
443 "lft",
444 "rgt"
Jamsheer4371c7e2018-08-01 18:40:05 +0530445 ]
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530446 # fields = [ "name", "is_group", "lft", "rgt" ]
447 filters = [["ifnull(`{0}`,'')".format(parent_fieldname), "=", "" if is_root else parent]]
Jamsheer4371c7e2018-08-01 18:40:05 +0530448
449 if is_root:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530450 fields += ["service_unit_type"] if doctype == "Healthcare Service Unit" else []
451 filters.append(["company", "=", company])
Jamsheer4371c7e2018-08-01 18:40:05 +0530452
453 else:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530454 fields += ["service_unit_type", "allow_appointments", "inpatient_occupancy", "occupancy_status"] if doctype == "Healthcare Service Unit" else []
455 fields += [parent_fieldname + " as parent"]
Jamsheer4371c7e2018-08-01 18:40:05 +0530456
457 hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters)
458
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530459 if doctype == "Healthcare Service Unit":
Jamsheer4371c7e2018-08-01 18:40:05 +0530460 for each in hc_service_units:
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530461 occupancy_msg = ""
462 if each["expandable"] == 1:
Jamsheer4371c7e2018-08-01 18:40:05 +0530463 occupied = False
464 vacant = False
Rucha Mahabalced978e2020-04-02 18:45:53 +0530465 child_list = frappe.db.sql(
466 '''
467 SELECT
468 name, occupancy_status
469 FROM
470 `tabHealthcare Service Unit`
471 WHERE
472 inpatient_occupancy = 1
473 and lft > %s and rgt < %s
474 ''', (each['lft'], each['rgt']))
475
Jamsheer4371c7e2018-08-01 18:40:05 +0530476 for child in child_list:
Jamsheer4371c7e2018-08-01 18:40:05 +0530477 if not occupied:
478 occupied = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530479 if child[1] == "Occupied":
Jamsheer4371c7e2018-08-01 18:40:05 +0530480 occupied += 1
481 if not vacant:
482 vacant = 0
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530483 if child[1] == "Vacant":
Jamsheer4371c7e2018-08-01 18:40:05 +0530484 vacant += 1
485 if vacant and occupied:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530486 occupancy_total = vacant + occupied
Rucha Mahabal05853ef2020-03-12 17:44:46 +0530487 occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total)
488 each["occupied_out_of_vacant"] = occupancy_msg
Jamsheer4371c7e2018-08-01 18:40:05 +0530489 return hc_service_units
Jamsheer5073ac42019-07-12 12:28:34 +0530490
Rucha Mahabal27512c82020-03-09 17:29:23 +0530491
Jamsheer5073ac42019-07-12 12:28:34 +0530492@frappe.whitelist()
493def get_patient_vitals(patient, from_date=None, to_date=None):
494 if not patient: return
Rucha Mahabal27512c82020-03-09 17:29:23 +0530495
496 vitals = frappe.db.get_all('Vital Signs', {
497 'docstatus': 1,
498 'patient': patient
Rucha Mahabal06d1b042020-03-12 12:16:23 +0530499 }, order_by='signs_date, signs_time')
Rucha Mahabal27512c82020-03-09 17:29:23 +0530500
501 if len(vitals):
Jamsheer5073ac42019-07-12 12:28:34 +0530502 return vitals
Rucha Mahabal27512c82020-03-09 17:29:23 +0530503 return False
504
Jamsheer5073ac42019-07-12 12:28:34 +0530505
506@frappe.whitelist()
507def render_docs_as_html(docs):
508 # docs key value pair {doctype: docname}
509 docs_html = "<div class='col-md-12 col-sm-12 text-muted'>"
510 for doc in docs:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530511 docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + '<br/>'
Jamsheer5073ac42019-07-12 12:28:34 +0530512 return {'html': docs_html}
513
Rucha Mahabal27512c82020-03-09 17:29:23 +0530514
Jamsheer5073ac42019-07-12 12:28:34 +0530515@frappe.whitelist()
516def render_doc_as_html(doctype, docname, exclude_fields = []):
517 #render document as html, three column layout will break
518 doc = frappe.get_doc(doctype, docname)
519 meta = frappe.get_meta(doctype)
520 doc_html = "<div class='col-md-12 col-sm-12'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530521 section_html = ''
522 section_label = ''
523 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530524 sec_on = False
525 col_on = 0
526 has_data = False
527 for df in meta.fields:
528 #on section break append append previous section and html to doc html
529 if df.fieldtype == "Section Break":
530 if has_data and col_on and sec_on:
531 doc_html += section_html + html + "</div>"
532 elif has_data and not col_on and sec_on:
533 doc_html += "<div class='col-md-12 col-sm-12'\
534 ><div class='col-md-12 col-sm-12'>" \
535 + section_html + html +"</div></div>"
536 while col_on:
537 doc_html += "</div>"
538 col_on -= 1
539 sec_on = True
540 has_data= False
541 col_on = 0
Rucha Mahabal27512c82020-03-09 17:29:23 +0530542 section_html = ''
543 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530544 if df.label:
545 section_label = df.label
546 continue
547 #on column break append html to section html or doc html
548 if df.fieldtype == "Column Break":
549 if sec_on and has_data:
550 section_html += "<div class='col-md-12 col-sm-12'\
551 ><div class='col-md-6 col\
552 -sm-6'><b>" + section_label + "</b>" + html + "</div><div \
553 class='col-md-6 col-sm-6'>"
554 elif has_data:
555 doc_html += "<div class='col-md-12 col-sm-12'><div class='col-m\
556 d-6 col-sm-6'>" + html + "</div><div class='col-md-6 col-sm-6'>"
557 elif sec_on and not col_on:
558 section_html += "<div class='col-md-6 col-sm-6'>"
Rucha Mahabal27512c82020-03-09 17:29:23 +0530559 html = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530560 col_on += 1
561 if df.label:
562 html += '<br>' + df.label
563 continue
564 #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 +0530565 if df.fieldtype == 'Table':
Jamsheer5073ac42019-07-12 12:28:34 +0530566 items = doc.get(df.fieldname)
567 if not items: continue
568 child_meta = frappe.get_meta(df.options)
569 if not has_data : has_data = True
Rucha Mahabal27512c82020-03-09 17:29:23 +0530570 table_head = ''
571 table_row = ''
Jamsheer5073ac42019-07-12 12:28:34 +0530572 create_head = True
573 for item in items:
574 table_row += '<tr>'
575 for cdf in child_meta.fields:
576 if cdf.in_list_view:
577 if create_head:
578 table_head += '<th>' + cdf.label + '</th>'
579 if item.get(cdf.fieldname):
580 table_row += '<td>' + str(item.get(cdf.fieldname)) \
581 + '</td>'
582 else:
583 table_row += '<td></td>'
584 create_head = False
585 table_row += '</tr>'
586 if sec_on:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530587 section_html += "<table class='table table-condensed \
588 bordered'>" + table_head + table_row + '</table>'
Jamsheer5073ac42019-07-12 12:28:34 +0530589 else:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530590 html += "<table class='table table-condensed table-bordered'>" \
591 + table_head + table_row + "</table>"
Jamsheer5073ac42019-07-12 12:28:34 +0530592 continue
593 #on other field types add label and value to html
594 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 +0530595 html += '<br>{0} : {1}'.format(df.label or df.fieldname, \
Jamsheer5073ac42019-07-12 12:28:34 +0530596 doc.get(df.fieldname))
597 if not has_data : has_data = True
598 if sec_on and col_on and has_data:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530599 doc_html += section_html + html + '</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530600 elif sec_on and not col_on and has_data:
601 doc_html += "<div class='col-md-12 col-sm-12'\
602 ><div class='col-md-12 col-sm-12'>" \
Rucha Mahabal27512c82020-03-09 17:29:23 +0530603 + section_html + html +'</div></div>'
Jamsheer5073ac42019-07-12 12:28:34 +0530604 if doc_html:
Rucha Mahabal27512c82020-03-09 17:29:23 +0530605 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 +0530606
607 return {'html': doc_html}