diff --git a/app/Filament/Resources/ProductionQuantityResource.php b/app/Filament/Resources/ProductionQuantityResource.php index 2972956b6..824d3e8f9 100644 --- a/app/Filament/Resources/ProductionQuantityResource.php +++ b/app/Filament/Resources/ProductionQuantityResource.php @@ -6,6 +6,7 @@ use App\Filament\Imports\ProductionQuantityImporter; use App\Filament\Resources\ProductionQuantityResource\Pages; use App\Filament\Resources\ProductionQuantityResource\RelationManagers; use App\Models\ProductionQuantity; +use Carbon\Carbon; use Filament\Forms; use Filament\Forms\Form; use Filament\Forms\Get; @@ -16,6 +17,7 @@ use Filament\Tables\Table; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\SoftDeletingScope; use Filament\Forms\Components\Section; +use Filament\Notifications\Notification; class ProductionQuantityResource extends Resource { @@ -129,10 +131,10 @@ class ProductionQuantityResource extends Resource } } - $shiftId = $get('shift_id'); + $curShiftId = $get('shift_id'); $set('line_id', null); - if (!$shiftId) { + if (!$curShiftId) { $set('pqShiftError', 'Please select a shift first.'); return; } @@ -189,8 +191,24 @@ class ProductionQuantityResource extends Resource $set('validationError', null); $set('pqLineError', null); $set('item_id', null); - $set('item_description', 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 = \App\Models\Item::where('id', $exists->item_id)->first(); + $set('recent_qr', $existCode->code.'|'.$exists->serial_number); + } + else + { + $set('recent_qr', null); + } } }) ->extraAttributes(fn ($get) => [ @@ -211,11 +229,12 @@ class ProductionQuantityResource extends Resource ->label('Item Code') // ->required() ->reactive() + ->autofocus(true) ->afterStateUpdated(function ($state, callable $get, callable $set) { // **Check if input is empty before processing** if (empty($state)) { $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); $set('validationError', null); return; @@ -225,52 +244,305 @@ class ProductionQuantityResource extends Resource if (!$get('plant_id')) { $set('item_code', null); $set('item_id', null); - $set('item_description', 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('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('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('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) + if ($exists) { - $set('item_code', null); - $set('item_id', null); - $set('item_description', null); - $set('serial_number', null); - $set('validationError', 'Please set production plan first.'); - return; + $currentDate = date('Y-m-d'); + + $shiftId = \App\Models\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 = \App\Models\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 = \App\Models\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 = \App\Models\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 = \App\Models\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); } @@ -278,8 +550,14 @@ class ProductionQuantityResource extends Resource if (strpos($state, '|') === false) { $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )'); $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); + + Notification::make() + ->title('Invalid QR') + ->body("Scan the valid QR code. (Ex: Item_Code|Serial_Number )") + ->danger() + ->send(); return; } else @@ -291,44 +569,70 @@ class ProductionQuantityResource extends Resource if (!ctype_alnum($iCode)) { $set('validationError', 'Item code must contain alpha-numeric values.'); $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); + Notification::make() + ->title('Invalid Item Code') + ->body("Item code must contain alpha-numeric values only.") + ->danger() + ->send(); return; } else if (strlen($iCode) < 6) { $set('validationError', 'Item code must be at least 6 digits.'); $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); + Notification::make() + ->title('Invalid Item Code') + ->body("Item code must be at least 6 digits.") + ->danger() + ->send(); return; } else if (!ctype_alnum($sNumber)) { $set('validationError', 'Serial Number must contain alpha-numeric values.'); $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); + Notification::make() + ->title('Invalid Serial Number') + ->body("Serial Number must contain alpha-numeric values only.") + ->danger() + ->send(); return; } else if (strlen($sNumber) < 9) { $set('validationError', 'Serial Number must be at least 9 digits.'); $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); + Notification::make() + ->title('Invalid Serial Number') + ->body("Serial Number must be at least 9 digits.") + ->danger() + ->send(); return; } } $set('validationError', 'Scan valid QR code. (Ex: Item_Code|Serial_Number )'); $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); + + 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('item_description', null); $set('serial_number', null); } @@ -337,45 +641,6 @@ class ProductionQuantityResource extends Resource $itemCode = trim($parts[0]); $serialNumber = isset($parts[1]) ? trim($parts[1]) : null; - // if (strlen($itemCode) < 6) { - // $set('validationError', 'Item code must be at least 6 digits.'); - // $set('item_id', null); - // $set('item_description', null); - // $set('serial_number', null); - // return; - // } - // else if (!ctype_alnum($itemCode)) { - // $set('validationError', 'Item code must contain alpha-numeric values.'); - // $set('item_id', null); - // $set('item_description', null); - // $set('serial_number', null); - // return; - // } - // else if ($serialNumber === '') { - // $set('validationError', 'Waiting for full QR scan...'); - // $set('item_id', null); - // $set('item_description', null); - // $set('serial_number', null); - // return; // Do not clear item_id, just wait for full input - // } - // else if (strlen($serialNumber) < 9) { - // $set('validationError', 'Serial Number must be at least 9 digits.'); - // $set('item_description', null); - // $set('serial_number', null); - // return; - // } - // else if (!ctype_alnum($serialNumber)) { - // $set('validationError', 'Serial Number must contain alpha-numeric values.'); - // $set('item_id', null); - // $set('item_description', null); - // $set('serial_number', null); - // return; - // } - // else - // { - // $set('validationError', null); - // } - // Fetch item using item code and plant_id $item = \App\Models\Item::where('code', $itemCode) ->where('plant_id', $get('plant_id')) @@ -384,21 +649,26 @@ class ProductionQuantityResource extends Resource if ($item) { $sNo = ProductionQuantity::where('serial_number', $serialNumber) - // ->where('plant_id', $get('plant_id')) + // ->where('plant_id', $get('plant_id')) ->exists(); if (!$sNo) { $set('serial_number', $serialNumber); $set('item_id', $item->id); $set('item_code', $itemCode); - $set('item_description', $item->description); + // $set('item_description', $item->description); } else { $set('validationError', 'Serial number already exist in database.'); $set('item_id', null); - $set('item_description', 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; } } @@ -406,8 +676,14 @@ class ProductionQuantityResource extends Resource { $set('validationError', 'Item code does not exist in master data.'); $set('item_id', null); - $set('item_description', null); + // $set('item_description', null); $set('serial_number', null); + + Notification::make() + ->title('Unknown Item Code') + ->body("Item code does not exist in master data.") + ->danger() + ->send(); return; } }) @@ -418,21 +694,16 @@ class ProductionQuantityResource extends Resource ->hintColor('danger'), Forms\Components\Hidden::make('item_id') ->required(), - Forms\Components\TextInput::make('item_description') - ->label('Description') - ->reactive() - ->readOnly(true) - ->required(), - // Forms\Components\Select::make('item_id') - // ->label('Description') - // ->relationship('item', 'description') - // ->required(), Forms\Components\TextInput::make('serial_number') ->required() ->unique(ignoreRecord: true) ->readOnly(true) ->autocapitalize('serial_number'), //->columnSpanFull(), + Forms\Components\TextInput::make('recent_qr') //item_description + ->label('Last scanned QR') + ->reactive() + ->readOnly(true), Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), @@ -516,4 +787,22 @@ class ProductionQuantityResource extends Resource 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, + // ]); + // } + // } }