diff --git a/app/Filament/Resources/TestingPanelReadingResource.php b/app/Filament/Resources/TestingPanelReadingResource.php index ee7c20d..6c49ecc 100644 --- a/app/Filament/Resources/TestingPanelReadingResource.php +++ b/app/Filament/Resources/TestingPanelReadingResource.php @@ -14,6 +14,7 @@ use App\Models\MotorTestingMaster; use App\Models\Plant; use App\Models\TestingPanelReading; use Barryvdh\DomPDF\Facade\Pdf; +use Closure; // use Barryvdh\Reflection\DocBlock\Type\Collection; use Filament\Facades\Filament; use Filament\Forms; @@ -36,6 +37,7 @@ use Illuminate\Database\Eloquent\SoftDeletingScope; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Response; // use Barryvdh\Snappy\Facades\SnappyPdf as PDF; +use Illuminate\Support\Str; use Maatwebsite\Excel\Facades\Excel; // use App\Exports\TestingPanelReadingExport; use PhpOffice\PhpSpreadsheet\IOFactory; // For loading Excel file @@ -95,7 +97,7 @@ class TestingPanelReadingResource extends Resource ->hintColor('danger'), Forms\Components\Select::make('line_id') ->label('Line Name') - ->relationship('line', 'name') + // ->relationship('line', 'name') ->searchable() ->options(function (callable $get) { $plantId = $get('plant_id'); @@ -115,11 +117,12 @@ class TestingPanelReadingResource extends Resource ->reactive() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('motor_testing_master_id', null); + $set('machine_id', null); $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\Select::make('machine_id') ->label('Work Center') - ->relationship('machine', 'work_center') + // ->relationship('machine', 'work_center') ->searchable() ->options(function (callable $get) { $lineId = $get('line_id'); @@ -183,17 +186,92 @@ class TestingPanelReadingResource extends Resource ->required() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); + }) + ->rule(function (callable $get) { + return function (string $attribute, $value, Closure $fail) use ($get) { + + $plantId = $get('plant_id'); + $lineId = $get('line_id'); + $machineId = $get('machine_id'); + $itemId = $get('motor_testing_master_id'); + $output = trim($value); + // $currentId = $get('id'); // current editing record id + + if (! $plantId || ! $lineId || ! $machineId || ! $itemId) { + return; + } + + if ($output == null || $output == '' || ! $output) { + $fail("Output can't be empty!"); // Output1 - Output99 + } elseif (Str::length($output) < 7) { + $fail('Output must be at least 7 characters long!'); + } elseif (Str::length($output) > 8) { + $fail('Output must be at most 8 characters long!'); + } elseif (! ctype_alnum($output)) { + $fail('Output should contain only alpha-numeric values!'); + } elseif (! preg_match('/^Output([1-9]|[1-9][0-9])$/', $output)) { + $fail("Output should be between 'Output1' and 'Output99'!"); + } + }; }), Forms\Components\TextInput::make('serial_number') ->label('Serial Number') ->required() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); + }) + ->rule(function (callable $get) { + return function (string $attribute, $value, Closure $fail) use ($get) { + + $plantId = $get('plant_id'); + $lineId = $get('line_id'); + $machineId = $get('machine_id'); + $itemId = $get('motor_testing_master_id'); + $serialNumber = trim($value); + // $currentId = $get('id'); // current editing record id + + if (! $plantId || ! $lineId || ! $machineId || ! $itemId) { + return; + } + + if ($serialNumber == null || $serialNumber == '' || ! $serialNumber) { + $fail("Serial number can't be empty!"); + } elseif (Str::length($serialNumber) < 9) { + $fail('Serial number should contain minimum 9 digits!'); + } elseif (! ctype_alnum($serialNumber)) { + $fail('Serial number should contain only alpha-numeric values!'); + } elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $serialNumber)) { + $fail("Serial number should not begin with '0' or letter!"); + } + }; }), Forms\Components\TextInput::make('winded_serial_number') ->label('Winded Serial Number') ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); + }) + ->rule(function (callable $get) { + return function (string $attribute, $value, Closure $fail) use ($get) { + + $plantId = $get('plant_id'); + $lineId = $get('line_id'); + $machineId = $get('machine_id'); + $itemId = $get('motor_testing_master_id'); + $serialNumber = trim($value); + // $currentId = $get('id'); // current editing record id + + if (! $plantId || ! $lineId || ! $machineId || ! $itemId || ! $serialNumber) { + return; + } + + if (Str::length($serialNumber) > 0 && Str::length($serialNumber) < 9) { + $fail('Winded serial number should contain minimum 9 digits!'); + } elseif (! ctype_alnum($serialNumber)) { + $fail('Winded serial number should contain only alpha-numeric values!'); + } elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $serialNumber)) { + $fail("Winded serial number should not begin with '0' or letter!"); + } + }; }), Forms\Components\TextInput::make('before_fr_volt') ->label('Before FR Volt') @@ -350,24 +428,40 @@ class TestingPanelReadingResource extends Resource ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), - Forms\Components\TextInput::make('hv_test') + Forms\Components\Select::make('hv_test') ->label('High Voltage Test') + ->options([ + 'With Stood' => 'With Stood', + 'Not With Stood' => 'Not With Stood', + ]) + ->selectablePlaceholder(false) + ->default('Not With Stood') + ->required() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('batch_number') ->label('Batch Number') + ->readOnly(fn (callable $get) => ! $get('id')) ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('batch_count') ->label('Batch Count') ->default('0') + ->readOnly(fn (callable $get) => ! $get('id')) ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), - Forms\Components\TextInput::make('result') + Forms\Components\Select::make('result') ->label('Result') + ->options([ + 'PASS' => 'PASS', // Not With Stood + 'FAIL' => 'FAIL', + ]) + ->selectablePlaceholder(false) + ->default('FAIL') + ->required() ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), @@ -379,27 +473,28 @@ class TestingPanelReadingResource extends Resource Forms\Components\TextInput::make('rework_count') ->label('Rework Count') ->default('0') + ->readOnly(fn (callable $get) => ! $get('id')) ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('update_count') ->label('Update Count') ->default('0') + ->readOnly(fn (callable $get) => ! $get('id')) ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), Forms\Components\TextInput::make('output_flag') ->label('Output Flag') ->default('0') + ->readOnly(fn (callable $get) => ! $get('id')) ->afterStateUpdated(function (callable $set, callable $get, ?string $state) { $set('updated_by', Filament::auth()->user()?->name); }), - Forms\Components\TextInput::make('tested_by') - ->label('Tested By') + Forms\Components\Hidden::make('tested_by') ->default(fn () => Filament::auth()->user()?->name) ->required(), - Forms\Components\TextInput::make('updated_by') - ->label('Updated By') + Forms\Components\Hidden::make('updated_by') ->default(fn () => Filament::auth()->user()?->name) ->required(), Forms\Components\TextInput::make('id') @@ -665,6 +760,24 @@ class TestingPanelReadingResource extends Resource ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('item_code', null); }), + Select::make('machine_name') + ->label('Search by Work Center') + ->searchable() + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + $lineId = $get('Line'); + + if (! $plantId || ! $lineId) { + return []; + } + + return Machine::where('plant_id', $plantId) + ->where('line_id', $lineId) + ->pluck('work_center', 'id') + ->toArray(); + }) + ->reactive(), Select::make('item_code') ->label('Search by Item Code') ->searchable() @@ -687,30 +800,37 @@ class TestingPanelReadingResource extends Resource ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('item_description', null); }), - Select::make('machine_name') - ->label('Search by Work Center') - ->searchable() - ->nullable() - ->options(function (callable $get) { - $plantId = $get('Plant'); - $lineId = $get('Line'); - - if (! $plantId || ! $lineId) { - return []; - } - - return Machine::where('plant_id', $plantId) - ->where('line_id', $lineId) - ->pluck('work_center', 'id') - ->toArray(); - }) - ->reactive(), TextInput::make('serial_number') ->label('Serial Number') ->reactive() ->placeholder('Enter Serial Number'), + Select::make('subassembly_code') + ->label('Search by Subassembly Code') + ->searchable() + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if ($plantId) { + return MotorTestingMaster::whereHas('testingPanelReadings', function ($query) { + $query->whereNotNull('id'); + })->orderBy('subassembly_code')->pluck('subassembly_code', 'id'); + } else { + return []; + // return Item::whereHas('motorTestingMasters') + // ->pluck('code', 'id') + // ->toArray(); + } + }) + ->reactive() + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('item_description', null); + }), + TextInput::make('winded_serial_number') + ->label('Winded Serial Number') + ->reactive() + ->placeholder('Enter Winded Serial Number'), Select::make('item_description') - ->label('Model') + ->label('Search by Model') ->searchable() ->options(function (callable $get) { $plantId = $get('Plant'); @@ -739,13 +859,12 @@ class TestingPanelReadingResource extends Resource $set('item_code', null); }), Select::make('output') - ->label('Output') + ->label('Search by Output') ->searchable() ->options(function () { - return TestingPanelReading::query() + return TestingPanelReading::whereNotNull('output') ->select('output') ->distinct() - ->whereNotNull('output') ->orderBy('output') ->pluck('output', 'output') // key and label are both the output value ->toArray(); @@ -781,6 +900,7 @@ class TestingPanelReadingResource extends Resource // }) // ->reactive(), // ... + Select::make('connection') ->label('Connection') ->required() @@ -805,13 +925,14 @@ class TestingPanelReadingResource extends Resource // .. TextInput::make('remark') ->label('Remark') - ->reactive(), + ->reactive() + ->placeholder(placeholder: 'Enter Remark'), TextInput::make('batch_number') ->label('Batch Number') ->reactive() ->placeholder(placeholder: 'Enter Batch Number'), Select::make('result') - ->label('Result') + ->label('Search by Result') ->searchable() ->options(function () { return TestingPanelReading::query() @@ -823,7 +944,19 @@ class TestingPanelReadingResource extends Resource ->toArray(); }) ->reactive(), - + Select::make('tested_by') + ->label('Tested By') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (! $plantId) { + return TestingPanelReading::whereNotNull('tested_by')->select('tested_by')->distinct()->pluck('tested_by', 'tested_by'); + } else { + return TestingPanelReading::where('plant_id', $plantId)->whereNotNull('tested_by')->select('tested_by')->distinct()->pluck('tested_by', 'tested_by'); + } + }) + ->searchable() + ->reactive(), DateTimePicker::make(name: 'created_from') ->label('Created From') ->placeholder(placeholder: 'Select From DateTime') @@ -834,12 +967,35 @@ class TestingPanelReadingResource extends Resource ->placeholder(placeholder: 'Select To DateTime') ->reactive() ->native(false), + Select::make('updated_by') + ->label('Updated By') + ->nullable() + ->options(function (callable $get) { + $plantId = $get('Plant'); + if (! $plantId) { + return TestingPanelReading::whereNotNull('updated_by')->select('updated_by')->distinct()->pluck('updated_by', 'updated_by'); + } else { + return TestingPanelReading::where('plant_id', $plantId)->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) { // dd($data); // Hide all records initially if no filters are applied - if (empty($data['Plant']) && empty($data['Line']) && empty($data['item_code']) && empty($data['machine_name']) && empty($data['item_description']) && empty($data['serial_number']) && empty($data['output']) && empty($data['phase']) && empty($data['connection']) && empty($data['remark']) && empty($data['batch_no']) && empty($data['result']) && empty($data['created_from']) && empty($data['created_to'])) { + if (empty($data['Plant']) && empty($data['Line']) && empty($data['item_code']) && empty($data['subassembly_code']) && empty($data['machine_name']) && empty($data['item_description']) && empty($data['serial_number']) && empty($data['winded_serial_number']) && empty($data['output']) && empty($data['phase']) && empty($data['connection']) && empty($data['remark']) && empty($data['batch_no']) && empty($data['result']) && empty($data['tested_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'); } @@ -868,12 +1024,20 @@ class TestingPanelReadingResource extends Resource }); } + if (! empty($data['subassembly_code'])) { + $query->where('motor_testing_master_id', $data['subassembly_code']); + } + if (! empty($data['machine_name'])) { $query->where('machine_id', $data['machine_name']); } if (! empty($data['serial_number'])) { - $query->where('serial_number', $data['serial_number']); + $query->where('serial_number', 'like', '%'.$data['serial_number'].'%'); + } + + if (! empty($data['winded_serial_number'])) { + $query->where('winded_serial_number', 'like', '%'.$data['winded_serial_number'].'%'); } if (! empty($data['item_description'])) { @@ -919,11 +1083,11 @@ class TestingPanelReadingResource extends Resource } if (! empty($data['remark'])) { - $query->where('remark', $data['remark']); + $query->where('remark', 'like', '%'.$data['remark'].'%'); } if (! empty($data['batch_number'])) { - $query->where('batch_number', $data['batch_number']); + $query->where('batch_number', 'like', '%'.$data['batch_number'].'%'); } if (! empty($data['result'])) { @@ -937,6 +1101,22 @@ class TestingPanelReadingResource extends Resource if (! empty($data['created_to'])) { $query->where('created_at', '<=', $data['created_to']); } + + if (! empty($data['tested_by'])) { + $query->where('tested_by', $data['tested_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']); + } + + if (! empty($data['updated_by'])) { + $query->where('updated_by', $data['updated_by']); + } }) ->indicateUsing(function (array $data) { $indicators = []; @@ -953,11 +1133,14 @@ class TestingPanelReadingResource extends Resource if (! empty($data['Line'])) { $indicators[] = 'Line Name: '.Line::where('id', $data['Line'])->value('name'); } + if (! empty($data['machine_name'])) { + $indicators[] = 'Work Center: '.Machine::where('id', $data['machine_name'])->value('work_center'); + } if (! empty($data['item_code'])) { $indicators[] = 'Item Code: '.Item::where('id', $data['item_code'])->value('code'); } - if (! empty($data['machine_name'])) { - $indicators[] = 'Work Center: '.Machine::where('id', $data['machine_name'])->value('work_center'); + if (! empty($data['subassembly_code'])) { + $indicators[] = 'Subassembly Code: '.MotorTestingMaster::where('id', $data['subassembly_code'])->value('subassembly_code'); } if (! empty($data['output'])) { $indicators[] = 'Output: '.$data['output']; @@ -986,6 +1169,10 @@ class TestingPanelReadingResource extends Resource $indicators[] = 'Serial Number: '.$data['serial_number']; } + if (! empty($data['tested_by'])) { + $indicators[] = 'Tested By: '.$data['tested_by']; + } + if (! empty($data['created_from'])) { $indicators[] = 'From: '.$data['created_from']; } @@ -994,6 +1181,18 @@ class TestingPanelReadingResource extends Resource $indicators[] = '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; }), ])