Patients can be admitted, transferred, and discharged from unit/bed stays with occupancy sync, so MAR and ward boards have inpatient context. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,7 +6,9 @@ use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
|
||||
use App\Models\CareUnit;
|
||||
use App\Models\Department;
|
||||
use App\Models\Visit;
|
||||
use App\Services\Care\AuditLogger;
|
||||
use App\Services\Care\CarePermissions;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -18,7 +20,7 @@ class CareUnitController extends Controller
|
||||
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$this->authorizeAbility($request, 'admin.departments.view');
|
||||
$this->authorizeCareUnitView($request);
|
||||
$organization = $this->organization($request);
|
||||
$owner = $this->ownerRef($request);
|
||||
|
||||
@@ -98,18 +100,63 @@ class CareUnitController extends Controller
|
||||
|
||||
public function show(Request $request, CareUnit $careUnit): View
|
||||
{
|
||||
$this->authorizeAbility($request, 'admin.departments.view');
|
||||
$this->authorizeCareUnitView($request);
|
||||
$this->authorizeOwner($request, $careUnit);
|
||||
$organization = $this->organization($request);
|
||||
abort_unless((int) $careUnit->organization_id === (int) $organization->id, 404);
|
||||
|
||||
$careUnit->load(['department.branch', 'beds']);
|
||||
|
||||
$placedVisits = Visit::owned($this->ownerRef($request))
|
||||
->where('organization_id', $organization->id)
|
||||
->where('care_unit_id', $careUnit->id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
->with(['patient', 'bed'])
|
||||
->orderBy('placed_at')
|
||||
->get();
|
||||
|
||||
$placeableVisits = Visit::owned($this->ownerRef($request))
|
||||
->where('organization_id', $organization->id)
|
||||
->where('branch_id', $careUnit->department?->branch_id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
->where(function ($q) use ($careUnit) {
|
||||
$q->whereNull('care_unit_id')
|
||||
->orWhere('care_unit_id', '!=', $careUnit->id);
|
||||
})
|
||||
->with(['patient', 'careUnit'])
|
||||
->orderByDesc('checked_in_at')
|
||||
->limit(50)
|
||||
->get();
|
||||
|
||||
$availableBeds = $careUnit->beds
|
||||
->where('is_active', true)
|
||||
->whereIn('status', ['available', 'reserved'])
|
||||
->values();
|
||||
|
||||
$canPlace = app(CarePermissions::class)->can($this->member($request), 'inpatient.placement.manage');
|
||||
|
||||
return view('care.admin.care-units.show', [
|
||||
'unit' => $careUnit,
|
||||
'kinds' => config('care.care_unit_kinds'),
|
||||
'bedStatuses' => config('care.bed_statuses'),
|
||||
'placedVisits' => $placedVisits,
|
||||
'placeableVisits' => $placeableVisits,
|
||||
'availableBeds' => $availableBeds,
|
||||
'canPlace' => $canPlace,
|
||||
]);
|
||||
}
|
||||
|
||||
protected function authorizeCareUnitView(Request $request): void
|
||||
{
|
||||
$permissions = app(CarePermissions::class);
|
||||
$member = $this->member($request);
|
||||
abort_unless(
|
||||
$permissions->can($member, 'admin.departments.view')
|
||||
|| $permissions->can($member, 'inpatient.placement.view'),
|
||||
403,
|
||||
);
|
||||
}
|
||||
|
||||
public function edit(Request $request, CareUnit $careUnit): View
|
||||
{
|
||||
$this->authorizeAbility($request, 'admin.departments.manage');
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Care;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
|
||||
use App\Models\Bed;
|
||||
use App\Models\CareUnit;
|
||||
use App\Models\Visit;
|
||||
use App\Services\Care\VisitPlacementService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class VisitPlacementController extends Controller
|
||||
{
|
||||
use ScopesToAccount;
|
||||
|
||||
public function store(Request $request, CareUnit $careUnit, VisitPlacementService $placements): RedirectResponse
|
||||
{
|
||||
$this->authorizeAbility($request, 'inpatient.placement.manage');
|
||||
$this->authorizeOwner($request, $careUnit);
|
||||
$organization = $this->organization($request);
|
||||
abort_unless((int) $careUnit->organization_id === (int) $organization->id, 404);
|
||||
|
||||
$validated = $request->validate([
|
||||
'visit_id' => ['required', 'integer', 'exists:care_visits,id'],
|
||||
'bed_id' => ['nullable', 'integer', 'exists:care_beds,id'],
|
||||
'notes' => ['nullable', 'string', 'max:2000'],
|
||||
]);
|
||||
|
||||
$visit = Visit::owned($this->ownerRef($request))
|
||||
->where('organization_id', $organization->id)
|
||||
->findOrFail($validated['visit_id']);
|
||||
$this->authorizeBranch($request, (int) $visit->branch_id);
|
||||
|
||||
$bed = null;
|
||||
if (! empty($validated['bed_id'])) {
|
||||
$bed = Bed::owned($this->ownerRef($request))
|
||||
->where('care_unit_id', $careUnit->id)
|
||||
->findOrFail($validated['bed_id']);
|
||||
}
|
||||
|
||||
$placements->place(
|
||||
$visit,
|
||||
$careUnit,
|
||||
$bed,
|
||||
$this->ownerRef($request),
|
||||
$this->actorRef($request),
|
||||
$validated['notes'] ?? null,
|
||||
);
|
||||
|
||||
return redirect()
|
||||
->route('care.care-units.show', $careUnit)
|
||||
->with('success', 'Patient placed on this care unit.');
|
||||
}
|
||||
|
||||
public function transfer(Request $request, Visit $visit, VisitPlacementService $placements): RedirectResponse
|
||||
{
|
||||
$this->authorizeAbility($request, 'inpatient.placement.manage');
|
||||
$this->authorizeOwner($request, $visit);
|
||||
$this->authorizeBranch($request, (int) $visit->branch_id);
|
||||
$organization = $this->organization($request);
|
||||
|
||||
$validated = $request->validate([
|
||||
'care_unit_id' => ['required', 'integer', 'exists:care_care_units,id'],
|
||||
'bed_id' => ['nullable', 'integer', 'exists:care_beds,id'],
|
||||
'notes' => ['nullable', 'string', 'max:2000'],
|
||||
]);
|
||||
|
||||
$unit = CareUnit::owned($this->ownerRef($request))
|
||||
->where('organization_id', $organization->id)
|
||||
->findOrFail($validated['care_unit_id']);
|
||||
|
||||
$bed = null;
|
||||
if (! empty($validated['bed_id'])) {
|
||||
$bed = Bed::owned($this->ownerRef($request))
|
||||
->where('care_unit_id', $unit->id)
|
||||
->findOrFail($validated['bed_id']);
|
||||
}
|
||||
|
||||
$placements->transfer(
|
||||
$visit,
|
||||
$unit,
|
||||
$bed,
|
||||
$this->ownerRef($request),
|
||||
$this->actorRef($request),
|
||||
$validated['notes'] ?? null,
|
||||
);
|
||||
|
||||
return redirect()
|
||||
->route('care.care-units.show', $unit)
|
||||
->with('success', 'Patient transferred.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, Visit $visit, VisitPlacementService $placements): RedirectResponse
|
||||
{
|
||||
$this->authorizeAbility($request, 'inpatient.placement.manage');
|
||||
$this->authorizeOwner($request, $visit);
|
||||
$this->authorizeBranch($request, (int) $visit->branch_id);
|
||||
|
||||
$unitId = $visit->care_unit_id;
|
||||
|
||||
$placements->dischargePlacement(
|
||||
$visit,
|
||||
$this->ownerRef($request),
|
||||
$this->actorRef($request),
|
||||
$request->input('notes'),
|
||||
);
|
||||
|
||||
if ($unitId) {
|
||||
return redirect()
|
||||
->route('care.care-units.show', $unitId)
|
||||
->with('success', 'Placement discharged; bed freed if applicable.');
|
||||
}
|
||||
|
||||
return back()->with('success', 'Placement discharged.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user