Merge pull request 'Added import serial invoice logic in serial validation' (#702) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 24s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 24s
Reviewed-on: #702
This commit was merged in pull request #702.
This commit is contained in:
@@ -324,65 +324,392 @@ class SerialValidationResource extends Resource
|
|||||||
// ->reactive()
|
// ->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'.<br>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:<br>'.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:<br>".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:<br>'.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:<br>'.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<br>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:<br>'.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')
|
Tables\Actions\Action::make('Import Serial Number')
|
||||||
->label('Import Serial Invoice')
|
->label('Import Serial Invoice')
|
||||||
->form([
|
->form([
|
||||||
Select::make('plant_id')
|
Select::make('plant_id')
|
||||||
// ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs
|
// ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs
|
||||||
->options(function (callable $get) {
|
->options(function (callable $get) {
|
||||||
$userHas = Filament::auth()->user()->plant_id;
|
$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();
|
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
|
||||||
})
|
})
|
||||||
->label('Select Plant')
|
->searchable()
|
||||||
->required()
|
->label('Search by Plant Name')
|
||||||
->default(function () {
|
->required()
|
||||||
return optional(SerialValidation::latest()->first())->plant_id;
|
->default(function () {
|
||||||
})
|
return optional(SerialValidation::latest()->first())->plant_id;
|
||||||
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
})
|
||||||
$set('invoice_serial_number', null);
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
})
|
$set('invoice_serial_number', null);
|
||||||
->reactive(),
|
$plantId = $get('plant_id');
|
||||||
|
if (! $plantId) {
|
||||||
|
$set('invoice_serial_number', null);
|
||||||
|
|
||||||
FileUpload::make('invoice_serial_number')
|
return;
|
||||||
->label('Invoice Serial Number')
|
}
|
||||||
// ->required()
|
$plantCode = Plant::find($plantId)?->code ?? null;
|
||||||
->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');
|
$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
|
return "uploads/temp/{$plantCode}";
|
||||||
$originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx
|
})
|
||||||
|
->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
|
$plantId = $data['plant_id'];
|
||||||
$path = $uploadedFile->storeAs('uploads/temp', $originalName, 'local'); // returns relative path
|
|
||||||
// uploads/temp/3RA0018735.xlsx
|
|
||||||
|
|
||||||
$fullPath = Storage::disk('local')->path($path);
|
$plant = Plant::find($plantId);
|
||||||
// /home/iot-dev/projects/pds/storage/app/private/uploads/temp/3RA0018735.xlsx
|
$plantCode = $plant?->code ?? null;
|
||||||
|
|
||||||
$totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->count();
|
// Get original filename
|
||||||
if ($totQuan > 0) {
|
$originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx
|
||||||
$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;
|
|
||||||
|
|
||||||
|
$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()
|
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()
|
->danger()
|
||||||
->send();
|
->send();
|
||||||
|
|
||||||
@@ -391,14 +718,71 @@ class SerialValidationResource extends Resource
|
|||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else {
|
}
|
||||||
$invoiceFirst = SerialValidation::with('plant')->where('invoice_number', $originalNameOnly)->first();
|
|
||||||
// $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null;
|
$fullPath = Storage::disk('local')->path($path);
|
||||||
$plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null;
|
// /home/iot-dev/projects/pds/storage/app/private/uploads/temp/{$plantCode}/3RA0018735.xlsx
|
||||||
$invoicePlantId = $invoiceFirst->plant_id;
|
|
||||||
if ($plantId != $invoicePlantId) {
|
$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()
|
Notification::make()
|
||||||
->title("Serial invoice number : '$originalNameOnly' already exists for plant : '$plantName'.<br>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'.<br>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()
|
->danger()
|
||||||
->send();
|
->send();
|
||||||
|
|
||||||
@@ -408,223 +792,186 @@ class SerialValidationResource extends Resource
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$totQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count();
|
$invalidMatCodes = [];
|
||||||
$scanSQuan = SerialValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count();
|
$invalidSerialCodes = [];
|
||||||
|
$materialCodes = [];
|
||||||
|
$missingSerials = [];
|
||||||
|
$duplicateSerials = [];
|
||||||
|
$seenSerialNumbers = [];
|
||||||
|
$validRowsFound = false;
|
||||||
|
|
||||||
if ($totQuan > 0 && $totQuan == $scanSQuan) {
|
foreach ($rows as $index => $row) {
|
||||||
Notification::make()
|
if ($index == 0) {
|
||||||
->title('Serial invoice already completed the scanning process for selected plant.')
|
continue;
|
||||||
->danger()
|
} // Skip header
|
||||||
->send();
|
|
||||||
|
|
||||||
if ($disk->exists($path)) {
|
$materialCode = trim($row[0]);
|
||||||
$disk->delete($path);
|
$serialNumber = trim($row[1]);
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
if (empty($materialCode) && empty($serialNumber)) {
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($fullPath && file_exists($fullPath)) {
|
if (! empty($materialCode)) {
|
||||||
$rows = Excel::toArray(null, $fullPath)[0];
|
if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) {
|
||||||
|
$invalidMatCodes[] = $materialCode;
|
||||||
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 {
|
} else {
|
||||||
if (in_array($serialNumber, $seenSerialNumbers)) {
|
if (empty($serialNumber)) {
|
||||||
$duplicateSerials[] = $serialNumber;
|
$missingSerials[] = $materialCode;
|
||||||
|
|
||||||
|
} elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) {
|
||||||
|
$invalidSerialCodes[] = $serialNumber;
|
||||||
} else {
|
} else {
|
||||||
$seenSerialNumbers[] = $serialNumber;
|
if (in_array($serialNumber, $seenSerialNumbers)) {
|
||||||
$materialCodes[] = $materialCode;
|
$duplicateSerials[] = $serialNumber;
|
||||||
$validRowsFound = true;
|
} 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:<br>'.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:<br>".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:<br>'.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:<br>'.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<br>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:<br>'.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 {
|
} 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:<br>'.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:<br>".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:<br>'.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:<br>'.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<br>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:<br>'.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 () {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view import serial validation invoice');
|
return Filament::auth()->user()->can('view import serial validation invoice');
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in New Issue
Block a user