diff --git a/src/main/java/com/divudi/bean/inward/BhtSummeryController.java b/src/main/java/com/divudi/bean/inward/BhtSummeryController.java index 5486c5ab8d..808a186f5a 100644 --- a/src/main/java/com/divudi/bean/inward/BhtSummeryController.java +++ b/src/main/java/com/divudi/bean/inward/BhtSummeryController.java @@ -1731,7 +1731,14 @@ public void discharge() { if (getPatientEncounter().getAdmissionType() != null && getPatientEncounter().getAdmissionType().isRoomChargesAllowed()) { - if (getPatientEncounter().getCurrentPatientRoom() != null && getPatientEncounter().getCurrentPatientRoom().getDischargedAt() == null) { + // Re-fetch the encounter from the DB/L2 cache to get the authoritative currentPatientRoom FK. + // The session-scoped patientEncounter may be stale: a room change via RoomChangeController + // updates PatientEncounter.currentPatientRoom in the DB but not in this session object, + // so reading currentPatientRoom directly from the session would check the OLD room (which + // is already discharged) and incorrectly let the guard pass while the new room is still active. + PatientEncounter freshEncounter = patientEncounterFacade.find(getPatientEncounter().getId()); + PatientRoom currentRoom = freshEncounter != null ? freshEncounter.getCurrentPatientRoom() : null; + if (currentRoom != null && currentRoom.getDischargedAt() == null) { JsfUtil.addErrorMessage("Cannot discharge patient: the current room has not been discharged. " + "Please discharge the room first to record an accurate billing end time."); return; }