def invoice_appointment(appointment_doc): if not appointment_doc.name: return False sales_invoice = frappe.new_doc("Sales Invoice") sales_invoice.customer = frappe.get_value("Patient", appointment_doc.patient, "customer") sales_invoice.appointment = appointment_doc.name sales_invoice.due_date = getdate() sales_invoice.is_pos = True sales_invoice.company = appointment_doc.company sales_invoice.debit_to = get_receivable_account(appointment_doc.company) item_line = sales_invoice.append("items") service_item, practitioner_charge = service_item_and_practitioner_charge(appointment_doc) item_line.item_code = service_item item_line.description = "Consulting Charges: " + appointment_doc.practitioner item_line.income_account = get_income_account(appointment_doc.practitioner, appointment_doc.company) item_line.rate = practitioner_charge item_line.amount = practitioner_charge item_line.qty = 1 item_line.reference_dt = "Patient Appointment" item_line.reference_dn = appointment_doc.name payments_line = sales_invoice.append("payments") payments_line.mode_of_payment = appointment_doc.mode_of_payment payments_line.amount = appointment_doc.paid_amount sales_invoice.set_missing_values(for_validate = True) sales_invoice.save(ignore_permissions=True) sales_invoice.submit() frappe.msgprint(_("Sales Invoice {0} created as paid".format(sales_invoice.name)), alert=True)
def get_encounters_to_invoice(patient, company): encounters_to_invoice = [] encounters = frappe.get_list('Patient Encounter', fields=['*'], filters={ 'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1 }) if encounters: for encounter in encounters: if not encounter.appointment: practitioner_charge = 0 income_account = None service_item = None if encounter.practitioner: service_item, practitioner_charge = get_service_item_and_practitioner_charge( encounter) income_account = get_income_account( encounter.practitioner, encounter.company) encounters_to_invoice.append({ 'reference_type': 'Patient Encounter', 'reference_name': encounter.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account }) return encounters_to_invoice
def create_sales_invoice(): patient = create_patient() medical_department = create_medical_department() insulin_resistance_template = create_lab_test_template() blood_test_template = create_blood_test_template(medical_department) sales_invoice = frappe.new_doc('Sales Invoice') sales_invoice.patient = patient sales_invoice.customer = frappe.db.get_value('Patient', patient, 'customer') sales_invoice.due_date = getdate() sales_invoice.company = '_Test Company' sales_invoice.debit_to = get_receivable_account('_Test Company') tests = [insulin_resistance_template, blood_test_template] for entry in tests: sales_invoice.append( 'items', { 'item_code': entry.item, 'item_name': entry.lab_test_name, 'description': entry.lab_test_description, 'qty': 1, 'uom': 'Nos', 'conversion_factor': 1, 'income_account': get_income_account(None, '_Test Company'), 'rate': entry.lab_test_rate, 'amount': entry.lab_test_rate }) sales_invoice.set_missing_values() sales_invoice.submit() return sales_invoice
def get_appointments_to_invoice(patient, company): appointments_to_invoice = [] patient_appointments = frappe.get_list( "Patient Appointment", fields="*", filters={ "patient": patient.name, "company": company, "invoiced": 0, "status": ["not in", "Cancelled"], }, order_by="appointment_date", ) for appointment in patient_appointments: # Procedure Appointments if appointment.procedure_template: if frappe.db.get_value("Clinical Procedure Template", appointment.procedure_template, "is_billable"): appointments_to_invoice.append({ "reference_type": "Patient Appointment", "reference_name": appointment.name, "service": appointment.procedure_template, }) # Consultation Appointments, should check fee validity else: if frappe.db.get_single_value( "Healthcare Settings", "enable_free_follow_ups") and frappe.db.exists( "Fee Validity Reference", {"appointment": appointment.name}): continue # Skip invoicing, fee validty present practitioner_charge = 0 income_account = None service_item = None if appointment.practitioner: details = get_service_item_and_practitioner_charge(appointment) service_item = details.get("service_item") practitioner_charge = details.get("practitioner_charge") income_account = get_income_account(appointment.practitioner, appointment.company) appointments_to_invoice.append({ "reference_type": "Patient Appointment", "reference_name": appointment.name, "service": service_item, "rate": practitioner_charge, "income_account": income_account, }) return appointments_to_invoice
def get_therapy_item(therapy, item): item.item_code = frappe.db.get_value('Therapy Type', therapy.therapy_type, 'item') item.description = _('Therapy Session Charges: {0}').format(therapy.practitioner) item.income_account = get_income_account(therapy.practitioner, therapy.company) item.cost_center = frappe.get_cached_value('Company', therapy.company, 'cost_center') item.rate = therapy.rate item.amount = therapy.rate item.qty = 1 item.reference_dt = 'Therapy Session' item.reference_dn = therapy.name return item
def get_appointment_item(appointment_doc, item): service_item, practitioner_charge = get_service_item_and_practitioner_charge(appointment_doc) item.item_code = service_item item.description = _('Consulting Charges: {0}').format(appointment_doc.practitioner) item.income_account = get_income_account(appointment_doc.practitioner, appointment_doc.company) item.cost_center = frappe.get_cached_value('Company', appointment_doc.company, 'cost_center') item.rate = practitioner_charge item.amount = practitioner_charge item.qty = 1 item.reference_dt = 'Patient Appointment' item.reference_dn = appointment_doc.name return item
def create_invoice_items(physician, company, invoice): item_line = invoice.append("items") item_line.item_name = "Consulting Charges" item_line.description = "Consulting Charges: " + physician item_line.qty = 1 item_line.uom = "Nos" item_line.conversion_factor = 1 item_line.income_account = get_income_account(physician, company) op_consulting_charge = frappe.db.get_value("Physician", physician, "op_consulting_charge") if op_consulting_charge: item_line.rate = op_consulting_charge item_line.amount = op_consulting_charge return invoice
def create_invoice_items(physician, invoice, company): item_line = invoice.append("items") item_line.item_name = "Consulting Charges" item_line.description = "Consulting Charges: " + physician item_line.qty = 1 item_line.uom = "Nos" item_line.conversion_factor = 1 item_line.income_account = get_income_account(physician, company) op_consulting_charge = frappe.get_value("Physician", physician, "op_consulting_charge") if op_consulting_charge: item_line.rate = op_consulting_charge item_line.amount = op_consulting_charge return invoice
def get_appointments_to_invoice(patient, company): appointments_to_invoice = [] patient_appointments = frappe.get_list('Patient Appointment', fields='*', filters={ 'patient': patient.name, 'company': company, 'invoiced': 0, 'status': ['not in', 'Cancelled'] }, order_by='appointment_date') for appointment in patient_appointments: # Procedure Appointments if appointment.procedure_template: if frappe.db.get_value('Clinical Procedure Template', appointment.procedure_template, 'is_billable'): appointments_to_invoice.append({ 'reference_type': 'Patient Appointment', 'reference_name': appointment.name, 'service': appointment.procedure_template }) # Consultation Appointments, should check fee validity else: if frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups') and \ frappe.db.exists('Fee Validity Reference', {'appointment': appointment.name}): continue # Skip invoicing, fee validty present practitioner_charge = 0 income_account = None service_item = None if appointment.practitioner: details = get_service_item_and_practitioner_charge(appointment) service_item = details.get('service_item') practitioner_charge = details.get('practitioner_charge') income_account = get_income_account(appointment.practitioner, appointment.company) appointments_to_invoice.append({ 'reference_type': 'Patient Appointment', 'reference_name': appointment.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account }) return appointments_to_invoice
def create_invoice_items(practitioner, invoice, company): item_line = invoice.append("items") item_line.item_name = "Consulting Charges" item_line.description = "Consulting Charges: " + practitioner item_line.qty = 1 item_line.uom = "Nos" item_line.conversion_factor = 1 item_line.income_account = get_income_account(practitioner, company) op_consulting_charge = frappe.get_value("Healthcare Practitioner", practitioner, "op_consulting_charge") if op_consulting_charge: item_line.rate = op_consulting_charge item_line.amount = op_consulting_charge return invoice
def get_appointment_item(appointment_doc, item): details = get_service_item_and_practitioner_charge(appointment_doc) charge = appointment_doc.paid_amount or details.get("practitioner_charge") item.item_code = details.get("service_item") item.description = _("Consulting Charges: {0}").format( appointment_doc.practitioner) item.income_account = get_income_account(appointment_doc.practitioner, appointment_doc.company) item.cost_center = frappe.get_cached_value("Company", appointment_doc.company, "cost_center") item.rate = charge item.amount = charge item.qty = 1 item.reference_dt = "Patient Appointment" item.reference_dn = appointment_doc.name return item
def make_invoice(patient, company): sales_invoice = frappe.new_doc("Sales Invoice") sales_invoice.customer = frappe.get_value("Patient", patient, "customer") sales_invoice.due_date = getdate() sales_invoice.company = company sales_invoice.is_pos = '0' sales_invoice.debit_to = get_receivable_account(company) item_line = sales_invoice.append("items") item_line.item_name = "Registeration Fee" item_line.description = "Registeration Fee" item_line.qty = 1 item_line.uom = "Nos" item_line.conversion_factor = 1 item_line.income_account = get_income_account(None, company) item_line.rate = frappe.get_value("Healthcare Settings", None, "registration_fee") item_line.amount = item_line.rate sales_invoice.set_missing_values() return sales_invoice
def get_encounters_to_invoice(patient, company): if not isinstance(patient, str): patient = patient.name encounters_to_invoice = [] encounters = frappe.get_list( "Patient Encounter", fields=["*"], filters={ "patient": patient, "company": company, "invoiced": False, "docstatus": 1 }, ) if encounters: for encounter in encounters: if not encounter.appointment: practitioner_charge = 0 income_account = None service_item = None if encounter.practitioner: if encounter.inpatient_record and frappe.db.get_single_value( "Healthcare Settings", "do_not_bill_inpatient_encounters"): continue details = get_service_item_and_practitioner_charge( encounter) service_item = details.get("service_item") practitioner_charge = details.get("practitioner_charge") income_account = get_income_account( encounter.practitioner, encounter.company) encounters_to_invoice.append({ "reference_type": "Patient Encounter", "reference_name": encounter.name, "service": service_item, "rate": practitioner_charge, "income_account": income_account, }) return encounters_to_invoice
def make_invoice(patient, company): uom = frappe.db.exists('UOM', 'Nos') or frappe.db.get_single_value('Stock Settings', 'stock_uom') sales_invoice = frappe.new_doc('Sales Invoice') sales_invoice.customer = frappe.db.get_value('Patient', patient, 'customer') sales_invoice.due_date = getdate() sales_invoice.company = company sales_invoice.is_pos = 0 sales_invoice.debit_to = get_receivable_account(company) item_line = sales_invoice.append('items') item_line.item_name = 'Registeration Fee' item_line.description = 'Registeration Fee' item_line.qty = 1 item_line.uom = uom item_line.conversion_factor = 1 item_line.income_account = get_income_account(None, company) item_line.rate = frappe.db.get_single_value('Healthcare Settings', 'registration_fee') item_line.amount = item_line.rate sales_invoice.set_missing_values() return sales_invoice
def get_encounters_to_invoice(patient, company): if not isinstance(patient, str): patient = patient.name encounters_to_invoice = [] encounters = frappe.get_list('Patient Encounter', fields=['*'], filters={ 'patient': patient, 'company': company, 'invoiced': False, 'docstatus': 1 }) if encounters: for encounter in encounters: if not encounter.appointment: practitioner_charge = 0 income_account = None service_item = None if encounter.practitioner: if encounter.inpatient_record and \ frappe.db.get_single_value('Healthcare Settings', 'do_not_bill_inpatient_encounters'): continue details = get_service_item_and_practitioner_charge( encounter) service_item = details.get('service_item') practitioner_charge = details.get('practitioner_charge') income_account = get_income_account( encounter.practitioner, encounter.company) encounters_to_invoice.append({ 'reference_type': 'Patient Encounter', 'reference_name': encounter.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account }) return encounters_to_invoice
def create_invoice_items(practitioner, company, invoice, procedure_template): item_line = invoice.append("items") if procedure_template: procedure_template_obj = frappe.get_doc("Clinical Procedure Template", procedure_template) item_line.item_code = procedure_template_obj.item_code item_line.item_name = procedure_template_obj.template item_line.description = procedure_template_obj.description else: item_line.item_name = "Consulting Charges" item_line.description = "Consulting Charges: " + practitioner item_line.uom = "Nos" item_line.conversion_factor = 1 item_line.income_account = get_income_account(practitioner, company) op_consulting_charge = frappe.db.get_value("Healthcare Practitioner", practitioner, "op_consulting_charge") if op_consulting_charge: item_line.rate = op_consulting_charge item_line.amount = op_consulting_charge item_line.qty = 1 return invoice
def create_sales_invoice(): patient = create_patient() medical_department = create_medical_department() insulin_resistance_template = create_lab_test_template() blood_test_template = create_blood_test_template(medical_department) sales_invoice = frappe.new_doc("Sales Invoice") sales_invoice.patient = patient sales_invoice.customer = frappe.db.get_value("Patient", patient, "customer") sales_invoice.due_date = getdate() sales_invoice.company = "_Test Company" sales_invoice.currency = "INR" sales_invoice.debit_to = get_receivable_account("_Test Company") tests = [insulin_resistance_template, blood_test_template] for entry in tests: sales_invoice.append( "items", { "item_code": entry.item, "item_name": entry.lab_test_name, "description": entry.lab_test_description, "qty": 1, "uom": "Nos", "conversion_factor": 1, "income_account": get_income_account(None, "_Test Company"), "rate": entry.lab_test_rate, "amount": entry.lab_test_rate, }, ) sales_invoice.set_missing_values() sales_invoice.submit() return sales_invoice
def get_healthcare_services_to_invoice(patient): patient = frappe.get_doc("Patient", patient) if patient: if patient.customer: item_to_invoice = [] patient_appointments = frappe.get_list("Patient Appointment", { 'patient': patient.name, 'invoiced': False }, order_by="appointment_date") if patient_appointments: fee_validity_details = [] valid_days = frappe.db.get_value("Healthcare Settings", None, "valid_days") max_visit = frappe.db.get_value("Healthcare Settings", None, "max_visit") for patient_appointment in patient_appointments: patient_appointment_obj = frappe.get_doc( "Patient Appointment", patient_appointment['name']) if patient_appointment_obj.procedure_template: if frappe.db.get_value( "Clinical Procedure Template", patient_appointment_obj.procedure_template, "is_billable") == 1: item_to_invoice.append({ 'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name, 'service': patient_appointment_obj.procedure_template }) else: practitioner_exist_in_list = False skip_invoice = False if fee_validity_details: for validity in fee_validity_details: if validity[ 'practitioner'] == patient_appointment_obj.practitioner: practitioner_exist_in_list = True if validity[ 'valid_till'] >= patient_appointment_obj.appointment_date: validity[ 'visits'] = validity['visits'] + 1 if int(max_visit) > validity['visits']: skip_invoice = True if not skip_invoice: validity['visits'] = 1 validity[ 'valid_till'] = patient_appointment_obj.appointment_date + datetime.timedelta( days=int(valid_days)) if not practitioner_exist_in_list: valid_till = patient_appointment_obj.appointment_date + datetime.timedelta( days=int(valid_days)) visits = 0 validity_exist = validity_exists( patient_appointment_obj.practitioner, patient_appointment_obj.patient) if validity_exist: fee_validity = frappe.get_doc( "Fee Validity", validity_exist[0][0]) valid_till = fee_validity.valid_till visits = fee_validity.visited fee_validity_details.append({ 'practitioner': patient_appointment_obj.practitioner, 'valid_till': valid_till, 'visits': visits }) if not skip_invoice: practitioner_charge = 0 income_account = None service_item = None if patient_appointment_obj.practitioner: service_item, practitioner_charge = service_item_and_practitioner_charge( patient_appointment_obj) income_account = get_income_account( patient_appointment_obj.practitioner, patient_appointment_obj.company) item_to_invoice.append({ 'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account }) encounters = frappe.get_list("Patient Encounter", { 'patient': patient.name, 'invoiced': False, 'docstatus': 1 }) if encounters: for encounter in encounters: encounter_obj = frappe.get_doc("Patient Encounter", encounter['name']) if not encounter_obj.appointment: practitioner_charge = 0 income_account = None service_item = None if encounter_obj.practitioner: service_item, practitioner_charge = service_item_and_practitioner_charge( encounter_obj) income_account = get_income_account( encounter_obj.practitioner, encounter_obj.company) item_to_invoice.append({ 'reference_type': 'Patient Encounter', 'reference_name': encounter_obj.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account }) lab_tests = frappe.get_list("Lab Test", { 'patient': patient.name, 'invoiced': False }) if lab_tests: for lab_test in lab_tests: lab_test_obj = frappe.get_doc("Lab Test", lab_test['name']) if frappe.db.get_value("Lab Test Template", lab_test_obj.template, "is_billable") == 1: item_to_invoice.append({ 'reference_type': 'Lab Test', 'reference_name': lab_test_obj.name, 'service': frappe.db.get_value("Lab Test Template", lab_test_obj.template, "item") }) lab_rxs = frappe.db.sql( """select lp.name from `tabPatient Encounter` et, `tabLab Prescription` lp where et.patient=%s and lp.parent=et.name and lp.lab_test_created=0 and lp.invoiced=0""", (patient.name)) if lab_rxs: for lab_rx in lab_rxs: rx_obj = frappe.get_doc("Lab Prescription", lab_rx[0]) if rx_obj.lab_test_code and (frappe.db.get_value( "Lab Test Template", rx_obj.lab_test_code, "is_billable") == 1): item_to_invoice.append({ 'reference_type': 'Lab Prescription', 'reference_name': rx_obj.name, 'service': frappe.db.get_value("Lab Test Template", rx_obj.lab_test_code, "item") }) procedures = frappe.get_list("Clinical Procedure", { 'patient': patient.name, 'invoiced': False }) if procedures: for procedure in procedures: procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name']) if not procedure_obj.appointment: if procedure_obj.procedure_template and ( frappe.db.get_value( "Clinical Procedure Template", procedure_obj.procedure_template, "is_billable") == 1): item_to_invoice.append({ 'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name, 'service': frappe.db.get_value( "Clinical Procedure Template", procedure_obj.procedure_template, "item") }) procedure_rxs = frappe.db.sql( """select pp.name from `tabPatient Encounter` et, `tabProcedure Prescription` pp where et.patient=%s and pp.parent=et.name and pp.procedure_created=0 and pp.invoiced=0 and pp.appointment_booked=0""", (patient.name)) if procedure_rxs: for procedure_rx in procedure_rxs: rx_obj = frappe.get_doc("Procedure Prescription", procedure_rx[0]) if frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "is_billable") == 1: item_to_invoice.append({ 'reference_type': 'Procedure Prescription', 'reference_name': rx_obj.name, 'service': frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "item") }) procedures = frappe.get_list( "Clinical Procedure", { 'patient': patient.name, 'invoice_separately_as_consumables': True, 'consumption_invoiced': False, 'consume_stock': True, 'status': 'Completed' }) if procedures: service_item = get_healthcare_service_item( 'clinical_procedure_consumable_item') if not service_item: msg = _(("Please Configure {0} in ").format("Clinical Procedure Consumable Item") \ + """<b><a href="#Form/Healthcare Settings">Healthcare Settings</a></b>""") frappe.throw(msg) for procedure in procedures: procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name']) item_to_invoice.append({ 'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name, 'service': service_item, 'rate': procedure_obj.consumable_total_amount, 'description': procedure_obj.consumption_details }) inpatient_services = frappe.db.sql( """select io.name, io.parent from `tabInpatient Record` ip, `tabInpatient Occupancy` io where ip.patient=%s and io.parent=ip.name and io.left=1 and io.invoiced=0""", (patient.name)) if inpatient_services: for inpatient_service in inpatient_services: inpatient_occupancy = frappe.get_doc( "Inpatient Occupancy", inpatient_service[0]) service_unit_type = frappe.get_doc( "Healthcare Service Unit Type", frappe.db.get_value("Healthcare Service Unit", inpatient_occupancy.service_unit, "service_unit_type")) if service_unit_type and service_unit_type.is_billable == 1: hours_occupied = time_diff_in_hours( inpatient_occupancy.check_out, inpatient_occupancy.check_in) qty = 0.5 if hours_occupied > 0: if service_unit_type.no_of_hours and service_unit_type.no_of_hours > 0: actual_qty = hours_occupied / service_unit_type.no_of_hours else: # 24 hours = 1 Day actual_qty = hours_occupied / 24 floor = math.floor(actual_qty) decimal_part = actual_qty - floor if decimal_part > 0.5: qty = rounded(floor + 1, 1) elif decimal_part < 0.5 and decimal_part > 0: qty = rounded(floor + 0.5, 1) if qty <= 0: qty = 0.5 item_to_invoice.append({ 'reference_type': 'Inpatient Occupancy', 'reference_name': inpatient_occupancy.name, 'service': service_unit_type.item, 'qty': qty }) return item_to_invoice else: frappe.throw( _("The Patient {0} do not have customer refrence to invoice"). format(patient.name))
def get_healthcare_service_order_to_invoice(patient, company, encounter, service_order_category=None, prescribed=None): encounter_dict = frappe.get_all("Patient Encounter", filters={ "reference_encounter": encounter, "docstatus": 1, 'is_not_billable': 0 }) encounter_list = [encounter] for i in encounter_dict: encounter_list.append(i.name) filters = { 'patient': patient.name, 'company': company, 'order_group': ["in", encounter_list], 'invoiced': 0, 'order_date': [">", add_days(nowdate(), -3)], 'docstatus': 1 } if service_order_category: filters['healthcare_service_order_category'] = service_order_category filters['prescribed'] = 1 services_to_invoice = [] services = frappe.get_list('Healthcare Service Order', fields=['*'], filters=filters) if services: for service in services: practitioner_charge = 0 income_account = None service_item = None if service.order_doctype and service.order: is_not_available_inhouse = frappe.get_value( service.order_doctype, service.order, "is_not_available_inhouse") if is_not_available_inhouse: continue if service.ordered_by: service_item = service.billing_item practitioner_charge = get_insurance_amount( service.insurance_subscription, service.billing_item, company, patient.name, service.insurance_company) income_account = get_income_account(service.ordered_by, company) services_to_invoice.append({ 'reference_type': 'Healthcare Service Order', 'reference_name': service.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account, 'qty': service.quantity }) return services_to_invoice
def get_healthcare_services_to_invoice(patient): patient = frappe.get_doc("Patient", patient) if patient: if patient.customer: item_to_invoice = [] patient_appointments = frappe.get_list("Patient Appointment",{'patient': patient.name, 'invoiced': False}, order_by="appointment_date") if patient_appointments: fee_validity_details = [] valid_days = frappe.db.get_value("Healthcare Settings", None, "valid_days") max_visit = frappe.db.get_value("Healthcare Settings", None, "max_visit") for patient_appointment in patient_appointments: patient_appointment_obj = frappe.get_doc("Patient Appointment", patient_appointment['name']) if patient_appointment_obj.procedure_template: if frappe.db.get_value("Clinical Procedure Template", patient_appointment_obj.procedure_template, "is_billable") == 1: item_to_invoice.append({'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name, 'service': patient_appointment_obj.procedure_template}) else: practitioner_exist_in_list = False skip_invoice = False if fee_validity_details: for validity in fee_validity_details: if validity['practitioner'] == patient_appointment_obj.practitioner: practitioner_exist_in_list = True if validity['valid_till'] >= patient_appointment_obj.appointment_date: validity['visits'] = validity['visits']+1 if int(max_visit) > validity['visits']: skip_invoice = True if not skip_invoice: validity['visits'] = 1 validity['valid_till'] = patient_appointment_obj.appointment_date + datetime.timedelta(days=int(valid_days)) if not practitioner_exist_in_list: valid_till = patient_appointment_obj.appointment_date + datetime.timedelta(days=int(valid_days)) visits = 0 validity_exist = validity_exists(patient_appointment_obj.practitioner, patient_appointment_obj.patient) if validity_exist: fee_validity = frappe.get_doc("Fee Validity", validity_exist[0][0]) valid_till = fee_validity.valid_till visits = fee_validity.visited fee_validity_details.append({'practitioner': patient_appointment_obj.practitioner, 'valid_till': valid_till, 'visits': visits}) if not skip_invoice: practitioner_charge = 0 income_account = None service_item = None if patient_appointment_obj.practitioner: service_item, practitioner_charge = service_item_and_practitioner_charge(patient_appointment_obj) income_account = get_income_account(patient_appointment_obj.practitioner, patient_appointment_obj.company) item_to_invoice.append({'reference_type': 'Patient Appointment', 'reference_name': patient_appointment_obj.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account}) encounters = frappe.get_list("Patient Encounter", {'patient': patient.name, 'invoiced': False, 'docstatus': 1}) if encounters: for encounter in encounters: encounter_obj = frappe.get_doc("Patient Encounter", encounter['name']) if not encounter_obj.appointment: practitioner_charge = 0 income_account = None service_item = None if encounter_obj.practitioner: service_item, practitioner_charge = service_item_and_practitioner_charge(encounter_obj) income_account = get_income_account(encounter_obj.practitioner, encounter_obj.company) item_to_invoice.append({'reference_type': 'Patient Encounter', 'reference_name': encounter_obj.name, 'service': service_item, 'rate': practitioner_charge, 'income_account': income_account}) lab_tests = frappe.get_list("Lab Test", {'patient': patient.name, 'invoiced': False}) if lab_tests: for lab_test in lab_tests: lab_test_obj = frappe.get_doc("Lab Test", lab_test['name']) if frappe.db.get_value("Lab Test Template", lab_test_obj.template, "is_billable") == 1: item_to_invoice.append({'reference_type': 'Lab Test', 'reference_name': lab_test_obj.name, 'service': frappe.db.get_value("Lab Test Template", lab_test_obj.template, "item")}) lab_rxs = frappe.db.sql("""select lp.name from `tabPatient Encounter` et, `tabLab Prescription` lp where et.patient=%s and lp.parent=et.name and lp.lab_test_created=0 and lp.invoiced=0""", (patient.name)) if lab_rxs: for lab_rx in lab_rxs: rx_obj = frappe.get_doc("Lab Prescription", lab_rx[0]) if rx_obj.lab_test_code and (frappe.db.get_value("Lab Test Template", rx_obj.lab_test_code, "is_billable") == 1): item_to_invoice.append({'reference_type': 'Lab Prescription', 'reference_name': rx_obj.name, 'service': frappe.db.get_value("Lab Test Template", rx_obj.lab_test_code, "item")}) procedures = frappe.get_list("Clinical Procedure", {'patient': patient.name, 'invoiced': False}) if procedures: for procedure in procedures: procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name']) if not procedure_obj.appointment: if procedure_obj.procedure_template and (frappe.db.get_value("Clinical Procedure Template", procedure_obj.procedure_template, "is_billable") == 1): item_to_invoice.append({'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name, 'service': frappe.db.get_value("Clinical Procedure Template", procedure_obj.procedure_template, "item")}) procedure_rxs = frappe.db.sql("""select pp.name from `tabPatient Encounter` et, `tabProcedure Prescription` pp where et.patient=%s and pp.parent=et.name and pp.procedure_created=0 and pp.invoiced=0 and pp.appointment_booked=0""", (patient.name)) if procedure_rxs: for procedure_rx in procedure_rxs: rx_obj = frappe.get_doc("Procedure Prescription", procedure_rx[0]) if frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "is_billable") == 1: item_to_invoice.append({'reference_type': 'Procedure Prescription', 'reference_name': rx_obj.name, 'service': frappe.db.get_value("Clinical Procedure Template", rx_obj.procedure, "item")}) procedures = frappe.get_list("Clinical Procedure", {'patient': patient.name, 'invoice_separately_as_consumables': True, 'consumption_invoiced': False, 'consume_stock': True, 'status': 'Completed'}) if procedures: service_item = get_healthcare_service_item('clinical_procedure_consumable_item') if not service_item: msg = _(("Please Configure {0} in ").format("Clinical Procedure Consumable Item") \ + """<b><a href="#Form/Healthcare Settings">Healthcare Settings</a></b>""") frappe.throw(msg) for procedure in procedures: procedure_obj = frappe.get_doc("Clinical Procedure", procedure['name']) item_to_invoice.append({'reference_type': 'Clinical Procedure', 'reference_name': procedure_obj.name, 'service': service_item, 'rate': procedure_obj.consumable_total_amount, 'description': procedure_obj.consumption_details}) inpatient_services = frappe.db.sql("""select io.name, io.parent from `tabInpatient Record` ip, `tabInpatient Occupancy` io where ip.patient=%s and io.parent=ip.name and io.left=1 and io.invoiced=0""", (patient.name)) if inpatient_services: for inpatient_service in inpatient_services: inpatient_occupancy = frappe.get_doc("Inpatient Occupancy", inpatient_service[0]) service_unit_type = frappe.get_doc("Healthcare Service Unit Type", frappe.db.get_value("Healthcare Service Unit", inpatient_occupancy.service_unit, "service_unit_type")) if service_unit_type and service_unit_type.is_billable == 1: hours_occupied = time_diff_in_hours(inpatient_occupancy.check_out, inpatient_occupancy.check_in) qty = 0.5 if hours_occupied > 0: actual_qty = hours_occupied / service_unit_type.no_of_hours floor = math.floor(actual_qty) decimal_part = actual_qty - floor if decimal_part > 0.5: qty = rounded(floor + 1, 1) elif decimal_part < 0.5 and decimal_part > 0: qty = rounded(floor + 0.5, 1) if qty <= 0: qty = 0.5 item_to_invoice.append({'reference_type': 'Inpatient Occupancy', 'reference_name': inpatient_occupancy.name, 'service': service_unit_type.item, 'qty': qty}) return item_to_invoice else: frappe.throw(_("The Patient {0} do not have customer refrence to invoice").format(patient.name))