Added validation functionality on import
This commit is contained in:
@@ -2,10 +2,15 @@
|
||||
|
||||
namespace App\Filament\Imports;
|
||||
|
||||
use App\Models\Plant;
|
||||
use App\Models\ReworkLocatorInvoiceValidation;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
|
||||
use Filament\Actions\Imports\ImportColumn;
|
||||
use Filament\Actions\Imports\Importer;
|
||||
use Filament\Actions\Imports\Models\Import;
|
||||
use Str;
|
||||
|
||||
class ReworkLocatorInvoiceValidationImporter extends Importer
|
||||
{
|
||||
@@ -16,10 +21,10 @@ class ReworkLocatorInvoiceValidationImporter extends Importer
|
||||
return [
|
||||
ImportColumn::make('plant')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Plant Name')
|
||||
->example('Ransar Industries-I')
|
||||
->label('Plant Name')
|
||||
->relationship(resolveUsing: 'name')
|
||||
->exampleHeader('Plant Code')
|
||||
->example('1000')
|
||||
->label('Plant Code')
|
||||
->relationship(resolveUsing: 'code')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('invoice_number')
|
||||
->requiredMapping()
|
||||
@@ -34,23 +39,51 @@ class ReworkLocatorInvoiceValidationImporter extends Importer
|
||||
->label('Serial Number')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('pallet_number')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Pallet Number')
|
||||
->example('')
|
||||
->label('Pallet Number'),
|
||||
ImportColumn::make('locator_number')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Locator Number')
|
||||
->example('')
|
||||
->label('Locator Number'),
|
||||
ImportColumn::make('scanned_status')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Scanned Status')
|
||||
->example('')
|
||||
->label('Scanned Status'),
|
||||
->label('Scanned Status')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('upload_status')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Upload Status')
|
||||
->example('Y')
|
||||
->label('Upload Status')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('created_at')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Created At')
|
||||
->example('2025-06-17 08:42:16')
|
||||
->label('Created At')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('scanned_at')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Scanned At')
|
||||
->example('2025-06-17 08:42:16')
|
||||
->label('Scanned At')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('updated_at')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Updated At')
|
||||
->example('2025-06-17 08:42:16')
|
||||
->label('Updated At')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('reworked_at')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Reworked At')
|
||||
->example('2025-06-17 08:42:16')
|
||||
->label('Reworked At')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('created_by')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Created By')
|
||||
@@ -58,43 +91,211 @@ class ReworkLocatorInvoiceValidationImporter extends Importer
|
||||
->label('Created By')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('scanned_by')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Scanned By')
|
||||
->example('admin')
|
||||
->label('Scanned By'),
|
||||
->label('Scanned By')
|
||||
->rules(['required']),
|
||||
ImportColumn::make('updated_by')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Updated By')
|
||||
->example('admin')
|
||||
->label('Updated By'),
|
||||
ImportColumn::make('reworked_by')
|
||||
->requiredMapping()
|
||||
->exampleHeader('Reworked By')
|
||||
->example('admin')
|
||||
->label('Reworked By'),
|
||||
ImportColumn::make('created_at')
|
||||
->exampleHeader('Created At')
|
||||
->example('2025-06-17 08:42:16')
|
||||
->label('Created At')
|
||||
->rules(['date_format:Y-m-d H:i:s']),
|
||||
ImportColumn::make('scanned_at')
|
||||
->exampleHeader('Scanned At')
|
||||
->example('2025-06-17 08:42:16')
|
||||
->label('Scanned At')
|
||||
->rules(['date_format:Y-m-d H:i:s']),
|
||||
ImportColumn::make('reworked_at')
|
||||
->exampleHeader('Reworked At')
|
||||
->example('2025-06-17 08:42:16')
|
||||
->label('Reworked At')
|
||||
->rules(['date_format:Y-m-d H:i:s']),
|
||||
->label('Reworked By')
|
||||
->rules(['required']),
|
||||
];
|
||||
}
|
||||
|
||||
public function resolveRecord(): ?ReworkLocatorInvoiceValidation
|
||||
{
|
||||
// return ReworkLocatorInvoiceValidation::firstOrNew([
|
||||
// // Update existing records, matching them by `$this->data['column_name']`
|
||||
// 'email' => $this->data['email'],
|
||||
// ]);
|
||||
$warnMsg = [];
|
||||
$plantCod = $this->data['plant'];
|
||||
$plant = null;
|
||||
$invoiceNo = $this->data['invoice_number'];
|
||||
$serialNo = $this->data['serial_number'];
|
||||
$palletNo = $this->data['pallet_number'];
|
||||
$locatorNo = $this->data['locator_number'];
|
||||
$scannedStat = $this->data['scanned_status'];
|
||||
$uploadStat = $this->data['upload_status'];
|
||||
$createdAt = $this->data['created_at'];
|
||||
$scannedAt = $this->data['scanned_at'];
|
||||
$updatedAt = $this->data['updated_at'];
|
||||
$reworkedAt = $this->data['reworked_at'];
|
||||
$cDateTime = null;
|
||||
$sDateTime = null;
|
||||
$uDateTime = null;
|
||||
$rDateTime = null;
|
||||
$createdBy = $this->data['created_by'];
|
||||
$scannedBy = $this->data['scanned_by'];
|
||||
$updatedBy = $this->data['updated_by'];
|
||||
$reworkedBy = $this->data['reworked_by'];
|
||||
|
||||
return new ReworkLocatorInvoiceValidation();
|
||||
if (Str::length($plantCod) < 4 || !is_numeric($plantCod) || !preg_match('/^[1-9]\d{3,}$/', $plantCod)) {
|
||||
$warnMsg[] = "Invalid plant code found";
|
||||
}
|
||||
else
|
||||
{
|
||||
$plant = Plant::where('code', $this->data['plant'])->first();
|
||||
if (!$plant) {
|
||||
$warnMsg[] = "Plant not found";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Str::length($invoiceNo) < 5 || !ctype_alnum($invoiceNo)) {
|
||||
$warnMsg[] = "Invalid invoice number found";
|
||||
}
|
||||
if (Str::length($serialNo) < 9 || !ctype_alnum($serialNo)) {
|
||||
$warnMsg[] = "Invalid serial number found";
|
||||
}
|
||||
if (Str::length($palletNo) > 0 && Str::length($palletNo) < 10) {
|
||||
$warnMsg[] = "Invalid pallet number found";
|
||||
}
|
||||
if (Str::length($locatorNo) > 0 && Str::length($locatorNo) < 7) {
|
||||
$warnMsg[] = "Invalid locator number found";
|
||||
}
|
||||
if ($scannedStat != 'Scanned') {
|
||||
$warnMsg[] = "Invalid scanned status found";
|
||||
}
|
||||
if ($uploadStat != 'Y' && $uploadStat != 'N') {
|
||||
$warnMsg[] = "Invalid upload status found";
|
||||
}
|
||||
$created = User::where('name', $createdBy)->first();
|
||||
if (!$created) {
|
||||
$warnMsg[] = "Created by not found";
|
||||
}
|
||||
$scanned = User::where('name', $scannedBy)->first();
|
||||
if (!$scanned) {
|
||||
$warnMsg[] = "Scanned by not found";
|
||||
}
|
||||
if (Str::length($updatedBy) > 0) {
|
||||
$updated = User::where('name', $updatedBy)->first();
|
||||
if (!$updated) {
|
||||
$warnMsg[] = "Updated by not found";
|
||||
}
|
||||
}
|
||||
$reworked = User::where('name', $reworkedBy)->first();
|
||||
if (!$reworked) {
|
||||
$warnMsg[] = "Reworked by not found";
|
||||
}
|
||||
|
||||
$formats = ['d-m-Y H:i', 'd-m-Y H:i:s']; //'07-05-2025 08:00' or '07-05-2025 08:00:00'
|
||||
|
||||
foreach ($formats as $format) {
|
||||
try {
|
||||
$cDateTime = Carbon::createFromFormat($format, $createdAt);
|
||||
break;
|
||||
} catch (\Exception $e) {
|
||||
// Optionally collect warning messages
|
||||
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($cDateTime)) {
|
||||
// throw new \Exception('Invalid date time format');
|
||||
$warnMsg[] = "Invalid 'Created DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||
}
|
||||
|
||||
foreach ($formats as $format) {
|
||||
try {
|
||||
$sDateTime = Carbon::createFromFormat($format, $scannedAt);
|
||||
break;
|
||||
} catch (\Exception $e) {
|
||||
// Optionally collect warning messages
|
||||
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($sDateTime)) {
|
||||
$warnMsg[] = "Invalid 'Scanned DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||
}
|
||||
|
||||
if (Str::length($updatedAt) > 0)
|
||||
{
|
||||
foreach ($formats as $format) {
|
||||
try {
|
||||
$uDateTime = Carbon::createFromFormat($format, $updatedAt);
|
||||
break;
|
||||
} catch (\Exception $e) {
|
||||
// Optionally collect warning messages
|
||||
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($uDateTime)) {
|
||||
$warnMsg[] = "Invalid 'Updated DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($cDateTime) && isset($uDateTime)) {
|
||||
if ($cDateTime->greaterThan($uDateTime)) {
|
||||
$warnMsg[] = "'Created DataTime' is greater than 'Updated DateTime'.";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($formats as $format) {
|
||||
try {
|
||||
$rDateTime = Carbon::createFromFormat($format, $reworkedAt);
|
||||
break;
|
||||
} catch (\Exception $e) {
|
||||
// Optionally collect warning messages
|
||||
// $warnMsg[] = "Date format mismatch with format: $format";
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($rDateTime)) {
|
||||
$warnMsg[] = "Invalid 'Reworked DateTime' format. Expected DD-MM-YYYY HH:MM:SS";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isset($cDateTime) && isset($rDateTime)) {
|
||||
if ($cDateTime->greaterThan($rDateTime)) {
|
||||
$warnMsg[] = "'Created DataTime' is greater than 'Reworked DateTime'.";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (!is_numeric($locatorQuantity) || $locatorQuantity < 0 || $locatorQuantity > 2) {
|
||||
// $warnMsg[] = "Invalid locator quantity found";
|
||||
// }
|
||||
|
||||
if (!empty($warnMsg)) {
|
||||
throw new RowImportFailedException(implode(', ', $warnMsg));
|
||||
}
|
||||
|
||||
ReworkLocatorInvoiceValidation::updateOrCreate(
|
||||
[
|
||||
'plant_id' => $plant->id,
|
||||
'serial_number' => $serialNo,
|
||||
'invoice_number' => $invoiceNo,
|
||||
'pallet_number' => $palletNo,
|
||||
'locator_number' => $locatorNo,
|
||||
'scanned_status' => $scannedStat,
|
||||
'upload_status' => $uploadStat,
|
||||
'created_at' => $cDateTime->format('Y-m-d H:i:s'),
|
||||
'scanned_at' => (Str::length($scannedAt) > 0) ? $sDateTime->format('Y-m-d H:i:s') : null,
|
||||
'updated_at' => (Str::length($updatedAt) > 0) ? $uDateTime->format('Y-m-d H:i:s') : null,
|
||||
'reworked_at' => (Str::length($reworkedAt) > 0) ? $rDateTime->format('Y-m-d H:i:s') : null,
|
||||
'created_by' => $createdBy,
|
||||
'scanned_by' => $scannedBy,
|
||||
'updated_by' => $updatedBy,
|
||||
'reworked_by' => $reworkedBy
|
||||
]
|
||||
);
|
||||
return null;
|
||||
// // return ReworkLocatorInvoiceValidation::firstOrNew([
|
||||
// // // Update existing records, matching them by `$this->data['column_name']`
|
||||
// // 'email' => $this->data['email'],
|
||||
// // ]);
|
||||
|
||||
// return new ReworkLocatorInvoiceValidation();
|
||||
}
|
||||
|
||||
public static function getCompletedNotificationBody(Import $import): string
|
||||
|
||||
Reference in New Issue
Block a user