Include checked-in clinic visits on outpatient nursing boards.
Deploy Ladill Care / deploy (push) Successful in 2m12s
Deploy Ladill Care / deploy (push) Successful in 2m12s
OPD/service units list open department appointments without manual place; nursing actions soft-place the visit onto the unit. Inpatient wards stay placement-only. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -9,6 +9,7 @@ use App\Models\Department;
|
||||
use App\Models\Visit;
|
||||
use App\Services\Care\AuditLogger;
|
||||
use App\Services\Care\CarePermissions;
|
||||
use App\Services\Care\CareUnitCensusService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -110,13 +111,8 @@ class CareUnitController extends Controller
|
||||
|
||||
$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();
|
||||
$census = app(CareUnitCensusService::class);
|
||||
$placedVisits = $census->visitsForUnit($careUnit, $this->ownerRef($request));
|
||||
|
||||
$placeableVisits = Visit::owned($this->ownerRef($request))
|
||||
->where('organization_id', $organization->id)
|
||||
@@ -126,6 +122,7 @@ class CareUnitController extends Controller
|
||||
$q->whereNull('care_unit_id')
|
||||
->orWhere('care_unit_id', '!=', $careUnit->id);
|
||||
})
|
||||
->whereNotIn('id', $placedVisits->pluck('id')->all() ?: [0])
|
||||
->with(['patient', 'careUnit'])
|
||||
->orderByDesc('checked_in_at')
|
||||
->limit(50)
|
||||
@@ -146,6 +143,7 @@ class CareUnitController extends Controller
|
||||
'placeableVisits' => $placeableVisits,
|
||||
'availableBeds' => $availableBeds,
|
||||
'canPlace' => $canPlace,
|
||||
'isClinicUnit' => $census->isClinicUnit($careUnit),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Models\Visit;
|
||||
use App\Services\Care\AssessmentService;
|
||||
use App\Services\Care\CareFeatures;
|
||||
use App\Services\Care\CarePermissions;
|
||||
use App\Services\Care\CareUnitCensusService;
|
||||
use App\Services\Care\NursingAssessmentService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -50,13 +51,16 @@ class NursingAssessmentController extends Controller
|
||||
Visit $visit,
|
||||
AssessmentService $assessments,
|
||||
CareFeatures $features,
|
||||
CareUnitCensusService $census,
|
||||
): RedirectResponse {
|
||||
$this->authorizeAbility($request, 'nursing.assessments.manage');
|
||||
$this->authorizeOwner($request, $careUnit);
|
||||
$this->authorizeOwner($request, $visit);
|
||||
abort_unless((int) $visit->care_unit_id === (int) $careUnit->id, 404);
|
||||
abort_unless((int) $careUnit->organization_id === (int) $this->organization($request)->id, 404);
|
||||
abort_unless($features->enabled($this->organization($request), CareFeatures::ASSESSMENTS_ENGINE), 404);
|
||||
|
||||
$visit = $census->ensurePlaced($visit, $careUnit, $this->ownerRef($request), $this->actorRef($request));
|
||||
|
||||
$features->ensureAssessmentCatalog();
|
||||
|
||||
$validated = $request->validate([
|
||||
@@ -84,11 +88,14 @@ class NursingAssessmentController extends Controller
|
||||
CareUnit $careUnit,
|
||||
Visit $visit,
|
||||
NursingAssessmentService $nursing,
|
||||
CareUnitCensusService $census,
|
||||
): RedirectResponse {
|
||||
$this->authorizeAbility($request, 'vitals.manage');
|
||||
$this->authorizeOwner($request, $careUnit);
|
||||
$this->authorizeOwner($request, $visit);
|
||||
abort_unless((int) $visit->care_unit_id === (int) $careUnit->id, 404);
|
||||
abort_unless((int) $careUnit->organization_id === (int) $this->organization($request)->id, 404);
|
||||
|
||||
$visit = $census->ensurePlaced($visit, $careUnit, $this->ownerRef($request), $this->actorRef($request));
|
||||
|
||||
$validated = $request->validate([
|
||||
'bp_systolic' => ['nullable', 'integer', 'min:40', 'max:300'],
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Models\CareUnit;
|
||||
use App\Models\Visit;
|
||||
use App\Models\WardHandover;
|
||||
use App\Services\Care\CarePermissions;
|
||||
use App\Services\Care\CareUnitCensusService;
|
||||
use App\Services\Care\NursingDocumentationService;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -18,30 +19,23 @@ class NursingDocumentationController extends Controller
|
||||
{
|
||||
use ScopesToAccount;
|
||||
|
||||
public function unitNotes(Request $request, CareUnit $careUnit, NursingDocumentationService $docs): View
|
||||
public function unitNotes(Request $request, CareUnit $careUnit, NursingDocumentationService $docs, CareUnitCensusService $census): View
|
||||
{
|
||||
$this->authorizeAbility($request, 'nursing.notes.view');
|
||||
$this->authorizeOwner($request, $careUnit);
|
||||
abort_unless((int) $careUnit->organization_id === (int) $this->organization($request)->id, 404);
|
||||
|
||||
$placedVisits = Visit::owned($this->ownerRef($request))
|
||||
->where('care_unit_id', $careUnit->id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
->with(['patient', 'bed'])
|
||||
->orderBy('placed_at')
|
||||
->get();
|
||||
|
||||
return view('care.nursing.notes', [
|
||||
'unit' => $careUnit->load('department.branch'),
|
||||
'notes' => $docs->notesForUnit($careUnit, $this->ownerRef($request)),
|
||||
'placedVisits' => $placedVisits,
|
||||
'placedVisits' => $census->visitsForUnit($careUnit, $this->ownerRef($request)),
|
||||
'canWrite' => app(CarePermissions::class)->can($this->member($request), 'nursing.notes.manage'),
|
||||
'noteTypes' => config('care.nursing_note_types'),
|
||||
'shiftCodes' => config('care.staff_shift_codes'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function storeNote(Request $request, CareUnit $careUnit, NursingDocumentationService $docs): RedirectResponse
|
||||
public function storeNote(Request $request, CareUnit $careUnit, NursingDocumentationService $docs, CareUnitCensusService $census): RedirectResponse
|
||||
{
|
||||
$this->authorizeAbility($request, 'nursing.notes.manage');
|
||||
$this->authorizeOwner($request, $careUnit);
|
||||
@@ -58,6 +52,8 @@ class NursingDocumentationController extends Controller
|
||||
->findOrFail($validated['visit_id']);
|
||||
$this->authorizeBranch($request, (int) $visit->branch_id);
|
||||
|
||||
$visit = $census->ensurePlaced($visit, $careUnit, $this->ownerRef($request), $this->actorRef($request));
|
||||
|
||||
$docs->addNote(
|
||||
$visit,
|
||||
$this->ownerRef($request),
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Care;
|
||||
|
||||
use App\Models\CareUnit;
|
||||
use App\Models\Visit;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Who appears on a care unit’s nursing boards.
|
||||
*
|
||||
* Inpatient: visits placed on the unit.
|
||||
* Outpatient / service: placed visits plus open clinic visits for the same
|
||||
* department (checked-in appointments) so OPD nurses do not need manual place.
|
||||
*/
|
||||
class CareUnitCensusService
|
||||
{
|
||||
/** @var list<string> */
|
||||
public const CLINIC_KINDS = ['outpatient', 'service'];
|
||||
|
||||
public function isClinicUnit(CareUnit $unit): bool
|
||||
{
|
||||
return in_array($unit->kind, self::CLINIC_KINDS, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Active visits nurses should see for this unit.
|
||||
*
|
||||
* @return Collection<int, Visit>
|
||||
*/
|
||||
public function visitsForUnit(CareUnit $unit, string $ownerRef): Collection
|
||||
{
|
||||
$unit->loadMissing('department');
|
||||
|
||||
return $this->queryForUnit($unit, $ownerRef)
|
||||
->with(['patient', 'bed', 'appointment'])
|
||||
->orderByRaw('care_unit_id is null') // placed first
|
||||
->orderBy('placed_at')
|
||||
->orderBy('checked_in_at')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function visitBelongsToUnit(Visit $visit, CareUnit $unit): bool
|
||||
{
|
||||
if ((int) $visit->organization_id !== (int) $unit->organization_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! in_array($visit->status, [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((int) $visit->care_unit_id === (int) $unit->id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->isClinicUnit($unit) || $visit->care_unit_id !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$unit->loadMissing('department');
|
||||
$branchId = $unit->department?->branch_id;
|
||||
if (! $branchId || (int) $visit->branch_id !== (int) $branchId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$departmentId = $unit->department_id;
|
||||
if (! $departmentId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$appointment = $visit->relationLoaded('appointment')
|
||||
? $visit->appointment
|
||||
: $visit->appointment()->first();
|
||||
|
||||
return $appointment !== null && (int) $appointment->department_id === (int) $departmentId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft-place an eligible clinic visit onto the unit when a nurse acts on it.
|
||||
*/
|
||||
public function ensurePlaced(
|
||||
Visit $visit,
|
||||
CareUnit $unit,
|
||||
string $ownerRef,
|
||||
?string $actorRef = null,
|
||||
): Visit {
|
||||
$visit->refresh();
|
||||
|
||||
if ((int) $visit->care_unit_id === (int) $unit->id) {
|
||||
return $visit;
|
||||
}
|
||||
|
||||
if (! $this->visitBelongsToUnit($visit, $unit)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
if ($visit->care_unit_id !== null) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
app(VisitPlacementService::class)->place(
|
||||
$visit,
|
||||
$unit,
|
||||
null,
|
||||
$ownerRef,
|
||||
$actorRef,
|
||||
'Auto-placed from outpatient nursing board',
|
||||
);
|
||||
|
||||
return $visit->fresh(['patient', 'bed', 'careUnit', 'appointment']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Builder<Visit>
|
||||
*/
|
||||
protected function queryForUnit(CareUnit $unit, string $ownerRef): Builder
|
||||
{
|
||||
$query = Visit::owned($ownerRef)
|
||||
->where('organization_id', $unit->organization_id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS]);
|
||||
|
||||
if (! $this->isClinicUnit($unit)) {
|
||||
return $query->where('care_unit_id', $unit->id);
|
||||
}
|
||||
|
||||
$unit->loadMissing('department');
|
||||
$branchId = $unit->department?->branch_id;
|
||||
$departmentId = $unit->department_id;
|
||||
|
||||
return $query->where(function (Builder $q) use ($unit, $branchId, $departmentId) {
|
||||
$q->where('care_unit_id', $unit->id);
|
||||
|
||||
if ($branchId && $departmentId) {
|
||||
$q->orWhere(function (Builder $clinic) use ($branchId, $departmentId) {
|
||||
$clinic->whereNull('care_unit_id')
|
||||
->where('branch_id', $branchId)
|
||||
->whereHas('appointment', fn (Builder $a) => $a->where('department_id', $departmentId));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -101,12 +101,7 @@ class MarService
|
||||
{
|
||||
$day = Carbon::parse($day ?? now())->startOfDay();
|
||||
|
||||
$visits = Visit::owned($ownerRef)
|
||||
->where('organization_id', $unit->organization_id)
|
||||
->where('care_unit_id', $unit->id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
->with(['patient', 'bed'])
|
||||
->get();
|
||||
$visits = app(CareUnitCensusService::class)->visitsForUnit($unit, $ownerRef);
|
||||
|
||||
$rows = [];
|
||||
foreach ($visits as $visit) {
|
||||
|
||||
@@ -100,9 +100,8 @@ class NursePerformanceService
|
||||
return [
|
||||
'from' => $from,
|
||||
'to' => $to,
|
||||
'placed_patients' => Visit::owned($ownerRef)
|
||||
->where('care_unit_id', $unit->id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
'placed_patients' => app(CareUnitCensusService::class)
|
||||
->visitsForUnit($unit, $ownerRef)
|
||||
->count(),
|
||||
'mar' => [
|
||||
'total' => $marTotal,
|
||||
|
||||
@@ -15,6 +15,10 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
class NurseStationService
|
||||
{
|
||||
public function __construct(
|
||||
protected CareUnitCensusService $census,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* units: Collection<int, CareUnit>,
|
||||
@@ -89,13 +93,7 @@ class NurseStationService
|
||||
|
||||
$patientsByUnit = [];
|
||||
foreach ($ordered as $id => $unit) {
|
||||
$patientsByUnit[$id] = Visit::owned($ownerRef)
|
||||
->where('organization_id', $organization->id)
|
||||
->where('care_unit_id', $id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
->with(['patient', 'bed'])
|
||||
->orderBy('placed_at')
|
||||
->get();
|
||||
$patientsByUnit[$id] = $this->census->visitsForUnit($unit, $ownerRef);
|
||||
}
|
||||
|
||||
$dutyByUnit = [];
|
||||
|
||||
@@ -48,12 +48,7 @@ class NursingAssessmentService
|
||||
public function unitBoard(CareUnit $unit, string $ownerRef): array
|
||||
{
|
||||
$templates = $this->nursingTemplates();
|
||||
$visits = Visit::owned($ownerRef)
|
||||
->where('care_unit_id', $unit->id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
->with(['patient', 'bed'])
|
||||
->orderBy('placed_at')
|
||||
->get();
|
||||
$visits = app(CareUnitCensusService::class)->visitsForUnit($unit, $ownerRef);
|
||||
|
||||
$rows = [];
|
||||
foreach ($visits as $visit) {
|
||||
|
||||
@@ -148,12 +148,7 @@ class NursingDocumentationService
|
||||
*/
|
||||
public function defaultPatientSummaries(CareUnit $unit, string $ownerRef): array
|
||||
{
|
||||
$visits = Visit::owned($ownerRef)
|
||||
->where('care_unit_id', $unit->id)
|
||||
->whereIn('status', [Visit::STATUS_OPEN, Visit::STATUS_IN_PROGRESS])
|
||||
->with(['patient', 'bed'])
|
||||
->orderBy('placed_at')
|
||||
->get();
|
||||
$visits = app(CareUnitCensusService::class)->visitsForUnit($unit, $ownerRef);
|
||||
|
||||
return $visits->map(fn (Visit $visit) => [
|
||||
'visit_id' => $visit->id,
|
||||
|
||||
Reference in New Issue
Block a user