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 |
| 7 | import datetime |
| 8 | from frappe import _ |
Jamsheer | 25dda3a | 2018-07-30 14:50:16 +0530 | [diff] [blame] | 9 | import math |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 10 | from frappe.utils import time_diff_in_hours, rounded, getdate, add_days |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 11 | from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_income_account |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 12 | from erpnext.healthcare.doctype.fee_validity.fee_validity import create_fee_validity, update_fee_validity |
Jamsheer | 0ae100b | 2018-08-01 14:29:43 +0530 | [diff] [blame] | 13 | from erpnext.healthcare.doctype.lab_test.lab_test import create_multiple |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 14 | |
| 15 | @frappe.whitelist() |
| 16 | def get_healthcare_services_to_invoice(patient): |
| 17 | patient = frappe.get_doc("Patient", patient) |
| 18 | if patient: |
| 19 | if patient.customer: |
| 20 | item_to_invoice = [] |
| 21 | patient_appointments = frappe.get_list("Patient Appointment",{'patient': patient.name, 'invoiced': False}, |
| 22 | order_by="appointment_date") |
| 23 | if patient_appointments: |
| 24 | fee_validity_details = [] |
| 25 | valid_days = frappe.db.get_value("Healthcare Settings", None, "valid_days") |
| 26 | max_visit = frappe.db.get_value("Healthcare Settings", None, "max_visit") |
| 27 | for patient_appointment in patient_appointments: |
| 28 | patient_appointment_obj = frappe.get_doc("Patient Appointment", patient_appointment['name']) |
| 29 | |
| 30 | if patient_appointment_obj.procedure_template: |
| 31 | if frappe.db.get_value("Clinical Procedure Template", patient_appointment_obj.procedure_template, "is_billable") == 1: |
| 32 | item_to_invoice.append({'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name, 'service': patient_appointment_obj.procedure_template}) |
| 33 | else: |
| 34 | practitioner_exist_in_list = False |
| 35 | skip_invoice = False |
| 36 | if fee_validity_details: |
| 37 | for validity in fee_validity_details: |
| 38 | if validity['practitioner'] == patient_appointment_obj.practitioner: |
| 39 | practitioner_exist_in_list = True |
| 40 | if validity['valid_till'] >= patient_appointment_obj.appointment_date: |
| 41 | validity['visits'] = validity['visits']+1 |
| 42 | if int(max_visit) > validity['visits']: |
| 43 | skip_invoice = True |
| 44 | if not skip_invoice: |
| 45 | validity['visits'] = 1 |
| 46 | validity['valid_till'] = patient_appointment_obj.appointment_date + datetime.timedelta(days=int(valid_days)) |
| 47 | if not practitioner_exist_in_list: |
| 48 | valid_till = patient_appointment_obj.appointment_date + datetime.timedelta(days=int(valid_days)) |
| 49 | visits = 0 |
| 50 | validity_exist = validity_exists(patient_appointment_obj.practitioner, patient_appointment_obj.patient) |
| 51 | if validity_exist: |
| 52 | fee_validity = frappe.get_doc("Fee Validity", validity_exist[0][0]) |
| 53 | valid_till = fee_validity.valid_till |
| 54 | visits = fee_validity.visited |
| 55 | fee_validity_details.append({'practitioner': patient_appointment_obj.practitioner, |
| 56 | 'valid_till': valid_till, 'visits': visits}) |
| 57 | |
| 58 | if not skip_invoice: |
| 59 | practitioner_charge = 0 |
| 60 | income_account = None |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 61 | service_item = None |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 62 | if patient_appointment_obj.practitioner: |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 63 | service_item, practitioner_charge = service_item_and_practitioner_charge(patient_appointment_obj) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 64 | income_account = get_income_account(patient_appointment_obj.practitioner, patient_appointment_obj.company) |
| 65 | item_to_invoice.append({'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name, |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 66 | 'service': service_item, 'rate': practitioner_charge, |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 67 | 'income_account': income_account}) |
| 68 | |
| 69 | encounters = frappe.get_list("Patient Encounter", {'patient': patient.name, 'invoiced': False, 'docstatus': 1}) |
| 70 | if encounters: |
| 71 | for encounter in encounters: |
| 72 | encounter_obj = frappe.get_doc("Patient Encounter", encounter['name']) |
| 73 | if not encounter_obj.appointment: |
| 74 | practitioner_charge = 0 |
| 75 | income_account = None |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 76 | service_item = None |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 77 | if encounter_obj.practitioner: |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 78 | service_item, practitioner_charge = service_item_and_practitioner_charge(encounter_obj) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 79 | income_account = get_income_account(encounter_obj.practitioner, encounter_obj.company) |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 80 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 81 | item_to_invoice.append({'reference_type': 'Patient Encounter', 'reference_name': encounter_obj.name, |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 82 | 'service': service_item, 'rate': practitioner_charge, |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 83 | 'income_account': income_account}) |
| 84 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 85 | lab_tests = frappe.get_list("Lab Test", {'patient': patient.name, 'invoiced': False, 'docstatus': 1}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 86 | if lab_tests: |
| 87 | for lab_test in lab_tests: |
| 88 | lab_test_obj = frappe.get_doc("Lab Test", lab_test['name']) |
| 89 | if frappe.db.get_value("Lab Test Template", lab_test_obj.template, "is_billable") == 1: |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 90 | item_to_invoice.append({'reference_type': 'Lab Test', 'reference_name': lab_test_obj.name, |
| 91 | 'service': frappe.db.get_value("Lab Test Template", lab_test_obj.template, "item")}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 92 | |
Jamsheer | c64880b | 2018-08-01 14:37:13 +0530 | [diff] [blame] | 93 | lab_rxs = frappe.db.sql("""select lp.name from `tabPatient Encounter` et, `tabLab Prescription` lp |
Jamsheer | c07e8e5 | 2018-09-18 10:54:03 +0530 | [diff] [blame] | 94 | where et.patient=%s and lp.parent=et.name and lp.lab_test_created=0 and lp.invoiced=0""", (patient.name)) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 95 | if lab_rxs: |
| 96 | for lab_rx in lab_rxs: |
| 97 | rx_obj = frappe.get_doc("Lab Prescription", lab_rx[0]) |
Jamsheer | c07e8e5 | 2018-09-18 10:54:03 +0530 | [diff] [blame] | 98 | if rx_obj.lab_test_code and (frappe.db.get_value("Lab Test Template", rx_obj.lab_test_code, "is_billable") == 1): |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 99 | item_to_invoice.append({'reference_type': 'Lab Prescription', 'reference_name': rx_obj.name, |
Jamsheer | c07e8e5 | 2018-09-18 10:54:03 +0530 | [diff] [blame] | 100 | 'service': frappe.db.get_value("Lab Test Template", rx_obj.lab_test_code, "item")}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 101 | |
| 102 | procedures = frappe.get_list("Clinical Procedure", {'patient': patient.name, 'invoiced': False}) |
| 103 | if procedures: |
| 104 | for procedure in procedures: |
| 105 | procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name']) |
| 106 | if not procedure_obj.appointment: |
| 107 | if procedure_obj.procedure_template and (frappe.db.get_value("Clinical Procedure Template", procedure_obj.procedure_template, "is_billable") == 1): |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 108 | item_to_invoice.append({'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name, |
| 109 | 'service': frappe.db.get_value("Clinical Procedure Template", procedure_obj.procedure_template, "item")}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 110 | |
| 111 | procedure_rxs = frappe.db.sql("""select pp.name from `tabPatient Encounter` et, |
| 112 | `tabProcedure Prescription` pp where et.patient=%s and pp.parent=et.name and |
| 113 | pp.procedure_created=0 and pp.invoiced=0 and pp.appointment_booked=0""", (patient.name)) |
| 114 | if procedure_rxs: |
| 115 | for procedure_rx in procedure_rxs: |
| 116 | rx_obj = frappe.get_doc("Procedure Prescription", procedure_rx[0]) |
| 117 | if frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "is_billable") == 1: |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 118 | item_to_invoice.append({'reference_type': 'Procedure Prescription', 'reference_name': rx_obj.name, |
| 119 | 'service': frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "item")}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 120 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 121 | procedures = frappe.get_list("Clinical Procedure", |
| 122 | {'patient': patient.name, 'invoice_separately_as_consumables': True, 'consumption_invoiced': False, |
| 123 | 'consume_stock': True, 'status': 'Completed'}) |
| 124 | if procedures: |
| 125 | service_item = get_healthcare_service_item('clinical_procedure_consumable_item') |
| 126 | if not service_item: |
| 127 | msg = _(("Please Configure {0} in ").format("Clinical Procedure Consumable Item") \ |
| 128 | + """<b><a href="#Form/Healthcare Settings">Healthcare Settings</a></b>""") |
| 129 | frappe.throw(msg) |
| 130 | for procedure in procedures: |
| 131 | procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name']) |
| 132 | item_to_invoice.append({'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name, |
| 133 | 'service': service_item, 'rate': procedure_obj.consumable_total_amount, 'description': procedure_obj.consumption_details}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 134 | |
Jamsheer | 54ae74e | 2018-07-13 21:10:14 +0530 | [diff] [blame] | 135 | inpatient_services = frappe.db.sql("""select io.name, io.parent from `tabInpatient Record` ip, |
| 136 | `tabInpatient Occupancy` io where ip.patient=%s and io.parent=ip.name and |
| 137 | io.left=1 and io.invoiced=0""", (patient.name)) |
| 138 | if inpatient_services: |
| 139 | for inpatient_service in inpatient_services: |
| 140 | inpatient_occupancy = frappe.get_doc("Inpatient Occupancy", inpatient_service[0]) |
| 141 | service_unit_type = frappe.get_doc("Healthcare Service Unit Type", frappe.db.get_value("Healthcare Service Unit", inpatient_occupancy.service_unit, "service_unit_type")) |
| 142 | if service_unit_type and service_unit_type.is_billable == 1: |
Jamsheer | 25dda3a | 2018-07-30 14:50:16 +0530 | [diff] [blame] | 143 | hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in) |
| 144 | qty = 0.5 |
| 145 | if hours_occupied > 0: |
| 146 | actual_qty = hours_occupied / service_unit_type.no_of_hours |
| 147 | floor = math.floor(actual_qty) |
| 148 | decimal_part = actual_qty - floor |
| 149 | if decimal_part > 0.5: |
| 150 | qty = rounded(floor + 1, 1) |
| 151 | elif decimal_part < 0.5 and decimal_part > 0: |
| 152 | qty = rounded(floor + 0.5, 1) |
| 153 | if qty <= 0: |
| 154 | qty = 0.5 |
Jamsheer | 54ae74e | 2018-07-13 21:10:14 +0530 | [diff] [blame] | 155 | item_to_invoice.append({'reference_type': 'Inpatient Occupancy', 'reference_name': inpatient_occupancy.name, |
| 156 | 'service': service_unit_type.item, 'qty': qty}) |
| 157 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 158 | return item_to_invoice |
| 159 | else: |
| 160 | frappe.throw(_("The Patient {0} do not have customer refrence to invoice").format(patient.name)) |
| 161 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 162 | def service_item_and_practitioner_charge(doc): |
| 163 | is_ip = doc_is_ip(doc) |
| 164 | if is_ip: |
Jamsheer | ee5f9c7 | 2018-07-30 12:42:06 +0530 | [diff] [blame] | 165 | service_item = get_practitioner_service_item(doc.practitioner, "inpatient_visit_charge_item") |
| 166 | if not service_item: |
| 167 | service_item = get_healthcare_service_item("inpatient_visit_charge_item") |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 168 | else: |
Jamsheer | ee5f9c7 | 2018-07-30 12:42:06 +0530 | [diff] [blame] | 169 | service_item = get_practitioner_service_item(doc.practitioner, "op_consulting_charge_item") |
| 170 | if not service_item: |
| 171 | service_item = get_healthcare_service_item("op_consulting_charge_item") |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 172 | if not service_item: |
| 173 | throw_config_service_item(is_ip) |
| 174 | |
| 175 | practitioner_charge = get_practitioner_charge(doc.practitioner, is_ip) |
| 176 | if not practitioner_charge: |
| 177 | throw_config_practitioner_charge(is_ip, doc.practitioner) |
| 178 | |
| 179 | return service_item, practitioner_charge |
| 180 | |
| 181 | def throw_config_service_item(is_ip): |
| 182 | service_item_lable = "Out Patient Consulting Charge Item" |
| 183 | if is_ip: |
| 184 | service_item_lable = "Inpatient Visit Charge Item" |
| 185 | |
| 186 | msg = _(("Please Configure {0} in ").format(service_item_lable) \ |
| 187 | + """<b><a href="#Form/Healthcare Settings">Healthcare Settings</a></b>""") |
| 188 | frappe.throw(msg) |
| 189 | |
| 190 | def throw_config_practitioner_charge(is_ip, practitioner): |
| 191 | charge_name = "OP Consulting Charge" |
| 192 | if is_ip: |
| 193 | charge_name = "Inpatient Visit Charge" |
| 194 | |
| 195 | msg = _(("Please Configure {0} for Healthcare Practitioner").format(charge_name) \ |
| 196 | + """ <b><a href="#Form/Healthcare Practitioner/{0}">{0}</a></b>""".format(practitioner)) |
| 197 | frappe.throw(msg) |
| 198 | |
Jamsheer | ee5f9c7 | 2018-07-30 12:42:06 +0530 | [diff] [blame] | 199 | def get_practitioner_service_item(practitioner, service_item_field): |
| 200 | return frappe.db.get_value("Healthcare Practitioner", practitioner, service_item_field) |
| 201 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 202 | def get_healthcare_service_item(service_item_field): |
| 203 | return frappe.db.get_value("Healthcare Settings", None, service_item_field) |
| 204 | |
| 205 | def doc_is_ip(doc): |
| 206 | is_ip = False |
| 207 | if doc.inpatient_record: |
| 208 | is_ip = True |
| 209 | return is_ip |
| 210 | |
| 211 | def get_practitioner_charge(practitioner, is_ip): |
| 212 | if is_ip: |
| 213 | practitioner_charge = frappe.db.get_value("Healthcare Practitioner", practitioner, "inpatient_visit_charge") |
| 214 | else: |
| 215 | practitioner_charge = frappe.db.get_value("Healthcare Practitioner", practitioner, "op_consulting_charge") |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 216 | if practitioner_charge: |
| 217 | return practitioner_charge |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 218 | return False |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 219 | |
| 220 | def manage_invoice_submit_cancel(doc, method): |
| 221 | if doc.items: |
| 222 | for item in doc.items: |
Nabin Hait | 2ce2d2a | 2018-09-10 13:16:14 +0530 | [diff] [blame] | 223 | if item.get("reference_dt") and item.get("reference_dn"): |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 224 | if frappe.get_meta(item.reference_dt).has_field("invoiced"): |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 225 | set_invoiced(item, method, doc.name) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 226 | |
Jamsheer | 0ae100b | 2018-08-01 14:29:43 +0530 | [diff] [blame] | 227 | if method=="on_submit" and frappe.db.get_value("Healthcare Settings", None, "create_test_on_si_submit") == '1': |
| 228 | create_multiple("Sales Invoice", doc.name) |
| 229 | |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 230 | def set_invoiced(item, method, ref_invoice=None): |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 231 | invoiced = False |
| 232 | if(method=="on_submit"): |
| 233 | validate_invoiced_on_submit(item) |
| 234 | invoiced = True |
| 235 | |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 236 | if item.reference_dt == 'Clinical Procedure': |
| 237 | if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code: |
| 238 | frappe.db.set_value(item.reference_dt, item.reference_dn, "consumption_invoiced", invoiced) |
| 239 | else: |
| 240 | frappe.db.set_value(item.reference_dt, item.reference_dn, "invoiced", invoiced) |
| 241 | else: |
| 242 | frappe.db.set_value(item.reference_dt, item.reference_dn, "invoiced", invoiced) |
| 243 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 244 | if item.reference_dt == 'Patient Appointment': |
| 245 | if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'): |
| 246 | dt_from_appointment = "Clinical Procedure" |
| 247 | else: |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 248 | manage_fee_validity(item.reference_dn, method, ref_invoice) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 249 | dt_from_appointment = "Patient Encounter" |
| 250 | manage_doc_for_appoitnment(dt_from_appointment, item.reference_dn, invoiced) |
| 251 | |
| 252 | elif item.reference_dt == 'Lab Prescription': |
Jamsheer | c07e8e5 | 2018-09-18 10:54:03 +0530 | [diff] [blame] | 253 | 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] | 254 | |
| 255 | elif item.reference_dt == 'Procedure Prescription': |
| 256 | manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, "Clinical Procedure", "procedure_created") |
| 257 | |
| 258 | def validate_invoiced_on_submit(item): |
Jamsheer | 8da6f4e | 2018-07-26 21:03:17 +0530 | [diff] [blame] | 259 | if item.reference_dt == 'Clinical Procedure' and get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code: |
| 260 | is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, "consumption_invoiced") |
| 261 | else: |
| 262 | is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, "invoiced") |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 263 | if is_invoiced == 1: |
| 264 | frappe.throw(_("The item referenced by {0} - {1} is already invoiced"\ |
| 265 | ).format(item.reference_dt, item.reference_dn)) |
| 266 | |
| 267 | def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field): |
| 268 | created = frappe.db.get_value(ref_dt, ref_dn, created_check_field) |
| 269 | if created == 1: |
| 270 | # Fetch the doc created for the prescription |
Jamsheer | eafb046 | 2018-07-25 13:15:12 +0530 | [diff] [blame] | 271 | doc_created = frappe.db.get_value(dt, {'prescription': ref_dn}) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 272 | frappe.db.set_value(dt, doc_created, 'invoiced', invoiced) |
| 273 | |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 274 | def validity_exists(practitioner, patient): |
| 275 | return frappe.db.exists({ |
| 276 | "doctype": "Fee Validity", |
| 277 | "practitioner": practitioner, |
| 278 | "patient": patient}) |
| 279 | |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 280 | def manage_fee_validity(appointment_name, method, ref_invoice=None): |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 281 | appointment_doc = frappe.get_doc("Patient Appointment", appointment_name) |
| 282 | validity_exist = validity_exists(appointment_doc.practitioner, appointment_doc.patient) |
| 283 | do_not_update = False |
| 284 | visited = 0 |
| 285 | if validity_exist: |
| 286 | fee_validity = frappe.get_doc("Fee Validity", validity_exist[0][0]) |
| 287 | # Check if the validity is valid |
| 288 | if (fee_validity.valid_till >= appointment_doc.appointment_date): |
| 289 | if (method == "on_cancel" and appointment_doc.status != "Closed"): |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 290 | if ref_invoice == fee_validity.ref_invoice: |
| 291 | visited = fee_validity.visited - 1 |
| 292 | if visited < 0: |
| 293 | visited = 0 |
| 294 | frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 295 | do_not_update = True |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 296 | elif (method == "on_submit" and fee_validity.visited < fee_validity.max_visit): |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 297 | visited = fee_validity.visited + 1 |
| 298 | frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited) |
| 299 | do_not_update = True |
| 300 | else: |
| 301 | do_not_update = False |
| 302 | |
| 303 | if not do_not_update: |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 304 | fee_validity = update_fee_validity(fee_validity, appointment_doc.appointment_date, ref_invoice) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 305 | visited = fee_validity.visited |
| 306 | else: |
Jamsheer | 146683b | 2018-07-25 11:30:30 +0530 | [diff] [blame] | 307 | fee_validity = create_fee_validity(appointment_doc.practitioner, appointment_doc.patient, appointment_doc.appointment_date, ref_invoice) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 308 | visited = fee_validity.visited |
| 309 | |
| 310 | # Mark All Patient Appointment invoiced = True in the validity range do not cross the max visit |
| 311 | if (method == "on_cancel"): |
| 312 | invoiced = True |
| 313 | else: |
| 314 | invoiced = False |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 315 | |
| 316 | patient_appointments = appointments_valid_in_fee_validity(appointment_doc, invoiced) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 317 | if patient_appointments and fee_validity: |
| 318 | visit = visited |
| 319 | for appointment in patient_appointments: |
| 320 | if (method == "on_cancel" and appointment.status != "Closed"): |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 321 | if ref_invoice == fee_validity.ref_invoice: |
| 322 | visited = visited - 1 |
| 323 | if visited < 0: |
| 324 | visited = 0 |
| 325 | frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 326 | frappe.db.set_value("Patient Appointment", appointment.name, "invoiced", False) |
| 327 | manage_doc_for_appoitnment("Patient Encounter", appointment.name, False) |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 328 | elif method == "on_submit" and int(fee_validity.max_visit) > visit: |
| 329 | if ref_invoice == fee_validity.ref_invoice: |
| 330 | visited = visited + 1 |
| 331 | frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 332 | frappe.db.set_value("Patient Appointment", appointment.name, "invoiced", True) |
| 333 | manage_doc_for_appoitnment("Patient Encounter", appointment.name, True) |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 334 | if ref_invoice == fee_validity.ref_invoice: |
| 335 | visit = visit + 1 |
| 336 | |
| 337 | if method == "on_cancel": |
| 338 | ref_invoice_in_fee_validity = frappe.db.get_value("Fee Validity", fee_validity.name, 'ref_invoice') |
| 339 | if ref_invoice_in_fee_validity == ref_invoice: |
| 340 | frappe.delete_doc("Fee Validity", fee_validity.name) |
| 341 | |
| 342 | def appointments_valid_in_fee_validity(appointment, invoiced): |
| 343 | valid_days = frappe.db.get_value("Healthcare Settings", None, "valid_days") |
| 344 | max_visit = frappe.db.get_value("Healthcare Settings", None, "max_visit") |
Jamsheer | 14c6ab0 | 2018-10-10 14:44:36 +0530 | [diff] [blame] | 345 | if int(max_visit) < 1: |
| 346 | max_visit = 1 |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 347 | valid_days_date = add_days(getdate(appointment.appointment_date), int(valid_days)) |
| 348 | return frappe.get_list("Patient Appointment",{'patient': appointment.patient, 'invoiced': invoiced, |
| 349 | 'appointment_date':("<=", valid_days_date), 'appointment_date':(">=", getdate(appointment.appointment_date)), |
| 350 | 'practitioner': appointment.practitioner}, order_by="appointment_date", limit=int(max_visit)-1) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 351 | |
| 352 | def manage_doc_for_appoitnment(dt_from_appointment, appointment, invoiced): |
| 353 | dn_from_appointment = frappe.db.exists( |
| 354 | dt_from_appointment, |
| 355 | { |
| 356 | "appointment": appointment |
| 357 | } |
| 358 | ) |
| 359 | if dn_from_appointment: |
| 360 | frappe.db.set_value(dt_from_appointment, dn_from_appointment, "invoiced", invoiced) |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 361 | |
| 362 | @frappe.whitelist() |
| 363 | def get_drugs_to_invoice(encounter): |
| 364 | encounter = frappe.get_doc("Patient Encounter", encounter) |
| 365 | if encounter: |
| 366 | patient = frappe.get_doc("Patient", encounter.patient) |
| 367 | if patient and patient.customer: |
| 368 | item_to_invoice = [] |
| 369 | for drug_line in encounter.drug_prescription: |
| 370 | if drug_line.drug_code: |
| 371 | qty = 1 |
| 372 | if frappe.db.get_value("Item", drug_line.drug_code, "stock_uom") == "Nos": |
| 373 | qty = drug_line.get_quantity() |
Jamsheer | c07e8e5 | 2018-09-18 10:54:03 +0530 | [diff] [blame] | 374 | description = False |
| 375 | if drug_line.dosage: |
| 376 | description = drug_line.dosage |
| 377 | if description and drug_line.period: |
| 378 | description += " for "+drug_line.period |
| 379 | if not description: |
| 380 | description = "" |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 381 | item_to_invoice.append({'drug_code': drug_line.drug_code, 'quantity': qty, |
Jamsheer | c07e8e5 | 2018-09-18 10:54:03 +0530 | [diff] [blame] | 382 | 'description': description}) |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 383 | return item_to_invoice |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 384 | |
| 385 | @frappe.whitelist() |
| 386 | def get_children(doctype, parent, company, is_root=False): |
| 387 | parent_fieldname = 'parent_' + doctype.lower().replace(' ', '_') |
| 388 | fields = [ |
| 389 | 'name as value', |
| 390 | 'is_group as expandable', |
| 391 | 'lft', |
| 392 | 'rgt' |
| 393 | ] |
| 394 | # fields = [ 'name', 'is_group', 'lft', 'rgt' ] |
| 395 | filters = [['ifnull(`{0}`,"")'.format(parent_fieldname), '=', '' if is_root else parent]] |
| 396 | |
| 397 | if is_root: |
| 398 | fields += ['service_unit_type'] if doctype == 'Healthcare Service Unit' else [] |
| 399 | filters.append(['company', '=', company]) |
| 400 | |
| 401 | else: |
| 402 | fields += ['service_unit_type', 'allow_appointments', 'inpatient_occupancy', 'occupancy_status'] if doctype == 'Healthcare Service Unit' else [] |
| 403 | fields += [parent_fieldname + ' as parent'] |
| 404 | |
| 405 | hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters) |
| 406 | |
| 407 | if doctype == 'Healthcare Service Unit': |
| 408 | for each in hc_service_units: |
| 409 | occupancy_msg = "" |
| 410 | if each['expandable'] == 1: |
| 411 | occupied = False |
| 412 | vacant = False |
| 413 | child_list = frappe.db.sql(""" |
| 414 | select name, occupancy_status from `tabHealthcare Service Unit` |
| 415 | where inpatient_occupancy = 1 and |
| 416 | lft > %s and rgt < %s""", |
| 417 | (each['lft'], each['rgt'])) |
| 418 | for child in child_list: |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 419 | if not occupied: |
| 420 | occupied = 0 |
| 421 | if child[1] == "Occupied": |
| 422 | occupied += 1 |
| 423 | if not vacant: |
| 424 | vacant = 0 |
| 425 | if child[1] == "Vacant": |
| 426 | vacant += 1 |
| 427 | if vacant and occupied: |
| 428 | occupancy_total = vacant+occupied |
| 429 | occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total) |
| 430 | each["occupied_out_of_vacant"] = occupancy_msg |
| 431 | return hc_service_units |
Jamsheer | 5073ac4 | 2019-07-12 12:28:34 +0530 | [diff] [blame] | 432 | |
| 433 | @frappe.whitelist() |
| 434 | def get_patient_vitals(patient, from_date=None, to_date=None): |
| 435 | if not patient: return |
| 436 | vitals = frappe.db.sql("""select * from `tabVital Signs` where \ |
| 437 | docstatus=1 and patient=%s order by signs_date, signs_time""", \ |
| 438 | (patient), as_dict=1) |
| 439 | if vitals and vitals[0]: |
| 440 | return vitals |
| 441 | else: |
| 442 | return False |
| 443 | |
| 444 | @frappe.whitelist() |
| 445 | def render_docs_as_html(docs): |
| 446 | # docs key value pair {doctype: docname} |
| 447 | docs_html = "<div class='col-md-12 col-sm-12 text-muted'>" |
| 448 | for doc in docs: |
| 449 | docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + "<br/>" |
| 450 | return {'html': docs_html} |
| 451 | |
| 452 | @frappe.whitelist() |
| 453 | def render_doc_as_html(doctype, docname, exclude_fields = []): |
| 454 | #render document as html, three column layout will break |
| 455 | doc = frappe.get_doc(doctype, docname) |
| 456 | meta = frappe.get_meta(doctype) |
| 457 | doc_html = "<div class='col-md-12 col-sm-12'>" |
| 458 | section_html = "" |
| 459 | section_label = "" |
| 460 | html = "" |
| 461 | sec_on = False |
| 462 | col_on = 0 |
| 463 | has_data = False |
| 464 | for df in meta.fields: |
| 465 | #on section break append append previous section and html to doc html |
| 466 | if df.fieldtype == "Section Break": |
| 467 | if has_data and col_on and sec_on: |
| 468 | doc_html += section_html + html + "</div>" |
| 469 | elif has_data and not col_on and sec_on: |
| 470 | doc_html += "<div class='col-md-12 col-sm-12'\ |
| 471 | ><div class='col-md-12 col-sm-12'>" \ |
| 472 | + section_html + html +"</div></div>" |
| 473 | while col_on: |
| 474 | doc_html += "</div>" |
| 475 | col_on -= 1 |
| 476 | sec_on = True |
| 477 | has_data= False |
| 478 | col_on = 0 |
| 479 | section_html = "" |
| 480 | html = "" |
| 481 | if df.label: |
| 482 | section_label = df.label |
| 483 | continue |
| 484 | #on column break append html to section html or doc html |
| 485 | if df.fieldtype == "Column Break": |
| 486 | if sec_on and has_data: |
| 487 | section_html += "<div class='col-md-12 col-sm-12'\ |
| 488 | ><div class='col-md-6 col\ |
| 489 | -sm-6'><b>" + section_label + "</b>" + html + "</div><div \ |
| 490 | class='col-md-6 col-sm-6'>" |
| 491 | elif has_data: |
| 492 | doc_html += "<div class='col-md-12 col-sm-12'><div class='col-m\ |
| 493 | d-6 col-sm-6'>" + html + "</div><div class='col-md-6 col-sm-6'>" |
| 494 | elif sec_on and not col_on: |
| 495 | section_html += "<div class='col-md-6 col-sm-6'>" |
| 496 | html = "" |
| 497 | col_on += 1 |
| 498 | if df.label: |
| 499 | html += '<br>' + df.label |
| 500 | continue |
| 501 | #on table iterate in items and create table based on in_list_view, append to section html or doc html |
| 502 | if df.fieldtype == "Table": |
| 503 | items = doc.get(df.fieldname) |
| 504 | if not items: continue |
| 505 | child_meta = frappe.get_meta(df.options) |
| 506 | if not has_data : has_data = True |
| 507 | table_head = "" |
| 508 | table_row = "" |
| 509 | create_head = True |
| 510 | for item in items: |
| 511 | table_row += '<tr>' |
| 512 | for cdf in child_meta.fields: |
| 513 | if cdf.in_list_view: |
| 514 | if create_head: |
| 515 | table_head += '<th>' + cdf.label + '</th>' |
| 516 | if item.get(cdf.fieldname): |
| 517 | table_row += '<td>' + str(item.get(cdf.fieldname)) \ |
| 518 | + '</td>' |
| 519 | else: |
| 520 | table_row += '<td></td>' |
| 521 | create_head = False |
| 522 | table_row += '</tr>' |
| 523 | if sec_on: |
| 524 | section_html += '<table class="table table-condensed \ |
| 525 | bordered">' + table_head + table_row + '</table>' |
| 526 | else: |
| 527 | html += '<table class="table table-condensed table-bordered">' \ |
| 528 | + table_head + table_row + '</table>' |
| 529 | continue |
| 530 | #on other field types add label and value to html |
| 531 | if not df.hidden and not df.print_hide and doc.get(df.fieldname) and df.fieldname not in exclude_fields: |
| 532 | html += "<br>{0} : {1}".format(df.label or df.fieldname, \ |
| 533 | doc.get(df.fieldname)) |
| 534 | if not has_data : has_data = True |
| 535 | if sec_on and col_on and has_data: |
| 536 | doc_html += section_html + html + "</div></div>" |
| 537 | elif sec_on and not col_on and has_data: |
| 538 | doc_html += "<div class='col-md-12 col-sm-12'\ |
| 539 | ><div class='col-md-12 col-sm-12'>" \ |
| 540 | + section_html + html +"</div></div>" |
| 541 | if doc_html: |
| 542 | 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>" |
| 543 | |
| 544 | return {'html': doc_html} |