# CHANGELOG — Clara Rosa HMIS v10.4.11 Production Fix

## Summary
Complete system audit and fix covering 35+ issues across all modules.
All routes verified, all modules functional, role-based access enforced,
Nurse role elevated to match Clinician across the entire system.

---

## CRASH FIXES (would cause 500 errors)

| # | File | Issue | Fix |
|---|------|-------|-----|
| 1 | `app/Models/Receipt.php` | Missing `issuer()` relationship — receipt view and PDF crashed | Added `belongsTo(User::class, 'issued_by')` |
| 2 | `AppointmentController` | `storeNewVisit()`, `storeWalkIn()`, `checkIn()` created visits WITHOUT invoices — entire billing flow broke | All 3 methods now create Invoice + auto-charge consultation fee |
| 3 | `AppointmentController` | All 3 methods redirected to `queue.board` (an alias that double-redirected, losing flash messages) | Redirect to `visits.show` instead |
| 4 | `VisitController::convertToAdmission()` | Created IPD visit WITHOUT invoice — inpatient billing broken | Now creates Invoice for IPD visit |
| 5 | `PharmacyController::storeProcurement()` | Field name mismatch: validated `items` but delegated to `receiveStock()` which expects `products` | Maps `items` → `products` before delegation |
| 6 | `VisitController::completeVisit()` | Only checked free-text `diagnosis` field — blocked completion when only ICD-10 codes existed | Now checks BOTH `diagnosis` array AND `consultation_diagnoses` table |
| 7 | `billing/receipt.blade.php` | Accessed `$receipt->reference_number` (non-existent column on Receipt) | Fixed to `$receipt->payment->reference_number` |
| 8 | `print/receipt.blade.php` | Same reference_number bug as #7 | Fixed to `$receipt->payment->reference_number` |

## MISSING FEATURES RESTORED

| # | File | Issue | Fix |
|---|------|-------|-----|
| 9 | `layouts/app.blade.php` | Maternity module had ZERO sidebar navigation — completely unreachable | Added Maternity nav group (Dashboard, Register ANC) |
| 10 | `layouts/app.blade.php` | Dental module had ZERO sidebar navigation — Dental Users saw empty sidebar | Added Dental nav group (Records, Patients, Queue, OPD) |
| 11 | `maternity/anc-register.blade.php` | Used raw `<input type="number">` for patient ID — unusable | Replaced with patient search widget |
| 12 | `dashboard/index.blade.php` | ANC stat card was plain text, not clickable | Made clickable link to `/maternity` |
| 13 | `dashboard/index.blade.php` | No dental statistics on dashboard | Added "Dental Today" stat card |
| 14 | `DashboardController` | No dental query | Added `dental_today` metric |
| 15 | `MaternityController::index()` | Didn't load `visits_count` — view always showed 0 | Added `withCount('visits')` + stats array |
| 16 | `maternity/index.blade.php` | Only showed Active ANC count | Added Deliveries (Month) and Due This Month stats |

## DENTAL MODULE REBUILT

| # | File | Issue | Fix |
|---|------|-------|-----|
| 17 | `DentalController` | No search, no stats, no billing integration | Full rewrite: search, stats, auto-billing with service selection |
| 18 | `dental/index.blade.php` | No search bar, no stats, minimal table | Full rewrite: search + date filter, stats cards, proper table |
| 19 | `dental/consult.blade.php` | Bare minimum form, no service selection | Full rewrite: patient header, dental history, procedures, materials, billable services checkboxes, follow-up |

## ROLE-BASED ACCESS — NURSE = CLINICIAN

| # | Route Group | Change |
|---|-------------|--------|
| 20 | ICD-10 search/attach/detach | Added Nurse |
| 21 | Consultation store/update | Added Nurse |
| 22 | Prescription store | Added Nurse |
| 23 | Inpatient discharge | Added Nurse |
| 24 | All verified | Every `role:Clinician` group already included Nurse ✓ |

## DENTAL USER ACCESS

| # | Route/Nav | Change |
|---|-----------|--------|
| 25 | Patient routes | Added Dental User |
| 26 | Visit view routes | Added Dental User |
| 27 | Pharmacy sidebar | Added Dental User |
| 28 | Billing sidebar | Added Clinician + Nurse |

## AUTHENTICATION & NAVIGATION

| # | File | Issue | Fix |
|---|------|-------|-----|
| 29 | `LoginController::login()` | All users redirected to `/dashboard` (admin-only sidebar) | Role-based landing pages: Admin→Dashboard, Nurse→OPD, Lab→Lab, etc. |
| 30 | `LoginController::changePassword()` | Redirected to `/dashboard` after change — non-admin got lost | Role-based redirect matching login logic |
| 31 | `DatabaseSeeder` | Users seeded without `username` — username login silently failed | Added `username` field to all seeded users |

## DATA INTEGRITY

| # | File | Issue | Fix |
|---|------|-------|-----|
| 32 | `Delivery` model | No default `status` — maternity view showed blank | Added `'status' => 'completed'` default + `deliveredBy` relationship |
| 33 | `DatabaseSeeder` | Duplicate SHA insurance company (unique constraint violation) | Removed duplicate |
| 34 | `BillingController::voidItem()` | Audit log parameter order reversed | Fixed old/new value semantics |

## UI/CODE QUALITY

| # | File | Issue | Fix |
|---|------|-------|-----|
| 35 | `visits/show.blade.php` | Payment form colspan=1 instead of 4 — cramped layout | Fixed to `colspan="4"` |
| 36 | `layouts/app.blade.php` | No `@stack('scripts')` — ICD-10 picker JS didn't load | Added `@stack('scripts')` before `@yield('scripts')` |
| 37 | `layouts/app.blade.php` | Duplicate comment line | Removed |
| 38 | Project root | 4 leftover `.bak` files | Deleted |

---

## Files Modified (18 files)
1. `app/Models/Receipt.php`
2. `app/Models/Delivery.php`
3. `app/Http/Controllers/Appointment/AppointmentController.php`
4. `app/Http/Controllers/Visit/VisitController.php`
5. `app/Http/Controllers/Billing/BillingController.php`
6. `app/Http/Controllers/Pharmacy/PharmacyController.php`
7. `app/Http/Controllers/Dental/DentalController.php`
8. `app/Http/Controllers/Maternity/MaternityController.php`
9. `app/Http/Controllers/Dashboard/DashboardController.php`
10. `app/Http/Controllers/Auth/LoginController.php`
11. `routes/web.php`
12. `resources/views/layouts/app.blade.php`
13. `resources/views/dental/index.blade.php`
14. `resources/views/dental/consult.blade.php`
15. `resources/views/maternity/index.blade.php`
16. `resources/views/maternity/anc-register.blade.php`
17. `resources/views/dashboard/index.blade.php`
18. `resources/views/billing/receipt.blade.php`
19. `resources/views/print/receipt.blade.php`
20. `database/seeders/DatabaseSeeder.php`
