diff --git a/app/Filament/Resources/CourierInwardResource.php b/app/Filament/Resources/CourierInwardResource.php index 4b2de8f3..a9ecd967 100644 --- a/app/Filament/Resources/CourierInwardResource.php +++ b/app/Filament/Resources/CourierInwardResource.php @@ -18,6 +18,10 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; use Filament\Tables\Actions\ExportAction; use Filament\Tables\Actions\ImportAction; +use Filament\Tables\Filters\Filter; +use Filament\Forms\Components\DateTimePicker; +use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; class CourierInwardResource extends Resource { @@ -185,7 +189,166 @@ class CourierInwardResource extends Resource ]) ->filters([ Tables\Filters\TrashedFilter::make(), + Filter::make('advanced_filters') + ->label('Advanced Filters') + ->form([ + Select::make('Plant') + ->label('Search by Plant Name') + ->nullable() + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; + + // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + if ($userHas && strlen($userHas) > 0) { + return Plant::where('id', $userHas)->pluck('name', 'id')->toArray(); + } else { + return Plant::whereHas('courierInwards', function ($query) { + $query->whereNotNull('id'); + })->orderBy('code')->pluck('name', 'id')->toArray(); + } + }) + ->searchable() + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get): void { + $set('type', null); + }), + Select::make('cover_material') + ->label('Cover/Material Type') + ->reactive() + ->options([ + 'Cover' => 'Cover', + 'Material' => 'Material', + 'Other' => 'Other', + ]) + ->placeholder('Choose Type') + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('name', null); + }), + TextInput::make('other_type') + ->label('Specify Type') + ->placeholder('Enter Type') + ->visible(fn (callable $get) => $get('type') == 'Other') + ->required(fn (callable $get) => $get('type') == 'Other'), + TextInput::make('pod_number') + ->label('POD Number') + ->reactive() + ->placeholder('Enter POD Number') + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('company', null); + }), + TextInput::make('courier_name') + ->label('Courier Name') + ->reactive() + ->placeholder('Enter Courier Name') + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('place', null); + }), + TextInput::make('place') + ->label('Place') + ->reactive() + ->placeholder('Enter Place'), + DateTimePicker::make('created_from') + ->label('Created From') + ->placeholder('Select From DateTime') + ->reactive() + ->native(false), + DateTimePicker::make('created_to') + ->label('Created To') + ->placeholder('Select To DateTime') + ->reactive() + ->native(false), + ]) + ->query(function ($query, array $data) { + + $hasAnyFilter = !empty($data['Plant']) + || !empty($data['cover_material']) + || !empty($data['pod_number']) + || !empty($data['courier_name']) + || !empty($data['place']) + || !empty($data['created_from']) + || !empty($data['created_to']); + + if (!$hasAnyFilter) { + $query->whereDate('courier_inwards.created_at', today()); + return; + } + + if (! empty($data['Plant'])) { + $query->where('plant_id', $data['Plant']); + } + + if (! empty($data['cover_material'])) { + if ($data['cover_material'] == 'Other') { + $query->where('cover_material', 'like', '%'.$data['other_type'].'%'); + } + else{ + $query->where('cover_material', $data['cover_material']); + } + } + + if (! empty($data['pod_number'])) { + $query->whereRaw( + 'LOWER(pod_number) LIKE ?', + ['%' . strtolower($data['pod_number']) . '%'] + ); + } + + if (! empty($data['courier_name'])) { + $query->where('courier_name', 'like', '%'.$data['courier_name'].'%'); + } + + if (! empty($data['place'])) { + $query->where('place', 'like', '%'.$data['place'].'%'); + } + + if (! empty($data['created_from'])) { + $query->where('created_at', '>=', $data['created_from']); + } + + if (! empty($data['created_to'])) { + $query->where('created_at', '<=', $data['created_to']); + } + // $query->orderBy('created_at', 'asc'); + }) + ->indicateUsing(function (array $data) { + $indicators = []; + + if (! empty($data['Plant'])) { + $indicators[] = 'Plant: '.Plant::where('id', $data['Plant'])->value('name'); + } + + if (! empty($data['cover_material'])) { + $indicators[] = 'Cover/Material: '.$data['cover_material']; + } + + if (! empty($data['other_type'])) { + $indicators[] = 'Specify Type: '.$data['other_type']; + } + + if (! empty($data['pod_number'])) { + $indicators[] = 'POD Number: '.$data['pod_number']; + } + + if (! empty($data['courier_name'])) { + $indicators[] = 'Courier Name: '.$data['courier_name']; + } + + if (! empty($data['place'])) { + $indicators[] = 'Place: '.$data['place']; + } + + if (! empty($data['created_from'])) { + $indicators[] = 'From: '.$data['created_from']; + } + + if (! empty($data['created_to'])) { + $indicators[] = 'To: '.$data['created_to']; + } + + return $indicators; + }), ]) + ->filtersFormMaxHeight('280px') ->actions([ Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(),