diff --git a/app/Filament/Pages/GateOutEntry.php b/app/Filament/Pages/GateOutEntry.php index 4bad32c..ff7e910 100644 --- a/app/Filament/Pages/GateOutEntry.php +++ b/app/Filament/Pages/GateOutEntry.php @@ -4,6 +4,7 @@ namespace App\Filament\Pages; use App\Models\Plant; use App\Models\VisitorEntry; +use Carbon\Carbon; use Filament\Facades\Filament; use Filament\Pages\Page; use Filament\Forms\Contracts\HasForms; @@ -37,50 +38,172 @@ class GateOutEntry extends Page implements HasForms ->schema([ Section::make('') // You can give your section a title or leave it blank ->schema([ - TextInput::make('scan_out_gate_pass') + Select::make('scan_out_gate_pass') + ->label('Scan Gate Pass') + ->required() + ->reactive() + ->options([ + 'In' => 'In', + 'Out' => 'Out', + ]), + TextInput::make('scan_out_gate_pass_in') + ->label('Scan In Gate Pass') + ->required() + ->reactive() + ->visible(fn ($get) => $get('scan_out_gate_pass') == 'In') + ->extraAttributes([ + 'wire:keydown.enter' => 'processGatePassIn($event.target.value)', + ]), + TextInput::make('scan_out_gate_pass_out') ->label('Scan Out Gate Pass') ->required() ->reactive() + ->visible(fn ($get) => $get('scan_out_gate_pass') == 'Out') ->extraAttributes([ - 'wire:keydown.enter' => 'processGatePass($event.target.value)', + 'wire:keydown.enter' => 'processGatePassOut($event.target.value)', ]), ]) ->columns(5) ]); } - public function processGatePass($gatePass) + public function processGatePassIn($gatePass) { - $entry = VisitorEntry::where('register_id', $gatePass)->first(); + $entry = VisitorEntry::where('register_id', $gatePass)->latest()->first(); - if ($entry) { + if ($entry) + { + if($entry->in_time && !$entry->out_time && (empty($entry->valid_upto) || $entry->valid_upto == '' || $entry->valid_upto == null)){ + Notification::make() + ->title('Already Entered') + ->body('Gate pass In has already been processed for entry.') + ->warning() + ->send(); + $this->filters['scan_out_gate_pass_in'] = ''; + return; + } + elseif ( + !empty($entry->valid_upto) && + Carbon::parse($entry->valid_upto)->endOfDay()->gte(now()) + ){ + if (empty($entry->out_time)) { + Notification::make() + ->title('Gate In Not Allowed') + ->body("Previous gate-out has not been punched. Please complete gate-out first for ID ' . $gatePass . '.") + ->warning() + ->send(); + + $this->filters['scan_out_gate_pass_in'] = ''; + return; + } + else{ + $newEntry = $entry->replicate(); + $newEntry->in_time = now(); + $newEntry->out_time = null; + + $newEntry->save(); + + Notification::make() + ->title('Gate In') + ->body('Gate in has been successfully processed. Visitor marked as entered.') + ->success() + ->send(); + $this->filters['scan_out_gate_pass_in'] = ''; + } + } + else{ + Notification::make() + ->title('Visitor Pass Expired') + ->body('Your visitor pass validity has expired.') + ->danger() + ->send(); + $this->filters['scan_out_gate_pass_in'] = ''; + return; + } + } + else + { + Notification::make() + ->title('Invalid Gate Pass') + ->body('Scanned gate in pass is not valid.') + ->danger() + ->send(); + return; + } + } + + public function processGatePassOut($gatePass) + { + $entry = VisitorEntry::where('register_id', $gatePass)->latest()->first(); + + if (!$entry) { + Notification::make() + ->title('Invalid Gate Pass') + ->body('Scanned gate out pass is not valid.') + ->danger() + ->send(); + $this->filters['scan_out_gate_pass_out'] = ''; + return; + } + + if (empty($entry->valid_upto) || $entry->valid_upto == '' || $entry->valid_upto == null) { if (!empty($entry->out_time)) { Notification::make() ->title('Already Exited') - ->body('Gate pass has already been processed. Out time was already punched.') + ->body('Gate pass has already been processed.') ->warning() ->send(); - $this->filters['scan_out_gate_pass'] = ''; + $this->filters['scan_out_gate_pass_out'] = ''; + return; + } + else + { + $entry->out_time = now(); + $entry->save(); + + Notification::make() + ->title('Gate Pass Processed') + ->body('Visitor marked as exited.') + ->success() + ->send(); + + $this->filters['scan_out_gate_pass_out'] = ''; return; } + } + elseif ((empty($entry->valid_upto) || $entry->valid_upto != '' || $entry->valid_upto != null)) + { + if (Carbon::parse($entry->valid_upto)->endOfDay()->lt(now())) + { + Notification::make() + ->title('Visitor Pass Expired') + ->body('Your visitor pass validity has expired.') + ->danger() + ->send(); + $this->filters['scan_out_gate_pass_out'] = ''; + return; + } + if (!empty($entry->out_time)) { + Notification::make() + ->title('Already Exited') + ->body('Gate pass has already been processed.') + ->warning() + ->send(); + $this->filters['scan_out_gate_pass_out'] = ''; + return; + } else{ $entry->out_time = now(); $entry->save(); Notification::make() - ->title('Gate Pass Processed') - ->body('Gate pass has been successfully processed. Visitor marked as exited.') + ->title('Gate Out') + ->body('Visitor marked as exited.') ->success() ->send(); - $this->filters['scan_out_gate_pass'] = ''; + $this->filters['scan_out_gate_pass_out'] = ''; } - } else { - Notification::make() - ->title('Invalid Gate Pass') - ->body('The scanned gate pass is not valid. Please try again.') - ->danger() - ->send(); } } diff --git a/app/Filament/Resources/VisitorEntryResource.php b/app/Filament/Resources/VisitorEntryResource.php index 125596f..7cc636c 100644 --- a/app/Filament/Resources/VisitorEntryResource.php +++ b/app/Filament/Resources/VisitorEntryResource.php @@ -187,6 +187,15 @@ class VisitorEntryResource extends Resource ->numeric() ->default(1) ->required(), + Forms\Components\Select::make('mode_of_travel') + ->label('Mode of Travel') + ->options([ + 'Rental' => 'Rental', + 'Car' => 'Car', + 'Bike' => 'Bike', + ]) + ->reactive() + ->placeholder('Select Mode of Travel'), Forms\Components\DateTimePicker::make('in_time') ->label('In Time') ->required() @@ -214,6 +223,7 @@ class VisitorEntryResource extends Resource public static function table(Table $table): Table { return $table + // ->modifyQueryUsing(fn (Builder $query) => $query->whereDate('created_at', today())) ->columns([ Tables\Columns\TextColumn::make('No.') ->label('NO') @@ -290,6 +300,11 @@ class VisitorEntryResource extends Resource ->searchable() ->alignCenter() ->sortable(), + Tables\Columns\TextColumn::make('mode_of_travel') + ->label('Mode of Travel') + ->searchable() + ->alignCenter() + ->sortable(), Tables\Columns\TextColumn::make('in_time') ->label('In Time') ->searchable() @@ -420,9 +435,23 @@ class VisitorEntryResource extends Resource ]) ->query(function ($query, array $data) { // Hide all records initially if no filters are applied - if (empty($data['register_id']) && empty($data['type']) && empty($data['name']) && empty($data['company']) && empty($data['employee_master_id']) && empty($data['created_from']) && empty($data['created_to'])) { - $query->where(function ($q) { - }); + // if (empty($data['register_id']) && empty($data['type']) && empty($data['name']) && empty($data['company']) && empty($data['employee_master_id']) && empty($data['created_from']) && empty($data['created_to'])) { + // $query->where(function ($q) { + // }); + // } + + $hasAnyFilter = !empty($data['register_id']) + || !empty($data['type']) + || !empty($data['name']) + || !empty($data['company']) + || !empty($data['employee_master_id']) + || !empty($data['employee_department']) + || !empty($data['created_from']) + || !empty($data['created_to']); + + if (!$hasAnyFilter) { + $query->whereDate('created_at', today()); + return; } if (! empty($data['register_id'])) { diff --git a/app/Filament/Resources/VisitorEntryResource/Pages/CreateVisitorEntry.php b/app/Filament/Resources/VisitorEntryResource/Pages/CreateVisitorEntry.php index 3dfc46d..0429d2d 100644 --- a/app/Filament/Resources/VisitorEntryResource/Pages/CreateVisitorEntry.php +++ b/app/Filament/Resources/VisitorEntryResource/Pages/CreateVisitorEntry.php @@ -21,37 +21,43 @@ class CreateVisitorEntry extends CreateRecord public function processMobile($mobile) { - $visitor = VisitorEntry::where('mobile_number', $mobile)->latest()->first(); - $registerId = $this->form->getState()['register_id'] ?? ''; + $registerId = $this->data['register_id'] ?? null; + $visitor = VisitorEntry::where('mobile_number', $mobile)->latest()->first(); if ($visitor) { $employee = EmployeeMaster::where('id', $visitor->employee_master_id)->first(); $this->form->fill([ - 'register_id' => $registerId, - 'mobile_number' => $mobile ?? '', - 'name' => $visitor->name ?? '', - 'company' => $visitor->company ?? '', - 'type' => $visitor->type ?? '', - 'department' => $employee->department ?? '', - 'employee_master_id' => $visitor->employee_master_id->name ?? '', - 'code' => $employee->code ?? '', + 'register_id' => $registerId ?? '', + 'mobile_number' => $visitor->mobile_number, + 'name' => $visitor->name ?? '', + 'company' => $visitor->company ?? '', + 'type' => $visitor->type ?? '', + 'department' => $employee?->department ?? '', + 'employee_master_id' => $visitor->employee_master_id, + 'code' => $employee?->code ?? '', + 'purpose_of_visit' => '', + 'in_time' => now(), + 'out_time' => null, + 'valid_upto' => null, ]); } else { $this->form->fill([ - 'register_id' => $registerId, - 'mobile_number' => $mobile, - 'name' => '', - 'company' => '', - 'type' => '', - 'department' => '', + 'register_id' => $registerId ?? '', + 'mobile_number' => $mobile, + 'name' => '', + 'company' => '', + 'type' => '', + 'department' => '', 'employee_master_id' => null, - 'code' => '', + 'code' => '', + 'purpose_of_visit' => '', + 'in_time' => now(), + 'out_time' => null, + 'valid_upto' => null, ]); - - return; } } diff --git a/app/Models/VisitorEntry.php b/app/Models/VisitorEntry.php index e5a6d63..17cc9aa 100644 --- a/app/Models/VisitorEntry.php +++ b/app/Models/VisitorEntry.php @@ -23,6 +23,7 @@ class VisitorEntry extends Model 'employee_master_id', 'number_of_person', 'valid_upto', + 'mode_of_travel', ]; public function employeeMaster(): BelongsTo diff --git a/database/migrations/2026_06_08_165020_add_mode_of_travel_to_visitor_entries.php b/database/migrations/2026_06_08_165020_add_mode_of_travel_to_visitor_entries.php new file mode 100644 index 0000000..00d7dda --- /dev/null +++ b/database/migrations/2026_06_08_165020_add_mode_of_travel_to_visitor_entries.php @@ -0,0 +1,31 @@ +