Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2018, earthians and contributors |
| 3 | # For license information, please see license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 6 | import math |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 7 | import frappe |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 8 | from frappe import _ |
Rucha Mahabal | 197165f | 2020-03-26 17:29:50 +0530 | [diff] [blame] | 9 | from frappe.utils import time_diff_in_hours, rounded |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 10 | from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_income_account |
Rucha Mahabal | 2f2c09b | 2020-03-17 18:10:39 +0530 | [diff] [blame] | 11 | from erpnext.healthcare.doctype.fee_validity.fee_validity import create_fee_validity |
Jamsheer | 0ae100b | 2018-08-01 14:29:43 +0530 | [diff] [blame] | 12 | from erpnext.healthcare.doctype.lab_test.lab_test import create_multiple |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 13 | |
| 14 | @frappe.whitelist() |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 15 | def get_healthcare_services_to_invoice(patient, company): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 16 | patient = frappe.get_doc('Patient', patient) |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 17 | items_to_invoice = [] |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 18 | if patient: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 19 | validate_customer_created(patient) |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 20 | # 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) |
Rucha Mahabal | 434791e | 2020-10-24 14:20:38 +0530 | [diff] [blame] | 26 | items_to_invoice += get_therapy_plans_to_invoice(patient, company) |
Rucha Mahabal | d730451 | 2020-04-23 00:52:47 +0530 | [diff] [blame] | 27 | items_to_invoice += get_therapy_sessions_to_invoice(patient, company) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 28 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 29 | return items_to_invoice |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 30 | |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 31 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 32 | def validate_customer_created(patient): |
| 33 | if not frappe.db.get_value('Patient', patient.name, 'customer'): |
| 34 | msg = _("Please set a Customer linked to the Patient") |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 35 | msg += " <b><a href='/app/Form/Patient/{0}'>{0}</a></b>".format(patient.name) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 36 | frappe.throw(msg, title=_('Customer Not Found')) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 37 | |
Rucha Mahabal | 434791e | 2020-10-24 14:20:38 +0530 | [diff] [blame] | 38 | |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 39 | def get_appointments_to_invoice(patient, company): |
| 40 | appointments_to_invoice = [] |
| 41 | patient_appointments = frappe.get_list( |
| 42 | 'Patient Appointment', |
| 43 | fields = '*', |
anoop | 5903002 | 2020-07-28 21:15:54 +0530 | [diff] [blame] | 44 | filters = {'patient': patient.name, 'company': company, 'invoiced': 0, 'status': ['not in', 'Cancelled']}, |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 45 | order_by = 'appointment_date' |
| 46 | ) |
| 47 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 48 | for appointment in patient_appointments: |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 49 | # Procedure Appointments |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 50 | if appointment.procedure_template: |
| 51 | if frappe.db.get_value('Clinical Procedure Template', appointment.procedure_template, 'is_billable'): |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 52 | appointments_to_invoice.append({ |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 53 | 'reference_type': 'Patient Appointment', |
| 54 | 'reference_name': appointment.name, |
| 55 | 'service': appointment.procedure_template |
| 56 | }) |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 57 | # Consultation Appointments, should check fee validity |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 58 | else: |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 59 | if frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups') and \ |
| 60 | frappe.db.exists('Fee Validity Reference', {'appointment': appointment.name}): |
| 61 | continue # Skip invoicing, fee validty present |
| 62 | practitioner_charge = 0 |
| 63 | income_account = None |
| 64 | service_item = None |
| 65 | if appointment.practitioner: |
| 66 | service_item, practitioner_charge = get_service_item_and_practitioner_charge(appointment) |
| 67 | income_account = get_income_account(appointment.practitioner, appointment.company) |
| 68 | appointments_to_invoice.append({ |
| 69 | 'reference_type': 'Patient Appointment', |
| 70 | 'reference_name': appointment.name, |
| 71 | 'service': service_item, |
| 72 | 'rate': practitioner_charge, |
| 73 | 'income_account': income_account |
| 74 | }) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 75 | |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 76 | return appointments_to_invoice |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 77 | |
| 78 | |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 79 | def get_encounters_to_invoice(patient, company): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 80 | encounters_to_invoice = [] |
| 81 | encounters = frappe.get_list( |
| 82 | 'Patient Encounter', |
| 83 | fields=['*'], |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 84 | filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1} |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 85 | ) |
| 86 | if encounters: |
| 87 | for encounter in encounters: |
| 88 | if not encounter.appointment: |
| 89 | practitioner_charge = 0 |
| 90 | income_account = None |
| 91 | service_item = None |
| 92 | if encounter.practitioner: |
| 93 | service_item, practitioner_charge = get_service_item_and_practitioner_charge(encounter) |
| 94 | income_account = get_income_account(encounter.practitioner, encounter.company) |
| 95 | |
| 96 | encounters_to_invoice.append({ |
| 97 | 'reference_type': 'Patient Encounter', |
| 98 | 'reference_name': encounter.name, |
| 99 | 'service': service_item, |
| 100 | 'rate': practitioner_charge, |
| 101 | 'income_account': income_account |
| 102 | }) |
| 103 | |
| 104 | return encounters_to_invoice |
| 105 | |
| 106 | |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 107 | def get_lab_tests_to_invoice(patient, company): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 108 | lab_tests_to_invoice = [] |
| 109 | lab_tests = frappe.get_list( |
| 110 | 'Lab Test', |
| 111 | fields=['name', 'template'], |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 112 | filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1} |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 113 | ) |
| 114 | for lab_test in lab_tests: |
Rucha Mahabal | 131452c | 2020-04-27 10:52:38 +0530 | [diff] [blame] | 115 | item, is_billable = frappe.get_cached_value('Lab Test Template', lab_test.template, ['item', 'is_billable']) |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 116 | if is_billable: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 117 | lab_tests_to_invoice.append({ |
| 118 | 'reference_type': 'Lab Test', |
| 119 | 'reference_name': lab_test.name, |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 120 | 'service': item |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 121 | }) |
| 122 | |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 123 | lab_prescriptions = frappe.db.sql( |
| 124 | ''' |
| 125 | SELECT |
| 126 | lp.name, lp.lab_test_code |
| 127 | FROM |
| 128 | `tabPatient Encounter` et, `tabLab Prescription` lp |
| 129 | WHERE |
| 130 | et.patient=%s |
| 131 | and lp.parent=et.name |
| 132 | and lp.lab_test_created=0 |
| 133 | and lp.invoiced=0 |
| 134 | ''', (patient.name), as_dict=1) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 135 | |
| 136 | for prescription in lab_prescriptions: |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 137 | item, is_billable = frappe.get_cached_value('Lab Test Template', prescription.lab_test_code, ['item', 'is_billable']) |
| 138 | if prescription.lab_test_code and is_billable: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 139 | lab_tests_to_invoice.append({ |
| 140 | 'reference_type': 'Lab Prescription', |
| 141 | 'reference_name': prescription.name, |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 142 | 'service': item |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 143 | }) |
| 144 | |
| 145 | return lab_tests_to_invoice |
| 146 | |
| 147 | |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 148 | def get_clinical_procedures_to_invoice(patient, company): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 149 | clinical_procedures_to_invoice = [] |
| 150 | procedures = frappe.get_list( |
| 151 | 'Clinical Procedure', |
| 152 | fields='*', |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 153 | filters={'patient': patient.name, 'company': company, 'invoiced': False} |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 154 | ) |
| 155 | for procedure in procedures: |
| 156 | if not procedure.appointment: |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 157 | item, is_billable = frappe.get_cached_value('Clinical Procedure Template', procedure.procedure_template, ['item', 'is_billable']) |
| 158 | if procedure.procedure_template and is_billable: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 159 | clinical_procedures_to_invoice.append({ |
| 160 | 'reference_type': 'Clinical Procedure', |
| 161 | 'reference_name': procedure.name, |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 162 | 'service': item |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 163 | }) |
| 164 | |
| 165 | # consumables |
| 166 | if procedure.invoice_separately_as_consumables and procedure.consume_stock \ |
| 167 | and procedure.status == 'Completed' and not procedure.consumption_invoiced: |
| 168 | |
| 169 | service_item = get_healthcare_service_item('clinical_procedure_consumable_item') |
| 170 | if not service_item: |
| 171 | msg = _('Please Configure Clinical Procedure Consumable Item in ') |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 172 | msg += '''<b><a href='/app/Form/Healthcare Settings'>Healthcare Settings</a></b>''' |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 173 | frappe.throw(msg, title=_('Missing Configuration')) |
| 174 | |
| 175 | clinical_procedures_to_invoice.append({ |
| 176 | 'reference_type': 'Clinical Procedure', |
| 177 | 'reference_name': procedure.name, |
| 178 | 'service': service_item, |
| 179 | 'rate': procedure.consumable_total_amount, |
| 180 | 'description': procedure.consumption_details |
| 181 | }) |
| 182 | |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 183 | procedure_prescriptions = frappe.db.sql( |
| 184 | ''' |
| 185 | SELECT |
| 186 | pp.name, pp.procedure |
| 187 | FROM |
| 188 | `tabPatient Encounter` et, `tabProcedure Prescription` pp |
| 189 | WHERE |
| 190 | et.patient=%s |
| 191 | and pp.parent=et.name |
| 192 | and pp.procedure_created=0 |
| 193 | and pp.invoiced=0 |
| 194 | and pp.appointment_booked=0 |
| 195 | ''', (patient.name), as_dict=1) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 196 | |
| 197 | for prescription in procedure_prescriptions: |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 198 | item, is_billable = frappe.get_cached_value('Clinical Procedure Template', prescription.procedure, ['item', 'is_billable']) |
| 199 | if is_billable: |
Rucha Mahabal | 197165f | 2020-03-26 17:29:50 +0530 | [diff] [blame] | 200 | clinical_procedures_to_invoice.append({ |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 201 | 'reference_type': 'Procedure Prescription', |
| 202 | 'reference_name': prescription.name, |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 203 | 'service': item |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 204 | }) |
| 205 | |
| 206 | return clinical_procedures_to_invoice |
| 207 | |
| 208 | |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 209 | def get_inpatient_services_to_invoice(patient, company): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 210 | services_to_invoice = [] |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 211 | inpatient_services = frappe.db.sql( |
| 212 | ''' |
| 213 | SELECT |
| 214 | io.* |
| 215 | FROM |
| 216 | `tabInpatient Record` ip, `tabInpatient Occupancy` io |
| 217 | WHERE |
| 218 | ip.patient=%s |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 219 | and ip.company=%s |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 220 | and io.parent=ip.name |
| 221 | and io.left=1 |
| 222 | and io.invoiced=0 |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 223 | ''', (patient.name, company), as_dict=1) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 224 | |
| 225 | for inpatient_occupancy in inpatient_services: |
| 226 | service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type') |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 227 | service_unit_type = frappe.get_cached_doc('Healthcare Service Unit Type', service_unit_type) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 228 | if service_unit_type and service_unit_type.is_billable: |
| 229 | hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in) |
| 230 | qty = 0.5 |
| 231 | if hours_occupied > 0: |
| 232 | actual_qty = hours_occupied / service_unit_type.no_of_hours |
| 233 | floor = math.floor(actual_qty) |
| 234 | decimal_part = actual_qty - floor |
| 235 | if decimal_part > 0.5: |
| 236 | qty = rounded(floor + 1, 1) |
| 237 | elif decimal_part < 0.5 and decimal_part > 0: |
| 238 | qty = rounded(floor + 0.5, 1) |
| 239 | if qty <= 0: |
| 240 | qty = 0.5 |
| 241 | services_to_invoice.append({ |
| 242 | 'reference_type': 'Inpatient Occupancy', |
| 243 | 'reference_name': inpatient_occupancy.name, |
| 244 | 'service': service_unit_type.item, 'qty': qty |
| 245 | }) |
| 246 | |
| 247 | return services_to_invoice |
| 248 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 249 | |
Rucha Mahabal | 434791e | 2020-10-24 14:20:38 +0530 | [diff] [blame] | 250 | def get_therapy_plans_to_invoice(patient, company): |
| 251 | therapy_plans_to_invoice = [] |
| 252 | therapy_plans = frappe.get_list( |
| 253 | 'Therapy Plan', |
| 254 | fields=['therapy_plan_template', 'name'], |
| 255 | filters={ |
| 256 | 'patient': patient.name, |
| 257 | 'invoiced': 0, |
| 258 | 'company': company, |
| 259 | 'therapy_plan_template': ('!=', '') |
| 260 | } |
| 261 | ) |
| 262 | for plan in therapy_plans: |
| 263 | therapy_plans_to_invoice.append({ |
| 264 | 'reference_type': 'Therapy Plan', |
| 265 | 'reference_name': plan.name, |
| 266 | 'service': frappe.db.get_value('Therapy Plan Template', plan.therapy_plan_template, 'linked_item') |
| 267 | }) |
| 268 | |
| 269 | return therapy_plans_to_invoice |
| 270 | |
| 271 | |
Rucha Mahabal | d730451 | 2020-04-23 00:52:47 +0530 | [diff] [blame] | 272 | def get_therapy_sessions_to_invoice(patient, company): |
Rucha Mahabal | eaa956b | 2020-04-22 13:07:12 +0530 | [diff] [blame] | 273 | therapy_sessions_to_invoice = [] |
Rucha Mahabal | 434791e | 2020-10-24 14:20:38 +0530 | [diff] [blame] | 274 | therapy_plans = frappe.db.get_all('Therapy Plan', {'therapy_plan_template': ('!=', '')}) |
| 275 | therapy_plans_created_from_template = [] |
| 276 | for entry in therapy_plans: |
| 277 | therapy_plans_created_from_template.append(entry.name) |
| 278 | |
Rucha Mahabal | eaa956b | 2020-04-22 13:07:12 +0530 | [diff] [blame] | 279 | therapy_sessions = frappe.get_list( |
| 280 | 'Therapy Session', |
| 281 | fields='*', |
Rucha Mahabal | 434791e | 2020-10-24 14:20:38 +0530 | [diff] [blame] | 282 | filters={ |
| 283 | 'patient': patient.name, |
| 284 | 'invoiced': 0, |
| 285 | 'company': company, |
| 286 | 'therapy_plan': ('not in', therapy_plans_created_from_template) |
| 287 | } |
Rucha Mahabal | eaa956b | 2020-04-22 13:07:12 +0530 | [diff] [blame] | 288 | ) |
| 289 | for therapy in therapy_sessions: |
| 290 | if not therapy.appointment: |
| 291 | if therapy.therapy_type and frappe.db.get_value('Therapy Type', therapy.therapy_type, 'is_billable'): |
| 292 | therapy_sessions_to_invoice.append({ |
| 293 | 'reference_type': 'Therapy Session', |
| 294 | 'reference_name': therapy.name, |
| 295 | 'service': frappe.db.get_value('Therapy Type', therapy.therapy_type, 'item') |
| 296 | }) |
| 297 | |
| 298 | return therapy_sessions_to_invoice |
| 299 | |
| 300 | |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 301 | def get_service_item_and_practitioner_charge(doc): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 302 | is_inpatient = doc.inpatient_record |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 303 | if is_inpatient: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 304 | service_item = get_practitioner_service_item(doc.practitioner, 'inpatient_visit_charge_item') |
Jamsheer | ee5f9c7 | 2018-07-30 12:42:06 +0530 | [diff] [blame] | 305 | if not service_item: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 306 | service_item = get_healthcare_service_item('inpatient_visit_charge_item') |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 307 | else: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 308 | service_item = get_practitioner_service_item(doc.practitioner, 'op_consulting_charge_item') |
Jamsheer | ee5f9c7 | 2018-07-30 12:42:06 +0530 | [diff] [blame] | 309 | if not service_item: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 310 | service_item = get_healthcare_service_item('op_consulting_charge_item') |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 311 | if not service_item: |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 312 | throw_config_service_item(is_inpatient) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 313 | |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 314 | practitioner_charge = get_practitioner_charge(doc.practitioner, is_inpatient) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 315 | if not practitioner_charge: |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 316 | throw_config_practitioner_charge(is_inpatient, doc.practitioner) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 317 | |
| 318 | return service_item, practitioner_charge |
| 319 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 320 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 321 | def throw_config_service_item(is_inpatient): |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 322 | service_item_label = _('Out Patient Consulting Charge Item') |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 323 | if is_inpatient: |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 324 | service_item_label = _('Inpatient Visit Charge Item') |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 325 | |
Rucha Mahabal | 4f9a147 | 2020-03-23 10:40:39 +0530 | [diff] [blame] | 326 | msg = _(('Please Configure {0} in ').format(service_item_label) \ |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 327 | + '''<b><a href='/app/Form/Healthcare Settings'>Healthcare Settings</a></b>''') |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 328 | frappe.throw(msg, title=_('Missing Configuration')) |
| 329 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 330 | |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 331 | def throw_config_practitioner_charge(is_inpatient, practitioner): |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 332 | charge_name = _('OP Consulting Charge') |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 333 | if is_inpatient: |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 334 | charge_name = _('Inpatient Visit Charge') |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 335 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 336 | msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \ |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 337 | + ''' <b><a href='/app/Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner)) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 338 | frappe.throw(msg, title=_('Missing Configuration')) |
| 339 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 340 | |
Jamsheer | ee5f9c7 | 2018-07-30 12:42:06 +0530 | [diff] [blame] | 341 | def get_practitioner_service_item(practitioner, service_item_field): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 342 | return frappe.db.get_value('Healthcare Practitioner', practitioner, service_item_field) |
| 343 | |
Jamsheer | ee5f9c7 | 2018-07-30 12:42:06 +0530 | [diff] [blame] | 344 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 345 | def get_healthcare_service_item(service_item_field): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 346 | return frappe.db.get_single_value('Healthcare Settings', service_item_field) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 347 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 348 | |
Rucha Mahabal | 24055e1 | 2020-02-24 19:09:50 +0530 | [diff] [blame] | 349 | def get_practitioner_charge(practitioner, is_inpatient): |
| 350 | if is_inpatient: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 351 | practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'inpatient_visit_charge') |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 352 | else: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 353 | practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'op_consulting_charge') |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 354 | if practitioner_charge: |
| 355 | return practitioner_charge |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 356 | return False |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 357 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 358 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 359 | def manage_invoice_submit_cancel(doc, method): |
| 360 | if doc.items: |
| 361 | for item in doc.items: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 362 | if item.get('reference_dt') and item.get('reference_dn'): |
| 363 | if frappe.get_meta(item.reference_dt).has_field('invoiced'): |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 364 | set_invoiced(item, method, doc.name) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 365 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 366 | if method=='on_submit' and frappe.db.get_single_value('Healthcare Settings', 'create_lab_test_on_si_submit'): |
| 367 | create_multiple('Sales Invoice', doc.name) |
| 368 | |
Jamsheer | 0ae100b | 2018-08-01 14:29:43 +0530 | [diff] [blame] | 369 | |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 370 | def set_invoiced(item, method, ref_invoice=None): |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 371 | invoiced = False |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 372 | if method=='on_submit': |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 373 | validate_invoiced_on_submit(item) |
| 374 | invoiced = True |
| 375 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 376 | if item.reference_dt == 'Clinical Procedure': |
| 377 | if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 378 | frappe.db.set_value(item.reference_dt, item.reference_dn, 'consumption_invoiced', invoiced) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 379 | else: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 380 | frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 381 | else: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 382 | frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 383 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 384 | if item.reference_dt == 'Patient Appointment': |
| 385 | if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 386 | dt_from_appointment = 'Clinical Procedure' |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 387 | else: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 388 | dt_from_appointment = 'Patient Encounter' |
Rucha Mahabal | 06d1b04 | 2020-03-12 12:16:23 +0530 | [diff] [blame] | 389 | manage_doc_for_appointment(dt_from_appointment, item.reference_dn, invoiced) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 390 | |
| 391 | elif item.reference_dt == 'Lab Prescription': |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 392 | manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Lab Test', 'lab_test_created') |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 393 | |
| 394 | elif item.reference_dt == 'Procedure Prescription': |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 395 | manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Clinical Procedure', 'procedure_created') |
| 396 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 397 | |
| 398 | def validate_invoiced_on_submit(item): |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 399 | if item.reference_dt == 'Clinical Procedure' and get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code: |
Rucha Mahabal | 06d1b04 | 2020-03-12 12:16:23 +0530 | [diff] [blame] | 400 | is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'consumption_invoiced') |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 401 | else: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 402 | is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'invoiced') |
| 403 | if is_invoiced: |
Rucha Mahabal | 434791e | 2020-10-24 14:20:38 +0530 | [diff] [blame] | 404 | frappe.throw(_('The item referenced by {0} - {1} is already invoiced').format( |
| 405 | item.reference_dt, item.reference_dn)) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 406 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 407 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 408 | def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field): |
| 409 | created = frappe.db.get_value(ref_dt, ref_dn, created_check_field) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 410 | if created: |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 411 | # Fetch the doc created for the prescription |
Jamsheer | eafb046 | 2018-07-25 13:15:12 +0530 | [diff] [blame] | 412 | doc_created = frappe.db.get_value(dt, {'prescription': ref_dn}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 413 | frappe.db.set_value(dt, doc_created, 'invoiced', invoiced) |
| 414 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 415 | |
Rucha Mahabal | cd31996 | 2020-03-13 15:39:31 +0530 | [diff] [blame] | 416 | def check_fee_validity(appointment): |
Rucha Mahabal | f2574dd | 2020-03-17 19:28:18 +0530 | [diff] [blame] | 417 | if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'): |
| 418 | return |
| 419 | |
Rucha Mahabal | cd31996 | 2020-03-13 15:39:31 +0530 | [diff] [blame] | 420 | validity = frappe.db.exists('Fee Validity', { |
| 421 | 'practitioner': appointment.practitioner, |
Rucha Mahabal | 2f2c09b | 2020-03-17 18:10:39 +0530 | [diff] [blame] | 422 | 'patient': appointment.patient, |
Rucha Mahabal | f2574dd | 2020-03-17 19:28:18 +0530 | [diff] [blame] | 423 | 'valid_till': ('>=', appointment.appointment_date) |
Rucha Mahabal | cd31996 | 2020-03-13 15:39:31 +0530 | [diff] [blame] | 424 | }) |
| 425 | if not validity: |
| 426 | return |
| 427 | |
Rucha Mahabal | f2574dd | 2020-03-17 19:28:18 +0530 | [diff] [blame] | 428 | validity = frappe.get_doc('Fee Validity', validity) |
| 429 | return validity |
| 430 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 431 | |
Rucha Mahabal | 2f2c09b | 2020-03-17 18:10:39 +0530 | [diff] [blame] | 432 | def manage_fee_validity(appointment): |
| 433 | fee_validity = check_fee_validity(appointment) |
anoop | 93d0c78 | 2020-04-13 16:42:03 +0530 | [diff] [blame] | 434 | |
Rucha Mahabal | cd31996 | 2020-03-13 15:39:31 +0530 | [diff] [blame] | 435 | if fee_validity: |
Rucha Mahabal | 2f2c09b | 2020-03-17 18:10:39 +0530 | [diff] [blame] | 436 | if appointment.status == 'Cancelled' and fee_validity.visited > 0: |
| 437 | fee_validity.visited -= 1 |
| 438 | frappe.db.delete('Fee Validity Reference', {'appointment': appointment.name}) |
Rucha Mahabal | 2cec6bd | 2020-03-26 14:38:12 +0530 | [diff] [blame] | 439 | elif fee_validity.status == 'Completed': |
| 440 | return |
Rucha Mahabal | cd31996 | 2020-03-13 15:39:31 +0530 | [diff] [blame] | 441 | else: |
Rucha Mahabal | 2f2c09b | 2020-03-17 18:10:39 +0530 | [diff] [blame] | 442 | fee_validity.visited += 1 |
| 443 | fee_validity.append('ref_appointments', { |
| 444 | 'appointment': appointment.name |
| 445 | }) |
| 446 | fee_validity.save(ignore_permissions=True) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 447 | else: |
Rucha Mahabal | 2f2c09b | 2020-03-17 18:10:39 +0530 | [diff] [blame] | 448 | fee_validity = create_fee_validity(appointment) |
| 449 | return fee_validity |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 450 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 451 | |
Rucha Mahabal | 06d1b04 | 2020-03-12 12:16:23 +0530 | [diff] [blame] | 452 | def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced): |
Rucha Mahabal | c4b2dce | 2020-03-09 23:57:00 +0530 | [diff] [blame] | 453 | dn_from_appointment = frappe.db.get_value( |
| 454 | dt_from_appointment, |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 455 | filters={'appointment': appointment} |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 456 | ) |
| 457 | if dn_from_appointment: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 458 | frappe.db.set_value(dt_from_appointment, dn_from_appointment, 'invoiced', invoiced) |
| 459 | |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 460 | |
| 461 | @frappe.whitelist() |
| 462 | def get_drugs_to_invoice(encounter): |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 463 | encounter = frappe.get_doc('Patient Encounter', encounter) |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 464 | if encounter: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 465 | patient = frappe.get_doc('Patient', encounter.patient) |
| 466 | if patient: |
| 467 | if patient.customer: |
| 468 | items_to_invoice = [] |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 469 | for drug_line in encounter.drug_prescription: |
| 470 | if drug_line.drug_code: |
| 471 | qty = 1 |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 472 | if frappe.db.get_value('Item', drug_line.drug_code, 'stock_uom') == 'Nos': |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 473 | qty = drug_line.get_quantity() |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 474 | |
| 475 | description = '' |
| 476 | if drug_line.dosage and drug_line.period: |
| 477 | description = _('{0} for {1}').format(drug_line.dosage, drug_line.period) |
| 478 | |
| 479 | items_to_invoice.append({ |
| 480 | 'drug_code': drug_line.drug_code, |
| 481 | 'quantity': qty, |
| 482 | 'description': description |
| 483 | }) |
| 484 | return items_to_invoice |
| 485 | else: |
| 486 | validate_customer_created(patient) |
| 487 | |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 488 | |
| 489 | @frappe.whitelist() |
| 490 | def get_children(doctype, parent, company, is_root=False): |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 491 | parent_fieldname = "parent_" + doctype.lower().replace(" ", "_") |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 492 | fields = [ |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 493 | "name as value", |
| 494 | "is_group as expandable", |
| 495 | "lft", |
| 496 | "rgt" |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 497 | ] |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 498 | # fields = [ "name", "is_group", "lft", "rgt" ] |
| 499 | filters = [["ifnull(`{0}`,'')".format(parent_fieldname), "=", "" if is_root else parent]] |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 500 | |
| 501 | if is_root: |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 502 | fields += ["service_unit_type"] if doctype == "Healthcare Service Unit" else [] |
| 503 | filters.append(["company", "=", company]) |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 504 | |
| 505 | else: |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 506 | fields += ["service_unit_type", "allow_appointments", "inpatient_occupancy", "occupancy_status"] if doctype == "Healthcare Service Unit" else [] |
| 507 | fields += [parent_fieldname + " as parent"] |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 508 | |
| 509 | hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters) |
| 510 | |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 511 | if doctype == "Healthcare Service Unit": |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 512 | for each in hc_service_units: |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 513 | occupancy_msg = "" |
| 514 | if each["expandable"] == 1: |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 515 | occupied = False |
| 516 | vacant = False |
Rucha Mahabal | ced978e | 2020-04-02 18:45:53 +0530 | [diff] [blame] | 517 | child_list = frappe.db.sql( |
| 518 | ''' |
| 519 | SELECT |
| 520 | name, occupancy_status |
| 521 | FROM |
| 522 | `tabHealthcare Service Unit` |
| 523 | WHERE |
| 524 | inpatient_occupancy = 1 |
| 525 | and lft > %s and rgt < %s |
| 526 | ''', (each['lft'], each['rgt'])) |
| 527 | |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 528 | for child in child_list: |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 529 | if not occupied: |
| 530 | occupied = 0 |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 531 | if child[1] == "Occupied": |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 532 | occupied += 1 |
| 533 | if not vacant: |
| 534 | vacant = 0 |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 535 | if child[1] == "Vacant": |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 536 | vacant += 1 |
| 537 | if vacant and occupied: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 538 | occupancy_total = vacant + occupied |
Rucha Mahabal | 05853ef | 2020-03-12 17:44:46 +0530 | [diff] [blame] | 539 | occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total) |
| 540 | each["occupied_out_of_vacant"] = occupancy_msg |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 541 | return hc_service_units |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 542 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 543 | |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 544 | @frappe.whitelist() |
| 545 | def get_patient_vitals(patient, from_date=None, to_date=None): |
| 546 | if not patient: return |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 547 | |
Rucha Mahabal | 4dd6b99 | 2020-05-25 18:42:01 +0530 | [diff] [blame] | 548 | vitals = frappe.db.get_all('Vital Signs', filters={ |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 549 | 'docstatus': 1, |
| 550 | 'patient': patient |
Rucha Mahabal | 4dd6b99 | 2020-05-25 18:42:01 +0530 | [diff] [blame] | 551 | }, order_by='signs_date, signs_time', fields=['*']) |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 552 | |
| 553 | if len(vitals): |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 554 | return vitals |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 555 | return False |
| 556 | |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 557 | |
| 558 | @frappe.whitelist() |
| 559 | def render_docs_as_html(docs): |
| 560 | # docs key value pair {doctype: docname} |
| 561 | docs_html = "<div class='col-md-12 col-sm-12 text-muted'>" |
| 562 | for doc in docs: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 563 | docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + '<br/>' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 564 | return {'html': docs_html} |
| 565 | |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 566 | |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 567 | @frappe.whitelist() |
| 568 | def render_doc_as_html(doctype, docname, exclude_fields = []): |
| 569 | #render document as html, three column layout will break |
| 570 | doc = frappe.get_doc(doctype, docname) |
| 571 | meta = frappe.get_meta(doctype) |
| 572 | doc_html = "<div class='col-md-12 col-sm-12'>" |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 573 | section_html = '' |
| 574 | section_label = '' |
| 575 | html = '' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 576 | sec_on = False |
| 577 | col_on = 0 |
| 578 | has_data = False |
| 579 | for df in meta.fields: |
| 580 | #on section break append append previous section and html to doc html |
| 581 | if df.fieldtype == "Section Break": |
| 582 | if has_data and col_on and sec_on: |
| 583 | doc_html += section_html + html + "</div>" |
| 584 | elif has_data and not col_on and sec_on: |
| 585 | doc_html += "<div class='col-md-12 col-sm-12'\ |
| 586 | ><div class='col-md-12 col-sm-12'>" \ |
| 587 | + section_html + html +"</div></div>" |
| 588 | while col_on: |
| 589 | doc_html += "</div>" |
| 590 | col_on -= 1 |
| 591 | sec_on = True |
| 592 | has_data= False |
| 593 | col_on = 0 |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 594 | section_html = '' |
| 595 | html = '' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 596 | if df.label: |
| 597 | section_label = df.label |
| 598 | continue |
| 599 | #on column break append html to section html or doc html |
| 600 | if df.fieldtype == "Column Break": |
| 601 | if sec_on and has_data: |
| 602 | section_html += "<div class='col-md-12 col-sm-12'\ |
| 603 | ><div class='col-md-6 col\ |
| 604 | -sm-6'><b>" + section_label + "</b>" + html + "</div><div \ |
| 605 | class='col-md-6 col-sm-6'>" |
| 606 | elif has_data: |
| 607 | doc_html += "<div class='col-md-12 col-sm-12'><div class='col-m\ |
| 608 | d-6 col-sm-6'>" + html + "</div><div class='col-md-6 col-sm-6'>" |
| 609 | elif sec_on and not col_on: |
| 610 | section_html += "<div class='col-md-6 col-sm-6'>" |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 611 | html = '' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 612 | col_on += 1 |
| 613 | if df.label: |
| 614 | html += '<br>' + df.label |
| 615 | continue |
| 616 | #on table iterate in items and create table based on in_list_view, append to section html or doc html |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 617 | if df.fieldtype == 'Table': |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 618 | items = doc.get(df.fieldname) |
| 619 | if not items: continue |
| 620 | child_meta = frappe.get_meta(df.options) |
| 621 | if not has_data : has_data = True |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 622 | table_head = '' |
| 623 | table_row = '' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 624 | create_head = True |
| 625 | for item in items: |
| 626 | table_row += '<tr>' |
| 627 | for cdf in child_meta.fields: |
| 628 | if cdf.in_list_view: |
| 629 | if create_head: |
| 630 | table_head += '<th>' + cdf.label + '</th>' |
| 631 | if item.get(cdf.fieldname): |
| 632 | table_row += '<td>' + str(item.get(cdf.fieldname)) \ |
| 633 | + '</td>' |
| 634 | else: |
| 635 | table_row += '<td></td>' |
| 636 | create_head = False |
| 637 | table_row += '</tr>' |
| 638 | if sec_on: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 639 | section_html += "<table class='table table-condensed \ |
| 640 | bordered'>" + table_head + table_row + '</table>' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 641 | else: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 642 | html += "<table class='table table-condensed table-bordered'>" \ |
| 643 | + table_head + table_row + "</table>" |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 644 | continue |
| 645 | #on other field types add label and value to html |
| 646 | if not df.hidden and not df.print_hide and doc.get(df.fieldname) and df.fieldname not in exclude_fields: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 647 | html += '<br>{0} : {1}'.format(df.label or df.fieldname, \ |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 648 | doc.get(df.fieldname)) |
| 649 | if not has_data : has_data = True |
| 650 | if sec_on and col_on and has_data: |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 651 | doc_html += section_html + html + '</div></div>' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 652 | elif sec_on and not col_on and has_data: |
| 653 | doc_html += "<div class='col-md-12 col-sm-12'\ |
| 654 | ><div class='col-md-12 col-sm-12'>" \ |
Rucha Mahabal | 27512c8 | 2020-03-09 17:29:23 +0530 | [diff] [blame] | 655 | + section_html + html +'</div></div>' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 656 | if doc_html: |
Rushabh Mehta | 542bc01 | 2020-11-18 15:00:34 +0530 | [diff] [blame] | 657 | doc_html = "<div class='small'><div class='col-md-12 text-right'><a class='btn btn-default btn-xs' href='/app/Form/%s/%s'></a></div>" %(doctype, docname) + doc_html + '</div>' |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 658 | |
| 659 | return {'html': doc_html} |