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