Added invoice pending reason import
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 33s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 47s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 18s
Laravel Pint / pint (pull_request) Successful in 2m11s
Laravel Larastan / larastan (pull_request) Failing after 3m22s
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 33s
Gemini PR Review / Gemini PR Review (pull_request) Failing after 47s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 18s
Laravel Pint / pint (pull_request) Successful in 2m11s
Laravel Larastan / larastan (pull_request) Failing after 3m22s
This commit is contained in:
133
app/Imports/InvoicePendingReasonImport.php
Normal file
133
app/Imports/InvoicePendingReasonImport.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace App\Imports;
|
||||
|
||||
use App\Models\InvoiceDataValidation;
|
||||
use App\Models\Plant;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\ToCollection;
|
||||
|
||||
class InvoicePendingReasonImport implements ToCollection
|
||||
{
|
||||
/**
|
||||
* @param Collection $collection
|
||||
*/
|
||||
|
||||
public array $errors = [];
|
||||
|
||||
public array $missingPlantCodes = [];
|
||||
public array $missingDocNo = [];
|
||||
public array $plantCodeEmpty = [];
|
||||
public array $docNoEmpty = [];
|
||||
public array $remarkEmpty = [];
|
||||
|
||||
public array $duplicateExcelDocs = [];
|
||||
|
||||
private array $seenExcelKeys = [];
|
||||
|
||||
public array $validRows = [];
|
||||
|
||||
public function collection(Collection $collection)
|
||||
{
|
||||
$rows = $collection;
|
||||
// foreach ($rows->skip(1) as $row) {
|
||||
|
||||
// $plantCode = trim($row[0] ?? '');
|
||||
// $documentNumber = trim($row[1] ?? '');
|
||||
// $remark = trim($row[2] ?? '');
|
||||
|
||||
// if (! $plantCode || ! $documentNumber || ! $remark) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $plantId = Plant::where('code', $plantCode)->value('id');
|
||||
|
||||
// InvoiceDataValidation::where('plant_id', $plantId)
|
||||
// ->where('document_number', $documentNumber)
|
||||
// ->update([
|
||||
// 'remark' => $remark,
|
||||
// 'updated_at' => now(),
|
||||
// ]);
|
||||
// }
|
||||
|
||||
foreach ($rows->skip(1) as $index => $row) {
|
||||
|
||||
$rowNo = $index + 2;
|
||||
|
||||
$plantCode = trim($row[0] ?? '');
|
||||
$documentNumber = trim($row[1] ?? '');
|
||||
$remark = trim($row[2] ?? '');
|
||||
|
||||
if (! $plantCode) {
|
||||
$this->plantCodeEmpty[] = [
|
||||
'row' => $rowNo,
|
||||
'reason' => "Plant Code can't be empty!",
|
||||
];
|
||||
continue;
|
||||
}
|
||||
else if (! $documentNumber) {
|
||||
$this->docNoEmpty[] = [
|
||||
'row' => $rowNo,
|
||||
'reason' => "Document number can't be empty!",
|
||||
];
|
||||
continue;
|
||||
}
|
||||
else if (! $remark) {
|
||||
$this->remarkEmpty[] = [
|
||||
'row' => $rowNo,
|
||||
'reason' => "Remark can't be empty!",
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
//Excel-level duplicate check
|
||||
$key = $plantCode . '|' . $documentNumber;
|
||||
|
||||
if (isset($this->seenExcelKeys[$key])) {
|
||||
$this->duplicateExcelDocs[$key][] = $rowNo;
|
||||
|
||||
$this->errors[] = [
|
||||
'row' => $rowNo,
|
||||
'reason' => "Duplicate Document Number in Excel ({$plantCode} - {$documentNumber})",
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->seenExcelKeys[$key] = true;
|
||||
|
||||
$plantId = Plant::where('code', $plantCode)->value('id');
|
||||
|
||||
if (! $plantId) {
|
||||
$this->missingPlantCodes[$plantCode] = true;
|
||||
|
||||
$this->errors[] = [
|
||||
'row' => $rowNo,
|
||||
'reason' => "Plant code not found: {$plantCode}",
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
$invoiceExists = InvoiceDataValidation::where('plant_id', $plantId)
|
||||
->where('document_number', $documentNumber)
|
||||
->first();
|
||||
|
||||
if (! $invoiceExists) {
|
||||
$this->missingDocNo[$documentNumber] = true;
|
||||
|
||||
$this->errors[] = [
|
||||
'row' => $rowNo,
|
||||
'reason' => "Document number not found: {$documentNumber}",
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
$this->validRows[] = [
|
||||
'plant_id' => $plantId,
|
||||
'document_number' => $documentNumber,
|
||||
'remark' => $remark,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user