From 3f0e286f8af18f94f48897ecc92d1f21a142d8c6 Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Thu, 4 Jun 2026 12:42:49 +0530 Subject: [PATCH] Added import serial invoice logic in serial validation --- .../Resources/SerialValidationResource.php | 863 ++++++++++++------ 1 file changed, 605 insertions(+), 258 deletions(-) diff --git a/app/Filament/Resources/SerialValidationResource.php b/app/Filament/Resources/SerialValidationResource.php index 0f6d121..96b7b99 100644 --- a/app/Filament/Resources/SerialValidationResource.php +++ b/app/Filament/Resources/SerialValidationResource.php @@ -324,65 +324,392 @@ class SerialValidationResource extends Resource // ->reactive() // ]), + // Tables\Actions\Action::make('Import Serial Number') + // ->label('Import Serial Invoice') + // ->form([ + // Select::make('plant_id') + // // ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs + // ->options(function (callable $get) { + // $userHas = Filament::auth()->user()->plant_id; + + // return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + // }) + // ->label('Select Plant') + // ->required() + // ->default(function () { + // return optional(SerialValidation::latest()->first())->plant_id; + // }) + // ->afterStateUpdated(function ($state, callable $set, callable $get) { + // $set('invoice_serial_number', null); + // }) + // ->reactive(), + + // FileUpload::make('invoice_serial_number') + // ->label('Invoice Serial Number') + // // ->required() + // ->preserveFilenames() // <- this keeps the original filename + // ->storeFiles(false) // prevent auto-storing, we will store manually + // ->reactive() + // ->required() + // ->disk('local') // 'local' refers to the local storage disk defined in config/filesystems.php, typically pointing to storage/app. + // ->visible(fn (Get $get) => ! empty($get('plant_id'))) + // ->directory('uploads/temp'), + // ]) + // ->action(function (array $data) { + // $uploadedFile = $data['invoice_serial_number']; + + // $disk = Storage::disk('local'); + + // $plantId = $data['plant_id']; + + // // Get original filename + // $originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx + + // $originalNameOnly = pathinfo($originalName, PATHINFO_FILENAME); + + // // Store manually using storeAs to keep original name + // $path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); // returns relative path + // // uploads/temp/3RA0018735.xlsx + + // $fullPath = Storage::disk('local')->path($path); + // // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx + + // $totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->count(); + // if ($totQuan > 0) { + // $scanSQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->count(); + // if ($totQuan == $scanSQuan) { + // $invoiceFirst = SerialValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); + // $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; + + // Notification::make() + // ->title("Serial invoice number : '$originalNameOnly' already completed the scanning process for plant : '$plantName'.") + // ->danger() + // ->send(); + + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } else { + // $invoiceFirst = SerialValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); + // // $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null; + // $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; + // $invoicePlantId = $invoiceFirst->plant_id; + // if ($plantId != $invoicePlantId) { + // Notification::make() + // ->title("Serial invoice number : '$originalNameOnly' already exists for plant : '$plantName'.
Choose the valid 'Plant' to proceed!") + // ->danger() + // ->send(); + + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } + // } + // } + + // $totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count(); + // $scanSQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + + // if ($totQuan > 0 && $totQuan == $scanSQuan) { + // Notification::make() + // ->title('Serial invoice already completed the scanning process for selected plant.') + // ->danger() + // ->send(); + + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } + + // if ($fullPath && file_exists($fullPath)) { + // $rows = Excel::toArray(null, $fullPath)[0]; + + // if ((count($rows) - 1) <= 0) { + // Notification::make() + // ->title('Records Not Found') + // ->body("Import the valid 'Serial Invoice' file to proceed..!") + // ->danger() + // ->send(); + + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } + + // $invalidMatCodes = []; + // $invalidSerialCodes = []; + // $materialCodes = []; + // $missingSerials = []; + // $duplicateSerials = []; + // $seenSerialNumbers = []; + // $validRowsFound = false; + + // foreach ($rows as $index => $row) { + // if ($index == 0) { + // continue; + // } // Skip header + + // $materialCode = trim($row[0]); + // $serialNumber = trim($row[1]); + + // if (empty($materialCode) && empty($serialNumber)) { + // continue; + // } + + // if (! empty($materialCode)) { + // if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { + // $invalidMatCodes[] = $materialCode; + // } else { + // if (empty($serialNumber)) { + // $missingSerials[] = $materialCode; + + // } elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) { + // $invalidSerialCodes[] = $serialNumber; + // } else { + // if (in_array($serialNumber, $seenSerialNumbers)) { + // $duplicateSerials[] = $serialNumber; + // } else { + // $seenSerialNumbers[] = $serialNumber; + // $materialCodes[] = $materialCode; + // $validRowsFound = true; + // } + // } + // } + // } else { + // continue; + // } + // } + + // $uniqueInvalidCodes = array_unique($invalidMatCodes); + + // $uniqueMissingSerials = array_unique($missingSerials); + + // $uniqueSerialCodes = array_unique($invalidSerialCodes); + + // $duplicateSerialCodes = array_unique($duplicateSerials); + + // if (! empty($uniqueInvalidCodes)) { + // Notification::make() + // ->title('Invalid Item Codes') + // ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) + // ->danger() + // ->send(); + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } elseif (! empty($uniqueMissingSerials)) { + // Notification::make() + // ->title('Missing Serial Numbers') + // ->body("The following item codes doesn't have valid serial number:
".implode(', ', $uniqueMissingSerials)) + // ->danger() + // ->send(); + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } elseif (! empty($uniqueSerialCodes)) { + // Notification::make() + // ->title('Invalid Serial Number') + // ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
'.implode(', ', $uniqueSerialCodes)) + // ->danger() + // ->send(); + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } elseif (! empty($duplicateSerialCodes)) { + // Notification::make() + // ->title('Duplicate Serial Numbers') + // ->body('The following serial numbers are already exist in imported excel:
'.implode(', ', $duplicateSerialCodes)) + // ->danger() + // ->send(); + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } + + // if (! $validRowsFound) { + // Notification::make() + // ->title('Invalid Serial Invoice') + // ->danger() // This makes the notification red to indicate an error + // ->body('Uploaded Excel sheet is empty or
contains no valid data.') + // ->send(); + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } + + // $uniqueCodes = array_unique($materialCodes); + + // $matchedItems = StickerMaster::with('item') + // ->whereHas('item', function ($query) use ($uniqueCodes) { + // $query->whereIn('code', $uniqueCodes); + // }) + // ->get(); + + // $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + // $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + // if (! empty($missingCodes)) { + // $missingCount = count($missingCodes); + + // $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
'.implode(', ', $missingCodes); + + // Notification::make() + // ->title('Unknown Item Codes') + // ->body($message) + // ->danger() + // ->send(); + + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } + + // // Check which codes have a material_type set (not null) + // $invalidCodes = $matchedItems + // ->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid + // ->pluck('item.code') + // ->toArray(); + + // if (count($invalidCodes) > 10) { + // Notification::make() + // ->title('Invalid item codes found') + // ->body(''.count($invalidCodes).'item codes found have material type.') + // ->danger() + // ->send(); + + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } elseif (count($invalidCodes) > 0) { + // Notification::make() + // ->title('Invalid item codes found') + // ->body('Material invoice Item Codes found : '.implode(', ', $invalidCodes)) + // ->danger() + // ->send(); + + // if ($disk->exists($path)) { + // $disk->delete($path); + // } + + // return; + // } else { + // // Save full file path to session + // session(['uploaded_invoice_path' => $fullPath]); + // Notification::make() + // ->title('Serial invoice imported successfully.') + // ->success() + // ->send(); + // } + // } + // }) Tables\Actions\Action::make('Import Serial Number') - ->label('Import Serial Invoice') - ->form([ - Select::make('plant_id') - // ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs - ->options(function (callable $get) { - $userHas = Filament::auth()->user()->plant_id; + ->label('Import Serial Invoice') + ->form([ + Select::make('plant_id') + // ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs + ->options(function (callable $get) { + $userHas = Filament::auth()->user()->plant_id; - return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); - }) - ->label('Select Plant') - ->required() - ->default(function () { - return optional(SerialValidation::latest()->first())->plant_id; - }) - ->afterStateUpdated(function ($state, callable $set, callable $get) { - $set('invoice_serial_number', null); - }) - ->reactive(), + return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray(); + }) + ->searchable() + ->label('Search by Plant Name') + ->required() + ->default(function () { + return optional(SerialValidation::latest()->first())->plant_id; + }) + ->afterStateUpdated(function ($state, callable $set, callable $get) { + $set('invoice_serial_number', null); + $plantId = $get('plant_id'); + if (! $plantId) { + $set('invoice_serial_number', null); - FileUpload::make('invoice_serial_number') - ->label('Invoice Serial Number') - // ->required() - ->preserveFilenames() // <- this keeps the original filename - ->storeFiles(false) // prevent auto-storing, we will store manually - ->reactive() - ->required() - ->disk('local') // 'local' refers to the local storage disk defined in config/filesystems.php, typically pointing to storage/app. - ->visible(fn (Get $get) => ! empty($get('plant_id'))) - ->directory('uploads/temp'), - ]) - ->action(function (array $data) { - $uploadedFile = $data['invoice_serial_number']; + return; + } + $plantCode = Plant::find($plantId)?->code ?? null; - $disk = Storage::disk('local'); + $directory = "uploads/temp/{$plantCode}"; + if ($plantId && ! Storage::disk('local')->exists($directory)) { + Storage::disk('local')->makeDirectory($directory); + } + }) + ->reactive(), - $plantId = $data['plant_id']; + FileUpload::make('invoice_serial_number') + ->label('Choose Serial Invoice') + ->required() + ->acceptedFileTypes([ + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + // 'application/vnd.ms-excel', // Legacy .xls fallback if needed + ]) + ->rules(['mimes:xlsx', 'max:50']) // Laravel validation: extension check + ->maxSize(50) + ->preserveFilenames() // <- this keeps the original filename + ->reactive() + ->storeFiles(false) // prevent auto-storing, we will store manually + ->disk('local') // 'local' refers to the local storage disk defined in config/filesystems.php, typically pointing to storage/app. + ->visible(fn (Get $get) => ! empty($get('plant_id'))) + ->directory(function (callable $get) { + $plant = Plant::find($get('plant_id')); + $plantCode = $plant?->code ?? null; - // Get original filename - $originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx + return "uploads/temp/{$plantCode}"; + }) + ->uploadingMessage('Uploading...') + ->helperText('Only .xlsx files are allowed (Excel files).'), + ]) + ->action(function (array $data) { + $uploadedFile = $data['invoice_serial_number']; - $originalNameOnly = pathinfo($originalName, PATHINFO_FILENAME); + $disk = Storage::disk('local'); - // Store manually using storeAs to keep original name - $path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); // returns relative path - // uploads/temp/3RA0018735.xlsx + $plantId = $data['plant_id']; - $fullPath = Storage::disk('local')->path($path); - // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx + $plant = Plant::find($plantId); + $plantCode = $plant?->code ?? null; - $totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->count(); - if ($totQuan > 0) { - $scanSQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->count(); - if ($totQuan == $scanSQuan) { - $invoiceFirst = SerialValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); - $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; + // Get original filename + $originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx + $extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION)); + if ($extension != 'xlsx') { + throw new \Exception('Only .xlsx files allowed.'); + } + + $originalNameOnly = strtoupper(pathinfo($originalName, PATHINFO_FILENAME)); + + $originalName = "{$originalNameOnly}.xlsx"; + + // Store manually using storeAs to keep original name + $path = $uploadedFile->storeAs("uploads/temp/{$plantCode}", $originalName, 'local'); // returns relative path + // uploads/temp/{$plantCode}/3RA0018735.xlsx + + if (strlen($originalNameOnly) < 8 || ! ctype_alnum($originalNameOnly)) { Notification::make() - ->title("Serial invoice number : '$originalNameOnly' already completed the scanning process for plant : '$plantName'.") + ->title("Serial invoice number : '$originalNameOnly' should contain minimum 8 digit alpha numeric values!") ->danger() ->send(); @@ -391,14 +718,71 @@ class SerialValidationResource extends Resource } return; - } else { - $invoiceFirst = SerialValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); - // $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null; - $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; - $invoicePlantId = $invoiceFirst->plant_id; - if ($plantId != $invoicePlantId) { + } + + $fullPath = Storage::disk('local')->path($path); + // /home/iot-dev/projects/pds/storage/app/private/uploads/temp/{$plantCode}/3RA0018735.xlsx + + $totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->count(); + if ($totQuan > 0) { + $scanSQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->count(); + if ($totQuan == $scanSQuan) { + $invoiceFirst = SerialValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); + $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; + Notification::make() - ->title("Serial invoice number : '$originalNameOnly' already exists for plant : '$plantName'.
Choose the valid 'Plant' to proceed!") + ->title("Serial invoice number : '$originalNameOnly' already completed the scanning process for plant : '$plantName'.") + ->danger() + ->send(); + + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } else { + $invoiceFirst = SerialValidation::with('plant')->where('invoice_number', $originalNameOnly)->first(); + // $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null; + $plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null; + $invoicePlantId = $invoiceFirst->plant_id; + if ($plantId != $invoicePlantId) { + Notification::make() + ->title("Serial invoice number : '$originalNameOnly' already exists for plant : '$plantName'.
Choose the valid 'Plant' to proceed!") + ->danger() + ->send(); + + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } + } + } + + $totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count(); + $scanSQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + + if ($totQuan > 0 && $totQuan == $scanSQuan) { + Notification::make() + ->title('Serial invoice already completed the scanning process for selected plant.') + ->danger() + ->send(); + + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } + + if ($fullPath && file_exists($fullPath)) { + $rows = Excel::toArray(null, $fullPath)[0]; + + if ((count($rows) - 1) <= 0) { + Notification::make() + ->title('Records Not Found') + ->body("Import the valid 'Serial Invoice' file to proceed..!") ->danger() ->send(); @@ -408,223 +792,186 @@ class SerialValidationResource extends Resource return; } - } - } - $totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count(); - $scanSQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count(); + $invalidMatCodes = []; + $invalidSerialCodes = []; + $materialCodes = []; + $missingSerials = []; + $duplicateSerials = []; + $seenSerialNumbers = []; + $validRowsFound = false; - if ($totQuan > 0 && $totQuan == $scanSQuan) { - Notification::make() - ->title('Serial invoice already completed the scanning process for selected plant.') - ->danger() - ->send(); + foreach ($rows as $index => $row) { + if ($index == 0) { + continue; + } // Skip header - if ($disk->exists($path)) { - $disk->delete($path); - } + $materialCode = trim($row[0]); + $serialNumber = trim($row[1]); - return; - } + if (empty($materialCode) && empty($serialNumber)) { + continue; + } - if ($fullPath && file_exists($fullPath)) { - $rows = Excel::toArray(null, $fullPath)[0]; - - if ((count($rows) - 1) <= 0) { - Notification::make() - ->title('Records Not Found') - ->body("Import the valid 'Serial Invoice' file to proceed..!") - ->danger() - ->send(); - - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } - - $invalidMatCodes = []; - $invalidSerialCodes = []; - $materialCodes = []; - $missingSerials = []; - $duplicateSerials = []; - $seenSerialNumbers = []; - $validRowsFound = false; - - foreach ($rows as $index => $row) { - if ($index == 0) { - continue; - } // Skip header - - $materialCode = trim($row[0]); - $serialNumber = trim($row[1]); - - if (empty($materialCode) && empty($serialNumber)) { - continue; - } - - if (! empty($materialCode)) { - if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { - $invalidMatCodes[] = $materialCode; - } else { - if (empty($serialNumber)) { - $missingSerials[] = $materialCode; - - } elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) { - $invalidSerialCodes[] = $serialNumber; + if (! empty($materialCode)) { + if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) { + $invalidMatCodes[] = $materialCode; } else { - if (in_array($serialNumber, $seenSerialNumbers)) { - $duplicateSerials[] = $serialNumber; + if (empty($serialNumber)) { + $missingSerials[] = $materialCode; + + } elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) { + $invalidSerialCodes[] = $serialNumber; } else { - $seenSerialNumbers[] = $serialNumber; - $materialCodes[] = $materialCode; - $validRowsFound = true; + if (in_array($serialNumber, $seenSerialNumbers)) { + $duplicateSerials[] = $serialNumber; + } else { + $seenSerialNumbers[] = $serialNumber; + $materialCodes[] = $materialCode; + $validRowsFound = true; + } } } + } else { + continue; } + } + + $uniqueInvalidCodes = array_unique($invalidMatCodes); + + $uniqueMissingSerials = array_unique($missingSerials); + + $uniqueSerialCodes = array_unique($invalidSerialCodes); + + $duplicateSerialCodes = array_unique($duplicateSerials); + + if (! empty($uniqueInvalidCodes)) { + Notification::make() + ->title('Invalid Item Codes') + ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) + ->danger() + ->send(); + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } elseif (! empty($uniqueMissingSerials)) { + Notification::make() + ->title('Missing Serial Numbers') + ->body("The following item codes doesn't have valid serial number:
".implode(', ', $uniqueMissingSerials)) + ->danger() + ->send(); + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } elseif (! empty($uniqueSerialCodes)) { + Notification::make() + ->title('Invalid Serial Number') + ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
'.implode(', ', $uniqueSerialCodes)) + ->danger() + ->send(); + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } elseif (! empty($duplicateSerialCodes)) { + Notification::make() + ->title('Duplicate Serial Numbers') + ->body('The following serial numbers are already exist in imported excel:
'.implode(', ', $duplicateSerialCodes)) + ->danger() + ->send(); + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } elseif (! $validRowsFound) { + Notification::make() + ->title('Invalid Serial Invoice') + ->danger() // This makes the notification red to indicate an error + ->body('Uploaded Excel sheet is empty or
contains no valid data.') + ->send(); + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } + + $uniqueCodes = array_unique($materialCodes); + + $matchedItems = StickerMaster::with('item')->where('plant_id', $plantId) + ->whereHas('item', function ($query) use ($uniqueCodes, $plantId) { + $query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId); + }) + ->get(); + + $matchedCodes = $matchedItems->pluck('item.code')->toArray(); + + $missingCodes = array_diff($uniqueCodes, $matchedCodes); + + if (! empty($missingCodes)) { + $missingCount = count($missingCodes); + + $message = $missingCount > 10 ? "'$missingCount' item codes are not found in sticker master." : 'The following item codes are not found in sticker master:
'.implode(', ', $missingCodes); + + Notification::make() + ->title('Unknown Item Codes') + ->body($message) + ->danger() + ->send(); + + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } + + // Check which codes have a material_type set (not null) + $invalidCodes = $matchedItems + ->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid + ->pluck('item.code') + ->toArray(); + + if (count($invalidCodes) > 10) { + Notification::make() + ->title('Invalid item codes found') + ->body(''.count($invalidCodes).' item codes have material type.') + ->danger() + ->send(); + + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; + } elseif (count($invalidCodes) > 0) { + Notification::make() + ->title('Invalid item codes found') + ->body('Material invoice Item Codes found : '.implode(', ', $invalidCodes)) + ->danger() + ->send(); + + if ($disk->exists($path)) { + $disk->delete($path); + } + + return; } else { - continue; + // Save full file path to session + session(['uploaded_invoice_path' => $fullPath]); + Notification::make() + ->title('Serial invoice imported successfully.') + ->success() + ->send(); } } - - $uniqueInvalidCodes = array_unique($invalidMatCodes); - - $uniqueMissingSerials = array_unique($missingSerials); - - $uniqueSerialCodes = array_unique($invalidSerialCodes); - - $duplicateSerialCodes = array_unique($duplicateSerials); - - if (! empty($uniqueInvalidCodes)) { - Notification::make() - ->title('Invalid Item Codes') - ->body('The following item codes should contain minimum 6 digit alpha numeric values:
'.implode(', ', $uniqueInvalidCodes)) - ->danger() - ->send(); - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } elseif (! empty($uniqueMissingSerials)) { - Notification::make() - ->title('Missing Serial Numbers') - ->body("The following item codes doesn't have valid serial number:
".implode(', ', $uniqueMissingSerials)) - ->danger() - ->send(); - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } elseif (! empty($uniqueSerialCodes)) { - Notification::make() - ->title('Invalid Serial Number') - ->body('The following serial numbers should contain minimum 9 digit alpha numeric values:
'.implode(', ', $uniqueSerialCodes)) - ->danger() - ->send(); - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } elseif (! empty($duplicateSerialCodes)) { - Notification::make() - ->title('Duplicate Serial Numbers') - ->body('The following serial numbers are already exist in imported excel:
'.implode(', ', $duplicateSerialCodes)) - ->danger() - ->send(); - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } - - if (! $validRowsFound) { - Notification::make() - ->title('Invalid Serial Invoice') - ->danger() // This makes the notification red to indicate an error - ->body('Uploaded Excel sheet is empty or
contains no valid data.') - ->send(); - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } - - $uniqueCodes = array_unique($materialCodes); - - $matchedItems = StickerMaster::with('item') - ->whereHas('item', function ($query) use ($uniqueCodes) { - $query->whereIn('code', $uniqueCodes); - }) - ->get(); - - $matchedCodes = $matchedItems->pluck('item.code')->toArray(); - - $missingCodes = array_diff($uniqueCodes, $matchedCodes); - - if (! empty($missingCodes)) { - $missingCount = count($missingCodes); - - $message = $missingCount > 10 ? "'$missingCount' item codes are not found in database." : 'The following item codes are not found in database:
'.implode(', ', $missingCodes); - - Notification::make() - ->title('Unknown Item Codes') - ->body($message) - ->danger() - ->send(); - - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } - - // Check which codes have a material_type set (not null) - $invalidCodes = $matchedItems - ->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid - ->pluck('item.code') - ->toArray(); - - if (count($invalidCodes) > 10) { - Notification::make() - ->title('Invalid item codes found') - ->body(''.count($invalidCodes).'item codes found have material type.') - ->danger() - ->send(); - - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } elseif (count($invalidCodes) > 0) { - Notification::make() - ->title('Invalid item codes found') - ->body('Material invoice Item Codes found : '.implode(', ', $invalidCodes)) - ->danger() - ->send(); - - if ($disk->exists($path)) { - $disk->delete($path); - } - - return; - } else { - // Save full file path to session - session(['uploaded_invoice_path' => $fullPath]); - Notification::make() - ->title('Serial invoice imported successfully.') - ->success() - ->send(); - } - } - }) + }) ->visible(function () { return Filament::auth()->user()->can('view import serial validation invoice'); }),