Add full Ambulance and Child Welfare specialty clinical suites.
Deploy Ladill Care / deploy (push) Successful in 54s

EMS clinic workflow (dispatch → scene → transport → handover) plus completed CWC suite, with shell stages, clinical records, reports/print, demo seed, and suite tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-19 22:25:52 +00:00
co-authored by Cursor
parent 8c18ecc5c9
commit c0e5d8ef00
32 changed files with 2672 additions and 22 deletions
@@ -0,0 +1,214 @@
<?php
namespace App\Http\Controllers\Care;
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Http\Controllers\Controller;
use App\Models\SpecialtyClinicalRecord;
use App\Models\Visit;
use App\Services\Care\Ambulance\AmbulanceAnalyticsService;
use App\Services\Care\Ambulance\AmbulanceWorkflowService;
use App\Services\Care\SpecialtyClinicalRecordService;
use App\Services\Care\SpecialtyModuleService;
use App\Services\Care\SpecialtyShellService;
use App\Services\Care\SpecialtyVisitStageService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class AmbulanceWorkspaceController extends Controller
{
use ScopesToAccount;
protected function assertAmbulanceAccess(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanAccess($organization, $this->member($request), 'ambulance'), 403);
}
protected function assertAmbulanceManage(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanManage($organization, $this->member($request), 'ambulance'), 403);
}
protected function assertVisit(Request $request, Visit $visit): void
{
abort_unless($visit->organization_id === $this->organization($request)->id, 404);
$this->authorizeBranch($request, (int) $visit->branch_id);
}
public function setStage(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyVisitStageService $stages,
SpecialtyShellService $shell,
): RedirectResponse {
$this->authorizeAbility($request, 'consultations.manage');
$this->assertAmbulanceManage($request, $modules);
$this->assertVisit($request, $visit);
$validated = $request->validate([
'stage' => ['required', 'string', 'max:32'],
]);
try {
$stages->setStage(
$this->organization($request),
$visit,
'ambulance',
$validated['stage'],
$this->ownerRef($request),
$this->actorRef($request),
);
} catch (\InvalidArgumentException $e) {
return back()->with('error', $e->getMessage());
}
return redirect()
->route('care.specialty.workspace', [
'module' => 'ambulance',
'visit' => $visit,
'tab' => $shell->workspaceTabForStage('ambulance', $validated['stage']),
])
->with('success', 'Visit stage updated.');
}
public function saveHandover(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyClinicalRecordService $clinical,
SpecialtyVisitStageService $stages,
AmbulanceWorkflowService $workflow,
): RedirectResponse {
$this->authorizeAbility($request, 'consultations.manage');
$this->assertAmbulanceManage($request, $modules);
$this->assertVisit($request, $visit);
$organization = $this->organization($request);
$owner = $this->ownerRef($request);
$payload = (array) $request->input('payload', []);
foreach ($clinical->fieldsFor('ambulance', 'amb_handover') as $field) {
if (($field['type'] ?? '') === 'boolean') {
$payload[$field['name']] = $request->boolean('payload.'.$field['name']);
}
}
$request->merge(['payload' => $payload, 'tab' => 'handover']);
$validated = $request->validate(array_merge([
'tab' => ['required', 'string'],
], $clinical->validationRules('ambulance', 'amb_handover')));
$record = $clinical->upsert(
$organization,
$visit,
'ambulance',
'amb_handover',
$clinical->payloadFromRequest($validated),
$owner,
$owner,
AmbulanceWorkflowService::STAGE_HANDOVER,
SpecialtyClinicalRecord::STATUS_COMPLETED,
);
try {
$stages->setStage(
$organization,
$visit,
'ambulance',
AmbulanceWorkflowService::STAGE_HANDOVER,
$owner,
$owner,
);
} catch (\InvalidArgumentException) {
}
if ($workflow->shouldCompleteVisit($record->payload ?? [])) {
try {
$stages->setStage(
$organization,
$visit,
'ambulance',
AmbulanceWorkflowService::STAGE_COMPLETED,
$owner,
$owner,
);
} catch (\InvalidArgumentException) {
}
$visit->refresh();
if ($visit->status !== Visit::STATUS_COMPLETED) {
$visit->update([
'status' => Visit::STATUS_COMPLETED,
'completed_at' => $visit->completed_at ?? now(),
]);
}
$appointment = $visit->appointment;
if ($appointment && $appointment->status !== \App\Models\Appointment::STATUS_COMPLETED) {
$appointment->update([
'status' => \App\Models\Appointment::STATUS_COMPLETED,
'completed_at' => $appointment->completed_at ?? now(),
]);
}
}
return redirect()
->route('care.specialty.workspace', ['module' => 'ambulance', 'visit' => $visit, 'tab' => 'handover'])
->with('success', 'Handover saved.');
}
public function reports(
Request $request,
SpecialtyModuleService $modules,
SpecialtyShellService $shell,
AmbulanceAnalyticsService $analytics,
): View {
$this->authorizeAbility($request, 'consultations.view');
$this->assertAmbulanceAccess($request, $modules);
$organization = $this->organization($request);
$branchScope = app(\App\Services\Care\OrganizationResolver::class)->branchScope($this->member($request));
$report = $analytics->report(
$organization,
$this->ownerRef($request),
$branchScope,
$request->query('from'),
$request->query('to'),
);
return view('care.specialty.ambulance.reports', [
'moduleKey' => 'ambulance',
'definition' => $modules->definition('ambulance'),
'shellNav' => $shell->navItems('ambulance'),
'report' => $report,
'currency' => strtoupper((string) config('care.billing.currency', config('care.currency', 'GHS'))),
]);
}
public function printSummary(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyClinicalRecordService $clinical,
): View {
$this->authorizeAbility($request, 'consultations.view');
$this->assertAmbulanceAccess($request, $modules);
$this->assertVisit($request, $visit);
$visit->loadMissing(['patient', 'appointment.practitioner', 'branch']);
return view('care.specialty.ambulance.print', [
'visit' => $visit,
'patient' => $visit->patient,
'dispatch' => $clinical->findForVisit($visit, 'ambulance', 'amb_dispatch'),
'scene' => $clinical->findForVisit($visit, 'ambulance', 'amb_scene'),
'enRoute' => $clinical->findForVisit($visit, 'ambulance', 'amb_en_route'),
'handover' => $clinical->findForVisit($visit, 'ambulance', 'amb_handover'),
]);
}
}
@@ -0,0 +1,214 @@
<?php
namespace App\Http\Controllers\Care;
use App\Http\Controllers\Care\Concerns\ScopesToAccount;
use App\Http\Controllers\Controller;
use App\Models\SpecialtyClinicalRecord;
use App\Models\Visit;
use App\Services\Care\ChildWelfare\ChildWelfareAnalyticsService;
use App\Services\Care\ChildWelfare\ChildWelfareWorkflowService;
use App\Services\Care\SpecialtyClinicalRecordService;
use App\Services\Care\SpecialtyModuleService;
use App\Services\Care\SpecialtyShellService;
use App\Services\Care\SpecialtyVisitStageService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class ChildWelfareWorkspaceController extends Controller
{
use ScopesToAccount;
protected function assertChildWelfareAccess(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanAccess($organization, $this->member($request), 'child_welfare'), 403);
}
protected function assertChildWelfareManage(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanManage($organization, $this->member($request), 'child_welfare'), 403);
}
protected function assertVisit(Request $request, Visit $visit): void
{
abort_unless($visit->organization_id === $this->organization($request)->id, 404);
$this->authorizeBranch($request, (int) $visit->branch_id);
}
public function setStage(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyVisitStageService $stages,
SpecialtyShellService $shell,
): RedirectResponse {
$this->authorizeAbility($request, 'consultations.manage');
$this->assertChildWelfareManage($request, $modules);
$this->assertVisit($request, $visit);
$validated = $request->validate([
'stage' => ['required', 'string', 'max:32'],
]);
try {
$stages->setStage(
$this->organization($request),
$visit,
'child_welfare',
$validated['stage'],
$this->ownerRef($request),
$this->actorRef($request),
);
} catch (\InvalidArgumentException $e) {
return back()->with('error', $e->getMessage());
}
return redirect()
->route('care.specialty.workspace', [
'module' => 'child_welfare',
'visit' => $visit,
'tab' => $shell->workspaceTabForStage('child_welfare', $validated['stage']),
])
->with('success', 'Visit stage updated.');
}
public function saveFollowUp(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyClinicalRecordService $clinical,
SpecialtyVisitStageService $stages,
ChildWelfareWorkflowService $workflow,
): RedirectResponse {
$this->authorizeAbility($request, 'consultations.manage');
$this->assertChildWelfareManage($request, $modules);
$this->assertVisit($request, $visit);
$organization = $this->organization($request);
$owner = $this->ownerRef($request);
$payload = (array) $request->input('payload', []);
foreach ($clinical->fieldsFor('child_welfare', 'cwc_followup') as $field) {
if (($field['type'] ?? '') === 'boolean') {
$payload[$field['name']] = $request->boolean('payload.'.$field['name']);
}
}
$request->merge(['payload' => $payload, 'tab' => 'followup']);
$validated = $request->validate(array_merge([
'tab' => ['required', 'string'],
], $clinical->validationRules('child_welfare', 'cwc_followup')));
$record = $clinical->upsert(
$organization,
$visit,
'child_welfare',
'cwc_followup',
$clinical->payloadFromRequest($validated),
$owner,
$owner,
ChildWelfareWorkflowService::STAGE_FOLLOWUP,
SpecialtyClinicalRecord::STATUS_COMPLETED,
);
try {
$stages->setStage(
$organization,
$visit,
'child_welfare',
ChildWelfareWorkflowService::STAGE_FOLLOWUP,
$owner,
$owner,
);
} catch (\InvalidArgumentException) {
}
if ($workflow->shouldCompleteVisit($record->payload ?? [])) {
try {
$stages->setStage(
$organization,
$visit,
'child_welfare',
ChildWelfareWorkflowService::STAGE_COMPLETED,
$owner,
$owner,
);
} catch (\InvalidArgumentException) {
}
$visit->refresh();
if ($visit->status !== Visit::STATUS_COMPLETED) {
$visit->update([
'status' => Visit::STATUS_COMPLETED,
'completed_at' => $visit->completed_at ?? now(),
]);
}
$appointment = $visit->appointment;
if ($appointment && $appointment->status !== \App\Models\Appointment::STATUS_COMPLETED) {
$appointment->update([
'status' => \App\Models\Appointment::STATUS_COMPLETED,
'completed_at' => $appointment->completed_at ?? now(),
]);
}
}
return redirect()
->route('care.specialty.workspace', ['module' => 'child_welfare', 'visit' => $visit, 'tab' => 'followup'])
->with('success', 'Intervention / follow-up saved.');
}
public function reports(
Request $request,
SpecialtyModuleService $modules,
SpecialtyShellService $shell,
ChildWelfareAnalyticsService $analytics,
): View {
$this->authorizeAbility($request, 'consultations.view');
$this->assertChildWelfareAccess($request, $modules);
$organization = $this->organization($request);
$branchScope = app(\App\Services\Care\OrganizationResolver::class)->branchScope($this->member($request));
$report = $analytics->report(
$organization,
$this->ownerRef($request),
$branchScope,
$request->query('from'),
$request->query('to'),
);
return view('care.specialty.child-welfare.reports', [
'moduleKey' => 'child_welfare',
'definition' => $modules->definition('child_welfare'),
'shellNav' => $shell->navItems('child_welfare'),
'report' => $report,
'currency' => strtoupper((string) config('care.billing.currency', config('care.currency', 'GHS'))),
]);
}
public function printSummary(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyClinicalRecordService $clinical,
): View {
$this->authorizeAbility($request, 'consultations.view');
$this->assertChildWelfareAccess($request, $modules);
$this->assertVisit($request, $visit);
$visit->loadMissing(['patient', 'appointment.practitioner', 'branch']);
return view('care.specialty.child-welfare.print', [
'visit' => $visit,
'patient' => $visit->patient,
'intake' => $clinical->findForVisit($visit, 'child_welfare', 'cwc_intake'),
'assessment' => $clinical->findForVisit($visit, 'child_welfare', 'cwc_assessment'),
'plan' => $clinical->findForVisit($visit, 'child_welfare', 'cwc_plan'),
'followup' => $clinical->findForVisit($visit, 'child_welfare', 'cwc_followup'),
]);
}
}
@@ -171,6 +171,8 @@ class SpecialtyModuleController extends Controller
'dermatology' => 'history',
'podiatry' => 'history',
'fertility' => 'history',
'child_welfare' => 'intake',
'ambulance' => 'dispatch',
default => (collect(array_keys($shellTabs))
->first(fn (string $tab) => $clinical->recordTypeForTab($module, $tab) !== null)
?? 'clinical_notes'),
@@ -260,6 +262,8 @@ class SpecialtyModuleController extends Controller
'dermatology' => 'history',
'podiatry' => 'history',
'fertility' => 'history',
'child_welfare' => 'intake',
'ambulance' => 'dispatch',
default => (collect(array_keys($shellTabs))
->first(fn (string $tab) => $clinical->recordTypeForTab($module, $tab) !== null)
?? 'clinical_notes'),
@@ -1127,6 +1131,70 @@ class SpecialtyModuleController extends Controller
}
}
if ($module === 'child_welfare' && in_array($recordType, ['cwc_intake', 'cwc_assessment', 'cwc_plan'], true)) {
$workflow = app(\App\Services\Care\ChildWelfare\ChildWelfareWorkflowService::class);
$stageService = app(\App\Services\Care\SpecialtyVisitStageService::class);
$suggested = match ($recordType) {
'cwc_intake' => $workflow->stageFromIntake($record->payload ?? []),
'cwc_assessment' => $workflow->stageFromAssessment($record->payload ?? []),
'cwc_plan' => $workflow->stageFromPlan($record->payload ?? []),
default => null,
};
$current = $visit->specialty_stage;
$early = ['check_in', 'waiting', null, ''];
$order = [
'check_in' => 0,
'intake' => 1,
'assessment' => 2,
'plan' => 3,
'followup' => 4,
'completed' => 5,
];
if ($suggested && (! $current || in_array($current, $early, true))) {
try {
$stageService->setStage($organization, $visit, 'child_welfare', $suggested, $this->ownerRef($request), $this->ownerRef($request));
} catch (\InvalidArgumentException) {
}
} elseif ($suggested && $suggested !== $current && ($order[$suggested] ?? 0) > ($order[$current] ?? 0)) {
try {
$stageService->setStage($organization, $visit, 'child_welfare', $suggested, $this->ownerRef($request), $this->ownerRef($request));
} catch (\InvalidArgumentException) {
}
}
}
if ($module === 'ambulance' && in_array($recordType, ['amb_dispatch', 'amb_scene', 'amb_en_route'], true)) {
$workflow = app(\App\Services\Care\Ambulance\AmbulanceWorkflowService::class);
$stageService = app(\App\Services\Care\SpecialtyVisitStageService::class);
$suggested = match ($recordType) {
'amb_dispatch' => $workflow->stageFromDispatch($record->payload ?? []),
'amb_scene' => $workflow->stageFromScene($record->payload ?? []),
'amb_en_route' => $workflow->stageFromEnRoute($record->payload ?? []),
default => null,
};
$current = $visit->specialty_stage;
$early = ['check_in', 'waiting', null, ''];
$order = [
'check_in' => 0,
'dispatch' => 1,
'on_scene' => 2,
'transport' => 3,
'handover' => 4,
'completed' => 5,
];
if ($suggested && (! $current || in_array($current, $early, true))) {
try {
$stageService->setStage($organization, $visit, 'ambulance', $suggested, $this->ownerRef($request), $this->ownerRef($request));
} catch (\InvalidArgumentException) {
}
} elseif ($suggested && $suggested !== $current && ($order[$suggested] ?? 0) > ($order[$current] ?? 0)) {
try {
$stageService->setStage($organization, $visit, 'ambulance', $suggested, $this->ownerRef($request), $this->ownerRef($request));
} catch (\InvalidArgumentException) {
}
}
}
return redirect()
->route('care.specialty.workspace', [
'module' => $module,
@@ -1644,6 +1712,18 @@ class SpecialtyModuleController extends Controller
$fertilityCycle = null;
$fertilityStageCodes = [];
$fertilityStageFlow = [];
$childWelfareIntake = null;
$childWelfareAssessment = null;
$childWelfarePlan = null;
$childWelfareFollowup = null;
$childWelfareStageCodes = [];
$childWelfareStageFlow = [];
$ambulanceDispatch = null;
$ambulanceScene = null;
$ambulanceEnRoute = null;
$ambulanceHandover = null;
$ambulanceStageCodes = [];
$ambulanceStageFlow = [];
$activeTab = (string) $request->query('tab', array_key_first($shell->workspaceTabs($module)) ?: 'overview');
$clinical = app(SpecialtyClinicalRecordService::class);
@@ -1926,6 +2006,24 @@ class SpecialtyModuleController extends Controller
$fertilityStageFlow = app(\App\Services\Care\Fertility\FertilityWorkflowService::class)->stageFlow();
}
if ($module === 'child_welfare') {
$childWelfareIntake = $clinical->findForVisit($workspaceVisit, 'child_welfare', 'cwc_intake');
$childWelfareAssessment = $clinical->findForVisit($workspaceVisit, 'child_welfare', 'cwc_assessment');
$childWelfarePlan = $clinical->findForVisit($workspaceVisit, 'child_welfare', 'cwc_plan');
$childWelfareFollowup = $clinical->findForVisit($workspaceVisit, 'child_welfare', 'cwc_followup');
$childWelfareStageCodes = collect($shell->stages('child_welfare'))->pluck('code')->all();
$childWelfareStageFlow = app(\App\Services\Care\ChildWelfare\ChildWelfareWorkflowService::class)->stageFlow();
}
if ($module === 'ambulance') {
$ambulanceDispatch = $clinical->findForVisit($workspaceVisit, 'ambulance', 'amb_dispatch');
$ambulanceScene = $clinical->findForVisit($workspaceVisit, 'ambulance', 'amb_scene');
$ambulanceEnRoute = $clinical->findForVisit($workspaceVisit, 'ambulance', 'amb_en_route');
$ambulanceHandover = $clinical->findForVisit($workspaceVisit, 'ambulance', 'amb_handover');
$ambulanceStageCodes = collect($shell->stages('ambulance'))->pluck('code')->all();
$ambulanceStageFlow = app(\App\Services\Care\Ambulance\AmbulanceWorkflowService::class)->stageFlow();
}
if ($patientHeader !== null) {
$patientHeader['clinical_alerts'] = $clinicalAlerts;
}
@@ -2135,6 +2233,18 @@ class SpecialtyModuleController extends Controller
'fertilityCycle' => $fertilityCycle,
'fertilityStageCodes' => $fertilityStageCodes,
'fertilityStageFlow' => $fertilityStageFlow,
'childWelfareIntake' => $childWelfareIntake,
'childWelfareAssessment' => $childWelfareAssessment,
'childWelfarePlan' => $childWelfarePlan,
'childWelfareFollowup' => $childWelfareFollowup,
'childWelfareStageCodes' => $childWelfareStageCodes,
'childWelfareStageFlow' => $childWelfareStageFlow,
'ambulanceDispatch' => $ambulanceDispatch,
'ambulanceScene' => $ambulanceScene,
'ambulanceEnRoute' => $ambulanceEnRoute,
'ambulanceHandover' => $ambulanceHandover,
'ambulanceStageCodes' => $ambulanceStageCodes,
'ambulanceStageFlow' => $ambulanceStageFlow,
'queueStubs' => $modules->queueStubsFor($organization, $module),
'branchId' => $branchId,
'branchLabel' => $branchLabel,