diff --git a/app/Filament/Resources/ProductionPlanResource.php b/app/Filament/Resources/ProductionPlanResource.php index d311b1043..7aaceb55b 100644 --- a/app/Filament/Resources/ProductionPlanResource.php +++ b/app/Filament/Resources/ProductionPlanResource.php @@ -6,6 +6,7 @@ use App\Filament\Imports\ProductionPlanImporter; use App\Filament\Resources\ProductionPlanResource\Pages; use App\Filament\Resources\ProductionPlanResource\RelationManagers; use App\Models\ProductionPlan; +use Carbon\Carbon; use Filament\Forms; use Filament\Forms\Form; use Filament\Forms\Get; @@ -106,6 +107,7 @@ class ProductionPlanResource extends Resource ->relationship('shift', 'name') ->required() // ->nullable() + ->autofocus(true) ->options(function (callable $get) { if (!$get('plant_id') || !$get('block_name')) { return []; @@ -129,10 +131,10 @@ class ProductionPlanResource extends Resource } } - $shiftId = $get('shift_id'); + $curShiftId = $get('shift_id'); $set('line_id', null); - if (!$shiftId) { + if (!$curShiftId) { $set('ppShiftError', 'Please select a shift first.'); return; } @@ -175,8 +177,15 @@ class ProductionPlanResource extends Resource $set('ppLineError', null); } } + else + { + $currentDT = Carbon::now()->toDateTimeString(); + $set('created_at', $currentDT); + $set('update_date', null); + } $lineId = $get('line_id'); + // $set('plan_quantity', null); if (!$lineId) { $set('ppLineError', 'Please select a line first.'); @@ -184,13 +193,6 @@ class ProductionPlanResource extends Resource } else { - //SELECT COUNT(*) FROM plant_quantity_details WHERE DATE(created_at) = '{DateTime.Now.ToString("yyyy-MM-dd")}' AND shift_id = '{userShiftId}' AND plant_id = '{userPlantId}' AND line_id = '{userLineId}' - //Day shift 'Plant Production Quantity' already updated into database - - // $currentPath = url()->current(); - // $currentPath = $_SERVER['REQUEST_URI']; - // dd($currentPath); - $isUpdate = !empty($get('id')); if (!$isUpdate) { @@ -198,6 +200,7 @@ class ProductionPlanResource extends Resource ->where('shift_id', $get('shift_id')) ->where('line_id', $get('line_id')) ->whereDate('created_at', today()) + ->latest() ->exists(); if ($exists) @@ -208,35 +211,194 @@ class ProductionPlanResource extends Resource } else { - $currentDate = date('Y-m-d'); - $shiftId = \App\Models\Shift::where('id', $get('shift_id')) + $existShifts = 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) + { + //$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; + [$hRs, $miNs] = explode('.', $shiftId->duration) + [0, 0]; + $hRs = (int) $hRs; + // $miNs = (int) $miNs;-*/ - $totalMinutes = $hRs * 60 + $miNs; + $totalMinutes = $hRs * 60 + $miNs; - $from_dt = $currentDate . ' ' . $shiftId->start_time; + $from_dt = $yesterday . ' ' . $shiftId->start_time; - $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); + $to_dt = date('Y-m-d H:i:s', strtotime($from_dt . " + $totalMinutes minutes")); - $currentDateTime = date('Y-m-d H:i:s'); + $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('line_id', null); + $set('ppLineError', 'Production plan already updated.'); + // $set('ppLineError', 'Valid (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('line_id', null); + $set('ppLineError', 'Choosed a invalid shift.'); + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + } - // 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('ppLineError', null); return; - } else { - // 'Invalid (From: '.$from_dt.', To: '.$to_dt.')' - $set('ppLineError', 'Choosed a invalid shift...'); + } + else + { + //$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..."; + + // here i'm updating created as yesterday + $set('created_at', $from_dt); + $set('update_date', '1'); + + $set('ppLineError', null); + // $set('ppLineError', 'Valid (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('line_id', null); + $set('ppLineError', 'Choosed a invalid shift.'); + // $set('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + return; + } + } + + $set('ppLineError', null); return; } - // if($shiftId->start_time > $shiftId->end_time) + // $exists = ProductionPlan::where('plant_id', $get('plant_id')) + // //->where('shift_id', $get('shift_id')) + // ->where('line_id', $get('line_id')) + // ->whereDate('created_at', today()) + // ->latest() // Orders by created_at DESC + // ->first(); + + // if ($exists) + // { + // $existingShifts = ProductionPlan::where('plant_id', $get('plant_id')) + // //->where('shift_id', $get('shift_id')) + // ->where('line_id', $get('line_id')) + // // ->whereDate('created_at', today()) + // ->whereDate('created_at', today()) + // ->get(); + + // foreach ($existingShifts as $shift) { + // $curShiftId = $shift->shift_id; + + // $currentDate = date('Y-m-d'); + + // $shiftId = \App\Models\Shift::where('id', $curShiftId) + // ->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('line_id', null); + // $set('ppLineError', 'Production plan already updated.'); + // return; + // } + // // else { + // // $set('ppLineError', 'Choosed a invalid shift...'); + // // return; + // // } + // } + // $set('ppLineError', null); + // return; + // } } } $set('ppLineError', null); @@ -256,6 +418,16 @@ class ProductionPlanResource extends Resource ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $planQuan = $get('plan_quantity'); + + if(!$get('update_date') ) + { + if(!$get('id')) + { + $currentDT = Carbon::now()->toDateTimeString(); + $set('created_at', $currentDT); + } + } + if (!$planQuan) { $set('ppPlanQuanError', 'Scan the valid plan quantity.'); return; @@ -279,11 +451,38 @@ class ProductionPlanResource extends Resource Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), + Forms\Components\TextInput::make('update_date') + ->hidden() + ->reactive() + ->readOnly(), + Forms\Components\DateTimePicker::make('created_at') + ->label('Created DateTime') + ->hidden() + ->reactive() + ->required() + ->readOnly(), ]) ->columns(2), ]); } + // public function submitForm(array $data) + // { + // if (empty($data['update_date'])) { + // $data['created_at'] = now(); // Set current datetime if 'update_date' is empty + // } + + // // Save or process form data + // ProductionPlan::create($data); + // } + // protected function afterSave(array $data) + // { + // if ($this->model instanceof ProductionPlan && empty($data['update_date'])) { + // $this->model->created_at = now(); // Set current datetime if 'update_date' is empty + // $this->model->save(); + // } + // } + public static function table(Table $table): Table { return $table