|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Filament\Organizations\Resources\Cases\Pages\InterventionPlan; |
| 6 | + |
| 7 | +use App\Actions\BackAction; |
| 8 | +use App\Filament\Organizations\Resources\Cases\CaseResource; |
| 9 | +use App\Models\Beneficiary; |
| 10 | +use App\Models\InterventionService; |
| 11 | +use Carbon\Carbon; |
| 12 | +use Filament\Infolists\Components\TextEntry; |
| 13 | +use Filament\Resources\Pages\ViewRecord; |
| 14 | +use Filament\Schemas\Components\Section; |
| 15 | +use Filament\Schemas\Schema; |
| 16 | +use Illuminate\Contracts\Support\Htmlable; |
| 17 | + |
| 18 | +class ViewCaseInterventionService extends ViewRecord |
| 19 | +{ |
| 20 | + protected static string $resource = CaseResource::class; |
| 21 | + |
| 22 | + public ?InterventionService $interventionService = null; |
| 23 | + |
| 24 | + public function mount(int|string $record): void |
| 25 | + { |
| 26 | + $this->record = $this->resolveRecord($record); |
| 27 | + |
| 28 | + if (! $this->record instanceof Beneficiary) { |
| 29 | + abort(404); |
| 30 | + } |
| 31 | + |
| 32 | + $plan = $this->record->interventionPlan; |
| 33 | + if (! $plan) { |
| 34 | + $this->redirect(CaseResource::getUrl('view_intervention_plan', ['record' => $this->record])); |
| 35 | + |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + $interventionServiceId = request()->route('interventionService'); |
| 40 | + $this->interventionService = InterventionService::query() |
| 41 | + ->where('intervention_plan_id', $plan->id) |
| 42 | + ->where('id', $interventionServiceId) |
| 43 | + ->with([ |
| 44 | + 'organizationServiceWithoutStatusCondition.serviceWithoutStatusCondition', |
| 45 | + 'specialist.user', |
| 46 | + 'specialist.role', |
| 47 | + ]) |
| 48 | + ->firstOrFail(); |
| 49 | + |
| 50 | + $this->authorizeAccess(); |
| 51 | + } |
| 52 | + |
| 53 | + protected function authorizeAccess(): void |
| 54 | + { |
| 55 | + abort_unless(CaseResource::canView($this->record), 403); |
| 56 | + } |
| 57 | + |
| 58 | + public function getTitle(): string|Htmlable |
| 59 | + { |
| 60 | + return $this->interventionService?->organizationServiceWithoutStatusCondition?->serviceWithoutStatusCondition?->name ?? __('intervention_plan.headings.services'); |
| 61 | + } |
| 62 | + |
| 63 | + public function getBreadcrumbs(): array |
| 64 | + { |
| 65 | + $record = $this->getRecord(); |
| 66 | + |
| 67 | + return [ |
| 68 | + CaseResource::getUrl('index') => __('case.view.breadcrumb_all'), |
| 69 | + CaseResource::getUrl('view', ['record' => $record]) => $record instanceof Beneficiary ? $record->getBreadcrumb() : '', |
| 70 | + CaseResource::getUrl('view_intervention_plan', ['record' => $record]) => __('intervention_plan.headings.view_page'), |
| 71 | + '' => $this->interventionService?->organizationServiceWithoutStatusCondition?->serviceWithoutStatusCondition?->name ?? __('intervention_plan.headings.services'), |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + protected function getHeaderActions(): array |
| 76 | + { |
| 77 | + return [ |
| 78 | + BackAction::make() |
| 79 | + ->url(CaseResource::getUrl('view_intervention_plan', ['record' => $this->getRecord()])), |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + public function infolist(Schema $schema): Schema |
| 84 | + { |
| 85 | + $service = $this->interventionService; |
| 86 | + |
| 87 | + return $schema |
| 88 | + ->record($service) |
| 89 | + ->components([ |
| 90 | + Section::make(__('intervention_plan.headings.services')) |
| 91 | + ->schema([ |
| 92 | + TextEntry::make('organizationServiceWithoutStatusCondition.serviceWithoutStatusCondition.name') |
| 93 | + ->label(__('intervention_plan.labels.service')) |
| 94 | + ->placeholder('—'), |
| 95 | + TextEntry::make('specialist.name_role') |
| 96 | + ->label(__('intervention_plan.labels.specialist')) |
| 97 | + ->placeholder('—'), |
| 98 | + TextEntry::make('institution') |
| 99 | + ->label(__('intervention_plan.labels.responsible_institution')) |
| 100 | + ->placeholder('—'), |
| 101 | + TextEntry::make('start_date_interval') |
| 102 | + ->label(__('intervention_plan.labels.start_date_interval')) |
| 103 | + ->formatStateUsing(fn ($state) => self::formatDate($state)) |
| 104 | + ->placeholder('—'), |
| 105 | + TextEntry::make('end_date_interval') |
| 106 | + ->label(__('intervention_plan.labels.end_date_interval')) |
| 107 | + ->formatStateUsing(fn ($state) => self::formatDate($state)) |
| 108 | + ->placeholder('—'), |
| 109 | + TextEntry::make('interventions_count') |
| 110 | + ->label(__('intervention_plan.labels.interventions_count')) |
| 111 | + ->state((string) $service->beneficiaryInterventions()->count()) |
| 112 | + ->placeholder('0'), |
| 113 | + TextEntry::make('meetings_count') |
| 114 | + ->label(__('intervention_plan.labels.meetings_count')) |
| 115 | + ->state((string) $service->meetings()->count()) |
| 116 | + ->placeholder('0'), |
| 117 | + ]) |
| 118 | + ->columns(2), |
| 119 | + ]); |
| 120 | + } |
| 121 | + |
| 122 | + private static function formatDate(?string $state): string |
| 123 | + { |
| 124 | + if ($state === null || $state === '') { |
| 125 | + return '—'; |
| 126 | + } |
| 127 | + |
| 128 | + try { |
| 129 | + return Carbon::parse($state)->translatedFormat('d.m.Y'); |
| 130 | + } catch (\Throwable) { |
| 131 | + return '—'; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments