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,12 +398,12 @@ class InvoiceOutValidationResource extends Resource
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
$successCount = 0;
|
$successCount = 0;
|
||||||
|
$updateCount = 0;
|
||||||
|
$insertCount = 0;
|
||||||
$failedRecords = [];
|
$failedRecords = [];
|
||||||
|
|
||||||
DB::beginTransaction();
|
foreach ($rows as $index => $row)
|
||||||
|
{
|
||||||
try {
|
|
||||||
foreach ($rows as $index => $row) {
|
|
||||||
if ($index == 0) {
|
if ($index == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -443,43 +443,28 @@ class InvoiceOutValidationResource extends Resource
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$record = InvoiceOutValidation::where('plant_id', $plant->id)
|
$exists = InvoiceOutValidation::where('plant_id', $plant->id)
|
||||||
->where('qr_code', $qrcode)
|
->where('qr_code', $qrcode)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$curStat = $record ? 'Updation' : 'Insertion';
|
InvoiceOutValidation::updateOrCreate(
|
||||||
|
[
|
||||||
if ($record) {
|
'plant_id' => $plant->id,
|
||||||
$record->update([
|
'qr_code' => $qrcode,
|
||||||
|
],
|
||||||
|
[
|
||||||
'scanned_at' => $formattedDate,
|
'scanned_at' => $formattedDate,
|
||||||
'scanned_by' => $scannedBy,
|
'scanned_by' => $scannedBy,
|
||||||
'updated_by' => $operatorName,
|
'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,
|
'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}");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$exists ? $updateCount++ : $insertCount++;
|
||||||
$successCount++;
|
$successCount++;
|
||||||
} catch (\Exception $e) {
|
|
||||||
|
}
|
||||||
|
catch (\Exception $e) {
|
||||||
$failedRecords[] = [
|
$failedRecords[] = [
|
||||||
'row' => $rowNumber,
|
'row' => $rowNumber,
|
||||||
'qrcode' => $qrcode ?? null,
|
'qrcode' => $qrcode ?? null,
|
||||||
@@ -488,34 +473,31 @@ class InvoiceOutValidationResource extends Resource
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DB::commit();
|
|
||||||
|
|
||||||
if (count($failedRecords) > 0) {
|
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("'{$successCount}' records inserted. ".count($failedRecords)." failed.\n\n{$failedSummary}")
|
->body(
|
||||||
|
"Total Success: {$successCount}\n" .
|
||||||
|
"Inserted: {$insertCount}\n" .
|
||||||
|
"Updated: {$updateCount}\n" .
|
||||||
|
"Failed: " . count($failedRecords) . "\n\n" .
|
||||||
|
$failedSummary
|
||||||
|
)
|
||||||
->warning()
|
->warning()
|
||||||
->send();
|
->send();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Notification::make()
|
Notification::make()
|
||||||
->title('Import Success')
|
->title('Import Success')
|
||||||
->body("Successfully imported '{$successCount}' records.")
|
->body("Successfully imported '{$successCount}' records.")
|
||||||
->success()
|
->success()
|
||||||
->send();
|
->send();
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
|
||||||
DB::rollBack();
|
|
||||||
Notification::make()
|
|
||||||
->title('Import Failed')
|
|
||||||
->body("No records were inserted. Error : {$e->getMessage()}")
|
|
||||||
->danger()
|
|
||||||
->send();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
->visible(function () {
|
->visible(function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user