Refactor: Clean up imports and enhance form/table structure in InvoiceDataValidation and InvoiceOutValidation resources
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (pull_request) Successful in 10s
Laravel Larastan / larastan (pull_request) Failing after 2m1s
Laravel Pint / pint (pull_request) Failing after 2m7s
Gemini PR Review / review (pull_request) Failing after 44s

This commit is contained in:
dhanabalan
2025-11-27 10:59:00 +05:30
parent c16c967757
commit 95c909b6ac
2 changed files with 237 additions and 205 deletions

View File

@@ -4,27 +4,23 @@ namespace App\Filament\Resources;
use App\Filament\Exports\InvoiceDataValidationExporter;
use App\Filament\Resources\InvoiceDataValidationResource\Pages;
use App\Filament\Resources\InvoiceDataValidationResource\RelationManagers;
use App\Models\InvoiceDataValidation;
use App\Models\Plant;
use DB;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Facades\Filament;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use App\Models\Plant;
use Filament\Forms\Get;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\StickerMaster;
use App\Models\User;
use DB;
use Filament\Tables\Actions\ExportAction;
class InvoiceDataValidationResource extends Resource
{
@@ -67,6 +63,7 @@ class InvoiceDataValidationResource extends Resource
->label('Created By')
->default(Filament::auth()->user()?->name),
Forms\Components\Hidden::make('updated_by')
->label('Updated By')
->default(Filament::auth()->user()?->name),
]);
}
@@ -77,10 +74,12 @@ class InvoiceDataValidationResource extends Resource
->columns([
Tables\Columns\TextColumn::make('No.')
->label('No.')
->alignCenter()
->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}),
Tables\Columns\TextColumn::make('plant.code')
@@ -100,8 +99,8 @@ class InvoiceDataValidationResource extends Resource
->sortable(),
Tables\Columns\TextColumn::make('document_number')
->label('Document Number')
->searchable()
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('document_date')
->label('Document Date')
@@ -127,15 +126,27 @@ class InvoiceDataValidationResource extends Resource
Tables\Columns\TextColumn::make('created_at')
->label('Created At')
->alignCenter()
->searchable()
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
->sortable(),
// ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('created_by')
->label('Created By')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('updated_at')
->label('Updated At')
->alignCenter()
->searchable()
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
->sortable(),
// ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_by')
->label('Updated By')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('deleted_at')
->label('Deleted At')
->alignCenter()
@@ -191,12 +202,10 @@ class InvoiceDataValidationResource extends Resource
$fullPath = Storage::disk('local')->path($path);
if ($fullPath && file_exists($fullPath))
{
if ($fullPath && file_exists($fullPath)) {
$rows = Excel::toArray(null, $fullPath)[0];
if ((count($rows) - 1) <= 0)
{
if ((count($rows) - 1) <= 0) {
Notification::make()
->title('Records Not Found')
->body("Import the valid 'Invoice Data' file to proceed..!")
@@ -206,6 +215,7 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
@@ -226,9 +236,10 @@ class InvoiceDataValidationResource extends Resource
// $duplicateEntries = [];
$duplicateEntriesExcel = [];
foreach ($rows as $index => $row)
{
if ($index == 0) continue; // Skip header
foreach ($rows as $index => $row) {
if ($index == 0) {
continue;
} // Skip header
$DisChaDesc = trim($row[3]);
$plantCode = trim($row[4]);
@@ -246,20 +257,16 @@ class InvoiceDataValidationResource extends Resource
if (empty($CustomerCode)) {
$invalidCustomerCode[] = "Row {$index}";
}
if (empty($DocNo))
{
if (empty($DocNo)) {
$invalidDocNo[] = "Row {$index}";
}
if (empty($CusTradeName))
{
if (empty($CusTradeName)) {
$invalidCusTradeName[] = "Row {$index}";
}
if (empty($CusLocation))
{
if (empty($CusLocation)) {
$invalidCusLocation[] = "Row {$index}";
}
if (empty($Location))
{
if (empty($Location)) {
$invalidLocation[] = "Row {$index}";
}
// if (empty($createdBy)) $invalidUser[] = "Row {$index}";
@@ -269,9 +276,7 @@ class InvoiceDataValidationResource extends Resource
}
if (! is_numeric($plantCode)) {
$invalidPlantType[] = $plantCode;
}
else if (!Plant::where('code', $plantCode)->first())
{
} elseif (! Plant::where('code', $plantCode)->first()) {
$invalidPlaCoFound[] = $plantCode;
}
@@ -296,17 +301,28 @@ class InvoiceDataValidationResource extends Resource
$seenPlantDoc[] = $uniqueKey;
}
if (!empty($invalidCustomerCode) || !empty($invalidDocNo) || !empty($invalidDocDate) || !empty($invalidCusTradeName) || !empty($invalidCusLocation))
{
if (! empty($invalidCustomerCode) || ! empty($invalidDocNo) || ! empty($invalidDocDate) || ! empty($invalidCusTradeName) || ! empty($invalidCusLocation)) {
$errorMsg = '';
// if (!empty($invalidDisChaDesc)) $errorMsg .= 'Missing Distribution Channel Description in rows: ' . implode(', ', $invalidDisChaDesc) . '<br>';
if (!empty($invalidCustomerCode)) $errorMsg .= 'Missing Customer Code in rows: ' . implode(', ', $invalidCustomerCode) . '<br>';
if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '<br>';
if (!empty($invalidDocDate)) $errorMsg .= 'Missing Document Date in rows: ' . implode(', ', $invalidDocDate) . '<br>';
if (!empty($invalidCusTradeName)) $errorMsg .= 'Missing Customer Trade Name in rows: ' . implode(', ', $invalidCusTradeName) . '<br>';
if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>';
if (!empty($invalidLocation)) $errorMsg .= 'Missing Location in rows: ' . implode(', ', $invalidLocation) . '<br>';
if (! empty($invalidCustomerCode)) {
$errorMsg .= 'Missing Customer Code in rows: '.implode(', ', $invalidCustomerCode).'<br>';
}
if (! empty($invalidDocNo)) {
$errorMsg .= 'Missing Document Number in rows: '.implode(', ', $invalidDocNo).'<br>';
}
if (! empty($invalidDocDate)) {
$errorMsg .= 'Missing Document Date in rows: '.implode(', ', $invalidDocDate).'<br>';
}
if (! empty($invalidCusTradeName)) {
$errorMsg .= 'Missing Customer Trade Name in rows: '.implode(', ', $invalidCusTradeName).'<br>';
}
if (! empty($invalidCusLocation)) {
$errorMsg .= 'Missing Customer Location in rows: '.implode(', ', $invalidCusLocation).'<br>';
}
if (! empty($invalidLocation)) {
$errorMsg .= 'Missing Location in rows: '.implode(', ', $invalidLocation).'<br>';
}
Notification::make()
->title('Missing Mandatory Fields')
@@ -317,10 +333,9 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
else if (!empty($invalidPlantCode))
{
} elseif (! empty($invalidPlantCode)) {
$invalidPlantCode = array_unique($invalidPlantCode);
Notification::make()
->title('Invalid Plant Codes')
@@ -330,10 +345,9 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
else if (!empty($invalidPlantType))
{
} elseif (! empty($invalidPlantType)) {
$invalidPlantType = array_unique($invalidPlantType);
Notification::make()
->title('Invalid Plant Codes')
@@ -343,10 +357,9 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
else if (!empty($invalidPlaCoFound))
{
} elseif (! empty($invalidPlaCoFound)) {
$invalidPlaCoFound = array_unique($invalidPlaCoFound);
Notification::make()
->title('Invalid Plant Codes')
@@ -356,6 +369,7 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
@@ -401,8 +415,7 @@ class InvoiceDataValidationResource extends Resource
// return;
// }
if (!empty($duplicateEntriesExcel))
{
if (! empty($duplicateEntriesExcel)) {
$duplicateGroupedByPlantExcel = [];
foreach ($duplicateEntriesExcel as $message) {// "/Document Number '([^']+)' already exist(?:s)?(?: for Plant (.+))?/"
@@ -415,8 +428,7 @@ class InvoiceDataValidationResource extends Resource
$errorMsg = 'Duplicate Document Number found in Uploaded File :<br>';
foreach ($duplicateGroupedByPlantExcel as $plant => $docNumbers)
{
foreach ($duplicateGroupedByPlantExcel as $plant => $docNumbers) {
// Remove duplicate document numbers for each plant
$uniqueDocNumbers = array_unique($docNumbers);
$count = count($uniqueDocNumbers);
@@ -439,6 +451,7 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
@@ -455,10 +468,11 @@ class InvoiceDataValidationResource extends Resource
DB::beginTransaction();
try
{
try {
foreach ($rows as $index => $row) {
if ($index == 0) continue;
if ($index == 0) {
continue;
}
$rowNumber = $index + 1;
@@ -508,7 +522,7 @@ class InvoiceDataValidationResource extends Resource
'customer_trade_name' => $CusTradeName,
'customer_location' => $CusLocation,
'location' => $Location,
'updated_by' => $operatorName
'updated_by' => $operatorName,
]);
$inserted = $record;
} else {
@@ -522,7 +536,7 @@ class InvoiceDataValidationResource extends Resource
'customer_trade_name' => $CusTradeName,
'customer_location' => $CusLocation,
'location' => $Location,
'created_by' => $operatorName
'created_by' => $operatorName,
]);
}
// $inserted = InvoiceDataValidation::create([
@@ -545,7 +559,7 @@ class InvoiceDataValidationResource extends Resource
$failedRecords[] = [
'row' => $rowNumber,
'document_number' => $DocNo ?? null,
'error' => $e->getMessage()
'error' => $e->getMessage(),
];
}
}
@@ -570,9 +584,7 @@ class InvoiceDataValidationResource extends Resource
->success()
->send();
}
}
catch (\Exception $e)
{
} catch (\Exception $e) {
DB::rollBack();
Notification::make()
->title('Import Failed')

View File

@@ -4,27 +4,25 @@ namespace App\Filament\Resources;
use App\Filament\Exports\InvoiceOutValidationExporter;
use App\Filament\Resources\InvoiceOutValidationResource\Pages;
use App\Filament\Resources\InvoiceOutValidationResource\RelationManagers;
use App\Models\InvoiceOutValidation;
use App\Models\Plant;
use App\Models\User;
use Carbon\Carbon;
use DB;
use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Section;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Facades\Filament;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Section;
use Filament\Notifications\Notification;
use Maatwebsite\Excel\Facades\Excel;
use Storage;
use Filament\Tables\Actions\ExportAction;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
use Storage;
class InvoiceOutValidationResource extends Resource
{
@@ -54,6 +52,7 @@ class InvoiceOutValidationResource extends Resource
->label('Created By')
->default(Filament::auth()->user()?->name),
Forms\Components\Hidden::make('updated_by')
->label('Updated By')
->default(Filament::auth()->user()?->name),
])
->columns(4),
@@ -66,15 +65,18 @@ class InvoiceOutValidationResource extends Resource
->columns([
Tables\Columns\TextColumn::make('No.')
->label('No.')
->alignCenter()
->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}),
Tables\Columns\TextColumn::make('plant.code')
->label('Plant')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('qr_code')
->label('QR Code')
@@ -93,8 +95,10 @@ class InvoiceOutValidationResource extends Resource
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('created_at')
->searchable()
->label('Created At')
->dateTime()
->searchable()
->alignCenter()
->sortable(),
// ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('created_by')
@@ -103,8 +107,10 @@ class InvoiceOutValidationResource extends Resource
->alignCenter()
->sortable(),
Tables\Columns\TextColumn::make('updated_at')
->label('Updated At')
->dateTime()
->searchable()
->alignCenter()
->sortable(),
// ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_by')
@@ -114,6 +120,7 @@ class InvoiceOutValidationResource extends Resource
->sortable(),
Tables\Columns\TextColumn::make('deleted_at')
->dateTime()
->alignCenter()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
@@ -165,12 +172,10 @@ class InvoiceOutValidationResource extends Resource
$fullPath = Storage::disk('local')->path($path);
if ($fullPath && file_exists($fullPath))
{
if ($fullPath && file_exists($fullPath)) {
$rows = Excel::toArray(null, $fullPath)[0];
if ((count($rows) - 1) <= 0)
{
if ((count($rows) - 1) <= 0) {
Notification::make()
->title('Records Not Found')
->body("Import the valid 'Invoice Data' file to proceed..!")
@@ -180,6 +185,7 @@ class InvoiceOutValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
@@ -194,25 +200,32 @@ class InvoiceOutValidationResource extends Resource
$duplicateQrExcel = [];
// $duplicateQrDb = [];
foreach ($rows as $index => $row)
{
if ($index == 0) continue;
foreach ($rows as $index => $row) {
if ($index == 0) {
continue;
}
$qrCode = trim($row[1]);
$plantCode = trim($row[2]);
$scannedAt = trim($row[3]);
$scannedby = trim($row[4]);
if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
if (empty($qrCode)) $invalidqrCode[] = "Row {$index}";
if (empty($scannedAt)) $invalidScannedAt[] = "Row {$index}";
if (empty($scannedby)) $invalidScannedBy[] = "Row {$index}";
if (empty($plantCode)) {
$invalidPlantCode[] = "Row {$index}";
}
if (empty($qrCode)) {
$invalidqrCode[] = "Row {$index}";
}
if (empty($scannedAt)) {
$invalidScannedAt[] = "Row {$index}";
}
if (empty($scannedby)) {
$invalidScannedBy[] = "Row {$index}";
}
if (strlen($plantCode) < 4) {
$invalidPlantCode[] = $plantCode;
}
else if(!Plant::where('code', $plantCode)->first())
{
} elseif (! Plant::where('code', $plantCode)->first()) {
$invalidPlaCoFound[] = $plantCode;
}
@@ -237,13 +250,18 @@ class InvoiceOutValidationResource extends Resource
// }
}
if (!empty($invalidqrCode) || !empty($invalidScannedAt) || !empty($invalidScannedBy) || !empty($invalidUser))
{
if (! empty($invalidqrCode) || ! empty($invalidScannedAt) || ! empty($invalidScannedBy) || ! empty($invalidUser)) {
$errorMsg = '';
if (!empty($invalidqrCode)) $errorMsg .= 'Missing Qr code in rows: '.implode(', ', $invalidqrCode) . '<br>';
if (!empty($invalidScannedAt)) $errorMsg .= 'Missing Scanned At in rows: '.implode(', ', $invalidScannedAt) . '<br>';
if (!empty($invalidScannedBy)) $errorMsg .= 'Missing Scanned By in rows: '.implode(', ', $invalidScannedBy) . '<br>';
if (! empty($invalidqrCode)) {
$errorMsg .= 'Missing Qr code in rows: '.implode(', ', $invalidqrCode).'<br>';
}
if (! empty($invalidScannedAt)) {
$errorMsg .= 'Missing Scanned At in rows: '.implode(', ', $invalidScannedAt).'<br>';
}
if (! empty($invalidScannedBy)) {
$errorMsg .= 'Missing Scanned By in rows: '.implode(', ', $invalidScannedBy).'<br>';
}
Notification::make()
->title('Missing Mandatory Fields')
@@ -254,6 +272,7 @@ class InvoiceOutValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
@@ -267,6 +286,7 @@ class InvoiceOutValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
@@ -280,6 +300,7 @@ class InvoiceOutValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
@@ -293,11 +314,11 @@ class InvoiceOutValidationResource extends Resource
if ($disk->exists($path)) {
$disk->delete($path);
}
return;
}
if (!empty($duplicateQrExcel))
{
if (! empty($duplicateQrExcel)) {
$duplicateGroupedByPlantQr = [];
foreach ($duplicateQrExcel as $message) {// "/Document Numbers '([^']+)' already exists for Plant Code (\S+)/"
@@ -380,10 +401,11 @@ class InvoiceOutValidationResource extends Resource
DB::beginTransaction();
try
{
try {
foreach ($rows as $index => $row) {
if ($index == 0) continue;
if ($index == 0) {
continue;
}
$rowNumber = $index + 1;
@@ -430,7 +452,7 @@ class InvoiceOutValidationResource extends Resource
$record->update([
'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy,
'updated_by' => $operatorName
'updated_by' => $operatorName,
]);
$inserted = $record;
} else {
@@ -440,7 +462,7 @@ class InvoiceOutValidationResource extends Resource
'qr_code' => $qrcode,
'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy,
'created_by' => $operatorName
'created_by' => $operatorName,
]);
}
// $inserted = InvoiceOutValidation::create([
@@ -460,7 +482,7 @@ class InvoiceOutValidationResource extends Resource
$failedRecords[] = [
'row' => $rowNumber,
'qrcode' => $qrcode ?? null,
'error' => $e->getMessage()
'error' => $e->getMessage(),
];
}
}
@@ -485,9 +507,7 @@ class InvoiceOutValidationResource extends Resource
->success()
->send();
}
}
catch (\Exception $e)
{
} catch (\Exception $e) {
DB::rollBack();
Notification::make()
->title('Import Failed')