feat: Allow Discharge despite Unbilled Healthcare Services
diff --git a/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.json b/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.json
index 0104386..b33c326 100644
--- a/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.json
+++ b/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.json
@@ -17,6 +17,8 @@
"enable_free_follow_ups",
"max_visits",
"valid_days",
+ "inpatient_settings_section",
+ "allow_discharge_despite_unbilled_services",
"healthcare_service_items",
"inpatient_visit_charge_item",
"op_consulting_charge_item",
@@ -302,11 +304,22 @@
"fieldname": "enable_free_follow_ups",
"fieldtype": "Check",
"label": "Enable Free Follow-ups"
+ },
+ {
+ "fieldname": "inpatient_settings_section",
+ "fieldtype": "Section Break",
+ "label": "Inpatient Settings"
+ },
+ {
+ "default": "0",
+ "fieldname": "allow_discharge_despite_unbilled_services",
+ "fieldtype": "Check",
+ "label": "Allow Discharge Despite Unbilled Healthcare Services"
}
],
"issingle": 1,
"links": [],
- "modified": "2020-07-08 15:17:21.543218",
+ "modified": "2021-01-04 10:19:22.329272",
"modified_by": "Administrator",
"module": "Healthcare",
"name": "Healthcare Settings",
diff --git a/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.py b/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.py
index a16fceb..e2ccc34 100644
--- a/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.py
+++ b/erpnext/healthcare/doctype/healthcare_settings/healthcare_settings.py
@@ -11,7 +11,7 @@
class HealthcareSettings(Document):
def validate(self):
- for key in ['collect_registration_fee', 'link_customer_to_patient', 'patient_name_by',
+ for key in ['collect_registration_fee', 'link_customer_to_patient', 'patient_name_by', 'allow_discharge_despite_unbilled_services',
'lab_test_approval_required', 'create_sample_collection_for_lab_test', 'default_medical_code_standard']:
frappe.db.set_default(key, self.get(key, ""))
diff --git a/erpnext/healthcare/doctype/inpatient_record/inpatient_record.py b/erpnext/healthcare/doctype/inpatient_record/inpatient_record.py
index bc76970..6a32aca 100644
--- a/erpnext/healthcare/doctype/inpatient_record/inpatient_record.py
+++ b/erpnext/healthcare/doctype/inpatient_record/inpatient_record.py
@@ -5,7 +5,7 @@
from __future__ import unicode_literals
import frappe, json
from frappe import _
-from frappe.utils import today, now_datetime, getdate, get_datetime
+from frappe.utils import today, now_datetime, getdate, get_datetime, get_link_to_form
from frappe.model.document import Document
from frappe.desk.reportview import get_match_cond
@@ -113,6 +113,7 @@
inpatient_record.status = 'Admission Scheduled'
inpatient_record.save(ignore_permissions = True)
+
@frappe.whitelist()
def schedule_discharge(args):
discharge_order = json.loads(args)
@@ -126,16 +127,19 @@
frappe.db.set_value('Patient', discharge_order['patient'], 'inpatient_status', inpatient_record.status)
frappe.db.set_value('Patient Encounter', inpatient_record.discharge_encounter, 'inpatient_status', inpatient_record.status)
+
def set_details_from_ip_order(inpatient_record, ip_order):
for key in ip_order:
inpatient_record.set(key, ip_order[key])
+
def set_ip_child_records(inpatient_record, inpatient_record_child, encounter_child):
for item in encounter_child:
table = inpatient_record.append(inpatient_record_child)
for df in table.meta.get('fields'):
table.set(df.fieldname, item.get(df.fieldname))
+
def check_out_inpatient(inpatient_record):
if inpatient_record.inpatient_occupancies:
for inpatient_occupancy in inpatient_record.inpatient_occupancies:
@@ -144,54 +148,88 @@
inpatient_occupancy.check_out = now_datetime()
frappe.db.set_value("Healthcare Service Unit", inpatient_occupancy.service_unit, "occupancy_status", "Vacant")
+
def discharge_patient(inpatient_record):
- validate_invoiced_inpatient(inpatient_record)
+ validate_inpatient_invoicing(inpatient_record)
inpatient_record.discharge_date = today()
inpatient_record.status = "Discharged"
inpatient_record.save(ignore_permissions = True)
-def validate_invoiced_inpatient(inpatient_record):
- pending_invoices = []
+
+def validate_inpatient_invoicing(inpatient_record):
+ if frappe.db.get_default("allow_discharge_despite_unbilled_services"):
+ return
+
+ pending_invoices = get_pending_invoices(inpatient_record)
+
+ if pending_invoices:
+ message = _("Cannot mark Inpatient Record as Discharged since there are unbilled services. ")
+
+ formatted_doc_rows = ''
+
+ for doctype, docnames in pending_invoices.items():
+ formatted_doc_rows += """
+ <td>{0}</td>
+ <td>{1}</td>
+ </tr>""".format(doctype, docnames)
+
+ message += """
+ <table class='table'>
+ <thead>
+ <th>{0}</th>
+ <th>{1}</th>
+ </thead>
+ {2}
+ </table>
+ """.format(_("Healthcare Service"), _("Documents"), formatted_doc_rows)
+
+ frappe.throw(message, title=_("Unbilled Services"), is_minimizable=True, wide=True)
+
+
+def get_pending_invoices(inpatient_record):
+ pending_invoices = {}
if inpatient_record.inpatient_occupancies:
service_unit_names = False
for inpatient_occupancy in inpatient_record.inpatient_occupancies:
- if inpatient_occupancy.invoiced != 1:
+ if not inpatient_occupancy.invoiced:
if service_unit_names:
service_unit_names += ", " + inpatient_occupancy.service_unit
else:
service_unit_names = inpatient_occupancy.service_unit
if service_unit_names:
- pending_invoices.append("Inpatient Occupancy (" + service_unit_names + ")")
+ pending_invoices["Inpatient Occupancy"] = service_unit_names
docs = ["Patient Appointment", "Patient Encounter", "Lab Test", "Clinical Procedure"]
for doc in docs:
- doc_name_list = get_inpatient_docs_not_invoiced(doc, inpatient_record)
+ doc_name_list = get_unbilled_inpatient_docs(doc, inpatient_record)
if doc_name_list:
pending_invoices = get_pending_doc(doc, doc_name_list, pending_invoices)
- if pending_invoices:
- frappe.throw(_("Can not mark Inpatient Record Discharged, there are Unbilled Invoices {0}").format(", "
- .join(pending_invoices)), title=_('Unbilled Invoices'))
+ return pending_invoices
+
def get_pending_doc(doc, doc_name_list, pending_invoices):
if doc_name_list:
doc_ids = False
for doc_name in doc_name_list:
+ doc_link = get_link_to_form(doc, doc_name.name)
if doc_ids:
- doc_ids += ", "+doc_name.name
+ doc_ids += ", " + doc_link
else:
- doc_ids = doc_name.name
+ doc_ids = doc_link
if doc_ids:
- pending_invoices.append(doc + " (" + doc_ids + ")")
+ pending_invoices[doc] = doc_ids
return pending_invoices
-def get_inpatient_docs_not_invoiced(doc, inpatient_record):
+
+def get_unbilled_inpatient_docs(doc, inpatient_record):
return frappe.db.get_list(doc, filters = {'patient': inpatient_record.patient,
'inpatient_record': inpatient_record.name, 'docstatus': 1, 'invoiced': 0})
+
def admit_patient(inpatient_record, service_unit, check_in, expected_discharge=None):
inpatient_record.admitted_datetime = check_in
inpatient_record.status = 'Admitted'
@@ -203,6 +241,7 @@
frappe.db.set_value('Patient', inpatient_record.patient, 'inpatient_status', 'Admitted')
frappe.db.set_value('Patient', inpatient_record.patient, 'inpatient_record', inpatient_record.name)
+
def transfer_patient(inpatient_record, service_unit, check_in):
item_line = inpatient_record.append('inpatient_occupancies', {})
item_line.service_unit = service_unit
@@ -212,6 +251,7 @@
frappe.db.set_value("Healthcare Service Unit", service_unit, "occupancy_status", "Occupied")
+
def patient_leave_service_unit(inpatient_record, check_out, leave_from):
if inpatient_record.inpatient_occupancies:
for inpatient_occupancy in inpatient_record.inpatient_occupancies:
@@ -221,6 +261,7 @@
frappe.db.set_value("Healthcare Service Unit", inpatient_occupancy.service_unit, "occupancy_status", "Vacant")
inpatient_record.save(ignore_permissions = True)
+
@frappe.whitelist()
@frappe.validate_and_sanitize_search_inputs
def get_leave_from(doctype, txt, searchfield, start, page_len, filters):