diff --git a/app/Filament/Resources/RequestCharacteristicResource.php b/app/Filament/Resources/RequestCharacteristicResource.php index a5d88d8..79cde89 100644 --- a/app/Filament/Resources/RequestCharacteristicResource.php +++ b/app/Filament/Resources/RequestCharacteristicResource.php @@ -7,6 +7,7 @@ use App\Filament\Resources\RequestCharacteristicResource\Pages; use App\Models\CharacteristicApproverMaster; use App\Models\Item; use App\Models\Machine; +use App\Models\Plant; use App\Models\RequestCharacteristic; use Closure; use Filament\Facades\Filament; @@ -65,12 +66,20 @@ class RequestCharacteristicResource extends Resource Section::make('') ->schema([ Forms\Components\Select::make('plant_id') - ->label('Plant') - ->reactive() + ->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 () { - return optional(RequestCharacteristic::latest()->first())->plant_id; + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? null : optional(RequestCharacteristic::latest()->first())->plant_id ?? null; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { $plantId = $get('plant_id'); @@ -80,8 +89,9 @@ class RequestCharacteristicResource extends Resource $set('aufnr', null); $set('machine_name', null); $set('characteristic_approver_master_id', null); - if (! $updRec && $plantId) { - $set('work_flow_id', self::isNewWorkFlow($get)); + $set('approver_type', null); + if (! $updRec) { + $set('work_flow_id', $plantId ? self::isNewWorkFlow($get) : null); } $set('updated_by', Filament::auth()->user()?->name); }) @@ -101,13 +111,16 @@ class RequestCharacteristicResource extends Resource }) ->required() ->default(function () { - return optional(RequestCharacteristic::latest()->first())->machine_id ?? []; + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? null : optional(RequestCharacteristic::latest()->first())->machine_id ?? null; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('item_id', null); $set('aufnr', null); $set('machine_name', null); $set('characteristic_approver_master_id', null); + $set('approver_type', null); $set('updated_by', Filament::auth()->user()?->name); }) ->disabled(fn ($get) => self::isFieldDisabled($get)), @@ -115,6 +128,7 @@ class RequestCharacteristicResource extends Resource ->label('Work Flow ID') ->readOnly() ->reactive() + ->required() ->default(function ($state, callable $set, callable $get) { $updRec = $get('id'); if (! $updRec) { @@ -128,9 +142,10 @@ class RequestCharacteristicResource extends Resource // ->ignore($get('id')); // }), Forms\Components\Select::make('item_id') - ->label('Item') + ->label('Item Code') // ->relationship('item', 'id') ->reactive() + ->required() ->searchable() ->options(function (callable $get) { $plantId = $get('plant_id'); @@ -140,44 +155,64 @@ class RequestCharacteristicResource extends Resource return Item::where('plant_id', $plantId)->pluck('code', 'id'); }) - ->required() ->afterStateUpdated(function ($state, callable $set, callable $get) { - $set('aufnr', null); + if (! $state) { + $set('aufnr', null); + } $set('updated_by', Filament::auth()->user()?->name); }) ->default(function () { - return optional(RequestCharacteristic::latest()->first())->item_id ?? []; + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? null : optional(RequestCharacteristic::latest()->first())->item_id ?? null; }) ->disabled(fn ($get) => self::isFieldDisabled($get)), Forms\Components\TextInput::make('aufnr') - ->label('Aufnr') + ->label('Job Number') ->reactive() - ->required() + ->required(function (callable $get) { + $item = $get('item_id'); + if ($item) { + return true; + } + + return false; + }) ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('updated_by', Filament::auth()->user()?->name); }) ->default(function () { - return optional(RequestCharacteristic::latest()->first())->aufnr ?? ''; + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? null : optional(RequestCharacteristic::latest()->first())->aufnr ?? null; }) + ->readOnly(fn ($get) => ($get('item_id') == null)) ->disabled(fn ($get) => self::isFieldDisabled($get)), Forms\Components\Select::make('machine_name') - ->label('Machines') + ->label('Machine Name') ->reactive() ->nullable() ->searchable() - ->options(function (callable $get) { + ->options(function (callable $set, callable $get) { $plantId = $get('plant_id'); $machineId = $get('machine_id'); if (! $plantId || ! $machineId) { + $set('characteristic_name', null); + $set('current_value', null); + $set('update_value', null); + return []; } return CharacteristicApproverMaster::where('plant_id', $plantId)->where('machine_id', $machineId)->pluck('machine_name', 'machine_name')->unique(); }) ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('approver_type', null); // dd($get('characteristic_approver_master_id')); $set('characteristic_approver_master_id', null); - // dd($get('characteristic_approver_master_id')); + $set('characteristic_name', null); + $set('current_value', null); + $set('update_value', null); $set('updated_by', Filament::auth()->user()?->name); }) ->default(function () { @@ -185,60 +220,121 @@ class RequestCharacteristicResource extends Resource $reqId = RequestCharacteristic::latest()->first()?->characteristic_approver_master_id; if ($reqId) { $reqMac = CharacteristicApproverMaster::where('id', $reqId)->first()?->machine_name; - if ($reqMac) { - $machineName = $reqMac; - } else { - $machineName = null; - } + $machineName = $reqMac ?? null; } else { $machineName = null; } - // return optional(RequestCharacteristic::latest()->first())->characteristic_approver_master_id ?? []; - return $machineName ?? []; + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? null : $machineName ?? null; }) ->afterStateHydrated(function ($component, $state, Get $get, Set $set) { if ($get('id')) { $reqId = RequestCharacteristic::where('id', $get('id'))->first()?->characteristic_approver_master_id; if ($reqId) { $reqMac = CharacteristicApproverMaster::where('id', $reqId)->first()?->machine_name; - if ($reqMac) { - $set('machine_name', $reqMac); - } else { - $set('machine_name', null); - } + $set('machine_name', $reqMac ?? null); } else { $set('machine_name', null); } } }) ->required(), - Forms\Components\Select::make('characteristic_approver_master_id') - ->label('Master Characteristic Field') - // ->relationship('characteristicApproverMaster', 'characteristic_field') + Forms\Components\Select::make('approver_type') + ->label('Request Type') + // ->columnSpan(1) ->reactive() ->nullable() ->searchable() - ->options(function (callable $get) { + ->required() + ->options(function (callable $set, callable $get) { $plantId = $get('plant_id'); $machineId = $get('machine_id'); $machineName = $get('machine_name'); if (! $plantId || ! $machineId || ! $machineName) { + $set('characteristic_name', null); + $set('current_value', null); + $set('update_value', null); + return []; } - return CharacteristicApproverMaster::where('plant_id', $plantId)->where('machine_id', $machineId)->where('machine_name', $machineName)->pluck('characteristic_field', 'id'); - }) - ->default(function () { - return optional(RequestCharacteristic::latest()->first())->characteristic_approver_master_id ?? []; + return [ + 'Characteristic' => 'Characteristic', + 'Quality' => 'Quality', + ]; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('characteristic_approver_master_id', null); + if ($get('approver_type') != 'Characteristic') { + $set('characteristic_name', null); + $set('current_value', null); + $set('update_value', null); + } + $set('updated_by', Filament::auth()->user()?->name); + }) + ->default(function () { + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? null : 'Characteristic'; + // $approverType = null; + // $reqId = RequestCharacteristic::latest()->first()?->characteristic_approver_master_id; + // if ($reqId) { + // $reqTyp = CharacteristicApproverMaster::where('id', $reqId)->first()?->approver_type; + // $approverType = $reqTyp ?? null; + // } else { + // $approverType = null; + // } + + // // return optional(CharacteristicApproverMaster::latest()->first())->approver_type ?? null; + // return $approverType ?? null; + }) + ->afterStateHydrated(function ($component, $state, Get $get, Set $set) { + if ($get('id')) { + $reqId = RequestCharacteristic::where('id', $get('id'))->first()?->characteristic_approver_master_id; + if ($reqId) { + $reqTyp = CharacteristicApproverMaster::where('id', $reqId)->first()?->approver_type; + $set('approver_type', $reqTyp ?? null); + } else { + $set('approver_type', null); + } + } + }), + Forms\Components\Select::make('characteristic_approver_master_id') + ->label('Master Characteristic Field') + // ->relationship('characteristicApproverMaster', 'characteristic_field') + ->columnSpan(2) + ->reactive() + ->nullable() + ->searchable() + ->options(function (callable $set, callable $get) { + $plantId = $get('plant_id'); + $machineId = $get('machine_id'); + $machineName = $get('machine_name'); + $reqTyp = $get('approver_type'); + + if (! $plantId || ! $machineId || ! $machineName || ! $reqTyp) { + return []; + } + + return CharacteristicApproverMaster::where('plant_id', $plantId)->where('machine_id', $machineId)->where('machine_name', $machineName)->where('approver_type', $reqTyp)->pluck('characteristic_field', 'id'); + }) + ->default(function () { + $userHas = Filament::auth()->user()->plant_id; + + return ($userHas && strlen($userHas) > 0) ? null : optional(RequestCharacteristic::latest()->first())->characteristic_approver_master_id ?? null; + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('characteristic_name', null); + $set('current_value', null); + $set('update_value', null); $set('updated_by', Filament::auth()->user()?->name); }) ->required(), // ->disabled(fn ($get) => self::isFieldDisabled($get)) - Section::make('Request Characteristic Field') + Section::make('Request Characteristic Details') // ->columnSpan(['default' => 2, 'sm' => 4]) ->reactive() ->schema([ @@ -249,7 +345,14 @@ class RequestCharacteristicResource extends Resource $set('characteristic_name', strtoupper($state)); $set('updated_by', Filament::auth()->user()?->name); }) - ->required() + ->required(function (callable $get) { + $reqTyp = $get('approver_type'); + if ($reqTyp == 'Characteristic') { + return true; + } + + return false; + }) ->rules([ // function (callable $get) { // return Rule::unique('request_characteristics', 'characteristic_name') @@ -288,7 +391,7 @@ class RequestCharacteristicResource extends Resource }; }, ]) - ->readOnly(fn ($get) => self::isFieldDisabled($get)), // disabled + ->readOnly(fn ($get) => ($get('approver_type') != 'Characteristic') ?? self::isFieldDisabled($get)), Forms\Components\TextInput::make('current_value') ->label('Current Value') ->reactive() @@ -296,14 +399,15 @@ class RequestCharacteristicResource extends Resource $set('updated_by', Filament::auth()->user()?->name); }) ->required(function (callable $get) { + $reqTyp = $get('approver_type'); $updateVal = $get('update_value'); - if ($updateVal == null || $updateVal == '') { + if ($reqTyp == 'Characteristic' && ($updateVal == null || $updateVal == '')) { return true; } return false; }) - ->readOnly(fn ($get) => self::isFieldDisabled($get)), + ->readOnly(fn ($get) => ($get('approver_type') != 'Characteristic') ?? self::isFieldDisabled($get)), Forms\Components\TextInput::make('update_value') ->label('Update Value') ->reactive() @@ -321,9 +425,17 @@ class RequestCharacteristicResource extends Resource }; }, ]) - ->readOnly(fn ($get) => self::isFieldDisabled($get)), + ->readOnly(fn ($get) => ($get('approver_type') != 'Characteristic') ?? self::isFieldDisabled($get)), ]) - ->collapsible() + // ->collapsed(function ($get): bool { + // $reqTyp = $get('approver_type'); + // if ($reqTyp == 'Characteristic') { + // return true; + // } + + // return false; + // }) + ->collapsed() ->columns(['default' => 1, 'sm' => 3]), Section::make(function ($get): string { $approverId = $get('characteristic_approver_master_id'); @@ -338,16 +450,19 @@ class RequestCharacteristicResource extends Resource Forms\Components\Select::make('approver_status1') ->label('Approver Status') ->reactive() - ->live() + // ->live() ->options([ 'Approved' => 'Approved', 'Hold' => 'Hold', 'Rejected' => 'Rejected', ]) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if ($state && empty($get('approved1_at'))) { - $set('approved1_at', now()); + if ($state) {// && empty($get('approved1_at')) + $set('approved1_at', now()->toDateTimeString()); // now()->format('d-m-Y H:i:s') + } else { + $set('approved1_at', null); } + $set('approver_remark1', null); $set('updated_by', Filament::auth()->user()?->name); }) ->disabled(function ($get) { @@ -377,10 +492,17 @@ class RequestCharacteristicResource extends Resource }), Forms\Components\TextInput::make('approver_remark1') ->label('Approver Remark') - ->reactive() + ->live() ->afterStateUpdated(function ($state, callable $set, callable $get) { + $appStat = $get('approver_status1'); + if ($appStat && $state) { + $set('approved1_at', now()->toDateTimeString()); // now()->format('d-m-Y H:i:s') + } else { + $set('approved1_at', null); + } $set('updated_by', Filament::auth()->user()?->name); }) + ->readOnly(fn ($get) => ($get('approver_status1') == null)) ->disabled(function ($get) { $currentUser = Filament::auth()->user(); $updId = $get('id'); @@ -402,10 +524,12 @@ class RequestCharacteristicResource extends Resource default => 'Updated At', }) ->reactive() - ->live() + // ->live() ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('updated_by', Filament::auth()->user()?->name); }) + // ->format('d-m-Y H:i:s') + ->readOnly(fn ($get) => ($get('approver_status1') == null)) ->disabled(function ($get) { $currentUser = Filament::auth()->user(); // logged-in user $approverId = $get('characteristic_approver_master_id'); @@ -448,9 +572,12 @@ class RequestCharacteristicResource extends Resource 'Rejected' => 'Rejected', ]) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if ($state && empty($get('approved2_at'))) { - $set('approved2_at', now()); + if ($state) { + $set('approved2_at', now()->toDateTimeString()); + } else { + $set('approved2_at', null); } + $set('approver_remark2', null); $set('updated_by', Filament::auth()->user()?->name); }) ->disabled(function ($get) { @@ -480,10 +607,17 @@ class RequestCharacteristicResource extends Resource }), Forms\Components\TextInput::make('approver_remark2') ->label('Approver Remark') - ->reactive() + ->live() ->afterStateUpdated(function ($state, callable $set, callable $get) { + $appStat = $get('approver_status2'); + if ($appStat && $state) { + $set('approved2_at', now()->toDateTimeString()); + } else { + $set('approved2_at', null); + } $set('updated_by', Filament::auth()->user()?->name); }) + ->readOnly(fn ($get) => ($get('approver_status2') == null)) ->disabled(function ($get) { $currentUser = Filament::auth()->user(); $updId = $get('id'); @@ -507,6 +641,7 @@ class RequestCharacteristicResource extends Resource ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('updated_by', Filament::auth()->user()?->name); }) + ->readOnly(fn ($get) => ($get('approver_status2') == null)) ->disabled(function ($get) { $currentUser = Filament::auth()->user(); // logged-in user $approverId = $get('characteristic_approver_master_id'); @@ -550,9 +685,12 @@ class RequestCharacteristicResource extends Resource 'Rejected' => 'Rejected', ]) ->afterStateUpdated(function ($state, callable $set, callable $get) { - if ($state && empty($get('approved3_at'))) { - $set('approved3_at', now()); + if ($state) { + $set('approved3_at', now()->toDateTimeString()); + } else { + $set('approved3_at', null); } + $set('approver_remark3', null); $set('updated_by', Filament::auth()->user()?->name); }) ->disabled(function ($get) { @@ -581,10 +719,17 @@ class RequestCharacteristicResource extends Resource }), Forms\Components\TextInput::make('approver_remark3') ->label('Approver Remark') - ->reactive() + ->live() ->afterStateUpdated(function ($state, callable $set, callable $get) { + $appStat = $get('approver_status3'); + if ($appStat && $state) { + $set('approved3_at', now()->toDateTimeString()); + } else { + $set('approved3_at', null); + } $set('updated_by', Filament::auth()->user()?->name); }) + ->readOnly(fn ($get) => ($get('approver_status3') == null)) ->disabled(function ($get) { $currentUser = Filament::auth()->user(); $updId = $get('id'); @@ -608,6 +753,7 @@ class RequestCharacteristicResource extends Resource ->afterStateUpdated(function ($state, callable $set, callable $get) { $set('updated_by', Filament::auth()->user()?->name); }) + ->readOnly(fn ($get) => ($get('approver_status3') == null)) ->disabled(function ($get) { $currentUser = Filament::auth()->user(); // logged-in user $approverId = $get('characteristic_approver_master_id'); @@ -695,12 +841,12 @@ class RequestCharacteristicResource extends Resource $lastSerial = substr($lastWorkflow->work_flow_id, strlen($prefix)); $nextSerial = str_pad( intval($lastSerial) + 1, - 3, + 4, '0', STR_PAD_LEFT ); } else { - $nextSerial = '001'; + $nextSerial = '0001'; } $workFlowId = "{$prefix}{$nextSerial}"; @@ -726,7 +872,7 @@ class RequestCharacteristicResource extends Resource ->alignCenter() ->searchable(), // isIndividual: true Tables\Columns\TextColumn::make('plant.name') - ->label('Plant') + ->label('Plant Name') ->alignCenter() ->searchable() ->sortable(), @@ -741,17 +887,22 @@ class RequestCharacteristicResource extends Resource ->searchable() ->sortable(), Tables\Columns\TextColumn::make('aufnr') - ->label('Aufnr') + ->label('Job Number') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('characteristicApproverMaster.machine_name') - ->label('Machine') + ->label('Machine Name') + ->alignCenter() + ->searchable() + ->sortable(), + Tables\Columns\TextColumn::make('characteristicApproverMaster.approver_type') + ->label('Request Type') ->alignCenter() ->searchable() ->sortable(), Tables\Columns\TextColumn::make('characteristicApproverMaster.characteristic_field') - ->label('Master Characteristic') + ->label('Master Characteristic Field') ->alignCenter() ->searchable() ->formatStateUsing(fn (string $state): string => strtoupper(__($state)))