diff --git a/app/Filament/Resources/ProductionLineStopResource.php b/app/Filament/Resources/ProductionLineStopResource.php index b121878..129fbb3 100644 --- a/app/Filament/Resources/ProductionLineStopResource.php +++ b/app/Filament/Resources/ProductionLineStopResource.php @@ -9,6 +9,7 @@ use App\Filament\Resources\ProductionLineStopResource\Pages; use App\Filament\Resources\ProductionLineStopResource\RelationManagers; use App\Models\Block; use App\Models\Line; +use App\Models\LineStop; use App\Models\Plant; use App\Models\ProductionLineStop; use App\Models\Shift; @@ -368,10 +369,12 @@ class ProductionLineStopResource extends Resource ->sortable(), Tables\Columns\TextColumn::make('stop_hour') ->label('Stop Hour') + ->alignCenter() ->numeric() ->sortable(), Tables\Columns\TextColumn::make('stop_min') ->label('Stop Minute') + ->alignCenter() ->numeric() ->sortable(), Tables\Columns\TextColumn::make('line.name') @@ -404,118 +407,132 @@ class ProductionLineStopResource extends Resource ]) ->filters([ + Tables\Filters\TrashedFilter::make(), + Filter::make('advanced_filters') + ->label('Advanced Filters') + ->form([ - Tables\Filters\TrashedFilter::make(), - Filter::make('advanced_filters') - ->label('Advanced Filters') - ->form([ + //plant + Select::make('Plant') + ->label('Select Plant') + ->nullable() + ->options(function () { + return Plant::pluck('name', 'id'); // Assuming 'name' is the column you want to display + }) + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('Line', null); + $set('Block', null); + $set('Shift', null); + $set('line_stop_id', null); + }), - Select::make('Plant') - ->label('Select Plant') - ->options(function () { - return Plant::pluck('name', 'id'); // Assuming 'name' is the column you want to display - }), + //line + Select::make('Line') + ->label('Select line') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); - //block - Select::make('Block') - ->label('Select Block') - ->options(fn (callable $get) => - $get('Plant') - ? Block::where('plant_id', $get('Plant'))->pluck('name', 'id') - : [] - ) - ->reactive(), + if (!$plantId ) { + return []; + } + return Line::where('plant_id', $plantId) + ->pluck('name', 'id'); + }) + ->reactive(), - //shift - Select::make('Shift') - ->label('Select Shift') - ->options(function (callable $get) { - $plantId = $get('Plant'); - $blockId = $get('Block'); + //block + Select::make('Block') + ->label('Select Block') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); - if (!$plantId || !$blockId) { - return []; // Return empty if plant or block is not selected - } + if (!$plantId ) { + return []; + } + return Block::where('plant_id', $get('Plant'))->pluck('name', 'id'); + }) + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('Shift', null); + }), - return Shift::where('plant_id', $plantId) + //shift + Select::make('Shift') + ->label('Select Shift') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + $blockId = $get('Block'); + + if (!$plantId || !$blockId) { + return []; // Return empty if plant or block is not selected + } + + return Shift::where('plant_id', $plantId) ->where('block_id', $blockId) ->pluck('name', 'id'); - }) - ->reactive(), + }) + ->reactive(), - //line - Select::make('line') - ->label('Select line') - ->options(function (callable $get) { - $plantId = $get('Plant'); + Select::make('line_stop_id') //linestop_id + ->label('Search by Line Stop Code') + ->nullable() + // ->options(fn () => LineStop::orderBy('code')->whereHas('productionLineStops')->pluck('code', 'id')) + ->options(function (callable $get) { + $pId = $get('Plant'); + return LineStop::orderBy('code')->whereHas('productionLineStops', function ($query) use ($pId) { + if ($pId) { + $query->where('plant_id', $pId); + } + })->pluck('code', 'id'); + }) + ->searchable() + ->reactive(), - if (!$plantId ) { - return []; - } - return Line::where('plant_id', $plantId) - ->pluck('name', 'id'); - }) - ->reactive(), + DateTimePicker::make(name: 'created_from') + ->label('Created From') + ->placeholder(placeholder: 'Select From DateTime') + ->reactive() + ->native(false), - TextInput::make('Line Stop Code'), - - - // Select::make('reason') - // ->label('Filter by Stop Reason') - // ->options(function () { - // return \App\Models\Item::whereHas('stickerMasters', function ($query) { - // $query->whereHas('qualityValidations'); - // })->pluck('code', 'id'); - // }) - // ->searchable(), - - DateTimePicker::make(name: 'created_from') - ->label('Created From') - ->reactive() - ->native(false), - - DateTimePicker::make('created_to') - ->label('Created To') - ->reactive() - ->native(false), + DateTimePicker::make('created_to') + ->label('Created To') + ->placeholder(placeholder: 'Select To DateTime') + ->reactive() + ->native(false), ]) ->query(function ($query, array $data) { + if (empty($data['Plant']) && empty($data['Shift']) && empty($data['Line']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['line_stop_id'])) { + return $query->whereRaw('1 = 0'); + } + if ($plant = $data['Plant'] ?? null) { $query->where('plant_id', $plant); } - // Filter by Shift if ($shift = $data['Shift'] ?? null) { - // Get shift data here, if needed, but no block_id filtering yet $query->where('shift_id', $shift); } - if ($block = $data['Block'] ?? null) { - // Use whereHas to filter by block_id in the Shift table - $query->whereHas('shift', function ($query) use ($block) { - $query->where('block_id', $block); - }); - } - - if ($line = $data['line'] ?? null) { + if ($line = $data['Line'] ?? null) { $query->where('line_id', $line); } - if ($code = $data['Line Stop Code'] ?? null) { + if ($code = $data['line_stop_id'] ?? null) { // Find the linestop_id by code entered - $lineStop = \App\Models\LineStop::where('code', 'like', "%{$code}%")->first(); + // $lineStop = \App\Models\LineStop::where('code', 'like', "%{$code}%")->first(); - // If we find a matching LineStop, use its id to filter production_line_stops - if ($lineStop) { - $query->where('linestop_id', $lineStop->id); - } else { - // If no match found, you can either handle it as an error or return no results - $query->where('linestop_id', null); // This will return no results if no match - } - } - - if ($reason = $data['reason'] ?? null) { - $query->where('reason_id', $reason); + // // If we find a matching LineStop, use its id to filter production_line_stops + // if ($lineStop) { + // $query->where('linestop_id', $lineStop->id); + // } else { + // // If no match found, you can either handle it as an error or return no results + // $query->where('linestop_id', null); // This will return no results if no match + // } + $query->where('linestop_id', $code); } if ($from = $data['created_from'] ?? null) { @@ -525,12 +542,39 @@ class ProductionLineStopResource extends Resource if ($to = $data['created_to'] ?? null) { $query->where('created_at', '<=', $to); } - - return $query; + // return $query; }) + ->indicateUsing(function (array $data) { + $indicators = []; + if (!empty($data['Plant'])) { + $indicators[] = 'Plant: ' . Plant::where('id', $data['Plant'])->value('name'); + } + + if (!empty($data['Shift'])) { + $indicators[] = 'Shift: ' . Shift::where('id', $data['Shift'])->value('name'); + } + + if (!empty($data['Line'])) { + $indicators[] = 'Line: ' . Line::where('id', $data['Line'])->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['line_stop_id'])) { + $lineStopCod = LineStop::find($data['line_stop_id'])->code ?? 'Unknown'; + $indicators[] = 'Line Stop Code: ' . $lineStopCod; + } + + return $indicators; + }) ]) - ->filtersFormMaxHeight('280px') ->actions([ Tables\Actions\ViewAction::make(), @@ -546,10 +590,15 @@ class ProductionLineStopResource extends Resource ]) ->headerActions([ ImportAction::make() - ->importer(ProductionLineStopImporter::class) - ->maxRows(100000), - ExportAction::make() - ->exporter(ProductionLineStopExporter::class), + ->importer(ProductionLineStopImporter::class) + ->visible(function() { + return Filament::auth()->user()->can('view import production line stop'); + }), + ExportAction::make() + ->exporter(ProductionLineStopExporter::class) + ->visible(function() { + return Filament::auth()->user()->can('view export production line stop'); + }), ]); }