diff --git a/app/Filament/Resources/GuardPatrolEntryResource.php b/app/Filament/Resources/GuardPatrolEntryResource.php index cd2a142..ddc1060 100644 --- a/app/Filament/Resources/GuardPatrolEntryResource.php +++ b/app/Filament/Resources/GuardPatrolEntryResource.php @@ -77,42 +77,48 @@ class GuardPatrolEntryResource extends Resource ]) ->hint(fn ($get) => $get('gPePlantError') ? $get('gPePlantError') : null) ->hintColor('danger'), - Forms\Components\Select::make('guard_name_id') + // Forms\Components\Select::make('guard_name_id') + // ->label('Guard Name') + // // ->relationship('guardNames', 'name') + // ->options(function (callable $get) { + // $plantId = $get('plant_id'); + // if (! $plantId) { + // return []; + // } + + // return GuardName::where('plant_id', $plantId) + // ->pluck('name', 'id') + // ->toArray(); + // }) + // ->required() + // ->reactive() + // ->default(function () { + // return optional(GuardPatrolEntry::where('created_by', Filament::auth()->user()?->name)->latest()->first())->guard_name_id; + // }) + // ->disabled(fn (Get $get) => ! empty($get('id'))) + // ->afterStateUpdated(function ($state, callable $set, callable $get) { + // $guardName = $get('guard_name_id'); + // if (! $guardName) { + // $set('gPeGuardNameError', 'Please select a guard name first.'); + + // return; + // } else { + // $set('patrol_time', now()->format('Y-m-d H:i:s')); + // $set('updated_by', Filament::auth()->user()?->name); + // $set('gPeGuardNameError', null); + // } + // }) + // ->extraAttributes(fn ($get) => [ + // 'class' => $get('gPeGuardNameError') ? 'border-red-500' : '', + // ]) + // ->hint(fn ($get) => $get('gPeGuardNameError') ? $get('gPeGuardNameError') : null) + // ->hintColor('danger'), + Forms\Components\TextInput::make('guard_name') ->label('Guard Name') - // ->relationship('guardNames', 'name') - ->options(function (callable $get) { - $plantId = $get('plant_id'); - if (! $plantId) { - return []; - } + ->default(fn () => Filament::auth()->user()?->name) + ->readOnly() + ->dehydrated(), - return GuardName::where('plant_id', $plantId) - ->pluck('name', 'id') - ->toArray(); - }) - ->required() - ->reactive() - ->default(function () { - return optional(GuardPatrolEntry::where('created_by', Filament::auth()->user()?->name)->latest()->first())->guard_name_id; - }) - ->disabled(fn (Get $get) => ! empty($get('id'))) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $guardName = $get('guard_name_id'); - if (! $guardName) { - $set('gPeGuardNameError', 'Please select a guard name first.'); - - return; - } else { - $set('patrol_time', now()->format('Y-m-d H:i:s')); - $set('updated_by', Filament::auth()->user()?->name); - $set('gPeGuardNameError', null); - } - }) - ->extraAttributes(fn ($get) => [ - 'class' => $get('gPeGuardNameError') ? 'border-red-500' : '', - ]) - ->hint(fn ($get) => $get('gPeGuardNameError') ? $get('gPeGuardNameError') : null) - ->hintColor('danger'), Forms\Components\Hidden::make('check_point_name')// TextInput ->label('Check Point Name') ->reactive() @@ -123,50 +129,88 @@ class GuardPatrolEntryResource extends Resource ->extraAttributes([ 'x-on:keydown.enter.prevent' => '$wire.processCheckPointName()', ]), - Forms\Components\Select::make('check_point_name_id') - ->label('Check Point Name') - // ->relationship('checkPointNames', 'name') - ->options(function (callable $get) { - $plantId = $get('plant_id'); - if (! $plantId) { - return []; - } + // Forms\Components\Select::make('check_point_name_id') + // ->label('Check Point Name') + // // ->relationship('checkPointNames', 'name') + // ->options(function (callable $get) { + // $plantId = $get('plant_id'); + // if (! $plantId) { + // return []; + // } - return CheckPointName::where('plant_id', $plantId) - ->pluck('name', 'id') - ->toArray(); - }) + // return CheckPointName::where('plant_id', $plantId) + // ->pluck('name', 'id') + // ->toArray(); + // }) + // ->required() + // ->reactive() + // // ->default(function () { + // // return optional(GuardPatrolEntry::where('created_by', Filament::auth()->user()?->name)->latest()->first())->check_point_name_id; + // // }) + // ->disabled(fn (Get $get) => ! empty($get('id'))) + // ->afterStateUpdated(function ($state, callable $set, callable $get) { + // $checkPointName = $get('check_point_name_id'); + // if (! $checkPointName) { + // $set('check_point_name_id', null); + // $set('gPeCheckPointNameError', 'Please select a check point name first.'); + + // return; + // } else { + // $set('patrol_time', now()->format('Y-m-d H:i:s')); + // $set('updated_by', Filament::auth()->user()?->name); + // $set('gPeCheckPointNameError', null); + // } + // }) + // ->extraAttributes(fn ($get) => [ + // 'class' => $get('gPeCheckPointNameError') ? 'border-red-500' : '', + // ]) + // ->hint(fn ($get) => $get('gPeCheckPointNameError') ? $get('gPeCheckPointNameError') : null) + // ->hintColor('danger') + // ->rule(function (callable $get) { + // return Rule::unique('guard_patrol_entries', 'check_point_name_id') + // ->where('guard_name_id', $get('guard_name_id')) + // ->where('patrol_time', now()) + // ->where('plant_id', $get('plant_id')) + // ->ignore($get('id')); + // }), + + Forms\Components\Hidden::make('check_point_name_id') + ->dehydrated(true), + Forms\Components\TextInput::make('check_point_name') + ->label('Check Point Name') ->required() ->reactive() - // ->default(function () { - // return optional(GuardPatrolEntry::where('created_by', Filament::auth()->user()?->name)->latest()->first())->check_point_name_id; - // }) ->disabled(fn (Get $get) => ! empty($get('id'))) ->afterStateUpdated(function ($state, callable $set, callable $get) { - $checkPointName = $get('check_point_name_id'); - if (! $checkPointName) { + + if (blank($state)) { $set('check_point_name_id', null); - $set('gPeCheckPointNameError', 'Please select a check point name first.'); + $set('gPeCheckPointNameError', 'Please enter a check point name.'); return; - } else { - $set('patrol_time', now()->format('Y-m-d H:i:s')); - $set('updated_by', Filament::auth()->user()?->name); - $set('gPeCheckPointNameError', null); } + + $checkPoint = CheckPointName::where('plant_id', $get('plant_id')) + ->whereRaw('LOWER(name) = ?', [strtolower(trim($state))]) + ->first(); + + if (! $checkPoint) { + $set('check_point_name_id', null); + $set('gPeCheckPointNameError', 'Invalid check point name.'); + + return; + } + + $set('check_point_name_id', $checkPoint->id); + $set('patrol_time', now()->format('Y-m-d H:i:s')); + $set('updated_by', Filament::auth()->user()?->name); + $set('gPeCheckPointNameError', null); }) ->extraAttributes(fn ($get) => [ 'class' => $get('gPeCheckPointNameError') ? 'border-red-500' : '', ]) - ->hint(fn ($get) => $get('gPeCheckPointNameError') ? $get('gPeCheckPointNameError') : null) - ->hintColor('danger') - ->rule(function (callable $get) { - return Rule::unique('guard_patrol_entries', 'check_point_name_id') - ->where('guard_name_id', $get('guard_name_id')) - ->where('patrol_time', now()) - ->where('plant_id', $get('plant_id')) - ->ignore($get('id')); - }), + ->hint(fn ($get) => $get('gPeCheckPointNameError')) + ->hintColor('danger'), Forms\Components\TextInput::make('reader_code') ->label('Reader Code') ->hidden(fn (Get $get) => ! $get('id')) diff --git a/app/Filament/Resources/GuardPatrolEntryResource/Pages/CreateGuardPatrolEntry.php b/app/Filament/Resources/GuardPatrolEntryResource/Pages/CreateGuardPatrolEntry.php index 08ee645..98cc7f4 100644 --- a/app/Filament/Resources/GuardPatrolEntryResource/Pages/CreateGuardPatrolEntry.php +++ b/app/Filament/Resources/GuardPatrolEntryResource/Pages/CreateGuardPatrolEntry.php @@ -3,6 +3,10 @@ namespace App\Filament\Resources\GuardPatrolEntryResource\Pages; use App\Filament\Resources\GuardPatrolEntryResource; +use App\Models\CheckPointName; +use App\Models\CheckPointTime; +use App\Models\GuardName; +use App\Models\GuardPatrolEntry; use Filament\Actions; use Filament\Facades\Filament; use Filament\Notifications\Notification; @@ -29,6 +33,100 @@ class CreateGuardPatrolEntry extends CreateRecord // public ?array $data = null; + protected function mutateFormDataBeforeCreate(array $data): array + { + $guardId = GuardName::where( + 'name', + Filament::auth()->user()?->name + )->value('id'); + + if (! $guardId) { + + Notification::make() + ->title('Guard Name Not Matched') + ->body('Logged-in user name was not found in Guard Name Master.') + ->danger() + ->send(); + $this->halt(); + } + + $data['guard_name_id'] = $guardId; + + $checkPoint = CheckPointName::where('plant_id', $data['plant_id']) + ->where('name', trim($data['check_point_name'])) + ->value('id'); + + if (! $checkPoint) { + Notification::make() + ->title('Invalid Check Point') + ->body('The entered check point name does not exist.') + ->danger() + ->send(); + $this->halt(); + } + + // $lastScan = GuardPatrolEntry::where('plant_id', $data['plant_id']) + // ->where('guard_name_id', $guardId) + // ->latest('id') + // ->first(); + + // if ($lastScan) { + + // $previousPoint = $lastScan->check_point_name_id; + + // $route = CheckPointTime::where('plant_id', $data['plant_id']) + // ->where('check_point1_id', $previousPoint) + // ->where('check_point2_id', $checkPoint->id) + // ->first(); + + + // if (! $route) { + + // Notification::make() + // ->title('Wrong Check Point Order') + // ->body('This checkpoint is not the next allowed point.') + // ->danger() + // ->send(); + + // $this->halt(); + // } + + + // } else { + + + // // First scan must start from sequence 1 + + // $firstRoute = CheckPointTime::where('plant_id', $data['plant_id']) + // ->where('check_point2_id', $checkPoint->id) + // ->where('sequence_number', 1) + // ->first(); + + + // if (! $firstRoute) { + + // Notification::make() + // ->title('Invalid Starting Point') + // ->body('Patrol must start from the first checkpoint.') + // ->danger() + // ->send(); + + // $this->halt(); + // } + + // } + + + + $data['check_point_name_id'] = $checkPoint; + + + unset($data['check_point_name']); + + + return $data; + } + public function processCheckPointName() {