diff --git a/app/Filament/Pages/ProductionQuantityPage.php b/app/Filament/Pages/ProductionQuantityPage.php index 075bc1f..216d1e8 100644 --- a/app/Filament/Pages/ProductionQuantityPage.php +++ b/app/Filament/Pages/ProductionQuantityPage.php @@ -3,6 +3,8 @@ namespace App\Filament\Pages; use App\Models\Item; +use App\Models\Line; +use App\Models\Machine; use App\Models\Plant; use App\Models\ProductionQuantity; use App\Models\Shift; @@ -37,16 +39,11 @@ class ProductionQuantityPage extends Page implements HasForms protected static ?string $navigationGroup = 'Production'; - public $qrData, $pId, $bId, $sId, $lId, $iId, $succId, $sNoId, $succStat, $recQr, $prodOrder; - - public $recent_qr; - public ?string $employee_qr = null; - public array $list_of_employee = []; - public ?int $no_of_employee = 0; - + public $qrData, $pId, $bId, $sId, $lId, $iId, $succId, $sNoId, $succStat, $recQr, $prodOrder, $workCenter, $mId; // public $recent_qr, $clear_qr; + use HasFiltersForm; public function mount(): void @@ -134,15 +131,11 @@ class ProductionQuantityPage extends Page implements HasForms ->schema([ Select::make('plant_id') + ->options(Plant::pluck('name', 'id')) ->label('Plant') ->reactive() ->required() ->columnSpan(1) - //->options(Plant::pluck('name', 'id')) - ->options(function (callable $get) { - $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray(); - }) ->default(function () { return optional(ProductionQuantity::latest()->first())->plant_id; }) @@ -150,11 +143,32 @@ class ProductionQuantityPage extends Page implements HasForms $plantId = $get('plant_id'); $set('block_name', null); + $now = Carbon::now()->format('H:i:s'); + $shiftType = ($now >= '08:00:00' && $now <= '19:29:59') + ? 'Day' + : 'Night'; + + $set('shift_id', $shiftType); + session(['select_plant' => $state]); if (!$plantId) { $set('pqPlantError', 'Please select a plant first.'); + $this->form->fill([ + 'plant_id'=> $this->pId, + 'block_name'=> null, + 'shift_id'=> null, + 'line_id'=> null, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> null, + 'sap_msg_status' => null, + 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); return; } else @@ -168,165 +182,109 @@ class ProductionQuantityPage extends Page implements HasForms ->hint(fn ($get) => $get('pqPlantError') ? $get('pqPlantError') : null) ->hintColor('danger'), - // Block Filter - Select::make('block_name') - ->label('Block') - ->required() - // ->nullable() - ->reactive() - ->columnSpan(1) - ->options(function (callable $get) { - if (!$get('plant_id')) { - return []; - } + // Select::make('line_id') + // ->relationship('line', 'name') + // ->required() + // ->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(); - }) - ->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); - } - } + // 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); + // } + // } - $blockId = $get('block_name'); - $set('shift_id', null); + // $lineId = $get('line_id'); + // $set('item_code', null); - if (!$blockId) { - $set('pqBlockError', 'Please select a block first.'); - return; - } - else - { - $this->bId = $blockId; - $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'), + // $now = Carbon::now()->format('H:i:s'); + // $shiftType = ($now >= '08:00:00' && $now <= '19:29:59') + // ? 'Day' + // : 'Night'; - Select::make('shift_id') - ->relationship('shift', 'name') + // $set('shift', $shiftType); + + + // session(['select_line' => $state]); + // // dd(session('select_line')); + + // $this->triggerChartUpdate(); + + // if (!$lineId) { + // $set('pqLineError', 'Please select a line first.'); + // return; + // } + // else + // { + // $this->lId = $lineId; + // $set('validationError', null); + // $set('pqLineError', null); + // $set('item_id', null); + // // $set('item_description', null); + // $set('serial_number', null); + // } + // }) + // ->extraAttributes(fn ($get) => [ + // 'class' => $get('pqLineError') ? 'border-red-500' : '', + // ]) + // ->hint(fn ($get) => $get('pqLineError') ? $get('pqLineError') : null) + // ->hintColor('danger'), + + + TextInput::make('machine_id') + ->label('Machine') ->required() ->columnSpan(1) - // ->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(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); - } - } + $plantId = $get('plant_id'); + $machineId = $get('machine_id'); - $curShiftId = $get('shift_id'); + $this->mId = $machineId; - $set('line_id', null); - - if (!$curShiftId) { - $set('pqShiftError', 'Please select a shift first.'); - return; - } - else - { - $this->sId = $curShiftId; - $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'), + // 'class' => $get('pqLineError') ? 'border-red-500' : '', + 'wire:keydown.enter' => 'processMachine($event.target.value)', + ]), + // ->hint(fn ($get) => $get('pqLineError') ? $get('pqLineError') : null) + // ->hintColor('danger'), - Select::make('line_id') - ->relationship('line', 'name') - ->required() - ->columnSpan(1) - ->options(function (callable $get) { - if (!$get('plant_id') || !$get('block_name') || !$get('shift_id')) { - return []; - } + TextInput::make('line_id') + ->label('Line') + ->reactive() + ->readOnly() + ->required() + ->afterStateUpdated(fn ($state) => $this->lId = $state), - 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_line' => $state]); - // dd(session('select_line')); - - $this->triggerChartUpdate(); - - if (!$lineId) { - $set('pqLineError', 'Please select a line first.'); - return; - } - else - { - $this->lId = $lineId; - $set('validationError', null); - $set('pqLineError', null); - $set('item_id', null); - // $set('item_description', null); - $set('serial_number', null); - } - }) - ->extraAttributes(fn ($get) => [ - 'class' => $get('pqLineError') ? 'border-red-500' : '', - ]) - ->hint(fn ($get) => $get('pqLineError') ? $get('pqLineError') : null) - ->hintColor('danger'), + TextInput::make('block_name') + ->label('Block') + ->reactive() + ->readOnly() + ->required(), + TextInput::make('shift_id') + ->label('Shift') + ->reactive() + ->readOnly() + ->required() + ->afterStateUpdated(fn ($state) => $this->sId = $state), TextInput::make('production_order') ->label('Production Order') ->reactive() @@ -362,22 +320,12 @@ class ProductionQuantityPage extends Page implements HasForms } }) ->extraAttributes(fn ($get) => [ - 'class' => $get('productionError') ? 'border-red-500' : '', + 'id' => 'scan_locator_no', + 'class' => $get('productionError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('productionError') ? $get('productionError') : null) ->hintColor('danger'), - // TextInput::make('item_code') - // ->label('Item Code') - // ->columnSpan(1) - // ->autofocus(true) - // //->reactive() - // ->live(onBlur: true) // avoids per-keystroke triggering - // ->default(fn () => $this->clear_qr) - // ->extraAttributes([ - // 'wire:keydown.enter' => 'processAllValues($event.target.value)', - // ]), - Hidden::make('serial_number') ->required(), Hidden::make('success_msg') @@ -388,59 +336,6 @@ class ProductionQuantityPage extends Page implements HasForms Hidden::make('sap_msg_description'), - TextInput::make('no_of_employee') - ->label('No Of Employee') - ->reactive() - ->columnSpan(['default' => 1, 'sm' => 1]) - ->readOnly(), - - TextInput::make('employee_qr') - ->label('Scan Employee QR') - ->reactive() - ->extraAttributes([ - 'wire:model.defer' => 'employee_qr', - 'wire:keydown.enter.prevent' => 'addEmployeeFromQr', - ]) - ->autofocus(), - - Select::make('list_of_employee') - ->label('List of Employee') - ->multiple() - ->options(fn () => collect($this->list_of_employee) - ->mapWithKeys(fn ($name) => [$name => $name]) - ) - ->reactive(), - //->hidden(fn ($get) => blank($get('employee_qr'))), - - TextInput::make('recent_qr') - ->label('Last scanned QR') - ->reactive() - ->columnSpan(['default' => 1, 'sm' => 1]) - //->columnSpan(2) - // ->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; - - // $this->recQr = $itemCode && $serialNumber ? "{$itemCode} | {$serialNumber}" : null; - - // }) - ->default(fn () => $this->recQr) - ->readOnly(true) - ->dehydrated(false) // prevents DB update if form is saved - ->afterStateHydrated(fn ($state) => $this->recent_qr = $state) - ->hidden(), TextInput::make('id') ->hidden() ->readOnly(), @@ -451,52 +346,55 @@ class ProductionQuantityPage extends Page implements HasForms ->columns(['default' => 1, 'sm' => 7]); } - // public function addEmployeeFromQr(): void - // { - // $employeeName = trim($this->employee_qr); + public function processMachine($value){ - // if ($employeeName != '') { - // if (!in_array($employeeName, $this->list_of_employee)) { - // $this->list_of_employee[] = $employeeName; - // } - // } + $plantId = $this->pId; + $workCenter = $value; - // // Clear the field inside the form state - // $this->form->fill([ - // 'employee_qr' => null, - // ]); - // } - public function addEmployeeFromQr(): void - { - $employeeName = trim($this->employee_qr); + $now = Carbon::now()->format('H:i:s'); + $this->sId = ($now >= '08:00:00' && $now <= '19:29:59') + ? 'Day' + : 'Night'; - if ($employeeName !== '') { - if (in_array($employeeName, $this->list_of_employee)) { - // remove if already exists - $this->list_of_employee = array_values( - array_filter($this->list_of_employee, fn($e) => $e != $employeeName) - ); - } else { - // add if not exists - $this->list_of_employee[] = $employeeName; - } + + $machine = Machine::where('plant_id', $plantId)->where('work_center', $workCenter)->with('line.block')->first(); + + if ($machine) { + $this->lId = Line::where('id', $machine->line_id)->value('name'); + $this->bId = $machine->line->block->name ?? null; + + // dd($lineName, $blockName); + + $this->form->fill([ + 'plant_id'=> $this->pId, + 'machine_id' => $this->mId, + 'block_name'=> $this->bId, + 'shift_id'=> $this->sId, + 'line_id'=> $this->lId, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> $this->prodOrder, + 'sap_msg_status' => null, + 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + // $this->dispatch('focus-production-order'); } - $this->no_of_employee = count($this->list_of_employee); + else + { - // Clear the scanned input field - $this->form->fill([ - 'no_of_employee' => $this->no_of_employee, - 'employee_qr' => null, - ]); + Notification::make() + ->title('Unknown WorkCenter') + ->body("Work Center not found") + ->danger() + ->send(); + return; + } } - - protected function getEmployeeNameFromQr(?string $qrCode): ?string - { - return $qrCode ? trim($qrCode) : null; - } - // Method to process the value when Enter is pressed #[On('handleQrScan')] @@ -537,6 +435,7 @@ class ProductionQuantityPage extends Page implements HasForms if (empty($formQRData)) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -555,7 +454,6 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } else @@ -563,6 +461,7 @@ class ProductionQuantityPage extends Page implements HasForms if (!$this->pId) { $this->form->fill([ 'plant_id'=> null, + 'machine_id'=> null, 'block_name'=> null, 'shift_id'=> null, 'line_id'=> null, @@ -580,12 +479,12 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Please select a plant first.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } - else if (!$this->bId) { + else if (!$this->mId) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> null, 'block_name'=> null, 'shift_id'=> null, 'line_id'=> null, @@ -599,39 +498,16 @@ class ProductionQuantityPage extends Page implements HasForms 'recent_qr' => $this->recQr, ]); Notification::make() - ->title('Choose Block') - ->body("Please select a block first.") + ->title('Unknown Machine') + ->body("Machine can't be empty!") ->danger() ->send(); - $this->dispatch('playWarnSound'); - return; - } - else if (!$this->sId) { - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> null, - 'line_id'=> null, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> null, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - Notification::make() - ->title('Choose Shift') - ->body("Please select a shift first.") - ->danger() - ->send(); - $this->dispatch('playWarnSound'); return; } else if (!$this->lId) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id' => $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> null, @@ -649,12 +525,59 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Please select a line first.") ->danger() ->send(); - $this->dispatch('playWarnSound'); + return; + } + else if (!$this->bId) { + + $this->form->fill([ + 'plant_id'=> $this->pId, + 'machine_id' => $this->mId, + 'block_name'=> null, + 'shift_id'=> $this->sId, + 'line_id'=> $this->lId, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> $this->prodOrder, + 'sap_msg_status' => null, + 'sap_msg_description' => null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Choose Line') + ->body("Please select a Block first.") + ->danger() + ->send(); + return; + } + else if (!$this->sId) { + $this->form->fill([ + 'plant_id'=> $this->pId, + 'machine_id' => $this->mId, + 'block_name'=> $this->bId, + 'shift_id'=> null, + 'line_id'=> null, + 'item_id'=> null, + 'serial_number'=> null, + 'success_msg'=> null, + 'production_order'=> null, + 'sap_msg_status' => null, + 'sap_msg_description' => null, + 'operator_id'=> $operatorName, + 'recent_qr' => $this->recQr, + ]); + Notification::make() + ->title('Choose Shift') + ->body("Please select a shift first.") + ->danger() + ->send(); return; } else if (!$this->prodOrder) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -671,7 +594,6 @@ class ProductionQuantityPage extends Page implements HasForms ->title('Please scan the production order first.') ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } @@ -679,6 +601,7 @@ class ProductionQuantityPage extends Page implements HasForms { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -697,13 +620,13 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Must contain numeric values only.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } else if (!preg_match('/^[1-9][0-9]{6,13}$/', $this->prodOrder)) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -722,357 +645,349 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Must be a numeric value with 7 to 14 digits.
Must start with a non-zero digit.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } - // ******************************** - $exists = \App\Models\ProductionPlan::where('plant_id', $this->pId) - ->where('shift_id', $this->sId) - ->where('line_id', $this->lId) - ->whereDate('created_at', today()) - ->latest() - ->exists(); + // $exists = \App\Models\ProductionPlan::where('plant_id', $this->pId) + // ->where('shift_id', $this->sId) + // ->where('line_id', $this->lId) + // ->whereDate('created_at', today()) + // ->latest() + // ->exists(); - if ($exists) - { - $currentDate = date('Y-m-d'); + // if ($exists) + // { + // $currentDate = date('Y-m-d'); - $shiftId = Shift::where('id', $this->sId) - ->first(); + // $shiftId = Shift::where('id', $this->sId) + // ->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 = $currentDate . ' ' . $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('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + // // 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.')'); - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - Notification::make() - ->title('Invalid Shift') - ->body("Please select a valid shift.") - ->danger() - ->send(); - $this->dispatch('playWarnSound'); - //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); - return; - } - else - { - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - } - } - else - { - $existShifts = \App\Models\ProductionPlan::where('plant_id', $this->pId) - ->where('shift_id', $this->sId) - ->where('line_id', $this->lId) - ->whereDate('created_at', Carbon::yesterday()) - ->latest() - ->exists(); + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // 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 + // { + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // } + // } + // else + // { + // $existShifts = \App\Models\ProductionPlan::where('plant_id', $this->pId) + // ->where('shift_id', $this->sId) + // ->where('line_id', $this->lId) + // ->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')); + // 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', $this->sId) - ->first(); + // $shiftId = Shift::where('id', $this->sId) + // ->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 = $yesterday . ' ' . $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) { - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - } - else - { - $currentDate = date('Y-m-d'); + // // Check if current date time is within the range + // if ($currentDateTime >= $from_dt && $currentDateTime < $to_dt) { + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // } + // else + // { + // $currentDate = date('Y-m-d'); - $shiftId = Shift::where('id', $this->sId) - ->first(); + // $shiftId = Shift::where('id', $this->sId) + // ->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 = $currentDate . ' ' . $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('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + // // 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.')'); - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - Notification::make() - ->title('Plan Not Found') - ->body("Please set production plan first.") - ->danger() - ->send(); - $this->dispatch('playWarnSound'); - //$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.')'); + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // 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.')'); - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - Notification::make() - ->title('Invalid Shift') - ->body("Please select a valid shift.") - ->danger() - ->send(); - $this->dispatch('playWarnSound'); - //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); - return; - } - } - } - else - { // record not exist on yesterday + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // 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')); + // //$currentDate = date('Y-m-d'); + // $yesterday = date('Y-m-d', strtotime('-1 days')); - $shiftId = Shift::where('id', $this->sId) - ->first(); + // $shiftId = Shift::where('id', $this->sId) + // ->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 = $yesterday . ' ' . $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('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + // // 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.')'); - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - Notification::make() - ->title('Plan Not Found') - ->body("Please set production plan first.") - ->danger() - ->send(); - $this->dispatch('playWarnSound'); - //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); - return; - } - else - { - $currentDate = date('Y-m-d'); + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // 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', $this->sId) - ->first(); + // $shiftId = Shift::where('id', $this->sId) + // ->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 = $currentDate . ' ' . $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('ppLineError', 'Valid (From: '.$from_dt.', To: '.$to_dt.')'); + // // 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.')'); - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - Notification::make() - ->title('Plan Not Found') - ->body("Please set production plan first.") - ->danger() - ->send(); - $this->dispatch('playWarnSound'); - //$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.')'); + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // 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.')'); - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); - Notification::make() - ->title('Invalid Shift') - ->body("Please select a valid shift.") - ->danger() - ->send(); - $this->dispatch('playWarnSound'); - //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); - return; - } - } - } - } + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); + // Notification::make() + // ->title('Invalid Shift') + // ->body("Please select a valid shift.") + // ->danger() + // ->send(); + // //$set('validationError', 'Curr.'.$currentDateTime.' (From: '.$from_dt.', To: '.$to_dt.')'); + // return; + // } + // } + // } + // } - // ******************************** - - $this->form->fill([ - 'plant_id'=> $this->pId, - 'block_name'=> $this->bId, - 'shift_id'=> $this->sId, - 'line_id'=> $this->lId, - 'item_id'=> null, - 'serial_number'=> null, - 'success_msg'=> null, - 'production_order'=> $this->prodOrder, - 'sap_msg_status' => null, - 'sap_msg_description' => null, - 'operator_id'=> $operatorName, - 'recent_qr' => $this->recQr, - ]); + // $this->form->fill([ + // 'plant_id'=> $this->pId, + // 'block_name'=> $this->bId, + // 'shift_id'=> $this->sId, + // 'line_id'=> $this->lId, + // 'item_id'=> null, + // 'serial_number'=> null, + // 'success_msg'=> null, + // 'production_order'=> $this->prodOrder, + // 'sap_msg_status' => null, + // 'sap_msg_description' => null, + // 'operator_id'=> $operatorName, + // 'recent_qr' => $this->recQr, + // ]); } if (!preg_match('/^[a-zA-Z0-9]{6,}+\|[1-9][a-zA-Z0-9]{8,}+(\|)?$/', $formQRData)) { + if (strpos($formQRData, '|') === false) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -1091,7 +1006,6 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } else @@ -1103,6 +1017,7 @@ class ProductionQuantityPage extends Page implements HasForms if (!ctype_alnum($iCode)) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -1121,12 +1036,12 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Item code must contain alpha-numeric values only.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } else if (strlen($iCode) < 6) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -1145,12 +1060,12 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Item code must be at least 6 digits.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } else if (!ctype_alnum($sNumber)) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -1170,12 +1085,12 @@ class ProductionQuantityPage extends Page implements HasForms ->danger() ->duration(800) ->send(); - $this->dispatch('playWarnSound'); return; } else if (strlen($sNumber) < 9) { $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -1195,13 +1110,13 @@ class ProductionQuantityPage extends Page implements HasForms ->danger() ->duration(800) ->send(); - $this->dispatch('playWarnSound'); return; } } $this->form->fill([ 'plant_id'=> $this->pId, + 'machine_id'=> $this->mId, 'block_name'=> $this->bId, 'shift_id'=> $this->sId, 'line_id'=> $this->lId, @@ -1220,7 +1135,6 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } else @@ -1269,7 +1183,6 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Item code does not exist in master data.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } @@ -1320,7 +1233,6 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Serial number already exist in database for choosed plant.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } } @@ -1346,11 +1258,10 @@ class ProductionQuantityPage extends Page implements HasForms ->body("Item code does not exist in master data for choosed plant.") ->danger() ->send(); - $this->dispatch('playWarnSound'); return; } - if ($this->succId == null) { + if ($this->succId === null) { $this->form->fill([ 'plant_id'=> $this->pId, 'block_name'=> $this->bId, @@ -1372,7 +1283,6 @@ class ProductionQuantityPage extends Page implements HasForms ->danger() // ->persistent() ->send(); - $this->dispatch('playWarnSound'); return; } else @@ -1384,22 +1294,61 @@ class ProductionQuantityPage extends Page implements HasForms $itemCode = trim($parts[0]); $this->sNoId = isset($parts[1]) ? trim($parts[1]) : null; + $machine = Machine::where('work_center', $this->mId)->where('plant_id', $this->pId)->first(); + + if (!$machine) { + Notification::make() + ->title('Unknown Machine') + ->body("Work center {$this->mId} not found for this plant.") + ->danger() + ->send(); + return; + } + + $machineId = $machine->id; + $this->mId = $machineId; + + $lineId = $machine->line_id; + $this->lId = $lineId; + + $line = Line::find($lineId); + if (!$line) { + Notification::make() + ->title('Invalid Line') + ->body('Line associated with the machine not found.') + ->danger() + ->send(); + return; + } + + $blockId = $line->block_id; + $this->bId = $blockId; + + $shift = Shift::where('block_id', $blockId)->first(); + if (!$shift) { + Notification::make() + ->title('No Shift Found') + ->body('No shift associated with this block.') + ->danger() + ->send(); + return; + } + + $shiftId = $shift->id; + $this->sId = $shiftId; + ProductionQuantity::create([ 'plant_id'=> $this->pId, + 'machine_id' => $this->mId, 'shift_id'=> $this->sId, 'line_id' => $this->lId, 'item_id'=> $this->iId, 'serial_number' => $this->sNoId, 'production_order'=> $this->prodOrder, 'operator_id'=> $operatorName, - 'list_of_employee' => implode(',', $this->list_of_employee ?? []), - 'no_of_employee' => count($this->list_of_employee ?? []), ]); $this->iId = null; - $recentQrValue = $itemCode.' | '.$this->sNoId; - - $this->recent_qr = $recentQrValue; // after success insertion $this->form->fill([ @@ -1416,8 +1365,7 @@ class ProductionQuantityPage extends Page implements HasForms 'sap_msg_status' => null, 'sap_msg_description' => null, 'operator_id'=> $operatorName, - //'recent_qr' => $itemCode.' | '.$this->sNoId, - 'recent_qr' => $recentQrValue, // updates Filament form field + 'recent_qr' => $itemCode.' | '.$this->sNoId, ]); Notification::make() @@ -1426,7 +1374,6 @@ class ProductionQuantityPage extends Page implements HasForms ->success() ->duration(200) ->send(); - $this->dispatch('playNotificationSound'); } }