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