Merge pull request 'Added logic in guard patrol entry page' (#767) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 1m19s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 1m19s
Reviewed-on: #767
This commit was merged in pull request #767.
This commit is contained in:
@@ -77,42 +77,48 @@ class GuardPatrolEntryResource extends Resource
|
|||||||
])
|
])
|
||||||
->hint(fn ($get) => $get('gPePlantError') ? $get('gPePlantError') : null)
|
->hint(fn ($get) => $get('gPePlantError') ? $get('gPePlantError') : null)
|
||||||
->hintColor('danger'),
|
->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')
|
->label('Guard Name')
|
||||||
// ->relationship('guardNames', 'name')
|
->default(fn () => Filament::auth()->user()?->name)
|
||||||
->options(function (callable $get) {
|
->readOnly()
|
||||||
$plantId = $get('plant_id');
|
->dehydrated(),
|
||||||
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\Hidden::make('check_point_name')// TextInput
|
Forms\Components\Hidden::make('check_point_name')// TextInput
|
||||||
->label('Check Point Name')
|
->label('Check Point Name')
|
||||||
->reactive()
|
->reactive()
|
||||||
@@ -123,50 +129,88 @@ class GuardPatrolEntryResource extends Resource
|
|||||||
->extraAttributes([
|
->extraAttributes([
|
||||||
'x-on:keydown.enter.prevent' => '$wire.processCheckPointName()',
|
'x-on:keydown.enter.prevent' => '$wire.processCheckPointName()',
|
||||||
]),
|
]),
|
||||||
Forms\Components\Select::make('check_point_name_id')
|
// Forms\Components\Select::make('check_point_name_id')
|
||||||
->label('Check Point Name')
|
// ->label('Check Point Name')
|
||||||
// ->relationship('checkPointNames', 'name')
|
// // ->relationship('checkPointNames', 'name')
|
||||||
->options(function (callable $get) {
|
// ->options(function (callable $get) {
|
||||||
$plantId = $get('plant_id');
|
// $plantId = $get('plant_id');
|
||||||
if (! $plantId) {
|
// if (! $plantId) {
|
||||||
return [];
|
// return [];
|
||||||
}
|
// }
|
||||||
|
|
||||||
return CheckPointName::where('plant_id', $plantId)
|
// return CheckPointName::where('plant_id', $plantId)
|
||||||
->pluck('name', 'id')
|
// ->pluck('name', 'id')
|
||||||
->toArray();
|
// ->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()
|
->required()
|
||||||
->reactive()
|
->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')))
|
->disabled(fn (Get $get) => ! empty($get('id')))
|
||||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
->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('check_point_name_id', null);
|
||||||
$set('gPeCheckPointNameError', 'Please select a check point name first.');
|
$set('gPeCheckPointNameError', 'Please enter a check point name.');
|
||||||
|
|
||||||
return;
|
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) => [
|
->extraAttributes(fn ($get) => [
|
||||||
'class' => $get('gPeCheckPointNameError') ? 'border-red-500' : '',
|
'class' => $get('gPeCheckPointNameError') ? 'border-red-500' : '',
|
||||||
])
|
])
|
||||||
->hint(fn ($get) => $get('gPeCheckPointNameError') ? $get('gPeCheckPointNameError') : null)
|
->hint(fn ($get) => $get('gPeCheckPointNameError'))
|
||||||
->hintColor('danger')
|
->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\TextInput::make('reader_code')
|
Forms\Components\TextInput::make('reader_code')
|
||||||
->label('Reader Code')
|
->label('Reader Code')
|
||||||
->hidden(fn (Get $get) => ! $get('id'))
|
->hidden(fn (Get $get) => ! $get('id'))
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
namespace App\Filament\Resources\GuardPatrolEntryResource\Pages;
|
namespace App\Filament\Resources\GuardPatrolEntryResource\Pages;
|
||||||
|
|
||||||
use App\Filament\Resources\GuardPatrolEntryResource;
|
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\Actions;
|
||||||
use Filament\Facades\Filament;
|
use Filament\Facades\Filament;
|
||||||
use Filament\Notifications\Notification;
|
use Filament\Notifications\Notification;
|
||||||
@@ -29,6 +33,100 @@ class CreateGuardPatrolEntry extends CreateRecord
|
|||||||
|
|
||||||
// public ?array $data = null;
|
// 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()
|
public function processCheckPointName()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user