- Implemented automatic bed release on patient discharge - Added bed transfer functionality in nursing workspace - Added date/time tracking for bed assignments with duration calculation - Fixed bed_master.current_ipd_id as source of truth for occupancy - Created data fix scripts for existing records - Added bed assignment history with duration display (hours/minutes) - Fixed display issues in bed assignment tab - Added route for bed transfer endpoint - Supports custom assignment datetime for backdated entries - Automatically marks previous bed as released on transfer
7.7 KiB
Bed Management Fix Summary - bed_master System
System Overview
Your hospital management system uses the NEW bed_master table for bed management (not the legacy hc_bed_master).
Database Schema
bed_master table columns:
id- Primary keybed_number- Bed number/namebed_code- Unique bed codeward_id- Reference to ward_masterbed_status- Status: 'available', 'occupied', 'maintenance', 'blocked', 'reserved', 'cleaning'current_ipd_id- IPD ID of patient currently occupying this bed (NULL when available)status- Record status: 'active', 'inactive'
How It Works
- Bed Assignment: When a patient is admitted,
bed_master.current_ipd_idis set to the IPD ID, andbed_statusis set to 'occupied' - Bed Release: When a patient is discharged,
current_ipd_idis cleared (set to NULL) andbed_statusis set to 'available' - Bed Status Dashboard: Queries
bed_masterjoining withipd_masteroncurrent_ipd_idto show current occupancy
Issues Identified
1. Beds Not Released on Discharge
Problem: When patients are discharged (ipd_status = 1), beds remain occupied:
bed_master.bed_statusstill shows 'occupied'bed_master.current_ipd_idstill has the IPD ID instead of NULL
Impact: Bed status dashboard shows 5 occupied beds but only 1 current admission, causing confusion and preventing bed reuse for new patients.
2. Blank "Bed Assign" Column
Problem: The Current Admission view may show blank in the "Bed No [Type]" column.
Root Cause: The query looks for beds where current_ipd_id matches the IPD, but if discharge didn't clear this field, the join fails.
Solutions Implemented
Fix 1: Add Bed Release Logic
File: app/Models/IpdModel.php
Added new method releaseBed(int $ipdId) that:
- Clears
current_ipd_id(sets to NULL) inbed_mastertable - Sets
bed_status = 'available'inbed_mastertable - Updates
bed_assignment_historywith release date and reason (if that table is used) - Also handles legacy
hc_bed_masterfor backward compatibility (if it exists)
Fix 2: Call Bed Release on Discharge
File: app/Controllers/Billing/Ipd.php
Modified the updateDischargeStatus() method to call releaseBed() when ipd_status is set to 1.
Fix 3: Update Bed Assignment Logic
File: app/Models/IpdModel.php
Updated bedAssign() method to:
- Set
bed_master.current_ipd_idto the IPD ID when assigning - Set
bed_master.bed_statusto 'occupied' - Clear any previous bed assigned to the same IPD (in case of bed transfer)
Additional Recommendations
1. Run the SQL Fix to Clean Up Existing Data
For existing IPDs where beds weren't released properly, run the SQL script fix_bed_management.sql:
Main fix (Step 2):
UPDATE bed_master bm
INNER JOIN ipd_master i ON i.id = bm.current_ipd_id
SET
bm.bed_status = 'available',
bm.current_ipd_id = NULL
WHERE i.ipd_status = 1
AND bm.current_ipd_id IS NOT NULL;
This releases all beds for patients who have been discharged.
2. Verify the Fix
After running the SQL, check the verification queries (Step 4) to confirm:
- All discharged patients' beds show as available
- Occupied bed count matches current admission count
- No beds have status mismatch
3. Monitor Bed Management
Use these pages to monitor:
/setting/admin/bed-status- View current bed occupancy by ward/floor/setting/admin/bed-management- Manage bed master data/billing/ipd/current-admission- View patients with their assigned beds
Testing Checklist
-
Test Bed Assignment:
- Admit a new patient
- Assign a bed
- Verify
bed_master.bed_status = 'occupied' - Verify
bed_master.current_ipd_id= IPD ID
-
Test Discharge:
- Discharge a patient
- Verify
bed_master.bed_status = 'available' - Verify
bed_master.current_ipd_id IS NULL
-
Test Current Admission View:
- Open
/billing/ipd/current-admission - Verify "Bed No [Type]" column shows bed information
- Verify all admitted patients show their bed assignments
- Open
-
Test Bed Status Panel:
- Open
/setting/admin/bed-status - Verify Available count is correct
- Verify Occupied count matches current admissions
- Click on occupied beds to see patient details
- Open
Files Modified
-
app/Models/IpdModel.php
- Updated
releaseBed()method - releases beds on discharge - Updated
bedAssign()method - sets current_ipd_id when assigning
- Updated
-
app/Controllers/Billing/Ipd.php
- Modified
updateDischargeStatus()to call bed release
- Modified
-
fix_bed_management.sql
- SQL script to fix existing data (discharged patients' beds)
-
BED_MANAGEMENT_FIX_SUMMARY.md
- This documentation file
Database Schema Reference
Tables Used
-
bed_master (Primary table - NEW system)
bed_status: 'available' | 'occupied' | 'maintenance' | 'blocked' | 'reserved' | 'cleaning'current_ipd_id: IPD ID using this bed (NULL = available)bed_number: Bed identifierbed_code: Unique codeward_id: Reference to ward_masterstatus: 'active' | 'inactive'
-
bed_assignment_history (Optional tracking table)
- Records all bed assignments with dates
released_date: When bed was freedrelease_reason: Why bed was freed- Provides audit trail for bed usage
-
ipd_master (Patient admission records)
ipd_status: 0=admitted, 1=dischargedregister_date: Admission datedischarge_date: Discharge date
-
ward_master (Ward/room information)
ward_name: Ward/room namefloor_number: Floor levelward_type: Type of ward
How Bed Management Works Now
Bed Assignment Flow:
- Patient admitted to IPD
- Bed selected from available beds
IpdModel::bedAssign()called:- Creates entry in
bed_assignment_history - Sets
bed_master.current_ipd_id= IPD ID - Sets
bed_master.bed_status= 'occupied'
- Creates entry in
Bed Release Flow:
- Patient discharged (ipd_status = 1)
IpdModel::releaseBed()called automatically:- Sets
bed_master.current_ipd_id= NULL - Sets
bed_master.bed_status= 'available' - Updates
bed_assignment_history.released_date
- Sets
Bed Status Dashboard Query:
SELECT b.*, i.ipd_code, i.register_date, p.p_fname
FROM bed_master b
LEFT JOIN ipd_master i ON i.id = b.current_ipd_id AND i.ipd_status = 0
LEFT JOIN patient_master p ON p.id = i.p_id
WHERE b.status = 'active'
Implementation Notes
- Legacy
hc_bed_mastersupport kept for backward compatibility (optional) - Main system now uses
bed_masterexclusively current_ipd_idis the key field linking beds to patients- Bed status transitions: available → occupied → available
Support
If issues persist after running the fix:
- Check
ipd_master.ipd_status(0=admitted, 1=discharged) - Check
bed_master.current_ipd_idvalues (should be NULL for available beds) - Check
bed_master.bed_statusvalues (should match occupancy) - Review application logs for errors in IpdModel methods
- Run the verification queries from Step 4 in the SQL script
- Check
/setting/admin/bed-statusdashboard for accurate counts
Quick Troubleshooting
Problem: Beds still show occupied after discharge
- Solution: Run Step 2 of fix_bed_management.sql
Problem: "Bed Assign" column is blank
- Solution: Ensure bed_master.current_ipd_id is set during admission
Problem: Bed count mismatch
- Solution: Run verification queries (Step 4) to identify discrepancies
Problem: Cannot assign bed (shows as occupied)
- Solution: Check if current_ipd_id is stuck with old IPD - run fix SQL
Contact
For additional support, check:
- Application logs in
writable/logs/ - Database for constraint violations
- CI4 debug toolbar for query errors