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