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
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:
@@ -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
|
||||
{
|
||||
@@ -39,24 +37,25 @@ class InvoiceOutValidationResource extends Resource
|
||||
return $form
|
||||
->schema([
|
||||
Section::make('')
|
||||
->schema([
|
||||
Forms\Components\Select::make('plant_id')
|
||||
->label('Plant')
|
||||
->relationship('plant', 'name')
|
||||
->required(),
|
||||
Forms\Components\TextInput::make('qr_code')
|
||||
->label('QR Code'),
|
||||
Forms\Components\DateTimePicker::make('scanned_at')
|
||||
->label('Scanned At'),
|
||||
Forms\Components\TextInput::make('scanned_by')
|
||||
->label('Scanned By'),
|
||||
Forms\Components\Hidden::make('created_by')
|
||||
->label('Created By')
|
||||
->default(Filament::auth()->user()?->name),
|
||||
Forms\Components\Hidden::make('updated_by')
|
||||
->default(Filament::auth()->user()?->name),
|
||||
])
|
||||
->columns(4),
|
||||
->schema([
|
||||
Forms\Components\Select::make('plant_id')
|
||||
->label('Plant')
|
||||
->relationship('plant', 'name')
|
||||
->required(),
|
||||
Forms\Components\TextInput::make('qr_code')
|
||||
->label('QR Code'),
|
||||
Forms\Components\DateTimePicker::make('scanned_at')
|
||||
->label('Scanned At'),
|
||||
Forms\Components\TextInput::make('scanned_by')
|
||||
->label('Scanned By'),
|
||||
Forms\Components\Hidden::make('created_by')
|
||||
->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,20 +95,24 @@ class InvoiceOutValidationResource extends Resource
|
||||
->alignCenter()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('created_at')
|
||||
->searchable()
|
||||
->label('Created At')
|
||||
->dateTime()
|
||||
->searchable()
|
||||
->alignCenter()
|
||||
->sortable(),
|
||||
//->toggleable(isToggledHiddenByDefault: true),
|
||||
// ->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('created_by')
|
||||
->label('Created By')
|
||||
->searchable()
|
||||
->alignCenter()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('updated_at')
|
||||
->label('Updated At')
|
||||
->dateTime()
|
||||
->searchable()
|
||||
->alignCenter()
|
||||
->sortable(),
|
||||
// ->toggleable(isToggledHiddenByDefault: true),
|
||||
// ->toggleable(isToggledHiddenByDefault: true),
|
||||
Tables\Columns\TextColumn::make('updated_by')
|
||||
->label('Updated By')
|
||||
->searchable()
|
||||
@@ -114,6 +120,7 @@ class InvoiceOutValidationResource extends Resource
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('deleted_at')
|
||||
->dateTime()
|
||||
->alignCenter()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
@@ -143,7 +150,7 @@ class InvoiceOutValidationResource extends Resource
|
||||
->reactive()
|
||||
->required()
|
||||
->disk('local')
|
||||
//->visible(fn (Get $get) => !empty($get('plant_id')))
|
||||
// ->visible(fn (Get $get) => !empty($get('plant_id')))
|
||||
->directory('uploads/temp'),
|
||||
])
|
||||
->action(function (array $data) {
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -192,27 +198,34 @@ class InvoiceOutValidationResource extends Resource
|
||||
$userNotFound = [];
|
||||
$seenPlantQr = [];
|
||||
$duplicateQrExcel = [];
|
||||
//$duplicateQrDb = [];
|
||||
// $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]);
|
||||
$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;
|
||||
}
|
||||
|
||||
@@ -220,7 +233,7 @@ class InvoiceOutValidationResource extends Resource
|
||||
|
||||
$plantId = $plant->id;
|
||||
|
||||
$uniqueKey = $plantCode . '_' . $qrCode;
|
||||
$uniqueKey = $plantCode.'_'.$qrCode;
|
||||
|
||||
if (in_array($uniqueKey, $seenPlantQr)) {
|
||||
$duplicateQrExcel[] = "Duplicate in file at Row {$index}: Document Number '{$qrCode}' already exists for Plant '{$plant->name}'";
|
||||
@@ -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,53 +272,56 @@ class InvoiceOutValidationResource extends Resource
|
||||
if ($disk->exists($path)) {
|
||||
$disk->delete($path);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($invalidPlantCode)) {
|
||||
if (! empty($invalidPlantCode)) {
|
||||
$invalidPlantCode = array_unique($invalidPlantCode);
|
||||
Notification::make()
|
||||
->title('Invalid Plant Codes')
|
||||
->body('The following plant codes should contain minimum 4 digits:<br>' . implode(', ', $invalidPlantCode))
|
||||
->danger()
|
||||
->send();
|
||||
->title('Invalid Plant Codes')
|
||||
->body('The following plant codes should contain minimum 4 digits:<br>'.implode(', ', $invalidPlantCode))
|
||||
->danger()
|
||||
->send();
|
||||
if ($disk->exists($path)) {
|
||||
$disk->delete($path);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($invalidPlaCoFound)) {
|
||||
if (! empty($invalidPlaCoFound)) {
|
||||
$invalidPlaCoFound = array_unique($invalidPlaCoFound);
|
||||
Notification::make()
|
||||
->title('Invalid Plant Codes')
|
||||
->body('The following plant codes not found in plants:<br>' . implode(', ', $invalidPlaCoFound))
|
||||
->danger()
|
||||
->send();
|
||||
->title('Invalid Plant Codes')
|
||||
->body('The following plant codes not found in plants:<br>'.implode(', ', $invalidPlaCoFound))
|
||||
->danger()
|
||||
->send();
|
||||
if ($disk->exists($path)) {
|
||||
$disk->delete($path);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!empty($userNotFound)) {
|
||||
if (! empty($userNotFound)) {
|
||||
$userNotFound = array_unique($userNotFound);
|
||||
Notification::make()
|
||||
->title('Invalid User')
|
||||
->body('The following user not found:<br>' . implode(', ', $userNotFound))
|
||||
->danger()
|
||||
->send();
|
||||
->title('Invalid User')
|
||||
->body('The following user not found:<br>'.implode(', ', $userNotFound))
|
||||
->danger()
|
||||
->send();
|
||||
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+)/"
|
||||
foreach ($duplicateQrExcel as $message) {// "/Document Numbers '([^']+)' already exists for Plant Code (\S+)/"
|
||||
if (preg_match("/Document Number '([^']+)' already exists for Plant '([^']+)'/", $message, $matches)) {
|
||||
$qrCode = $matches[1];
|
||||
$plantCode = $matches[2];
|
||||
@@ -318,13 +339,13 @@ class InvoiceOutValidationResource extends Resource
|
||||
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : {$count} Document Numbers already exist in uploaded file<br>";
|
||||
} else {
|
||||
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : '"
|
||||
. implode(', ', $uniqueQrCodes)
|
||||
. "' already exist in uploaded file<br>";
|
||||
.implode(', ', $uniqueQrCodes)
|
||||
."' already exist in uploaded file<br>";
|
||||
}
|
||||
}
|
||||
|
||||
Notification::make()
|
||||
//->title('Duplicate Document Number in Uploaded File')
|
||||
// ->title('Duplicate Document Number in Uploaded File')
|
||||
->body($errorMsg)
|
||||
->danger()
|
||||
->send();
|
||||
@@ -380,15 +401,16 @@ class InvoiceOutValidationResource extends Resource
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
foreach ($rows as $index => $row) {
|
||||
if ($index == 0) continue;
|
||||
if ($index == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rowNumber = $index + 1;
|
||||
|
||||
try {
|
||||
$qrcode = trim($row[1]);
|
||||
$qrcode = trim($row[1]);
|
||||
$plantCode = trim($row[2]);
|
||||
$scannedAt = trim($row[3]);
|
||||
$scannedBy = trim($row[4]);
|
||||
@@ -398,12 +420,12 @@ class InvoiceOutValidationResource extends Resource
|
||||
}
|
||||
|
||||
$plant = Plant::where('code', $plantCode)->first();
|
||||
if (!$plant) {
|
||||
if (! $plant) {
|
||||
throw new \Exception("Invalid plant code : '{$plantCode}'");
|
||||
}
|
||||
|
||||
$formattedDate = null;
|
||||
if (!empty($scannedAt)) {
|
||||
if (! empty($scannedAt)) {
|
||||
try {
|
||||
// $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
|
||||
// ->format('Y-m-d H:i:s');
|
||||
@@ -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([
|
||||
@@ -451,7 +473,7 @@ class InvoiceOutValidationResource extends Resource
|
||||
// 'created_by' => $operatorName
|
||||
// ]);
|
||||
|
||||
if (!$inserted) {
|
||||
if (! $inserted) {
|
||||
throw new \Exception("{$curStat} failed for QR : {$qrcode}");
|
||||
}
|
||||
|
||||
@@ -460,7 +482,7 @@ class InvoiceOutValidationResource extends Resource
|
||||
$failedRecords[] = [
|
||||
'row' => $rowNumber,
|
||||
'qrcode' => $qrcode ?? null,
|
||||
'error' => $e->getMessage()
|
||||
'error' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -469,13 +491,13 @@ class InvoiceOutValidationResource extends Resource
|
||||
|
||||
if (count($failedRecords) > 0) {
|
||||
$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
|
||||
->implode("\n");
|
||||
|
||||
Notification::make()
|
||||
->title('Partial Import Warning')
|
||||
->body("'{$successCount}' records inserted. " . count($failedRecords) . " failed.\n\n{$failedSummary}")
|
||||
->body("'{$successCount}' records inserted. ".count($failedRecords)." failed.\n\n{$failedSummary}")
|
||||
->warning()
|
||||
->send();
|
||||
} else {
|
||||
@@ -485,9 +507,7 @@ class InvoiceOutValidationResource extends Resource
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Notification::make()
|
||||
->title('Import Failed')
|
||||
@@ -497,14 +517,14 @@ class InvoiceOutValidationResource extends Resource
|
||||
}
|
||||
}
|
||||
})
|
||||
->visible(function() {
|
||||
->visible(function () {
|
||||
return Filament::auth()->user()->can('view import invoice out validation');
|
||||
}),
|
||||
ExportAction::make()
|
||||
->label('Export Invoice Out Data')
|
||||
->color('warning')
|
||||
->exporter(InvoiceOutValidationExporter::class)
|
||||
->visible(function() {
|
||||
->visible(function () {
|
||||
return Filament::auth()->user()->can('view export invoice out validation');
|
||||
}),
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user