2018-07-06 10:28:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2018, earthians and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2020-04-13 11:12:03 +00:00
|
|
|
import math
|
2018-07-06 10:28:13 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2020-03-26 11:59:50 +00:00
|
|
|
from frappe.utils import time_diff_in_hours, rounded
|
2018-07-06 10:28:13 +00:00
|
|
|
from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_income_account
|
2020-03-17 12:40:39 +00:00
|
|
|
from erpnext.healthcare.doctype.fee_validity.fee_validity import create_fee_validity
|
2018-08-01 08:59:43 +00:00
|
|
|
from erpnext.healthcare.doctype.lab_test.lab_test import create_multiple
|
2018-07-06 10:28:13 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2020-04-13 11:12:03 +00:00
|
|
|
def get_healthcare_services_to_invoice(patient, company):
|
2020-03-09 11:59:23 +00:00
|
|
|
patient = frappe.get_doc('Patient', patient)
|
2020-04-13 11:12:03 +00:00
|
|
|
items_to_invoice = []
|
2018-07-06 10:28:13 +00:00
|
|
|
if patient:
|
2020-03-09 11:59:23 +00:00
|
|
|
validate_customer_created(patient)
|
2020-04-13 11:12:03 +00:00
|
|
|
# Customer validated, build a list of billable services
|
|
|
|
items_to_invoice += get_appointments_to_invoice(patient, company)
|
|
|
|
items_to_invoice += get_encounters_to_invoice(patient, company)
|
|
|
|
items_to_invoice += get_lab_tests_to_invoice(patient, company)
|
|
|
|
items_to_invoice += get_clinical_procedures_to_invoice(patient, company)
|
|
|
|
items_to_invoice += get_inpatient_services_to_invoice(patient, company)
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
return items_to_invoice
|
|
|
|
|
2020-04-13 11:12:03 +00:00
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
def validate_customer_created(patient):
|
|
|
|
if not frappe.db.get_value('Patient', patient.name, 'customer'):
|
|
|
|
msg = _("Please set a Customer linked to the Patient")
|
|
|
|
msg += " <b><a href='#Form/Patient/{0}'>{0}</a></b>".format(patient.name)
|
|
|
|
frappe.throw(msg, title=_('Customer Not Found'))
|
|
|
|
|
2020-03-17 13:58:18 +00:00
|
|
|
|
2020-04-13 11:12:03 +00:00
|
|
|
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},
|
|
|
|
order_by = 'appointment_date'
|
|
|
|
)
|
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
for appointment in patient_appointments:
|
2020-04-13 11:12:03 +00:00
|
|
|
# Procedure Appointments
|
2020-03-09 11:59:23 +00:00
|
|
|
if appointment.procedure_template:
|
|
|
|
if frappe.db.get_value('Clinical Procedure Template', appointment.procedure_template, 'is_billable'):
|
2020-04-13 11:12:03 +00:00
|
|
|
appointments_to_invoice.append({
|
2020-03-09 11:59:23 +00:00
|
|
|
'reference_type': 'Patient Appointment',
|
|
|
|
'reference_name': appointment.name,
|
|
|
|
'service': appointment.procedure_template
|
|
|
|
})
|
2020-04-13 11:12:03 +00:00
|
|
|
# Consultation Appointments, should check fee validity
|
2018-07-06 10:28:13 +00:00
|
|
|
else:
|
2020-04-13 11:12:03 +00:00
|
|
|
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:
|
|
|
|
service_item, practitioner_charge = get_service_item_and_practitioner_charge(appointment)
|
|
|
|
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
|
|
|
|
})
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2020-04-13 11:12:03 +00:00
|
|
|
return appointments_to_invoice
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
|
2020-04-13 11:12:03 +00:00
|
|
|
def get_encounters_to_invoice(patient, company):
|
2020-03-09 11:59:23 +00:00
|
|
|
encounters_to_invoice = []
|
|
|
|
encounters = frappe.get_list(
|
|
|
|
'Patient Encounter',
|
|
|
|
fields=['*'],
|
2020-04-13 11:12:03 +00:00
|
|
|
filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1}
|
2020-03-09 11:59:23 +00:00
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-04-13 11:12:03 +00:00
|
|
|
def get_lab_tests_to_invoice(patient, company):
|
2020-03-09 11:59:23 +00:00
|
|
|
lab_tests_to_invoice = []
|
|
|
|
lab_tests = frappe.get_list(
|
|
|
|
'Lab Test',
|
|
|
|
fields=['name', 'template'],
|
2020-04-13 11:12:03 +00:00
|
|
|
filters={'patient': patient.name, 'company': company, 'invoiced': False, 'docstatus': 1}
|
2020-03-09 11:59:23 +00:00
|
|
|
)
|
|
|
|
for lab_test in lab_tests:
|
2020-04-02 13:15:53 +00:00
|
|
|
item, is_billable = frappe.get_cached_value('Lab Test Template', lab_test.lab_test_code, ['item', 'is_billable'])
|
|
|
|
if is_billable:
|
2020-03-09 11:59:23 +00:00
|
|
|
lab_tests_to_invoice.append({
|
|
|
|
'reference_type': 'Lab Test',
|
|
|
|
'reference_name': lab_test.name,
|
2020-04-02 13:15:53 +00:00
|
|
|
'service': item
|
2020-03-09 11:59:23 +00:00
|
|
|
})
|
|
|
|
|
2020-04-02 13:15:53 +00:00
|
|
|
lab_prescriptions = frappe.db.sql(
|
|
|
|
'''
|
|
|
|
SELECT
|
|
|
|
lp.name, lp.lab_test_code
|
|
|
|
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), as_dict=1)
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
for prescription in lab_prescriptions:
|
2020-04-02 13:15:53 +00:00
|
|
|
item, is_billable = frappe.get_cached_value('Lab Test Template', prescription.lab_test_code, ['item', 'is_billable'])
|
|
|
|
if prescription.lab_test_code and is_billable:
|
2020-03-09 11:59:23 +00:00
|
|
|
lab_tests_to_invoice.append({
|
|
|
|
'reference_type': 'Lab Prescription',
|
|
|
|
'reference_name': prescription.name,
|
2020-04-02 13:15:53 +00:00
|
|
|
'service': item
|
2020-03-09 11:59:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return lab_tests_to_invoice
|
|
|
|
|
|
|
|
|
2020-04-13 11:12:03 +00:00
|
|
|
def get_clinical_procedures_to_invoice(patient, company):
|
2020-03-09 11:59:23 +00:00
|
|
|
clinical_procedures_to_invoice = []
|
|
|
|
procedures = frappe.get_list(
|
|
|
|
'Clinical Procedure',
|
|
|
|
fields='*',
|
2020-04-13 11:12:03 +00:00
|
|
|
filters={'patient': patient.name, 'company': company, 'invoiced': False}
|
2020-03-09 11:59:23 +00:00
|
|
|
)
|
|
|
|
for procedure in procedures:
|
|
|
|
if not procedure.appointment:
|
2020-04-02 13:15:53 +00:00
|
|
|
item, is_billable = frappe.get_cached_value('Clinical Procedure Template', procedure.procedure_template, ['item', 'is_billable'])
|
|
|
|
if procedure.procedure_template and is_billable:
|
2020-03-09 11:59:23 +00:00
|
|
|
clinical_procedures_to_invoice.append({
|
|
|
|
'reference_type': 'Clinical Procedure',
|
|
|
|
'reference_name': procedure.name,
|
2020-04-02 13:15:53 +00:00
|
|
|
'service': item
|
2020-03-09 11:59:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# consumables
|
|
|
|
if procedure.invoice_separately_as_consumables and procedure.consume_stock \
|
|
|
|
and procedure.status == 'Completed' and not procedure.consumption_invoiced:
|
|
|
|
|
|
|
|
service_item = get_healthcare_service_item('clinical_procedure_consumable_item')
|
|
|
|
if not service_item:
|
|
|
|
msg = _('Please Configure Clinical Procedure Consumable Item in ')
|
|
|
|
msg += '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>'''
|
|
|
|
frappe.throw(msg, title=_('Missing Configuration'))
|
|
|
|
|
|
|
|
clinical_procedures_to_invoice.append({
|
|
|
|
'reference_type': 'Clinical Procedure',
|
|
|
|
'reference_name': procedure.name,
|
|
|
|
'service': service_item,
|
|
|
|
'rate': procedure.consumable_total_amount,
|
|
|
|
'description': procedure.consumption_details
|
|
|
|
})
|
|
|
|
|
2020-04-02 13:15:53 +00:00
|
|
|
procedure_prescriptions = frappe.db.sql(
|
|
|
|
'''
|
|
|
|
SELECT
|
|
|
|
pp.name, pp.procedure
|
|
|
|
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), as_dict=1)
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
for prescription in procedure_prescriptions:
|
2020-04-02 13:15:53 +00:00
|
|
|
item, is_billable = frappe.get_cached_value('Clinical Procedure Template', prescription.procedure, ['item', 'is_billable'])
|
|
|
|
if is_billable:
|
2020-03-26 11:59:50 +00:00
|
|
|
clinical_procedures_to_invoice.append({
|
2020-03-09 11:59:23 +00:00
|
|
|
'reference_type': 'Procedure Prescription',
|
|
|
|
'reference_name': prescription.name,
|
2020-04-02 13:15:53 +00:00
|
|
|
'service': item
|
2020-03-09 11:59:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return clinical_procedures_to_invoice
|
|
|
|
|
|
|
|
|
2020-04-13 11:12:03 +00:00
|
|
|
def get_inpatient_services_to_invoice(patient, company):
|
2020-03-09 11:59:23 +00:00
|
|
|
services_to_invoice = []
|
2020-04-02 13:15:53 +00:00
|
|
|
inpatient_services = frappe.db.sql(
|
|
|
|
'''
|
|
|
|
SELECT
|
|
|
|
io.*
|
|
|
|
FROM
|
|
|
|
`tabInpatient Record` ip, `tabInpatient Occupancy` io
|
|
|
|
WHERE
|
|
|
|
ip.patient=%s
|
2020-04-13 11:12:03 +00:00
|
|
|
and ip.company=%s
|
2020-04-02 13:15:53 +00:00
|
|
|
and io.parent=ip.name
|
|
|
|
and io.left=1
|
|
|
|
and io.invoiced=0
|
2020-04-13 11:12:03 +00:00
|
|
|
''', (patient.name, company), as_dict=1)
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
for inpatient_occupancy in inpatient_services:
|
|
|
|
service_unit_type = frappe.db.get_value('Healthcare Service Unit', inpatient_occupancy.service_unit, 'service_unit_type')
|
2020-04-02 13:15:53 +00:00
|
|
|
service_unit_type = frappe.get_cached_doc('Healthcare Service Unit Type', service_unit_type)
|
2020-03-09 11:59:23 +00:00
|
|
|
if service_unit_type and service_unit_type.is_billable:
|
|
|
|
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
|
|
|
|
services_to_invoice.append({
|
|
|
|
'reference_type': 'Inpatient Occupancy',
|
|
|
|
'reference_name': inpatient_occupancy.name,
|
|
|
|
'service': service_unit_type.item, 'qty': qty
|
|
|
|
})
|
|
|
|
|
|
|
|
return services_to_invoice
|
|
|
|
|
2018-07-06 10:28:13 +00:00
|
|
|
|
2020-02-24 13:39:50 +00:00
|
|
|
def get_service_item_and_practitioner_charge(doc):
|
2020-03-09 11:59:23 +00:00
|
|
|
is_inpatient = doc.inpatient_record
|
2020-02-24 13:39:50 +00:00
|
|
|
if is_inpatient:
|
2020-03-09 11:59:23 +00:00
|
|
|
service_item = get_practitioner_service_item(doc.practitioner, 'inpatient_visit_charge_item')
|
2018-07-30 07:12:06 +00:00
|
|
|
if not service_item:
|
2020-03-09 11:59:23 +00:00
|
|
|
service_item = get_healthcare_service_item('inpatient_visit_charge_item')
|
2018-07-26 15:33:17 +00:00
|
|
|
else:
|
2020-03-09 11:59:23 +00:00
|
|
|
service_item = get_practitioner_service_item(doc.practitioner, 'op_consulting_charge_item')
|
2018-07-30 07:12:06 +00:00
|
|
|
if not service_item:
|
2020-03-09 11:59:23 +00:00
|
|
|
service_item = get_healthcare_service_item('op_consulting_charge_item')
|
2018-07-26 15:33:17 +00:00
|
|
|
if not service_item:
|
2020-02-24 13:39:50 +00:00
|
|
|
throw_config_service_item(is_inpatient)
|
2018-07-26 15:33:17 +00:00
|
|
|
|
2020-02-24 13:39:50 +00:00
|
|
|
practitioner_charge = get_practitioner_charge(doc.practitioner, is_inpatient)
|
2018-07-26 15:33:17 +00:00
|
|
|
if not practitioner_charge:
|
2020-02-24 13:39:50 +00:00
|
|
|
throw_config_practitioner_charge(is_inpatient, doc.practitioner)
|
2018-07-26 15:33:17 +00:00
|
|
|
|
|
|
|
return service_item, practitioner_charge
|
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2020-02-24 13:39:50 +00:00
|
|
|
def throw_config_service_item(is_inpatient):
|
2020-04-02 13:15:53 +00:00
|
|
|
service_item_label = _('Out Patient Consulting Charge Item')
|
2020-02-24 13:39:50 +00:00
|
|
|
if is_inpatient:
|
2020-04-02 13:15:53 +00:00
|
|
|
service_item_label = _('Inpatient Visit Charge Item')
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2020-03-23 05:10:39 +00:00
|
|
|
msg = _(('Please Configure {0} in ').format(service_item_label) \
|
2020-03-09 11:59:23 +00:00
|
|
|
+ '''<b><a href='#Form/Healthcare Settings'>Healthcare Settings</a></b>''')
|
|
|
|
frappe.throw(msg, title=_('Missing Configuration'))
|
2018-07-26 15:33:17 +00:00
|
|
|
|
|
|
|
|
2020-02-24 13:39:50 +00:00
|
|
|
def throw_config_practitioner_charge(is_inpatient, practitioner):
|
2020-04-02 13:15:53 +00:00
|
|
|
charge_name = _('OP Consulting Charge')
|
2020-02-24 13:39:50 +00:00
|
|
|
if is_inpatient:
|
2020-04-02 13:15:53 +00:00
|
|
|
charge_name = _('Inpatient Visit Charge')
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
msg = _(('Please Configure {0} for Healthcare Practitioner').format(charge_name) \
|
|
|
|
+ ''' <b><a href='#Form/Healthcare Practitioner/{0}'>{0}</a></b>'''.format(practitioner))
|
|
|
|
frappe.throw(msg, title=_('Missing Configuration'))
|
2018-07-26 15:33:17 +00:00
|
|
|
|
|
|
|
|
2018-07-30 07:12:06 +00:00
|
|
|
def get_practitioner_service_item(practitioner, service_item_field):
|
2020-03-09 11:59:23 +00:00
|
|
|
return frappe.db.get_value('Healthcare Practitioner', practitioner, service_item_field)
|
|
|
|
|
2018-07-30 07:12:06 +00:00
|
|
|
|
2018-07-26 15:33:17 +00:00
|
|
|
def get_healthcare_service_item(service_item_field):
|
2020-03-09 11:59:23 +00:00
|
|
|
return frappe.db.get_single_value('Healthcare Settings', service_item_field)
|
2018-07-26 15:33:17 +00:00
|
|
|
|
|
|
|
|
2020-02-24 13:39:50 +00:00
|
|
|
def get_practitioner_charge(practitioner, is_inpatient):
|
|
|
|
if is_inpatient:
|
2020-03-09 11:59:23 +00:00
|
|
|
practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'inpatient_visit_charge')
|
2018-07-26 15:33:17 +00:00
|
|
|
else:
|
2020-03-09 11:59:23 +00:00
|
|
|
practitioner_charge = frappe.db.get_value('Healthcare Practitioner', practitioner, 'op_consulting_charge')
|
2018-07-06 10:28:13 +00:00
|
|
|
if practitioner_charge:
|
|
|
|
return practitioner_charge
|
2018-07-26 15:33:17 +00:00
|
|
|
return False
|
2018-07-06 10:28:13 +00:00
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2018-07-06 10:28:13 +00:00
|
|
|
def manage_invoice_submit_cancel(doc, method):
|
|
|
|
if doc.items:
|
|
|
|
for item in doc.items:
|
2020-03-09 11:59:23 +00:00
|
|
|
if item.get('reference_dt') and item.get('reference_dn'):
|
|
|
|
if frappe.get_meta(item.reference_dt).has_field('invoiced'):
|
2018-07-25 06:00:30 +00:00
|
|
|
set_invoiced(item, method, doc.name)
|
2018-07-06 10:28:13 +00:00
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
if method=='on_submit' and frappe.db.get_single_value('Healthcare Settings', 'create_lab_test_on_si_submit'):
|
|
|
|
create_multiple('Sales Invoice', doc.name)
|
|
|
|
|
2018-08-01 08:59:43 +00:00
|
|
|
|
2018-07-25 06:00:30 +00:00
|
|
|
def set_invoiced(item, method, ref_invoice=None):
|
2018-07-06 10:28:13 +00:00
|
|
|
invoiced = False
|
2020-03-09 11:59:23 +00:00
|
|
|
if method=='on_submit':
|
2018-07-06 10:28:13 +00:00
|
|
|
validate_invoiced_on_submit(item)
|
|
|
|
invoiced = True
|
|
|
|
|
2018-07-26 15:33:17 +00:00
|
|
|
if item.reference_dt == 'Clinical Procedure':
|
|
|
|
if get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
|
2020-03-09 11:59:23 +00:00
|
|
|
frappe.db.set_value(item.reference_dt, item.reference_dn, 'consumption_invoiced', invoiced)
|
2018-07-26 15:33:17 +00:00
|
|
|
else:
|
2020-03-09 11:59:23 +00:00
|
|
|
frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
|
2018-07-26 15:33:17 +00:00
|
|
|
else:
|
2020-03-09 11:59:23 +00:00
|
|
|
frappe.db.set_value(item.reference_dt, item.reference_dn, 'invoiced', invoiced)
|
2018-07-26 15:33:17 +00:00
|
|
|
|
2018-07-06 10:28:13 +00:00
|
|
|
if item.reference_dt == 'Patient Appointment':
|
|
|
|
if frappe.db.get_value('Patient Appointment', item.reference_dn, 'procedure_template'):
|
2020-03-09 11:59:23 +00:00
|
|
|
dt_from_appointment = 'Clinical Procedure'
|
2018-07-06 10:28:13 +00:00
|
|
|
else:
|
2020-03-09 11:59:23 +00:00
|
|
|
dt_from_appointment = 'Patient Encounter'
|
2020-03-12 06:46:23 +00:00
|
|
|
manage_doc_for_appointment(dt_from_appointment, item.reference_dn, invoiced)
|
2018-07-06 10:28:13 +00:00
|
|
|
|
|
|
|
elif item.reference_dt == 'Lab Prescription':
|
2020-03-09 11:59:23 +00:00
|
|
|
manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Lab Test', 'lab_test_created')
|
2018-07-06 10:28:13 +00:00
|
|
|
|
|
|
|
elif item.reference_dt == 'Procedure Prescription':
|
2020-03-09 11:59:23 +00:00
|
|
|
manage_prescriptions(invoiced, item.reference_dt, item.reference_dn, 'Clinical Procedure', 'procedure_created')
|
|
|
|
|
2018-07-06 10:28:13 +00:00
|
|
|
|
|
|
|
def validate_invoiced_on_submit(item):
|
2018-07-26 15:33:17 +00:00
|
|
|
if item.reference_dt == 'Clinical Procedure' and get_healthcare_service_item('clinical_procedure_consumable_item') == item.item_code:
|
2020-03-12 06:46:23 +00:00
|
|
|
is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'consumption_invoiced')
|
2018-07-26 15:33:17 +00:00
|
|
|
else:
|
2020-03-09 11:59:23 +00:00
|
|
|
is_invoiced = frappe.db.get_value(item.reference_dt, item.reference_dn, 'invoiced')
|
|
|
|
if is_invoiced:
|
|
|
|
frappe.throw(_('The item referenced by {0} - {1} is already invoiced'\
|
2018-07-06 10:28:13 +00:00
|
|
|
).format(item.reference_dt, item.reference_dn))
|
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2018-07-06 10:28:13 +00:00
|
|
|
def manage_prescriptions(invoiced, ref_dt, ref_dn, dt, created_check_field):
|
|
|
|
created = frappe.db.get_value(ref_dt, ref_dn, created_check_field)
|
2020-03-09 11:59:23 +00:00
|
|
|
if created:
|
2018-07-06 10:28:13 +00:00
|
|
|
# Fetch the doc created for the prescription
|
2018-07-25 07:45:12 +00:00
|
|
|
doc_created = frappe.db.get_value(dt, {'prescription': ref_dn})
|
2018-07-06 10:28:13 +00:00
|
|
|
frappe.db.set_value(dt, doc_created, 'invoiced', invoiced)
|
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2020-03-13 10:09:31 +00:00
|
|
|
def check_fee_validity(appointment):
|
2020-03-17 13:58:18 +00:00
|
|
|
if not frappe.db.get_single_value('Healthcare Settings', 'enable_free_follow_ups'):
|
|
|
|
return
|
|
|
|
|
2020-03-13 10:09:31 +00:00
|
|
|
validity = frappe.db.exists('Fee Validity', {
|
|
|
|
'practitioner': appointment.practitioner,
|
2020-03-17 12:40:39 +00:00
|
|
|
'patient': appointment.patient,
|
2020-03-17 13:58:18 +00:00
|
|
|
'valid_till': ('>=', appointment.appointment_date)
|
2020-03-13 10:09:31 +00:00
|
|
|
})
|
|
|
|
if not validity:
|
|
|
|
return
|
|
|
|
|
2020-03-17 13:58:18 +00:00
|
|
|
validity = frappe.get_doc('Fee Validity', validity)
|
|
|
|
return validity
|
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2020-03-17 12:40:39 +00:00
|
|
|
def manage_fee_validity(appointment):
|
|
|
|
fee_validity = check_fee_validity(appointment)
|
2020-04-13 11:12:03 +00:00
|
|
|
|
2020-03-13 10:09:31 +00:00
|
|
|
if fee_validity:
|
2020-03-17 12:40:39 +00:00
|
|
|
if appointment.status == 'Cancelled' and fee_validity.visited > 0:
|
|
|
|
fee_validity.visited -= 1
|
|
|
|
frappe.db.delete('Fee Validity Reference', {'appointment': appointment.name})
|
2020-03-26 09:08:12 +00:00
|
|
|
elif fee_validity.status == 'Completed':
|
|
|
|
return
|
2020-03-13 10:09:31 +00:00
|
|
|
else:
|
2020-03-17 12:40:39 +00:00
|
|
|
fee_validity.visited += 1
|
|
|
|
fee_validity.append('ref_appointments', {
|
|
|
|
'appointment': appointment.name
|
|
|
|
})
|
|
|
|
fee_validity.save(ignore_permissions=True)
|
2020-03-09 18:27:00 +00:00
|
|
|
else:
|
2020-03-17 12:40:39 +00:00
|
|
|
fee_validity = create_fee_validity(appointment)
|
|
|
|
return fee_validity
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2018-07-06 10:28:13 +00:00
|
|
|
|
2020-03-12 06:46:23 +00:00
|
|
|
def manage_doc_for_appointment(dt_from_appointment, appointment, invoiced):
|
2020-03-09 18:27:00 +00:00
|
|
|
dn_from_appointment = frappe.db.get_value(
|
|
|
|
dt_from_appointment,
|
2020-03-09 11:59:23 +00:00
|
|
|
filters={'appointment': appointment}
|
2018-07-06 10:28:13 +00:00
|
|
|
)
|
|
|
|
if dn_from_appointment:
|
2020-03-09 11:59:23 +00:00
|
|
|
frappe.db.set_value(dt_from_appointment, dn_from_appointment, 'invoiced', invoiced)
|
|
|
|
|
2018-07-30 05:58:37 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_drugs_to_invoice(encounter):
|
2020-03-09 11:59:23 +00:00
|
|
|
encounter = frappe.get_doc('Patient Encounter', encounter)
|
2018-07-30 05:58:37 +00:00
|
|
|
if encounter:
|
2020-03-09 11:59:23 +00:00
|
|
|
patient = frappe.get_doc('Patient', encounter.patient)
|
|
|
|
if patient:
|
|
|
|
if patient.customer:
|
|
|
|
items_to_invoice = []
|
2018-07-30 05:58:37 +00:00
|
|
|
for drug_line in encounter.drug_prescription:
|
|
|
|
if drug_line.drug_code:
|
|
|
|
qty = 1
|
2020-03-09 11:59:23 +00:00
|
|
|
if frappe.db.get_value('Item', drug_line.drug_code, 'stock_uom') == 'Nos':
|
2018-07-30 05:58:37 +00:00
|
|
|
qty = drug_line.get_quantity()
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
description = ''
|
|
|
|
if drug_line.dosage and drug_line.period:
|
|
|
|
description = _('{0} for {1}').format(drug_line.dosage, drug_line.period)
|
|
|
|
|
|
|
|
items_to_invoice.append({
|
|
|
|
'drug_code': drug_line.drug_code,
|
|
|
|
'quantity': qty,
|
|
|
|
'description': description
|
|
|
|
})
|
|
|
|
return items_to_invoice
|
|
|
|
else:
|
|
|
|
validate_customer_created(patient)
|
|
|
|
|
2018-08-01 13:10:05 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_children(doctype, parent, company, is_root=False):
|
2020-03-12 12:14:46 +00:00
|
|
|
parent_fieldname = "parent_" + doctype.lower().replace(" ", "_")
|
2018-08-01 13:10:05 +00:00
|
|
|
fields = [
|
2020-03-12 12:14:46 +00:00
|
|
|
"name as value",
|
|
|
|
"is_group as expandable",
|
|
|
|
"lft",
|
|
|
|
"rgt"
|
2018-08-01 13:10:05 +00:00
|
|
|
]
|
2020-03-12 12:14:46 +00:00
|
|
|
# fields = [ "name", "is_group", "lft", "rgt" ]
|
|
|
|
filters = [["ifnull(`{0}`,'')".format(parent_fieldname), "=", "" if is_root else parent]]
|
2018-08-01 13:10:05 +00:00
|
|
|
|
|
|
|
if is_root:
|
2020-03-12 12:14:46 +00:00
|
|
|
fields += ["service_unit_type"] if doctype == "Healthcare Service Unit" else []
|
|
|
|
filters.append(["company", "=", company])
|
2018-08-01 13:10:05 +00:00
|
|
|
|
|
|
|
else:
|
2020-03-12 12:14:46 +00:00
|
|
|
fields += ["service_unit_type", "allow_appointments", "inpatient_occupancy", "occupancy_status"] if doctype == "Healthcare Service Unit" else []
|
|
|
|
fields += [parent_fieldname + " as parent"]
|
2018-08-01 13:10:05 +00:00
|
|
|
|
|
|
|
hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters)
|
|
|
|
|
2020-03-12 12:14:46 +00:00
|
|
|
if doctype == "Healthcare Service Unit":
|
2018-08-01 13:10:05 +00:00
|
|
|
for each in hc_service_units:
|
2020-03-12 12:14:46 +00:00
|
|
|
occupancy_msg = ""
|
|
|
|
if each["expandable"] == 1:
|
2018-08-01 13:10:05 +00:00
|
|
|
occupied = False
|
|
|
|
vacant = False
|
2020-04-02 13:15:53 +00:00
|
|
|
child_list = frappe.db.sql(
|
|
|
|
'''
|
|
|
|
SELECT
|
|
|
|
name, occupancy_status
|
|
|
|
FROM
|
|
|
|
`tabHealthcare Service Unit`
|
|
|
|
WHERE
|
|
|
|
inpatient_occupancy = 1
|
|
|
|
and lft > %s and rgt < %s
|
|
|
|
''', (each['lft'], each['rgt']))
|
|
|
|
|
2018-08-01 13:10:05 +00:00
|
|
|
for child in child_list:
|
|
|
|
if not occupied:
|
|
|
|
occupied = 0
|
2020-03-12 12:14:46 +00:00
|
|
|
if child[1] == "Occupied":
|
2018-08-01 13:10:05 +00:00
|
|
|
occupied += 1
|
|
|
|
if not vacant:
|
|
|
|
vacant = 0
|
2020-03-12 12:14:46 +00:00
|
|
|
if child[1] == "Vacant":
|
2018-08-01 13:10:05 +00:00
|
|
|
vacant += 1
|
|
|
|
if vacant and occupied:
|
2020-03-09 11:59:23 +00:00
|
|
|
occupancy_total = vacant + occupied
|
2020-03-12 12:14:46 +00:00
|
|
|
occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total)
|
|
|
|
each["occupied_out_of_vacant"] = occupancy_msg
|
2018-08-01 13:10:05 +00:00
|
|
|
return hc_service_units
|
2019-07-12 06:58:34 +00:00
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2019-07-12 06:58:34 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_patient_vitals(patient, from_date=None, to_date=None):
|
|
|
|
if not patient: return
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
vitals = frappe.db.get_all('Vital Signs', {
|
|
|
|
'docstatus': 1,
|
|
|
|
'patient': patient
|
2020-03-12 06:46:23 +00:00
|
|
|
}, order_by='signs_date, signs_time')
|
2020-03-09 11:59:23 +00:00
|
|
|
|
|
|
|
if len(vitals):
|
2019-07-12 06:58:34 +00:00
|
|
|
return vitals
|
2020-03-09 11:59:23 +00:00
|
|
|
return False
|
|
|
|
|
2019-07-12 06:58:34 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def render_docs_as_html(docs):
|
|
|
|
# docs key value pair {doctype: docname}
|
|
|
|
docs_html = "<div class='col-md-12 col-sm-12 text-muted'>"
|
|
|
|
for doc in docs:
|
2020-03-09 11:59:23 +00:00
|
|
|
docs_html += render_doc_as_html(doc['doctype'], doc['docname'])['html'] + '<br/>'
|
2019-07-12 06:58:34 +00:00
|
|
|
return {'html': docs_html}
|
|
|
|
|
2020-03-09 11:59:23 +00:00
|
|
|
|
2019-07-12 06:58:34 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def render_doc_as_html(doctype, docname, exclude_fields = []):
|
|
|
|
#render document as html, three column layout will break
|
|
|
|
doc = frappe.get_doc(doctype, docname)
|
|
|
|
meta = frappe.get_meta(doctype)
|
|
|
|
doc_html = "<div class='col-md-12 col-sm-12'>"
|
2020-03-09 11:59:23 +00:00
|
|
|
section_html = ''
|
|
|
|
section_label = ''
|
|
|
|
html = ''
|
2019-07-12 06:58:34 +00:00
|
|
|
sec_on = False
|
|
|
|
col_on = 0
|
|
|
|
has_data = False
|
|
|
|
for df in meta.fields:
|
|
|
|
#on section break append append previous section and html to doc html
|
|
|
|
if df.fieldtype == "Section Break":
|
|
|
|
if has_data and col_on and sec_on:
|
|
|
|
doc_html += section_html + html + "</div>"
|
|
|
|
elif has_data and not col_on and sec_on:
|
|
|
|
doc_html += "<div class='col-md-12 col-sm-12'\
|
|
|
|
><div class='col-md-12 col-sm-12'>" \
|
|
|
|
+ section_html + html +"</div></div>"
|
|
|
|
while col_on:
|
|
|
|
doc_html += "</div>"
|
|
|
|
col_on -= 1
|
|
|
|
sec_on = True
|
|
|
|
has_data= False
|
|
|
|
col_on = 0
|
2020-03-09 11:59:23 +00:00
|
|
|
section_html = ''
|
|
|
|
html = ''
|
2019-07-12 06:58:34 +00:00
|
|
|
if df.label:
|
|
|
|
section_label = df.label
|
|
|
|
continue
|
|
|
|
#on column break append html to section html or doc html
|
|
|
|
if df.fieldtype == "Column Break":
|
|
|
|
if sec_on and has_data:
|
|
|
|
section_html += "<div class='col-md-12 col-sm-12'\
|
|
|
|
><div class='col-md-6 col\
|
|
|
|
-sm-6'><b>" + section_label + "</b>" + html + "</div><div \
|
|
|
|
class='col-md-6 col-sm-6'>"
|
|
|
|
elif has_data:
|
|
|
|
doc_html += "<div class='col-md-12 col-sm-12'><div class='col-m\
|
|
|
|
d-6 col-sm-6'>" + html + "</div><div class='col-md-6 col-sm-6'>"
|
|
|
|
elif sec_on and not col_on:
|
|
|
|
section_html += "<div class='col-md-6 col-sm-6'>"
|
2020-03-09 11:59:23 +00:00
|
|
|
html = ''
|
2019-07-12 06:58:34 +00:00
|
|
|
col_on += 1
|
|
|
|
if df.label:
|
|
|
|
html += '<br>' + df.label
|
|
|
|
continue
|
|
|
|
#on table iterate in items and create table based on in_list_view, append to section html or doc html
|
2020-03-09 11:59:23 +00:00
|
|
|
if df.fieldtype == 'Table':
|
2019-07-12 06:58:34 +00:00
|
|
|
items = doc.get(df.fieldname)
|
|
|
|
if not items: continue
|
|
|
|
child_meta = frappe.get_meta(df.options)
|
|
|
|
if not has_data : has_data = True
|
2020-03-09 11:59:23 +00:00
|
|
|
table_head = ''
|
|
|
|
table_row = ''
|
2019-07-12 06:58:34 +00:00
|
|
|
create_head = True
|
|
|
|
for item in items:
|
|
|
|
table_row += '<tr>'
|
|
|
|
for cdf in child_meta.fields:
|
|
|
|
if cdf.in_list_view:
|
|
|
|
if create_head:
|
|
|
|
table_head += '<th>' + cdf.label + '</th>'
|
|
|
|
if item.get(cdf.fieldname):
|
|
|
|
table_row += '<td>' + str(item.get(cdf.fieldname)) \
|
|
|
|
+ '</td>'
|
|
|
|
else:
|
|
|
|
table_row += '<td></td>'
|
|
|
|
create_head = False
|
|
|
|
table_row += '</tr>'
|
|
|
|
if sec_on:
|
2020-03-09 11:59:23 +00:00
|
|
|
section_html += "<table class='table table-condensed \
|
|
|
|
bordered'>" + table_head + table_row + '</table>'
|
2019-07-12 06:58:34 +00:00
|
|
|
else:
|
2020-03-09 11:59:23 +00:00
|
|
|
html += "<table class='table table-condensed table-bordered'>" \
|
|
|
|
+ table_head + table_row + "</table>"
|
2019-07-12 06:58:34 +00:00
|
|
|
continue
|
|
|
|
#on other field types add label and value to html
|
|
|
|
if not df.hidden and not df.print_hide and doc.get(df.fieldname) and df.fieldname not in exclude_fields:
|
2020-03-09 11:59:23 +00:00
|
|
|
html += '<br>{0} : {1}'.format(df.label or df.fieldname, \
|
2019-07-12 06:58:34 +00:00
|
|
|
doc.get(df.fieldname))
|
|
|
|
if not has_data : has_data = True
|
|
|
|
if sec_on and col_on and has_data:
|
2020-03-09 11:59:23 +00:00
|
|
|
doc_html += section_html + html + '</div></div>'
|
2019-07-12 06:58:34 +00:00
|
|
|
elif sec_on and not col_on and has_data:
|
|
|
|
doc_html += "<div class='col-md-12 col-sm-12'\
|
|
|
|
><div class='col-md-12 col-sm-12'>" \
|
2020-03-09 11:59:23 +00:00
|
|
|
+ section_html + html +'</div></div>'
|
2019-07-12 06:58:34 +00:00
|
|
|
if doc_html:
|
2020-03-09 11:59:23 +00:00
|
|
|
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>'
|
2019-07-12 06:58:34 +00:00
|
|
|
|
|
|
|
return {'html': doc_html}
|