changed update scenario logic in invoice out validations page
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 11s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 16s
Laravel Pint / pint (pull_request) Successful in 2m50s
Laravel Larastan / larastan (pull_request) Failing after 3m39s
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 10s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 11s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 16s
Laravel Pint / pint (pull_request) Successful in 2m50s
Laravel Larastan / larastan (pull_request) Failing after 3m39s
This commit is contained in:
@@ -398,122 +398,104 @@ class InvoiceOutValidationResource extends Resource
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
$successCount = 0;
|
$successCount = 0;
|
||||||
|
$updateCount = 0;
|
||||||
|
$insertCount = 0;
|
||||||
$failedRecords = [];
|
$failedRecords = [];
|
||||||
|
|
||||||
DB::beginTransaction();
|
foreach ($rows as $index => $row)
|
||||||
|
{
|
||||||
try {
|
if ($index == 0) {
|
||||||
foreach ($rows as $index => $row) {
|
continue;
|
||||||
if ($index == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$rowNumber = $index + 1;
|
|
||||||
|
|
||||||
try {
|
|
||||||
$qrcode = trim($row[1]);
|
|
||||||
$plantCode = trim($row[2]);
|
|
||||||
$scannedAt = trim($row[3]);
|
|
||||||
$scannedBy = trim($row[4]);
|
|
||||||
|
|
||||||
if (empty($qrcode)) {
|
|
||||||
throw new \Exception("Row '{$rowNumber}' Missing QR Code");
|
|
||||||
}
|
|
||||||
|
|
||||||
$plant = Plant::where('code', $plantCode)->first();
|
|
||||||
if (! $plant) {
|
|
||||||
throw new \Exception("Invalid plant code : '{$plantCode}'");
|
|
||||||
}
|
|
||||||
|
|
||||||
$formattedDate = null;
|
|
||||||
if (! empty($scannedAt)) {
|
|
||||||
try {
|
|
||||||
// $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
|
|
||||||
// ->format('Y-m-d H:i:s');
|
|
||||||
if (is_numeric($scannedAt)) {
|
|
||||||
$formattedDate = ExcelDate::excelToDateTimeObject($scannedAt)
|
|
||||||
->format('Y-m-d H:i:s');
|
|
||||||
} else {
|
|
||||||
// Or handle as manual string date (d-m-Y H:i:s)
|
|
||||||
$formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
|
|
||||||
->format('Y-m-d H:i:s');
|
|
||||||
}
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
throw new \Exception("Invalid date format : '{$scannedAt}'");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$record = InvoiceOutValidation::where('plant_id', $plant->id)
|
|
||||||
->where('qr_code', $qrcode)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
$curStat = $record ? 'Updation' : 'Insertion';
|
|
||||||
|
|
||||||
if ($record) {
|
|
||||||
$record->update([
|
|
||||||
'scanned_at' => $formattedDate,
|
|
||||||
'scanned_by' => $scannedBy,
|
|
||||||
'updated_by' => $operatorName,
|
|
||||||
]);
|
|
||||||
$inserted = $record;
|
|
||||||
} else {
|
|
||||||
// Record does not exist, create with 'created_by'
|
|
||||||
$inserted = InvoiceOutValidation::create([
|
|
||||||
'plant_id' => $plant->id,
|
|
||||||
'qr_code' => $qrcode,
|
|
||||||
'scanned_at' => $formattedDate,
|
|
||||||
'scanned_by' => $scannedBy,
|
|
||||||
'created_by' => $operatorName,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
// $inserted = InvoiceOutValidation::create([
|
|
||||||
// 'plant_id' => $plant->id,
|
|
||||||
// 'qr_code' => $qrcode,
|
|
||||||
// 'scanned_at' => $formattedDate,
|
|
||||||
// 'scanned_by' => $scannedBy,
|
|
||||||
// 'created_by' => $operatorName
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
if (! $inserted) {
|
|
||||||
throw new \Exception("{$curStat} failed for QR : {$qrcode}");
|
|
||||||
}
|
|
||||||
|
|
||||||
$successCount++;
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$failedRecords[] = [
|
|
||||||
'row' => $rowNumber,
|
|
||||||
'qrcode' => $qrcode ?? null,
|
|
||||||
'error' => $e->getMessage(),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DB::commit();
|
$rowNumber = $index + 1;
|
||||||
|
|
||||||
if (count($failedRecords) > 0) {
|
try {
|
||||||
|
$qrcode = trim($row[1]);
|
||||||
|
$plantCode = trim($row[2]);
|
||||||
|
$scannedAt = trim($row[3]);
|
||||||
|
$scannedBy = trim($row[4]);
|
||||||
|
|
||||||
|
if (empty($qrcode)) {
|
||||||
|
throw new \Exception("Row '{$rowNumber}' Missing QR Code");
|
||||||
|
}
|
||||||
|
|
||||||
|
$plant = Plant::where('code', $plantCode)->first();
|
||||||
|
if (! $plant) {
|
||||||
|
throw new \Exception("Invalid plant code : '{$plantCode}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
$formattedDate = null;
|
||||||
|
if (! empty($scannedAt)) {
|
||||||
|
try {
|
||||||
|
// $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
|
||||||
|
// ->format('Y-m-d H:i:s');
|
||||||
|
if (is_numeric($scannedAt)) {
|
||||||
|
$formattedDate = ExcelDate::excelToDateTimeObject($scannedAt)
|
||||||
|
->format('Y-m-d H:i:s');
|
||||||
|
} else {
|
||||||
|
// Or handle as manual string date (d-m-Y H:i:s)
|
||||||
|
$formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
|
||||||
|
->format('Y-m-d H:i:s');
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
throw new \Exception("Invalid date format : '{$scannedAt}'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$exists = InvoiceOutValidation::where('plant_id', $plant->id)
|
||||||
|
->where('qr_code', $qrcode)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
InvoiceOutValidation::updateOrCreate(
|
||||||
|
[
|
||||||
|
'plant_id' => $plant->id,
|
||||||
|
'qr_code' => $qrcode,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'scanned_at' => $formattedDate,
|
||||||
|
'scanned_by' => $scannedBy,
|
||||||
|
'updated_by' => $operatorName,
|
||||||
|
'created_by' => $operatorName,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$exists ? $updateCount++ : $insertCount++;
|
||||||
|
$successCount++;
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
|
$failedRecords[] = [
|
||||||
|
'row' => $rowNumber,
|
||||||
|
'qrcode' => $qrcode ?? null,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($failedRecords) > 0) {
|
||||||
$failedSummary = collect($failedRecords)
|
$failedSummary = collect($failedRecords)
|
||||||
->map(fn ($f) => "Row {$f['row']} ({$f['qrcode']}) : {$f['error']}")
|
->map(fn ($f) => "Row {$f['row']} ({$f['qrcode']}) : {$f['error']}")
|
||||||
->take(5) // limit preview to first 5 errors
|
->take(5)
|
||||||
->implode("\n");
|
->implode("\n");
|
||||||
|
Notification::make()
|
||||||
Notification::make()
|
->title('Partial Import Warning')
|
||||||
->title('Partial Import Warning')
|
->body(
|
||||||
->body("'{$successCount}' records inserted. ".count($failedRecords)." failed.\n\n{$failedSummary}")
|
"Total Success: {$successCount}\n" .
|
||||||
->warning()
|
"Inserted: {$insertCount}\n" .
|
||||||
->send();
|
"Updated: {$updateCount}\n" .
|
||||||
} else {
|
"Failed: " . count($failedRecords) . "\n\n" .
|
||||||
Notification::make()
|
$failedSummary
|
||||||
->title('Import Success')
|
)
|
||||||
->body("Successfully imported '{$successCount}' records.")
|
->warning()
|
||||||
->success()
|
->send();
|
||||||
->send();
|
}
|
||||||
}
|
else
|
||||||
} catch (\Exception $e) {
|
{
|
||||||
DB::rollBack();
|
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Import Failed')
|
->title('Import Success')
|
||||||
->body("No records were inserted. Error : {$e->getMessage()}")
|
->body("Successfully imported '{$successCount}' records.")
|
||||||
->danger()
|
->success()
|
||||||
->send();
|
->send();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user