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 | |
| 85 | lab_tests = frappe.get_list("Lab Test", {'patient': patient.name, 'invoiced': False}) |
| 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 | 34e5618 | 2018-08-09 15:51:37 +0530 | [diff] [blame] | 94 | where et.patient=%s and lp.parent=et.name and lp.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]) |
| 98 | if rx_obj.test_code and (frappe.db.get_value("Lab Test Template", rx_obj.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, |
| 100 | 'service': frappe.db.get_value("Lab Test Template", rx_obj.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: |
| 223 | if item.reference_dt and item.reference_dn: |
| 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': |
| 253 | manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, "Lab Test", "test_created") |
| 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 | |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 310 | print "do_not_update: ", do_not_update |
| 311 | print "visited: ", visited |
| 312 | |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 313 | # Mark All Patient Appointment invoiced = True in the validity range do not cross the max visit |
| 314 | if (method == "on_cancel"): |
| 315 | invoiced = True |
| 316 | else: |
| 317 | invoiced = False |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 318 | |
| 319 | patient_appointments = appointments_valid_in_fee_validity(appointment_doc, invoiced) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 320 | if patient_appointments and fee_validity: |
| 321 | visit = visited |
| 322 | for appointment in patient_appointments: |
| 323 | if (method == "on_cancel" and appointment.status != "Closed"): |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 324 | if ref_invoice == fee_validity.ref_invoice: |
| 325 | visited = visited - 1 |
| 326 | if visited < 0: |
| 327 | visited = 0 |
| 328 | frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 329 | frappe.db.set_value("Patient Appointment", appointment.name, "invoiced", False) |
| 330 | manage_doc_for_appoitnment("Patient Encounter", appointment.name, False) |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 331 | elif method == "on_submit" and int(fee_validity.max_visit) > visit: |
| 332 | if ref_invoice == fee_validity.ref_invoice: |
| 333 | visited = visited + 1 |
| 334 | frappe.db.set_value("Fee Validity", fee_validity.name, "visited", visited) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 335 | frappe.db.set_value("Patient Appointment", appointment.name, "invoiced", True) |
| 336 | manage_doc_for_appoitnment("Patient Encounter", appointment.name, True) |
Jamsheer | 363deb6 | 2018-08-04 12:56:36 +0530 | [diff] [blame] | 337 | if ref_invoice == fee_validity.ref_invoice: |
| 338 | visit = visit + 1 |
| 339 | |
| 340 | if method == "on_cancel": |
| 341 | ref_invoice_in_fee_validity = frappe.db.get_value("Fee Validity", fee_validity.name, 'ref_invoice') |
| 342 | if ref_invoice_in_fee_validity == ref_invoice: |
| 343 | frappe.delete_doc("Fee Validity", fee_validity.name) |
| 344 | |
| 345 | def appointments_valid_in_fee_validity(appointment, invoiced): |
| 346 | valid_days = frappe.db.get_value("Healthcare Settings", None, "valid_days") |
| 347 | max_visit = frappe.db.get_value("Healthcare Settings", None, "max_visit") |
| 348 | valid_days_date = add_days(getdate(appointment.appointment_date), int(valid_days)) |
| 349 | return frappe.get_list("Patient Appointment",{'patient': appointment.patient, 'invoiced': invoiced, |
| 350 | 'appointment_date':("<=", valid_days_date), 'appointment_date':(">=", getdate(appointment.appointment_date)), |
| 351 | 'practitioner': appointment.practitioner}, order_by="appointment_date", limit=int(max_visit)-1) |
Jamsheer | ba11972 | 2018-07-06 15:58:13 +0530 | [diff] [blame] | 352 | |
| 353 | def manage_doc_for_appoitnment(dt_from_appointment, appointment, invoiced): |
| 354 | dn_from_appointment = frappe.db.exists( |
| 355 | dt_from_appointment, |
| 356 | { |
| 357 | "appointment": appointment |
| 358 | } |
| 359 | ) |
| 360 | if dn_from_appointment: |
| 361 | frappe.db.set_value(dt_from_appointment, dn_from_appointment, "invoiced", invoiced) |
Jamsheer | e82f27a | 2018-07-30 11:28:37 +0530 | [diff] [blame] | 362 | |
| 363 | @frappe.whitelist() |
| 364 | def get_drugs_to_invoice(encounter): |
| 365 | encounter = frappe.get_doc("Patient Encounter", encounter) |
| 366 | if encounter: |
| 367 | patient = frappe.get_doc("Patient", encounter.patient) |
| 368 | if patient and patient.customer: |
| 369 | item_to_invoice = [] |
| 370 | for drug_line in encounter.drug_prescription: |
| 371 | if drug_line.drug_code: |
| 372 | qty = 1 |
| 373 | if frappe.db.get_value("Item", drug_line.drug_code, "stock_uom") == "Nos": |
| 374 | qty = drug_line.get_quantity() |
| 375 | item_to_invoice.append({'drug_code': drug_line.drug_code, 'quantity': qty, |
| 376 | 'description': drug_line.dosage+" for "+drug_line.period}) |
| 377 | return item_to_invoice |
Jamsheer | 4371c7e | 2018-08-01 18:40:05 +0530 | [diff] [blame] | 378 | |
| 379 | @frappe.whitelist() |
| 380 | def get_children(doctype, parent, company, is_root=False): |
| 381 | parent_fieldname = 'parent_' + doctype.lower().replace(' ', '_') |
| 382 | fields = [ |
| 383 | 'name as value', |
| 384 | 'is_group as expandable', |
| 385 | 'lft', |
| 386 | 'rgt' |
| 387 | ] |
| 388 | # fields = [ 'name', 'is_group', 'lft', 'rgt' ] |
| 389 | filters = [['ifnull(`{0}`,"")'.format(parent_fieldname), '=', '' if is_root else parent]] |
| 390 | |
| 391 | if is_root: |
| 392 | fields += ['service_unit_type'] if doctype == 'Healthcare Service Unit' else [] |
| 393 | filters.append(['company', '=', company]) |
| 394 | |
| 395 | else: |
| 396 | fields += ['service_unit_type', 'allow_appointments', 'inpatient_occupancy', 'occupancy_status'] if doctype == 'Healthcare Service Unit' else [] |
| 397 | fields += [parent_fieldname + ' as parent'] |
| 398 | |
| 399 | hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters) |
| 400 | |
| 401 | if doctype == 'Healthcare Service Unit': |
| 402 | for each in hc_service_units: |
| 403 | occupancy_msg = "" |
| 404 | if each['expandable'] == 1: |
| 405 | occupied = False |
| 406 | vacant = False |
| 407 | child_list = frappe.db.sql(""" |
| 408 | select name, occupancy_status from `tabHealthcare Service Unit` |
| 409 | where inpatient_occupancy = 1 and |
| 410 | lft > %s and rgt < %s""", |
| 411 | (each['lft'], each['rgt'])) |
| 412 | for child in child_list: |
| 413 | print child[0], child[1] |
| 414 | if not occupied: |
| 415 | occupied = 0 |
| 416 | if child[1] == "Occupied": |
| 417 | occupied += 1 |
| 418 | if not vacant: |
| 419 | vacant = 0 |
| 420 | if child[1] == "Vacant": |
| 421 | vacant += 1 |
| 422 | if vacant and occupied: |
| 423 | occupancy_total = vacant+occupied |
| 424 | occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total) |
| 425 | each["occupied_out_of_vacant"] = occupancy_msg |
| 426 | return hc_service_units |