Merge pull request 'Added filter logic in line resource page' (#764) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 17s
Reviewed-on: #764
This commit was merged in pull request #764.
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Filament\Exports\LineExporter;
|
|||||||
use App\Filament\Imports\LineImporter;
|
use App\Filament\Imports\LineImporter;
|
||||||
use App\Filament\Resources\LineResource\Pages;
|
use App\Filament\Resources\LineResource\Pages;
|
||||||
use App\Models\Block;
|
use App\Models\Block;
|
||||||
|
use App\Models\Item;
|
||||||
use App\Models\Line;
|
use App\Models\Line;
|
||||||
use App\Models\Plant;
|
use App\Models\Plant;
|
||||||
use App\Models\WorkGroupMaster;
|
use App\Models\WorkGroupMaster;
|
||||||
@@ -24,6 +25,11 @@ use Illuminate\Database\Eloquent\Builder;
|
|||||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Illuminate\Validation\Rules\Unique;
|
use Illuminate\Validation\Rules\Unique;
|
||||||
|
use Filament\Forms\Components\DateTimePicker;
|
||||||
|
use Filament\Forms\Components\FileUpload;
|
||||||
|
use Filament\Forms\Components\Select;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Tables\Filters\Filter;
|
||||||
|
|
||||||
class LineResource extends Resource
|
class LineResource extends Resource
|
||||||
{
|
{
|
||||||
@@ -1184,7 +1190,207 @@ class LineResource extends Resource
|
|||||||
])
|
])
|
||||||
->filters([
|
->filters([
|
||||||
Tables\Filters\TrashedFilter::make(),
|
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('items', function ($query) {
|
||||||
|
$query->whereNotNull('id');
|
||||||
|
})->orderBy('code')->pluck('name', 'id')->toArray();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->searchable()
|
||||||
|
->reactive()
|
||||||
|
->afterStateUpdated(function ($state, callable $set, callable $get): void {
|
||||||
|
$set('code', null);
|
||||||
|
$set('operator_id', null);
|
||||||
|
}),
|
||||||
|
Select::make('name')
|
||||||
|
->label('Search by Line Name')
|
||||||
|
->nullable()
|
||||||
|
->options(function (callable $get) {
|
||||||
|
$plantId = $get('Plant');
|
||||||
|
|
||||||
|
return $plantId ? Line::where('plant_id', $plantId)->pluck('name', 'id') : [];
|
||||||
|
})
|
||||||
|
->searchable()
|
||||||
|
->reactive(),
|
||||||
|
Select::make('type')
|
||||||
|
->label('Search by Line Type')
|
||||||
|
->nullable()
|
||||||
|
->options(function (callable $get) {
|
||||||
|
$plantId = $get('Plant');
|
||||||
|
|
||||||
|
return $plantId ? Line::where('plant_id', $plantId)->distinct()->pluck('type', 'type')->toArray(): [];
|
||||||
|
|
||||||
|
})
|
||||||
|
->searchable()
|
||||||
|
->reactive(),
|
||||||
|
Select::make('work_group_id')
|
||||||
|
->label('Search by WorkGroupCenter')
|
||||||
|
->nullable()
|
||||||
|
->options(function (callable $get) {
|
||||||
|
$plantId = $get('Plant');
|
||||||
|
|
||||||
|
$workGroupIds = Line::where('plant_id', $plantId)
|
||||||
|
->get([
|
||||||
|
'work_group1_id',
|
||||||
|
'work_group2_id',
|
||||||
|
'work_group3_id',
|
||||||
|
'work_group4_id',
|
||||||
|
'work_group5_id',
|
||||||
|
'work_group6_id',
|
||||||
|
'work_group7_id',
|
||||||
|
'work_group8_id',
|
||||||
|
'work_group9_id',
|
||||||
|
'work_group10_id',
|
||||||
|
])
|
||||||
|
->flatMap(function ($line) {
|
||||||
|
return [
|
||||||
|
$line->work_group1_id,
|
||||||
|
$line->work_group2_id,
|
||||||
|
$line->work_group3_id,
|
||||||
|
$line->work_group4_id,
|
||||||
|
$line->work_group5_id,
|
||||||
|
$line->work_group6_id,
|
||||||
|
$line->work_group7_id,
|
||||||
|
$line->work_group8_id,
|
||||||
|
$line->work_group9_id,
|
||||||
|
$line->work_group10_id,
|
||||||
|
];
|
||||||
|
})
|
||||||
|
->filter()
|
||||||
|
->unique()
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
return WorkGroupMaster::whereIn('id', $workGroupIds)
|
||||||
|
->pluck('name', 'id')
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
})
|
||||||
|
->searchable()
|
||||||
|
->reactive(),
|
||||||
|
DateTimePicker::make(name: 'created_from')
|
||||||
|
->label('Created From')
|
||||||
|
->placeholder(placeholder: 'Select From DateTime')
|
||||||
|
->reactive()
|
||||||
|
->native(false),
|
||||||
|
DateTimePicker::make('created_to')
|
||||||
|
->label('Created To')
|
||||||
|
->placeholder(placeholder: 'Select To DateTime')
|
||||||
|
->reactive()
|
||||||
|
->native(false),
|
||||||
|
])
|
||||||
|
->query(function ($query, array $data) {
|
||||||
|
// Hide all records initially if no filters are applied
|
||||||
|
if (empty($data['Plant']) && empty($data['name']) && empty($data['type']) && empty($data['work_group_id']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_from']) && empty($data['updated_to'])) {
|
||||||
|
// return $query->whereRaw('1 = 0');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['Plant'])) { // $plant = $data['Plant'] ?? null
|
||||||
|
$query->where('plant_id', $data['Plant']);
|
||||||
|
} else {
|
||||||
|
$userHas = Filament::auth()->user()->plant_id;
|
||||||
|
|
||||||
|
if ($userHas && strlen($userHas) > 0) {
|
||||||
|
return $query->whereRaw('1 = 0');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['name'])) {
|
||||||
|
$query->where('id', $data['name']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['type'])) {
|
||||||
|
$query->where('type', $data['type']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (! empty($data['work_group_id'])) {
|
||||||
|
// $query->where('name', $data['work_group_id']);
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (! empty($data['work_group_id'])) {
|
||||||
|
|
||||||
|
$query->where(function ($q) use ($data) {
|
||||||
|
|
||||||
|
$q->where('work_group1_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group2_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group3_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group4_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group5_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group6_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group7_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group8_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group9_id', $data['work_group_id'])
|
||||||
|
->orWhere('work_group10_id', $data['work_group_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['created_from'])) {
|
||||||
|
$query->where('created_at', '>=', $data['created_from']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['created_to'])) {
|
||||||
|
$query->where('created_at', '<=', $data['created_to']);
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
->indicateUsing(function (array $data) {
|
||||||
|
$indicators = [];
|
||||||
|
|
||||||
|
if (! empty($data['Plant'])) {
|
||||||
|
$indicators[] = 'Plant: '.Plant::where('id', $data['Plant'])->value('name');
|
||||||
|
} else {
|
||||||
|
$userHas = Filament::auth()->user()->plant_id;
|
||||||
|
|
||||||
|
if ($userHas && strlen($userHas) > 0) {
|
||||||
|
return 'Plant: Choose plant to filter records.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['name'])) {
|
||||||
|
$indicators[] = 'Line Name: '.Line::where('id', $data['name'])->value('name');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['type'])) {
|
||||||
|
$indicators[] = 'Line Type: '.Line::where('type', $data['type'])->value('type');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['work_group_id'])) {
|
||||||
|
$indicators[] = 'Work Group: ' .
|
||||||
|
WorkGroupMaster::where('id', $data['work_group_id'])->value('name');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['created_from'])) {
|
||||||
|
$indicators[] = 'From: '.$data['created_from'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['created_to'])) {
|
||||||
|
$indicators[] = 'To: '.$data['created_to'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['updated_from'])) {
|
||||||
|
$indicators[] = 'From: '.$data['updated_from'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($data['updated_to'])) {
|
||||||
|
$indicators[] = 'To: '.$data['updated_to'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $indicators;
|
||||||
|
}),
|
||||||
])
|
])
|
||||||
|
->filtersFormMaxHeight('280px')
|
||||||
->actions([
|
->actions([
|
||||||
Tables\Actions\ViewAction::make(),
|
Tables\Actions\ViewAction::make(),
|
||||||
Tables\Actions\EditAction::make(),
|
Tables\Actions\EditAction::make(),
|
||||||
|
|||||||
Reference in New Issue
Block a user