Add full Physiotherapy and Maternity specialty clinical suites.
Deploy Ladill Care / deploy (push) Successful in 52s

Mirror Eye Care depth with stages, workspace tabs, clinical JSON, stage Move→tab routing, reports/print, demo seeds, and suite feature tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
isaacclad
2026-07-19 16:39:22 +00:00
co-authored by Cursor
parent 2f724daf49
commit cfa71c2c15
30 changed files with 2794 additions and 24 deletions
@@ -0,0 +1,216 @@
<?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\Maternity\MaternityAnalyticsService;
use App\Services\Care\Maternity\MaternityWorkflowService;
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 MaternityWorkspaceController extends Controller
{
use ScopesToAccount;
protected function assertMaternityAccess(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanAccess($organization, $this->member($request), 'maternity'), 403);
}
protected function assertMaternityManage(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanManage($organization, $this->member($request), 'maternity'), 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->assertMaternityManage($request, $modules);
$this->assertVisit($request, $visit);
$validated = $request->validate([
'stage' => ['required', 'string', 'max:32'],
]);
try {
$stages->setStage(
$this->organization($request),
$visit,
'maternity',
$validated['stage'],
$this->ownerRef($request),
$this->actorRef($request),
);
} catch (\InvalidArgumentException $e) {
return back()->with('error', $e->getMessage());
}
return redirect()
->route('care.specialty.workspace', [
'module' => 'maternity',
'visit' => $visit,
'tab' => $shell->workspaceTabForStage('maternity', $validated['stage']),
])
->with('success', 'Visit stage updated.');
}
public function savePostnatal(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyClinicalRecordService $clinical,
SpecialtyVisitStageService $stages,
MaternityWorkflowService $workflow,
): RedirectResponse {
$this->authorizeAbility($request, 'consultations.manage');
$this->assertMaternityManage($request, $modules);
$this->assertVisit($request, $visit);
$organization = $this->organization($request);
$owner = $this->ownerRef($request);
$payload = (array) $request->input('payload', []);
foreach ($clinical->fieldsFor('maternity', 'postnatal_note') as $field) {
if (($field['type'] ?? '') === 'boolean') {
$payload[$field['name']] = $request->boolean('payload.'.$field['name']);
}
}
$request->merge(['payload' => $payload, 'tab' => 'postnatal']);
$validated = $request->validate(array_merge([
'tab' => ['required', 'string'],
], $clinical->validationRules('maternity', 'postnatal_note')));
$record = $clinical->upsert(
$organization,
$visit,
'maternity',
'postnatal_note',
$clinical->payloadFromRequest($validated),
$owner,
$owner,
MaternityWorkflowService::STAGE_POSTNATAL,
SpecialtyClinicalRecord::STATUS_COMPLETED,
);
try {
$stages->setStage(
$organization,
$visit,
'maternity',
MaternityWorkflowService::STAGE_POSTNATAL,
$owner,
$owner,
);
} catch (\InvalidArgumentException) {
}
if ($workflow->shouldCompleteVisit($record->payload ?? [])) {
try {
$stages->setStage(
$organization,
$visit,
'maternity',
MaternityWorkflowService::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' => 'maternity', 'visit' => $visit, 'tab' => 'postnatal'])
->with('success', 'Delivery / postnatal note saved.');
}
public function reports(
Request $request,
SpecialtyModuleService $modules,
SpecialtyShellService $shell,
MaternityAnalyticsService $analytics,
): View {
$this->authorizeAbility($request, 'consultations.view');
$this->assertMaternityAccess($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.maternity.reports', [
'moduleKey' => 'maternity',
'definition' => $modules->definition('maternity'),
'shellNav' => $shell->navItems('maternity'),
'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->assertMaternityAccess($request, $modules);
$this->assertVisit($request, $visit);
$visit->loadMissing(['patient', 'appointment.practitioner', 'branch']);
return view('care.specialty.maternity.print', [
'visit' => $visit,
'patient' => $visit->patient,
'history' => $clinical->findForVisit($visit, 'maternity', 'anc_history'),
'exam' => $clinical->findForVisit($visit, 'maternity', 'obstetric_exam'),
'fetal' => $clinical->findForVisit($visit, 'maternity', 'fetal_notes'),
'investigation' => $clinical->findForVisit($visit, 'maternity', 'mat_investigation'),
'plan' => $clinical->findForVisit($visit, 'maternity', 'mat_plan'),
'postnatal' => $clinical->findForVisit($visit, 'maternity', 'postnatal_note'),
]);
}
}
@@ -0,0 +1,208 @@
<?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\Physiotherapy\PhysiotherapyAnalyticsService;
use App\Services\Care\Physiotherapy\PhysiotherapyWorkflowService;
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 PhysiotherapyWorkspaceController extends Controller
{
use ScopesToAccount;
protected function assertPhysiotherapyAccess(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanAccess($organization, $this->member($request), 'physiotherapy'), 403);
}
protected function assertPhysiotherapyManage(Request $request, SpecialtyModuleService $modules): void
{
$organization = $this->organization($request);
abort_unless($modules->memberCanManage($organization, $this->member($request), 'physiotherapy'), 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->assertPhysiotherapyManage($request, $modules);
$this->assertVisit($request, $visit);
$validated = $request->validate([
'stage' => ['required', 'string', 'max:32'],
]);
try {
$stages->setStage(
$this->organization($request),
$visit,
'physiotherapy',
$validated['stage'],
$this->ownerRef($request),
$this->actorRef($request),
);
} catch (\InvalidArgumentException $e) {
return back()->with('error', $e->getMessage());
}
return redirect()
->route('care.specialty.workspace', [
'module' => 'physiotherapy',
'visit' => $visit,
'tab' => $shell->workspaceTabForStage('physiotherapy', $validated['stage']),
])
->with('success', 'Visit stage updated.');
}
public function saveSession(
Request $request,
Visit $visit,
SpecialtyModuleService $modules,
SpecialtyClinicalRecordService $clinical,
SpecialtyVisitStageService $stages,
PhysiotherapyWorkflowService $workflow,
): RedirectResponse {
$this->authorizeAbility($request, 'consultations.manage');
$this->assertPhysiotherapyManage($request, $modules);
$this->assertVisit($request, $visit);
$organization = $this->organization($request);
$owner = $this->ownerRef($request);
$payload = (array) $request->input('payload', []);
foreach ($clinical->fieldsFor('physiotherapy', 'pt_session') as $field) {
if (($field['type'] ?? '') === 'boolean') {
$payload[$field['name']] = $request->boolean('payload.'.$field['name']);
}
}
$request->merge(['payload' => $payload, 'tab' => 'session']);
$validated = $request->validate(array_merge([
'tab' => ['required', 'string'],
], $clinical->validationRules('physiotherapy', 'pt_session')));
$record = $clinical->upsert(
$organization,
$visit,
'physiotherapy',
'pt_session',
$clinical->payloadFromRequest($validated),
$owner,
$owner,
PhysiotherapyWorkflowService::STAGE_SESSION,
SpecialtyClinicalRecord::STATUS_COMPLETED,
);
$suggested = $workflow->stageFromSession($record->payload ?? []);
try {
$stages->setStage($organization, $visit, 'physiotherapy', $suggested, $owner, $owner);
} catch (\InvalidArgumentException) {
}
if ($workflow->shouldCompleteVisit($record->payload ?? [])) {
try {
$stages->setStage(
$organization,
$visit,
'physiotherapy',
PhysiotherapyWorkflowService::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' => 'physiotherapy', 'visit' => $visit, 'tab' => 'session'])
->with('success', 'Therapy session saved.');
}
public function reports(
Request $request,
SpecialtyModuleService $modules,
SpecialtyShellService $shell,
PhysiotherapyAnalyticsService $analytics,
): View {
$this->authorizeAbility($request, 'consultations.view');
$this->assertPhysiotherapyAccess($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.physiotherapy.reports', [
'moduleKey' => 'physiotherapy',
'definition' => $modules->definition('physiotherapy'),
'shellNav' => $shell->navItems('physiotherapy'),
'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->assertPhysiotherapyAccess($request, $modules);
$this->assertVisit($request, $visit);
$visit->loadMissing(['patient', 'appointment.practitioner', 'branch']);
return view('care.specialty.physiotherapy.print', [
'visit' => $visit,
'patient' => $visit->patient,
'assessment' => $clinical->findForVisit($visit, 'physiotherapy', 'pt_assessment'),
'plan' => $clinical->findForVisit($visit, 'physiotherapy', 'pt_plan'),
'session' => $clinical->findForVisit($visit, 'physiotherapy', 'pt_session'),
'homeProgram' => $clinical->findForVisit($visit, 'physiotherapy', 'pt_home_program'),
]);
}
}
@@ -154,6 +154,8 @@ class SpecialtyModuleController extends Controller
'emergency' => 'triage',
'blood_bank' => 'requests',
'ophthalmology' => 'refraction',
'physiotherapy' => 'assessment',
'maternity' => 'history',
default => (collect(array_keys($shellTabs))
->first(fn (string $tab) => $clinical->recordTypeForTab($module, $tab) !== null)
?? 'clinical_notes'),
@@ -226,6 +228,8 @@ class SpecialtyModuleController extends Controller
'emergency' => 'triage',
'blood_bank' => 'requests',
'ophthalmology' => 'refraction',
'physiotherapy' => 'assessment',
'maternity' => 'history',
default => (collect(array_keys($shellTabs))
->first(fn (string $tab) => $clinical->recordTypeForTab($module, $tab) !== null)
?? 'clinical_notes'),
@@ -409,6 +413,103 @@ class SpecialtyModuleController extends Controller
}
}
if ($module === 'physiotherapy' && in_array($recordType, ['pt_assessment', 'pt_plan', 'pt_session', 'pt_home_program'], true)) {
$workflow = app(\App\Services\Care\Physiotherapy\PhysiotherapyWorkflowService::class);
$stageService = app(\App\Services\Care\SpecialtyVisitStageService::class);
$suggested = match ($recordType) {
'pt_assessment' => $workflow->stageFromAssessment($record->payload ?? []),
'pt_plan' => $workflow->stageFromPlan($record->payload ?? []),
'pt_session' => $workflow->stageFromSession($record->payload ?? []),
'pt_home_program' => $workflow->stageFromHomeProgram($record->payload ?? []),
default => null,
};
$current = $visit->specialty_stage;
$early = ['check_in', 'waiting', null, ''];
$order = [
'check_in' => 0,
'assessment' => 1,
'treatment_plan' => 2,
'session' => 3,
'progress' => 4,
'completed' => 5,
];
if ($suggested && (! $current || in_array($current, $early, true))) {
try {
$stageService->setStage(
$organization,
$visit,
'physiotherapy',
$suggested,
$this->ownerRef($request),
$this->ownerRef($request),
);
} catch (\InvalidArgumentException) {
}
} elseif ($suggested && $suggested !== $current && ($order[$suggested] ?? 0) > ($order[$current] ?? 0)) {
try {
$stageService->setStage(
$organization,
$visit,
'physiotherapy',
$suggested,
$this->ownerRef($request),
$this->ownerRef($request),
);
} catch (\InvalidArgumentException) {
}
}
}
if ($module === 'maternity' && in_array($recordType, ['anc_history', 'obstetric_exam', 'fetal_notes', 'mat_investigation', 'mat_plan'], true)) {
$workflow = app(\App\Services\Care\Maternity\MaternityWorkflowService::class);
$stageService = app(\App\Services\Care\SpecialtyVisitStageService::class);
$suggested = match ($recordType) {
'anc_history' => $workflow->stageFromHistory($record->payload ?? []),
'obstetric_exam' => $workflow->stageFromExam($record->payload ?? []),
'fetal_notes' => $workflow->stageFromFetal($record->payload ?? []),
'mat_investigation' => \App\Services\Care\Maternity\MaternityWorkflowService::STAGE_INVESTIGATION,
'mat_plan' => $workflow->stageFromPlan($record->payload ?? []),
default => null,
};
$current = $visit->specialty_stage;
$early = ['check_in', 'waiting', null, ''];
$order = [
'check_in' => 0,
'history' => 1,
'exam' => 2,
'investigation' => 3,
'plan' => 4,
'postnatal' => 5,
'completed' => 6,
];
if ($suggested && (! $current || in_array($current, $early, true))) {
try {
$stageService->setStage(
$organization,
$visit,
'maternity',
$suggested,
$this->ownerRef($request),
$this->ownerRef($request),
);
} catch (\InvalidArgumentException) {
}
} elseif ($suggested && $suggested !== $current && ($order[$suggested] ?? 0) > ($order[$current] ?? 0)) {
try {
$stageService->setStage(
$organization,
$visit,
'maternity',
$suggested,
$this->ownerRef($request),
$this->ownerRef($request),
);
} catch (\InvalidArgumentException) {
}
}
}
return redirect()
->route('care.specialty.workspace', [
'module' => $module,
@@ -809,6 +910,20 @@ class SpecialtyModuleController extends Controller
$ophthalmologyProcedure = null;
$ophthalmologyStageCodes = [];
$ophthalmologyStageFlow = [];
$physiotherapyAssessment = null;
$physiotherapyPlan = null;
$physiotherapySession = null;
$physiotherapyHomeProgram = null;
$physiotherapyStageCodes = [];
$physiotherapyStageFlow = [];
$maternityHistory = null;
$maternityExam = null;
$maternityFetal = null;
$maternityInvestigation = null;
$maternityPlan = null;
$maternityPostnatal = null;
$maternityStageCodes = [];
$maternityStageFlow = [];
$activeTab = (string) $request->query('tab', array_key_first($shell->workspaceTabs($module)) ?: 'overview');
$clinical = app(SpecialtyClinicalRecordService::class);
@@ -925,6 +1040,26 @@ class SpecialtyModuleController extends Controller
$ophthalmologyStageFlow = app(\App\Services\Care\Ophthalmology\OphthalmologyWorkflowService::class)->stageFlow();
}
if ($module === 'physiotherapy') {
$physiotherapyAssessment = $clinical->findForVisit($workspaceVisit, 'physiotherapy', 'pt_assessment');
$physiotherapyPlan = $clinical->findForVisit($workspaceVisit, 'physiotherapy', 'pt_plan');
$physiotherapySession = $clinical->findForVisit($workspaceVisit, 'physiotherapy', 'pt_session');
$physiotherapyHomeProgram = $clinical->findForVisit($workspaceVisit, 'physiotherapy', 'pt_home_program');
$physiotherapyStageCodes = collect($shell->stages('physiotherapy'))->pluck('code')->all();
$physiotherapyStageFlow = app(\App\Services\Care\Physiotherapy\PhysiotherapyWorkflowService::class)->stageFlow();
}
if ($module === 'maternity') {
$maternityHistory = $clinical->findForVisit($workspaceVisit, 'maternity', 'anc_history');
$maternityExam = $clinical->findForVisit($workspaceVisit, 'maternity', 'obstetric_exam');
$maternityFetal = $clinical->findForVisit($workspaceVisit, 'maternity', 'fetal_notes');
$maternityInvestigation = $clinical->findForVisit($workspaceVisit, 'maternity', 'mat_investigation');
$maternityPlan = $clinical->findForVisit($workspaceVisit, 'maternity', 'mat_plan');
$maternityPostnatal = $clinical->findForVisit($workspaceVisit, 'maternity', 'postnatal_note');
$maternityStageCodes = collect($shell->stages('maternity'))->pluck('code')->all();
$maternityStageFlow = app(\App\Services\Care\Maternity\MaternityWorkflowService::class)->stageFlow();
}
if ($patientHeader !== null) {
$patientHeader['clinical_alerts'] = $clinicalAlerts;
}
@@ -1019,6 +1154,20 @@ class SpecialtyModuleController extends Controller
'ophthalmologyProcedure' => $ophthalmologyProcedure,
'ophthalmologyStageCodes' => $ophthalmologyStageCodes,
'ophthalmologyStageFlow' => $ophthalmologyStageFlow,
'physiotherapyAssessment' => $physiotherapyAssessment,
'physiotherapyPlan' => $physiotherapyPlan,
'physiotherapySession' => $physiotherapySession,
'physiotherapyHomeProgram' => $physiotherapyHomeProgram,
'physiotherapyStageCodes' => $physiotherapyStageCodes,
'physiotherapyStageFlow' => $physiotherapyStageFlow,
'maternityHistory' => $maternityHistory,
'maternityExam' => $maternityExam,
'maternityFetal' => $maternityFetal,
'maternityInvestigation' => $maternityInvestigation,
'maternityPlan' => $maternityPlan,
'maternityPostnatal' => $maternityPostnatal,
'maternityStageCodes' => $maternityStageCodes,
'maternityStageFlow' => $maternityStageFlow,
'queueStubs' => $modules->queueStubsFor($organization, $module),
'branchId' => $branchId,
'branchLabel' => $branchLabel,