Added migration, model, policy, resource, importer, exporter, controller (Get API) with validation and functionality for Testing Line Visualization
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 16s

This commit is contained in:
dhanabalan
2026-11-03 11:19:07 +05:30
parent 20e64ba356
commit 5c0ed45f74
11 changed files with 1477 additions and 0 deletions

View File

@@ -0,0 +1,734 @@
<?php
namespace App\Filament\Resources;
use App\Filament\Exports\TestingLineVisualizationExporter;
use App\Filament\Imports\TestingLineVisualizationImporter;
use App\Filament\Resources\TestingLineVisualizationResource\Pages;
use App\Models\Configuration;
use App\Models\Plant;
use App\Models\TestingLineVisualization;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Actions\ImportAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
class TestingLineVisualizationResource extends Resource
{
protected static ?string $model = TestingLineVisualization::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $navigationGroup = 'Pump Testing Panel';
protected static ?int $navigationSort = 4;
public static function form(Form $form): Form
{
return $form
->schema([
Section::make('')
->schema([
Forms\Components\Select::make('plant_id')
->label('Plant Name')
->relationship('plant', 'name')
->reactive()
->searchable()
->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();
})
->required()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->plant_id ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->disabled(fn (Get $get) => ! empty($get('id'))),
Forms\Components\TextInput::make('test_bed_group')
->label('Test Bed Group')
->placeholder('Enter Test Bed Group')
->reactive()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->test_bed_group ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\TextInput::make('line_no')
->label('Line Number')
->placeholder('Enter Line Number')
->reactive()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->line_no ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\TextInput::make('pump_model')
->label('Pump Model')
->placeholder('Enter Pump Model')
->reactive()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->pump_model ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\TextInput::make('pump_type')
->label('Pump Type')
->placeholder('Enter Pump Type')
->reactive()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->pump_type ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\TextInput::make('motor_details')
->label('Motor Details')
->placeholder('Enter Motor Details')
->reactive()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->motor_details ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\Select::make('test_type')
->label('Test Type')
->nullable()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return Configuration::where('plant_id', $plantId)
->where('c_name', 'VISUALIZATION_TEST_TYPE')
->orderBy('created_at')
->pluck('c_value', 'c_value')
->toArray();
} else {
return Configuration::where('c_name', 'VISUALIZATION_TEST_TYPE')
->orderBy('created_at')
->pluck('c_value', 'c_value')
->toArray();
}
})
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->test_type ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->searchable()
->reactive()
->required(),
Forms\Components\TextInput::make('flow')
->label('Flow')
->placeholder('Enter Flow')
->reactive()
->numeric()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->flow ?? '0';
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\TextInput::make('head')
->label('Head')
->placeholder('Enter Head')
->reactive()
->numeric()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->head ?? '0';
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\TextInput::make('run_time')
->label('Run Time')
->placeholder('Enter Run Time')
->reactive()
->numeric()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->run_time ?? '0.0';
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->required(),
Forms\Components\Select::make('live_status')
->label('Status')
->nullable()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return Configuration::where('plant_id', $plantId)
->where('c_name', 'VISUALIZATION_LIVE_STATUS')
->orderBy('created_at')
->pluck('c_value', 'c_value')
->toArray();
} else {
return Configuration::where('c_name', 'VISUALIZATION_LIVE_STATUS')
->orderBy('created_at')
->pluck('c_value', 'c_value')
->toArray();
}
})
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->live_status ?? null;
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
})
->searchable()
->reactive()
->required(),
Forms\Components\TextInput::make('observation_remarks')
->label('Observation Remarks')
->placeholder('Enter Observation Remarks')
->reactive()
->default(function () {
$userHas = Filament::auth()->user()->plant_id;
return ($userHas && strlen($userHas) > 0) ? null : optional(TestingLineVisualization::latest()->first())->observation_remarks ?? 'Normal';
})
->afterStateUpdated(function ($state, callable $set, callable $get) {
$set('updated_by', Filament::auth()->user()?->name);
}),
Forms\Components\Hidden::make('created_by')
->label('Created By')
->default(Filament::auth()->user()?->name),
Forms\Components\Hidden::make('updated_by')
->label('Updated By')
->default(Filament::auth()->user()?->name),
Forms\Components\TextInput::make('id')
->hidden()
->readOnly(),
])
->columns(['default' => 1, 'sm' => 2]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('No.')
->label('No.')
->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}),
Tables\Columns\TextColumn::make('plant.name')
->label('Plant Name')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('test_bed_group')
->label('Test Bed Group')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('line_no')
->label('Line No')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('pump_model')
->label('Pump Model')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('pump_type')
->label('Pump Type')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('motor_details')
->label('Motor Details')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('test_type')
->label('Test Type')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('flow')
->label('Flow')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('head')
->label('Head')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('run_time')
->label('Run Time')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('live_status')
->label('Status')
->alignCenter()
->searchable()
->sortable()
->color(fn (string $state): string => match ($state) {
'Running' => 'success',
'OFF' => 'danger',
'Aligning' => 'warning',
'MTN' => 'info',
default => 'gray',
})
->badge()
->default('-'),
Tables\Columns\TextColumn::make('observation_remarks')
->label('Observation / Remarks')
->alignCenter()
->searchable()
->sortable()
->color(fn (string $state): string => match ($state) {
default => 'gray',
})
->badge()
->default('-'),
Tables\Columns\TextColumn::make('created_at')
->label('Created At')
->dateTime()
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('created_by')
->label('Created By')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('updated_at')
->label('Updated At')
->dateTime()
->alignCenter()
->searchable()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_by')
->label('Updated By')
->alignCenter()
->searchable()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('deleted_at')
->label('Deleted At')
->dateTime()
->alignCenter()
->searchable()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\TrashedFilter::make(),
Filter::make('advanced_filters')
->label('Advanced Filters')
->form([
Select::make('Plant')
->label('Search by Plant Name')
->nullable()
->searchable()
->reactive()
->options(function (callable $get) {
$userHas = Filament::auth()->user()->plant_id;
if ($userHas && strlen($userHas) > 0) {
return Plant::where('id', $userHas)->pluck('name', 'id')->toArray();
} else {
return Plant::whereHas('testingLineVisualizations', function ($query) {
$query->whereNotNull('id');
})->orderBy('code')->pluck('name', 'id');
}
})
->afterStateUpdated(function ($state, callable $set, callable $get): void {
$set('created_by', null);
$set('updated_by', null);
}),
TextInput::make('test_bed_group')
->label('Test Bed Group')
->placeholder('Enter Test Bed Group')
->live(500),
TextInput::make('line_no')
->label('Line No')
->placeholder('Enter Line No')
->live(500),
TextInput::make('pump_model')
->label('Pump Model')
->placeholder('Enter Pump Model')
->live(500),
TextInput::make('pump_type')
->label('Pump Type')
->placeholder('Enter Pump Type')
->live(500),
TextInput::make('motor_details')
->label('Motor Details')
->placeholder('Enter Motor Details')
->live(500),
Select::make('test_type')
->label('Test Type')
->nullable()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return TestingLineVisualization::where('plant_id', $plantId)->whereNotNull('test_type')->select('test_type')->distinct()->pluck('test_type', 'test_type');
} else {
return TestingLineVisualization::whereNotNull('test_type')->select('test_type')->distinct()->pluck('test_type', 'test_type');
}
})
->searchable()
->reactive(),
TextInput::make('flow')
->label('Flow')
->placeholder('Enter Flow')
->reactive()
->numeric(),
TextInput::make('head')
->label('Head')
->placeholder('Enter Head')
->reactive()
->numeric(),
TextInput::make('run_time')
->label('Run Time')
->placeholder('Enter Run Time')
->reactive()
->numeric(),
Select::make('live_status')
->label('Status')
->nullable()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return TestingLineVisualization::where('plant_id', $plantId)->whereNotNull('live_status')->select('live_status')->distinct()->pluck('live_status', 'live_status');
} else {
return TestingLineVisualization::whereNotNull('live_status')->select('live_status')->distinct()->pluck('live_status', 'live_status');
}
})
->searchable()
->reactive(),
TextInput::make('observation_remarks')
->label('Observation / Remarks')
->placeholder('Enter Observation / Remarks')
->live(500),
Select::make('created_by')
->label('Search by Created By')
->nullable()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return TestingLineVisualization::where('plant_id', $plantId)->whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by');
} else {
return TestingLineVisualization::whereNotNull('created_by')->select('created_by')->distinct()->pluck('created_by', 'created_by');
}
})
->searchable()
->reactive(),
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),
Select::make('updated_by')
->label('Search by Updated By')
->nullable()
->options(function (callable $get) {
$plantId = $get('Plant');
if ($plantId) {
return TestingLineVisualization::where('plant_id', $plantId)->whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by');
} else {
return TestingLineVisualization::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by');
}
})
->searchable()
->reactive(),
DateTimePicker::make(name: 'updated_from')
->label('Updated From')
->placeholder(placeholder: 'Select From DateTime')
->reactive()
->native(false),
DateTimePicker::make('updated_to')
->label('Updated 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['test_bed_group']) && empty($data['line_no']) && empty($data['pump_model']) && empty($data['pump_type']) && empty($data['motor_details']) && empty($data['test_type']) && Str::length($data['flow']) <= 0 && Str::length($data['head']) <= 0 && Str::length($data['run_time']) <= 0 && empty($data['live_status']) && empty($data['observation_remarks']) && empty($data['created_by']) && empty($data['created_from']) && empty($data['created_to']) && empty($data['updated_by']) && 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['test_bed_group'])) {
$query->where('test_bed_group', 'like', '%'.$data['test_bed_group'].'%');
}
if (! empty($data['line_no'])) {
$query->where('line_no', $data['line_no']);
}
if (! empty($data['pump_model'])) {
$query->where('pump_model', 'like', '%'.$data['pump_model'].'%');
}
if (! empty($data['pump_type'])) {
$query->where('pump_type', 'like', '%'.$data['pump_type'].'%');
}
if (! empty($data['motor_details'])) {
$query->where('motor_details', 'like', '%'.$data['motor_details'].'%');
}
if (! empty($data['test_type'])) {
$query->where('test_type', $data['test_type']);
}
if (Str::length($data['flow']) > 0) {
$query->where('flow', $data['flow']);
}
if (Str::length($data['head']) > 0) {
$query->where('head', $data['head']);
}
if (Str::length($data['run_time']) > 0) {
$query->where('run_time', $data['run_time']);
}
if (! empty($data['live_status'])) {
$query->where('live_status', $data['live_status']);
}
if (! empty($data['observation_remarks'])) {
$query->where('observation_remarks', 'like', '%'.$data['observation_remarks'].'%');
}
if (! empty($data['created_by'])) {
$query->where('created_by', $data['created_by']);
}
if (! empty($data['created_from'])) {
$query->where('created_at', '>=', $data['created_from']);
}
if (! empty($data['created_to'])) {
$query->where('created_at', '<=', $data['created_to']);
}
if (! empty($data['updated_by'])) {
$query->where('updated_by', $data['updated_by']);
}
if (! empty($data['updated_from'])) {
$query->where('updated_at', '>=', $data['updated_from']);
}
if (! empty($data['updated_to'])) {
$query->where('updated_at', '<=', $data['updated_to']);
}
})
->indicateUsing(function (array $data) {
$indicators = [];
if (! empty($data['Plant'])) {
$indicators[] = 'Plant Name: '.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['test_bed_group'])) {
$indicators[] = 'Test Bed Group: '.$data['test_bed_group'];
}
if (! empty($data['line_no'])) {
$indicators[] = 'Line No: '.$data['line_no'];
}
if (! empty($data['pump_model'])) {
$indicators[] = 'Pump Model: '.$data['pump_model'];
}
if (! empty($data['pump_type'])) {
$indicators[] = 'Pump Type: '.$data['pump_type'];
}
if (! empty($data['motor_details'])) {
$indicators[] = 'Motor Details: '.$data['motor_details'];
}
if (! empty($data['test_type'])) {
$indicators[] = 'Test Type: '.$data['test_type'];
}
if (Str::length($data['flow']) > 0) {
$indicators[] = 'Flow: '.$data['flow'];
}
if (Str::length($data['head']) > 0) {
$indicators[] = 'Head: '.$data['head'];
}
if (Str::length($data['run_time']) > 0) {
$indicators[] = 'Run Time: '.$data['run_time'];
}
if (! empty($data['live_status'])) {
$indicators[] = 'Status: '.$data['live_status'];
}
if (! empty($data['observation_remarks'])) {
$indicators[] = 'Observation / Remarks: '.$data['observation_remarks'];
}
if (! empty($data['created_by'])) {
$indicators[] = 'Created By: '.$data['created_by'];
}
if (! empty($data['created_from'])) {
$indicators[] = 'Created From: '.$data['created_from'];
}
if (! empty($data['created_to'])) {
$indicators[] = 'Created To: '.$data['created_to'];
}
if (! empty($data['updated_by'])) {
$indicators[] = 'Updated By: '.$data['updated_by'];
}
if (! empty($data['updated_from'])) {
$indicators[] = 'Updated From: '.$data['updated_from'];
}
if (! empty($data['updated_to'])) {
$indicators[] = 'Updated To: '.$data['updated_to'];
}
return $indicators;
}),
])
->filtersFormMaxHeight('280px')
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
]),
])
->headerActions([
ImportAction::make()
->label('Import Testing Line Visualizations')
->color('warning')
->importer(TestingLineVisualizationImporter::class)
->visible(function () {
return Filament::auth()->user()->can('view import testing line visualizations');
}),
ExportAction::make()
->label('Export Testing Line Visualizations')
->color('warning')
->exporter(TestingLineVisualizationExporter::class)
->visible(function () {
return Filament::auth()->user()->can('view export testing line visualizations');
}),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListTestingLineVisualizations::route('/'),
'create' => Pages\CreateTestingLineVisualization::route('/create'),
'view' => Pages\ViewTestingLineVisualization::route('/{record}'),
'edit' => Pages\EditTestingLineVisualization::route('/{record}/edit'),
];
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Filament\Resources\TestingLineVisualizationResource\Pages;
use App\Filament\Resources\TestingLineVisualizationResource;
use Filament\Resources\Pages\CreateRecord;
class CreateTestingLineVisualization extends CreateRecord
{
protected static string $resource = TestingLineVisualizationResource::class;
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('create');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Filament\Resources\TestingLineVisualizationResource\Pages;
use App\Filament\Resources\TestingLineVisualizationResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditTestingLineVisualization extends EditRecord
{
protected static string $resource = TestingLineVisualizationResource::class;
protected function getHeaderActions(): array
{
return [
Actions\ViewAction::make(),
Actions\DeleteAction::make(),
Actions\ForceDeleteAction::make(),
Actions\RestoreAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\TestingLineVisualizationResource\Pages;
use App\Filament\Resources\TestingLineVisualizationResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListTestingLineVisualizations extends ListRecords
{
protected static string $resource = TestingLineVisualizationResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\TestingLineVisualizationResource\Pages;
use App\Filament\Resources\TestingLineVisualizationResource;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;
class ViewTestingLineVisualization extends ViewRecord
{
protected static string $resource = TestingLineVisualizationResource::class;
protected function getHeaderActions(): array
{
return [
Actions\EditAction::make(),
];
}
}