diff --git a/app/Filament/Pages/ProductionQuantityPage.php b/app/Filament/Pages/ProductionQuantityPage.php
new file mode 100644
index 000000000..4d5a6f0ae
--- /dev/null
+++ b/app/Filament/Pages/ProductionQuantityPage.php
@@ -0,0 +1,1266 @@
+ 'processAllValues'];
+
+ protected static ?string $navigationGroup = 'Production';
+
+ public $qrData, $pId, $bId, $sId, $lId, $iId, $succId, $sNoId, $succStat, $recQr, $prodOrder;
+
+ public $recent_qr, $clear_qr;
+
+
+ use HasFiltersForm;
+
+ public function mount(): void
+ {
+ session()->forget(['select_plant', 'select_line']);
+ $this->filtersForm->fill([
+ 'plant' => null,
+ 'line' => null,
+ ]);
+ }
+ public function boot(): void
+ {
+ Filament::registerRenderHook(
+ 'panels::body.start',
+ function () {
+ // Check if the current route matches '/admin/production-quantity'
+ if (Request::is('admin/production-quantity*')) {
+ echo <<
+ /* Hide sidebar and topbar */
+ .fi-sidebar,
+ .fi-topbar {
+ display: none !important;
+ }
+
+ /* Expand main container to full screen */
+ .fi-main {
+ margin: 0 !important;
+ padding: 0 !important;
+ max-width: 100% !important;
+ /* overflow-y: auto; Allow vertical scrolling */
+ height: 100vh; /* Ensure full height of the viewport */
+ }
+
+ /* Expand page area fully */
+ .fi-main > .fi-page {
+ padding: 0 !important;
+ max-width: 100% !important;
+ }
+
+ /* Allow scroll on body again */
+ body {
+ overflow: auto;
+ }
+
+
+
+
+ HTML;
+ }
+ }
+ );
+ }
+
+ public function form(Form $form): Form
+ {
+ return $form
+ ->statePath('filters')
+ ->schema([
+
+ Select::make('plant_id')
+ ->options(Plant::pluck('name', 'id'))
+ ->label('Plant')
+ ->reactive()
+ ->live()
+ ->required()
+ ->columnSpan(1)
+ ->default(function () {
+ return optional(ProductionQuantity::latest()->first())->plant_id;
+ })
+ ->afterStateUpdated(function ($state, $set, callable $get,$livewire) {
+ $plantId = $get('plant_id');
+ $set('block_name', null);
+
+ session(['select_plant' => $state]);
+ if (!$plantId)
+ {
+ $set('pqPlantError', 'Please select a plant first.');
+ return;
+ }
+ else
+ {
+ $this->pId = $plantId;
+ $set('validationError', null);
+ $set('pqPlantError', null);
+ }
+ $this->triggerChartUpdate();
+ })
+ ->hint(fn ($get) => $get('pqPlantError') ? $get('pqPlantError') : null)
+ ->hintColor('danger'),
+
+ // Block Filter
+ Select::make('block_name')
+ ->label('Block')
+ ->required()
+ // ->nullable()
+ ->live()
+ ->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);
+
+ 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'),
+
+ Select::make('shift_id')
+ ->relationship('shift', 'name')
+ ->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);
+ }
+ }
+
+ $curShiftId = $get('shift_id');
+
+ $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'),
+
+ 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 [];
+ }
+
+ return \App\Models\Line::where('plant_id', $get('plant_id'))
+ ->pluck('name', 'id')
+ ->toArray();
+ })
+ ->reactive()
+ ->live()
+ ->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]);
+
+ $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('production_order')
+ ->label('Production Order')
+ ->reactive()
+ ->live()
+ ->required()
+ ->columnSpan(1)
+ ->afterStateUpdated(function ($state, callable $get, callable $set): void {
+ $set('item_code', null);
+ $set('item_id', null);
+ // $set('item_description', null);
+ $set('serial_number', null);
+ $set('validationError', null);
+ $this->prodOrder = $state;
+
+ return;
+ // if (empty($state)) {
+ // }
+ }),
+
+ TextInput::make('item_code')
+ ->label('Item Code')
+ ->columnSpan(1)
+ ->autofocus(true)
+ ->reactive()
+ ->live()
+ ->default(fn () => $this->clear_qr)
+ ->extraAttributes([
+ 'wire:keydown.enter' => 'processAllValues($event.target.value)',
+ ]),
+
+ Hidden::make('serial_number')
+ ->required(),
+ Hidden::make('success_msg')
+ ->required(),
+
+ TextInput::make('recent_qr')
+ ->label('Last scanned QR')
+ ->reactive()
+ ->live()
+ ->columnSpan(1)
+ // ->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;
+
+ // dd($itemCode, $serialNumber);
+
+ // // Combine 'code' and 'serial_number' into the desired format
+ // return $itemCode && $serialNumber ? "{$itemCode} | {$serialNumber}" : null;
+ // })
+ // ->default(fn () => $this->recent_qr)
+ ->readOnly(true),
+
+ TextInput::make('id')
+ ->hidden()
+ ->readOnly(),
+ Hidden::make('operator_id')
+ ->default(Filament::auth()->user()->name),
+ ])
+ ->columns(7);
+ }
+
+
+ // Method to process the value when Enter is pressed
+ public function processAllValues($formQRData)
+ {
+
+ //dd($formQRData);
+ //$formValues = [];
+ // This will get all form data from the request
+ $this->clear_qr = null;
+ $this->qrData = null;
+ $this->iId = null;
+ $this->succId = null;
+ $this->sNoId = null;
+ $this->succStat = null;
+ $this->validateAndProcessForm( $formQRData); // Process the form values
+ }
+
+ public function validateAndProcessForm($formQRData)
+ {
+ $user = Filament::auth()->user(); //->name
+ $operatorName = $user->name;
+ $this->qrData = $formQRData;
+ // dd($formData, $formQRData);
+ // dd($this->form->getState()['serial_number']);
+
+ // try {
+ // // Access the nested form data
+ // $componentData = json_decode($formValues['components'][0]['snapshot'], true);
+ // $formData = $componentData['data']['data'][0]; // Access first item in data array
+
+ // // Extract specific values
+ // $this->qrData = $formQRData ?? $formData['item_code'];
+ // $this->pId = $formData['plant_id'] ?? null;
+ // $this->bId = $formData['block_name'] ?? null;
+ // $this->sId = $formData['shift_id'] ?? null;
+ // $this->lId = $formData['line_id'] ?? null;
+ // $this->iId = $formData['item_id'] ?? null;
+ // $this->succId = $formData['success_msg'] ?? null;
+ // // $this->sNoId = $formData['serial_number'] ?? null;
+ // $this->prodOrder = $formData['production_order'] ?? null;
+ // $this->recQr = $formData['recent_qr'] ?? null;
+ // $this->dispatch('plant-line-updated', $this->pId,$this->lId);
+ // } catch (\Exception $e) {
+ // //dd('Error parsing form data:', $e->getMessage(), $formValues);
+ // Notification::make()
+ // ->title("Error Parsing Form Data") // {$operatorName}
+ // ->body($e->getMessage())
+ // ->danger()
+ // // ->persistent()
+ // ->send();
+ // return;
+ // }
+
+ if (empty($formQRData)) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Invalid QR')
+ ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )")
+ ->danger()
+ ->send();
+ return;
+ }
+ else
+ {
+ if (!$this->pId) {
+ $this->form->fill([
+ 'plant_id'=> null,
+ 'block_name'=> null,
+ 'shift_id'=> null,
+ 'line_id'=> null,
+ 'item_id'=> null,
+ 'serial_number'=> null,
+ 'success_msg'=> null,
+ 'production_order'=> null,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Choose Plant')
+ ->body("Please select a plant first.")
+ ->danger()
+ ->send();
+ return;
+ }
+ else if (!$this->bId) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Choose Block')
+ ->body("Please select a block first.")
+ ->danger()
+ ->send();
+ 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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Choose Shift')
+ ->body("Please select a shift first.")
+ ->danger()
+ ->send();
+ return;
+ }
+ else if (!$this->lId) {
+ $this->form->fill([
+ 'plant_id'=> $this->pId,
+ 'block_name'=> $this->bId,
+ 'shift_id'=> $this->sId,
+ 'line_id'=> null,
+ 'item_id'=> null,
+ 'serial_number'=> null,
+ 'success_msg'=> null,
+ 'production_order'=> null,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Choose Line')
+ ->body("Please select a line first.")
+ ->danger()
+ ->send();
+ return;
+ }
+ else if (!$this->prodOrder) {
+ $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'=> null,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Please scan the production order first.')
+ ->danger()
+ ->send();
+ return;
+ }
+
+ if(!is_numeric($this->prodOrder))
+ {
+ $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'=> null,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid Production Order')
+ ->body("Must contain numeric values only.")
+ ->danger()
+ ->send();
+ return;
+ }
+ else if (!preg_match('/^[1-9][0-9]{6,}$/', $this->prodOrder))
+ {
+ $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'=> null,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid Production Order')
+ ->body("Must contain at least 7 digits.
Must start with a non-zero digit.")
+ ->danger()
+ ->send();
+ 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();
+
+ if ($exists)
+ {
+ $currentDate = date('Y-m-d');
+
+ $shiftId = Shift::where('id', $this->sId)
+ ->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.')');
+
+ $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,
+ '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,
+ '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'));
+
+ $shiftId = Shift::where('id', $this->sId)
+ ->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) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ }
+ else
+ {
+ $currentDate = date('Y-m-d');
+
+ $shiftId = Shift::where('id', $this->sId)
+ ->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.')');
+
+ $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,
+ '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,
+ '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'));
+
+ $shiftId = Shift::where('id', $this->sId)
+ ->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.')');
+
+ $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,
+ '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();
+
+ [$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.')');
+
+ $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,
+ '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,
+ '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,
+ '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,
+ '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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid QR')
+ ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )")
+ ->danger()
+ ->send();
+ return;
+ }
+ else
+ {
+ $splits = explode('|', $formQRData);
+ $iCode = trim($splits[0]);
+ $sNumber = isset($splits[1]) ? trim($splits[1]) : null;
+
+ if (!ctype_alnum($iCode)) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid Item Code')
+ ->body("Item code must contain alpha-numeric values only.")
+ ->danger()
+ ->send();
+ return;
+ }
+ else if (strlen($iCode) < 6) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid Item Code')
+ ->body("Item code must be at least 6 digits.")
+ ->danger()
+ ->send();
+ return;
+ }
+ else if (!ctype_alnum($sNumber)) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid Serial Number')
+ ->body("Serial Number must contain alpha-numeric values only.")
+ ->danger()
+ ->duration(800)
+ ->send();
+ return;
+ }
+ else if (strlen($sNumber) < 9) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid Serial Number')
+ ->body("Serial Number must be at least 9 digits.")
+ ->danger()
+ ->duration(800)
+ ->send();
+ 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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Invalid QR')
+ ->body("Scan the valid QR code.
(Ex: Item_Code|Serial_Number )")
+ ->danger()
+ ->send();
+ 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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ }
+
+ // Only search when all parent IDs are selected
+ $parts = explode('|', $formQRData);
+ $itemCode = trim($parts[0]);
+ $serialNumber = isset($parts[1]) ? trim($parts[1]) : null;
+
+ // Fetch item using item code and plant_id
+ $masItem = Item::where('code', $itemCode)->first();
+ if (!$masItem)
+ {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Unknown Item Code')
+ ->body("Item code does not exist in master data.")
+ ->danger()
+ ->send();
+ return;
+ }
+
+ $item = Item::where('code', $itemCode)
+ ->where('plant_id', $this->pId)
+ ->first();
+
+ if ($item)
+ {
+ $sNo = ProductionQuantity::where('serial_number', $serialNumber)->where('plant_id', $this->pId)->exists();
+ if (!$sNo)
+ {
+ $this->form->fill([
+ 'plant_id'=> $this->pId,
+ 'block_name'=> $this->bId,
+ 'shift_id'=> $this->sId,
+ 'line_id'=> $this->lId,
+ 'item_id'=> $item->id,
+ 'serial_number'=> null,
+ 'success_msg'=> 'Y',
+ 'production_order'=> $this->prodOrder,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ $this->succId = 'Y';
+ $this->iId = $item->id;
+ }
+ 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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+ Notification::make()
+ ->title('Duplicate Serial Number')
+ ->body("Serial number already exist in database for choosed plant.")
+ ->danger()
+ ->send();
+ 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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title('Unknown Item Code')
+ ->body("Item code does not exist in master data for choosed plant.")
+ ->danger()
+ ->send();
+ return;
+ }
+
+ if ($this->succId === null) {
+ $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,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $this->recQr,
+ ]);
+
+ Notification::make()
+ ->title("Invalid QR Found") // {$operatorName}
+ ->body("Please, scan the valid QR code.")
+ ->danger()
+ // ->persistent()
+ ->send();
+ return;
+ }
+ else
+ {
+ // // Perform any additional processing or database operations
+ // $this->saveFormData($formValues);
+
+ $parts = explode('|', $this->qrData);
+ $itemCode = trim($parts[0]);
+ $this->sNoId = isset($parts[1]) ? trim($parts[1]) : null;
+
+ ProductionQuantity::create([
+ 'plant_id'=> $this->pId,
+ 'shift_id'=> $this->sId,
+ 'line_id' => $this->lId,
+ 'item_id'=> $this->iId,
+ 'serial_number' => $this->sNoId,
+ 'production_order'=> $this->prodOrder,
+ 'operator_id'=> $operatorName,
+ ]);
+
+ $this->iId = null;
+
+ // after success insertion
+ $this->form->fill([
+
+ 'plant_id'=> $this->pId,
+ 'block_name'=> $this->bId,
+ 'shift_id'=> $this->sId,
+ 'line_id'=> $this->lId,
+ 'item_id'=> null,
+ // 'item_code'=> null,
+ 'serial_number'=> null,
+ 'success_msg'=> null,
+ 'production_order'=> $this->prodOrder,
+ 'operator_id'=> $operatorName,
+ 'recent_qr' => $itemCode.' | '.$this->sNoId,
+ ]);
+
+ Notification::make()
+ ->title("Valid QR Found") // {$operatorName}
+ ->body("Valid QR code scanned: {$this->qrData}.")
+ ->success()
+ ->send();
+ }
+ }
+
+ protected function saveFormData($formValues)
+ {
+ // Save the form data to the database or perform other operations
+ // For example:
+ $model = ProductionQuantity::create($formValues);
+
+ // dd('Production Updated Event Dispatched');
+
+ $this->dispatch('productionUpdated');
+
+ // // Optionally, you can emit an event or perform a redirect after saving
+ // $this->emit('formSaved', $model->id);
+ }
+ public function triggerChartUpdate(): void
+ {
+ if (session()->has('select_plant') && session()->has('select_line')) {
+ $this->dispatch('filtersUpdated');
+ }
+ }
+
+ // Override the getTitle method
+ public function getTitle(): string
+ {
+ return ''; // Return an empty string to remove the title
+ }
+
+ public static function getNavigationLabel(): string
+ {
+ return 'Production Quantities';
+ }
+
+ public static function canAccess(): bool
+ {
+ return Auth::check() && Auth::user()->can('create ProductionQuantities');
+ }
+
+}