schema([ Section::make('') ->schema([ Forms\Components\Select::make('plant_id') ->relationship('plant', 'name') ->required() // ->nullable() ->reactive() ->columnSpan(1) // ->default(fn () => optional(ProductionQuantity::latest()->first())->plant_id) ->default(function () { return optional(ProductionQuantity::latest()->first())->plant_id; }) ->disabled(fn (Get $get) => !empty($get('id'))) // ->afterStateUpdated(fn ($set) => $set('block_name', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { $plantId = $get('plant_id'); //... session(['select_plant' => $state]); session()->forget('select_line'); // Reset line filter //... $set('block_name', null); if (!$plantId) { $set('pqPlantError', 'Please select a plant first.'); return; } else { $set('validationError', null); $set('pqPlantError', null); } }) ->extraAttributes([ 'x-on:change' => " if (\$event.target.value) { \$wire.dispatch('filtersUpdated', { plantId: \$event.target.value }); } ", ]) // ->extraAttributes([ // 'x-on:change' => "\$wire.dispatch('filtersUpdated', { plantId: \$event.target.value })" // ]) // ->extraAttributes([ // 'x-on:change' => "\$wire.set('plantId', \$event.target.value)" // ]) ->extraAttributes(fn ($get) => [ 'class' => $get('pqPlantError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pqPlantError') ? $get('pqPlantError') : null) ->hintColor('danger'), Forms\Components\Select::make('block_name') ->required() // ->nullable() ->columnSpan(1) ->options(function (callable $get) { if (!$get('plant_id')) { return []; } return \App\Models\Block::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) ->reactive() ->default(function () { $latestShiftId = optional(ProductionQuantity::latest()->first())->shift_id; return optional(Shift::where('id', $latestShiftId)->first())->block_id; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { if($get('id')) { $getShift = ProductionQuantity::where('id', $get('id'))->first(); $getBlock = Shift::where('id', $getShift->shift_id)->first(); if($getBlock->block_id) { $set('block_name', $getBlock->block_id); $set('pqBlockError', null); } } $blockId = $get('block_name'); $set('shift_id', null); session(['select_plant' => $get('plant_id')]); session()->forget('select_line'); if (!$blockId) { $set('pqBlockError', 'Please select a block first.'); return; } else { $set('validationError', null); $set('pqBlockError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('pqBlockError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pqBlockError') ? $get('pqBlockError') : null) ->hintColor('danger'), Forms\Components\Select::make('shift_id') ->relationship('shift', 'name') ->required() // ->nullable() ->options(function (callable $get) { if (!$get('plant_id') || !$get('block_name')) { return []; } return Shift::where('plant_id', $get('plant_id')) ->where('block_id', $get('block_name')) ->pluck('name', 'id') ->toArray(); }) ->reactive() ->default(function () { return optional(ProductionQuantity::latest()->first())->shift_id; }) //->afterStateUpdated(fn ($set) => $set('line_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { if($get('id')) { $getShift = ProductionQuantity::where('id', $get('id'))->first(); if($getShift->shift_id) { $set('shift_id', $getShift->shift_id); $set('pqShiftError', null); } } $curShiftId = $get('shift_id'); $set('line_id', null); session(['select_plant' => $get('plant_id')]); session()->forget('select_line'); if (!$curShiftId) { $set('pqShiftError', 'Please select a shift first.'); return; } else { $set('validationError', null); $set('pqShiftError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('pqShiftError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pqShiftError') ? $get('pqShiftError') : null) ->hintColor('danger'), Forms\Components\Select::make('line_id') ->relationship('line', 'name') ->required() // ->nullable() // ->options(fn (callable $get) => // \App\Models\Line::where('plant_id', $get('plant_id')) // ->pluck('name', 'id') // ->toArray() // Convert collection to array // ) ->options(function (callable $get) { if (!$get('plant_id') || !$get('block_name') || !$get('shift_id')) { return []; } return \App\Models\Line::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) ->reactive() ->default(function () { return optional(ProductionQuantity::latest()->first())->line_id; }) ->afterStateUpdated(function ($state, callable $set, callable $get) { if($get('id')) { $getShift = ProductionQuantity::where('id', $get('id'))->first(); if($getShift->line_id) { $set('line_id', $getShift->line_id); $set('pqLineError', null); } } $lineId = $get('line_id'); $set('item_code', null); session(['select_plant' => $get('plant_id')]); session()->forget('select_line'); session(['select_line' => $state]); if (!$lineId) { $set('pqLineError', 'Please select a line first.'); return; } else { $set('validationError', null); $set('pqLineError', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $exists = ProductionQuantity::where('plant_id', $get('plant_id')) //->where('shift_id', $get('shift_id')) ->where('line_id', $get('line_id')) ->latest() // Orders by created_at DESC ->first(); // if($exists) // { // $existCode = Item::where('id', $exists->item_id)->first(); // $set('recent_qr', $existCode->code.'|'.$exists->serial_number); // } // else // { // $set('recent_qr', null); // } } }) // ->extraAttributes([ // 'x-on:change' => "\$wire.dispatch('filtersUpdated')", // Dispatch Livewire event from Alpine.js // ]) ->extraAttributes([ 'x-on:change' => "\$wire.dispatch('filtersUpdated', { lineId: \$event.target.value })" ]) ->extraAttributes(fn ($get) => [ 'class' => $get('pqLineError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pqLineError') ? $get('pqLineError') : null) ->hintColor('danger'), // Forms\Components\Select::make('item_id') // ->label('Item Code') // ->relationship('item', 'code') // ->required(), // // Forms\Components\TextInput::make('item_code') // // ->required() // // ->autocapitalize('item_code'), // // //->columnSpanFull(), // Virtual field for code input (not stored in DB) Forms\Components\TextInput::make('item_code') ->label('Item Code') // ->required() ->reactive() ->autofocus(true) ->debounce(100) // ->submitOnEnter() ->afterStateUpdated(function ($state, callable $get, callable $set) { $set('success_msg', null); if (empty($state)) { $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', null); return; } else { if (!$get('plant_id')) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please select a plant first.'); Notification::make() ->title('Choose Plant') ->body("Please select a plant first.") ->danger() ->send(); return; } else if (!$get('block_name')) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please select a block first.'); Notification::make() ->title('Choose Block') ->body("Please select a block first.") ->danger() ->send(); return; } else if (!$get('shift_id')) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please select a shift first.'); Notification::make() ->title('Choose Shift') ->body("Please select a shift first.") ->danger() ->send(); return; } else if (!$get('line_id')) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please select a line first.'); Notification::make() ->title('Choose Line') ->body("Please select a line first.") ->danger() ->send(); return; } // ******************************** $exists = \App\Models\ProductionPlan::where('plant_id', $get('plant_id')) ->where('shift_id', $get('shift_id')) ->where('line_id', $get('line_id')) ->whereDate('created_at', today()) ->latest() ->exists(); if ($exists) { $currentDate = date('Y-m-d'); $shiftId = Shift::where('id', $get('shift_id')) ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; //$miNs = (int) $miNs;-*/ $totalMinutes = $hRs * 60 + $miNs; $from_dt = $currentDate . ' ' . $shiftId->start_time; $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range if (!($currentDateTime >= $from_dt && $currentDateTime < $to_dt)) { //echo "Choosed a valid shift..."; // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please select a valid shift.'); Notification::make() ->title('Invalid Shift') ->body("Please select a valid shift.") ->danger() ->send(); //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); return; } else { $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', null); } } else { $existShifts = \App\Models\ProductionPlan::where('plant_id', $get('plant_id')) ->where('shift_id', $get('shift_id')) ->where('line_id', $get('line_id')) ->whereDate('created_at', Carbon::yesterday()) ->latest() ->exists(); if ($existShifts) //if ($existShifts->count() > 0) { // record exist on yesterday //$currentDate = date('Y-m-d'); $yesterday = date('Y-m-d', strtotime('-1 days')); $shiftId = Shift::where('id', $get('shift_id')) ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; // $miNs = (int) $miNs;-*/ $totalMinutes = $hRs * 60 + $miNs; $from_dt = $yesterday . ' ' . $shiftId->start_time; $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', null); } else { $currentDate = date('Y-m-d'); $shiftId = Shift::where('id', $get('shift_id')) ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; // $miNs = (int) $miNs;-*/ $totalMinutes = $hRs * 60 + $miNs; $from_dt = $currentDate . ' ' . $shiftId->start_time; $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { //echo "Choosed a valid shift..."; // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please set production plan first.'); Notification::make() ->title('Plan Not Found') ->body("Please set production plan first.") ->danger() ->send(); //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); return; } else { //echo "Choosed a valid shift..."; // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please select a valid shift.'); Notification::make() ->title('Invalid Shift') ->body("Please select a valid shift.") ->danger() ->send(); //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); return; } } } else { // record not exist on yesterday //$currentDate = date('Y-m-d'); $yesterday = date('Y-m-d', strtotime('-1 days')); $shiftId = Shift::where('id', $get('shift_id')) ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; // $miNs = (int) $miNs;-*/ $totalMinutes = $hRs * 60 + $miNs; $from_dt = $yesterday . ' ' . $shiftId->start_time; $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { //echo "Choosed a valid shift..."; // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please set production plan first.'); Notification::make() ->title('Plan Not Found') ->body("Please set production plan first.") ->danger() ->send(); //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); return; } else { $currentDate = date('Y-m-d'); $shiftId = Shift::where('id', $get('shift_id')) ->first(); [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; $hRs = (int) $hRs; // $miNs = (int) $miNs;-*/ $totalMinutes = $hRs * 60 + $miNs; $from_dt = $currentDate . ' ' . $shiftId->start_time; $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); $currentDateTime = date('Y-m-d H:i:s'); // Check if current date time is within the range if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { //echo "Choosed a valid shift..."; // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please set production plan first.'); Notification::make() ->title('Plan Not Found') ->body("Please set production plan first.") ->danger() ->send(); //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); return; } else { //echo "Choosed a valid shift..."; // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Please select a valid shift.'); Notification::make() ->title('Invalid Shift') ->body("Please select a valid shift.") ->danger() ->send(); //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); return; } } } } // ******************************** $set('item_id', null); $set('serial_number', null); $set('validationError', null); } if (!preg_match('/^[a-zA-Z0-9]{6,}+\|[1-9][a-zA-Z0-9]{8,}+(\|)?$/', $state)) { if (strpos($state, '|') === false) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )'); Notification::make() ->title('Invalid QR') ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") ->danger() ->send(); return; } else { $splits = explode('|', $state); $iCode = trim($splits[0]); $sNumber = isset($splits[1]) ? trim($splits[1]) : null; if (!ctype_alnum($iCode)) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Item code must contain alpha-numeric values.'); Notification::make() ->title('Invalid Item Code') ->body("Item code must contain alpha-numeric values only.") ->danger() ->send(); return; } else if (strlen($iCode) < 6) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Item code must be at least 6 digits.'); Notification::make() ->title('Invalid Item Code') ->body("Item code must be at least 6 digits.") ->danger() ->send(); return; } else if (!ctype_alnum($sNumber)) { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Serial Number must contain alpha-numeric values.'); Notification::make() ->title('Invalid Serial Number') ->body("Serial Number must contain alpha-numeric values only.") ->danger() ->send(); return; } else if (strlen($sNumber) < 9) { // $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Serial Number must be at least 9 digits.'); Notification::make() ->title('Invalid Serial Number') ->body("Serial Number must be at least 9 digits.") ->danger() ->send(); return; } } $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )'); Notification::make() ->title('Invalid QR') ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") ->danger() ->send(); return; } else { $set('validationError', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); } // Only search when all parent IDs are selected $parts = explode('|', $state); $itemCode = trim($parts[0]); $serialNumber = isset($parts[1]) ? trim($parts[1]) : null; // Fetch item using item code and plant_id $item = Item::where('code', $itemCode) ->where('plant_id', $get('plant_id')) ->first(); if ($item) { $sNo = ProductionQuantity::where('serial_number', $serialNumber) // ->where('plant_id', $get('plant_id')) ->exists(); if (!$sNo) { $set('success_msg', 'Y'); // if (preg_match('/\n/', $state)) { // dd($state.': Enter key pressed'); //$set('serial_number', $serialNumber); $set('item_id', $item->id); //$set('item_code', $itemCode); // } // if (str_ends_with($state, "\n")) { // // Enter key was pressed (newline character detected) // //$state = trim($state); // Remove the newline // dd($state.': Enter key pressed'); // // Perform your custom logic here // // For example, you could trigger a form submission: // // $this->submit(); // $set('serial_number', $serialNumber); // $set('item_id', $item->id); // $set('item_code', $itemCode); // // $set('item_description', $item->description); // } } else { $set('validationError', 'Serial number already exist in database.'); ////$set('item_code', null); //246118|651616516155667 $set('item_id', null); // $set('item_description', null); $set('serial_number', null); Notification::make() ->title('Duplicate Serial Number') ->body("Serial number already exist in database.") ->danger() ->send(); return; } } else { $set('item_code', null); $set('item_id', null); // $set('item_description', null); $set('serial_number', null); $set('validationError', 'Item code does not exist in master data.'); Notification::make() ->title('Unknown Item Code') ->body("Item code does not exist in master data.") ->danger() ->send(); return; } }) ->extraAttributes(fn ($get) => [ 'class' => $get('validationError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('validationError') ? $get('validationError') : null) ->hintColor('danger') ->extraAttributes([ 'x-data' => '{}', // 'x-data' => '{ value: "" }', // 'x-model' => 'value', // 'x-on:keydown.enter.prevent' => '$wire.processQr(value)', //'wire:keydown.enter.prevent' => 'processQr(value)', 'x-on:keydown.enter.prevent' => ' const formData = new FormData($event.target.form); const data = Object.fromEntries(formData.entries()); $wire.processAllValues(data) ', ]), Forms\Components\Hidden::make('item_id') ->required(), Forms\Components\Hidden::make('success_msg') ->required(), Forms\Components\Hidden::make('serial_number') ->required(), //->unique(ignoreRecord: true), // ->autocapitalize('characters'), // ->columnSpanFull(), Forms\Components\TextInput::make('recent_qr') //item_description ->label('Last scanned QR') ->reactive() ->default(function () { // Get the latest 'item_id' foreign key from 'production_quantities' table $latestProductionQuantity = ProductionQuantity::latest()->first(); if (!$latestProductionQuantity) { return null; // Return null if no production quantities exist } // Get the corresponding 'code' from 'items' table where 'id' matches 'item_id' $itemCode = optional(Item::find($latestProductionQuantity->item_id))->code; // Get the latest 'serial_number' from 'production_quantities' table $serialNumber = $latestProductionQuantity->serial_number; // Combine 'code' and 'serial_number' into the desired format return $itemCode && $serialNumber ? "{$itemCode} | {$serialNumber}" : null; }) ->readOnly(true), Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), ]) ->columns(6), ]); } public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('id') ->label('ID') ->numeric() ->sortable(), Tables\Columns\TextColumn::make('item.code') ->sortable(), Tables\Columns\TextColumn::make('serial_number') ->sortable(), Tables\Columns\TextColumn::make('line.name') ->sortable(), Tables\Columns\TextColumn::make('shift.name') ->sortable(), Tables\Columns\TextColumn::make('plant.name') ->sortable(), Tables\Columns\TextColumn::make('created_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('deleted_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ Tables\Filters\TrashedFilter::make(), ]) ->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() ->importer(ProductionQuantityImporter::class) ->chunkSize(1000), // ->maxRows(100000), ExportAction::make() ->exporter(ProductionQuantityExporter::class), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListProductionQuantities::route('/'), 'create' => Pages\CreateProductionQuantity::route('/create'), 'view' => Pages\ViewProductionQuantity::route('/{record}'), 'edit' => Pages\EditProductionQuantity::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } // public function mount(): void // { // // Fetch the value from the database based on your conditions // $recentScanned = ProductionQuantity::where('plant_id', $this->plant_id) // // ->where('shift_id', $this->shift_id) // // ->where('line_id', $this->line_id) // // ->whereDate('created_at', today()) // ->latest() // ->first(); // // Set the default value for 'plan_quantity' if a record exists // if ($recentScanned) { // $this->form()->fill([ // 'recent_qr' => $recentScanned->plan_quantity, // ]); // } // } }