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