Merge pull request 'Refactor: Clean up imports and enhance form/table structure in InvoiceDataValidation and InvoiceOutValidation resources' (#15) from ranjith-dev into master
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s

Reviewed-on: #15
This commit was merged in pull request #15.
This commit is contained in:
2025-11-27 05:29:19 +00:00
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\Exports\InvoiceDataValidationExporter;
use App\Filament\Resources\InvoiceDataValidationResource\Pages; use App\Filament\Resources\InvoiceDataValidationResource\Pages;
use App\Filament\Resources\InvoiceDataValidationResource\RelationManagers;
use App\Models\InvoiceDataValidation; use App\Models\InvoiceDataValidation;
use App\Models\Plant;
use DB;
use Filament\Facades\Filament;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Tables; use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Table; use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope; 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 Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use App\Models\StickerMaster;
use App\Models\User;
use DB;
use Filament\Tables\Actions\ExportAction;
class InvoiceDataValidationResource extends Resource class InvoiceDataValidationResource extends Resource
{ {
@@ -67,6 +63,7 @@ class InvoiceDataValidationResource extends Resource
->label('Created By') ->label('Created By')
->default(Filament::auth()->user()?->name), ->default(Filament::auth()->user()?->name),
Forms\Components\Hidden::make('updated_by') Forms\Components\Hidden::make('updated_by')
->label('Updated By')
->default(Filament::auth()->user()?->name), ->default(Filament::auth()->user()?->name),
]); ]);
} }
@@ -77,10 +74,12 @@ class InvoiceDataValidationResource extends Resource
->columns([ ->columns([
Tables\Columns\TextColumn::make('No.') Tables\Columns\TextColumn::make('No.')
->label('No.') ->label('No.')
->alignCenter()
->getStateUsing(function ($record, $livewire, $column, $rowLoop) { ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords(); $paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration; return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}), }),
Tables\Columns\TextColumn::make('plant.code') Tables\Columns\TextColumn::make('plant.code')
@@ -100,8 +99,8 @@ class InvoiceDataValidationResource extends Resource
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('document_number') Tables\Columns\TextColumn::make('document_number')
->label('Document Number') ->label('Document Number')
->searchable()
->alignCenter() ->alignCenter()
->searchable()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('document_date') Tables\Columns\TextColumn::make('document_date')
->label('Document Date') ->label('Document Date')
@@ -127,15 +126,27 @@ class InvoiceDataValidationResource extends Resource
Tables\Columns\TextColumn::make('created_at') Tables\Columns\TextColumn::make('created_at')
->label('Created At') ->label('Created At')
->alignCenter() ->alignCenter()
->searchable()
->dateTime() ->dateTime()
->sortable() ->sortable(),
->toggleable(isToggledHiddenByDefault: true), // ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('created_by')
->label('Created By')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('updated_at') Tables\Columns\TextColumn::make('updated_at')
->label('Updated At') ->label('Updated At')
->alignCenter() ->alignCenter()
->searchable()
->dateTime() ->dateTime()
->sortable() ->sortable(),
->toggleable(isToggledHiddenByDefault: true), // ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_by')
->label('Updated By')
->alignCenter()
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('deleted_at') Tables\Columns\TextColumn::make('deleted_at')
->label('Deleted At') ->label('Deleted At')
->alignCenter() ->alignCenter()
@@ -169,7 +180,7 @@ class InvoiceDataValidationResource extends Resource
->reactive() ->reactive()
->required() ->required()
->disk('local') ->disk('local')
//->visible(fn (Get $get) => !empty($get('plant_id'))) // ->visible(fn (Get $get) => !empty($get('plant_id')))
->directory('uploads/temp'), ->directory('uploads/temp'),
]) ])
->action(function (array $data) { ->action(function (array $data) {
@@ -191,12 +202,10 @@ class InvoiceDataValidationResource extends Resource
$fullPath = Storage::disk('local')->path($path); $fullPath = Storage::disk('local')->path($path);
if ($fullPath && file_exists($fullPath)) if ($fullPath && file_exists($fullPath)) {
{
$rows = Excel::toArray(null, $fullPath)[0]; $rows = Excel::toArray(null, $fullPath)[0];
if ((count($rows) - 1) <= 0) if ((count($rows) - 1) <= 0) {
{
Notification::make() Notification::make()
->title('Records Not Found') ->title('Records Not Found')
->body("Import the valid 'Invoice Data' file to proceed..!") ->body("Import the valid 'Invoice Data' file to proceed..!")
@@ -206,6 +215,7 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
@@ -223,43 +233,40 @@ class InvoiceDataValidationResource extends Resource
$invalidLocation = []; $invalidLocation = [];
$seenPlantDoc = []; $seenPlantDoc = [];
//$duplicateEntries = []; // $duplicateEntries = [];
$duplicateEntriesExcel = []; $duplicateEntriesExcel = [];
foreach ($rows as $index => $row) foreach ($rows as $index => $row) {
{ if ($index == 0) {
if ($index == 0) continue; // Skip header continue;
} // Skip header
$DisChaDesc = trim($row[3]); $DisChaDesc = trim($row[3]);
$plantCode = trim($row[4]); $plantCode = trim($row[4]);
$CustomerCode = trim($row[5]); $CustomerCode = trim($row[5]);
$DocNo = trim($row[6]); $DocNo = trim($row[6]);
$DocDate = trim($row[7]); $DocDate = trim($row[7]);
$CusTradeName = trim($row[9]); $CusTradeName = trim($row[9]);
$CusLocation = trim($row[10]); $CusLocation = trim($row[10]);
$Location = trim($row[36]); $Location = trim($row[36]);
// if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}"; // if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}";
if (empty($DisChaDesc)){ if (empty($DisChaDesc)) {
$invalidDisChaDesc[] = "Row {$index}"; $invalidDisChaDesc[] = "Row {$index}";
} }
if (empty($CustomerCode)){ if (empty($CustomerCode)) {
$invalidCustomerCode[] = "Row {$index}"; $invalidCustomerCode[] = "Row {$index}";
} }
if (empty($DocNo)) if (empty($DocNo)) {
{
$invalidDocNo[] = "Row {$index}"; $invalidDocNo[] = "Row {$index}";
} }
if (empty($CusTradeName)) if (empty($CusTradeName)) {
{
$invalidCusTradeName[] = "Row {$index}"; $invalidCusTradeName[] = "Row {$index}";
} }
if (empty($CusLocation)) if (empty($CusLocation)) {
{
$invalidCusLocation[] = "Row {$index}"; $invalidCusLocation[] = "Row {$index}";
} }
if (empty($Location)) if (empty($Location)) {
{
$invalidLocation[] = "Row {$index}"; $invalidLocation[] = "Row {$index}";
} }
// if (empty($createdBy)) $invalidUser[] = "Row {$index}"; // if (empty($createdBy)) $invalidUser[] = "Row {$index}";
@@ -267,11 +274,9 @@ class InvoiceDataValidationResource extends Resource
if (strlen($plantCode) < 4) { if (strlen($plantCode) < 4) {
$invalidPlantCode[] = $plantCode; $invalidPlantCode[] = $plantCode;
} }
if (!is_numeric($plantCode)) { if (! is_numeric($plantCode)) {
$invalidPlantType[] = $plantCode; $invalidPlantType[] = $plantCode;
} } elseif (! Plant::where('code', $plantCode)->first()) {
else if (!Plant::where('code', $plantCode)->first())
{
$invalidPlaCoFound[] = $plantCode; $invalidPlaCoFound[] = $plantCode;
} }
@@ -288,25 +293,36 @@ class InvoiceDataValidationResource extends Resource
// $duplicateEntries[] = "Duplicate record: Document Number '{$DocNo}' already exists for Plant '{$plant->name}'"; // $duplicateEntries[] = "Duplicate record: Document Number '{$DocNo}' already exists for Plant '{$plant->name}'";
// } // }
//Also check duplicates within the same file --- // Also check duplicates within the same file ---
$uniqueKey = $plantCode . '_' . $DocNo; $uniqueKey = $plantCode.'_'.$DocNo;
if (in_array($uniqueKey, $seenPlantDoc)) { if (in_array($uniqueKey, $seenPlantDoc)) {
$duplicateEntriesExcel[] = "Duplicate in file at Row {$index}: Document Number '{$DocNo}' already exists for Plant '{$plant->name}'"; $duplicateEntriesExcel[] = "Duplicate in file at Row {$index}: Document Number '{$DocNo}' already exists for Plant '{$plant->name}'";
} }
$seenPlantDoc[] = $uniqueKey; $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 = ''; $errorMsg = '';
//if (!empty($invalidDisChaDesc)) $errorMsg .= 'Missing Distribution Channel Description in rows: ' . implode(', ', $invalidDisChaDesc) . '<br>'; // 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($invalidCustomerCode)) {
if (!empty($invalidDocNo)) $errorMsg .= 'Missing Document Number in rows: ' . implode(', ', $invalidDocNo) . '<br>'; $errorMsg .= 'Missing Customer Code in rows: '.implode(', ', $invalidCustomerCode).'<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($invalidDocNo)) {
if (!empty($invalidCusLocation)) $errorMsg .= 'Missing Customer Location in rows: ' . implode(', ', $invalidCusLocation) . '<br>'; $errorMsg .= 'Missing Document Number in rows: '.implode(', ', $invalidDocNo).'<br>';
if (!empty($invalidLocation)) $errorMsg .= 'Missing Location in rows: ' . implode(', ', $invalidLocation) . '<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() Notification::make()
->title('Missing Mandatory Fields') ->title('Missing Mandatory Fields')
@@ -317,45 +333,43 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} } elseif (! empty($invalidPlantCode)) {
else if (!empty($invalidPlantCode))
{
$invalidPlantCode = array_unique($invalidPlantCode); $invalidPlantCode = array_unique($invalidPlantCode);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
->body('The following plant codes should contain minimum 4 digits:<br>' . implode(', ', $invalidPlantCode)) ->body('The following plant codes should contain minimum 4 digits:<br>'.implode(', ', $invalidPlantCode))
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} } elseif (! empty($invalidPlantType)) {
else if (!empty($invalidPlantType))
{
$invalidPlantType = array_unique($invalidPlantType); $invalidPlantType = array_unique($invalidPlantType);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
->body('The following plant codes should contain numeric values:<br>' . implode(', ', $invalidPlantType)) ->body('The following plant codes should contain numeric values:<br>'.implode(', ', $invalidPlantType))
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} } elseif (! empty($invalidPlaCoFound)) {
else if (!empty($invalidPlaCoFound))
{
$invalidPlaCoFound = array_unique($invalidPlaCoFound); $invalidPlaCoFound = array_unique($invalidPlaCoFound);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
->body('The following plant codes not found in plants:<br>' . implode(', ', $invalidPlaCoFound)) ->body('The following plant codes not found in plants:<br>'.implode(', ', $invalidPlaCoFound))
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
@@ -401,11 +415,10 @@ class InvoiceDataValidationResource extends Resource
// return; // return;
// } // }
if (!empty($duplicateEntriesExcel)) if (! empty($duplicateEntriesExcel)) {
{
$duplicateGroupedByPlantExcel = []; $duplicateGroupedByPlantExcel = [];
foreach ($duplicateEntriesExcel as $message) {//"/Document Number '([^']+)' already exist(?:s)?(?: for Plant (.+))?/" foreach ($duplicateEntriesExcel as $message) {// "/Document Number '([^']+)' already exist(?:s)?(?: for Plant (.+))?/"
if (preg_match("/Document Number '([^']+)' already exists for Plant '([^']+)'/", $message, $matches)) { if (preg_match("/Document Number '([^']+)' already exists for Plant '([^']+)'/", $message, $matches)) {
$docNo = $matches[1]; $docNo = $matches[1];
$plantName = $matches[2] ?? 'Unknown'; $plantName = $matches[2] ?? 'Unknown';
@@ -415,8 +428,7 @@ class InvoiceDataValidationResource extends Resource
$errorMsg = 'Duplicate Document Number found in Uploaded File :<br>'; $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 // Remove duplicate document numbers for each plant
$uniqueDocNumbers = array_unique($docNumbers); $uniqueDocNumbers = array_unique($docNumbers);
$count = count($uniqueDocNumbers); $count = count($uniqueDocNumbers);
@@ -425,13 +437,13 @@ class InvoiceDataValidationResource extends Resource
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plant}</b> : {$count} Document Numbers already exist in uploaded file<br>"; $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plant}</b> : {$count} Document Numbers already exist in uploaded file<br>";
} else { } else {
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plant}</b> : '" $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plant}</b> : '"
. implode(', ', $uniqueDocNumbers) .implode(', ', $uniqueDocNumbers)
. "' already exist in uploaded file<br>"; ."' already exist in uploaded file<br>";
} }
} }
Notification::make() Notification::make()
//->title('Duplicate Document Number in Uploaded File') // ->title('Duplicate Document Number in Uploaded File')
->body($errorMsg) ->body($errorMsg)
->danger() ->danger()
->send(); ->send();
@@ -439,6 +451,7 @@ class InvoiceDataValidationResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
@@ -455,33 +468,34 @@ class InvoiceDataValidationResource extends Resource
DB::beginTransaction(); DB::beginTransaction();
try try {
{
foreach ($rows as $index => $row) { foreach ($rows as $index => $row) {
if ($index == 0) continue; if ($index == 0) {
continue;
}
$rowNumber = $index + 1; $rowNumber = $index + 1;
try { try {
$DisChaDesc = trim($row[3]); $DisChaDesc = trim($row[3]);
$plantCode = trim($row[4]); $plantCode = trim($row[4]);
$CustomerCode = trim($row[5]); $CustomerCode = trim($row[5]);
$DocNo = trim($row[6]); $DocNo = trim($row[6]);
$DocDate = trim($row[7]); $DocDate = trim($row[7]);
$CusTradeName = trim($row[9]); $CusTradeName = trim($row[9]);
$CusLocation = trim($row[10]); $CusLocation = trim($row[10]);
$Location = trim($row[36]); $Location = trim($row[36]);
if (empty($DocNo)) { if (empty($DocNo)) {
throw new \Exception("Row '{$rowNumber}' Missing QR Code"); throw new \Exception("Row '{$rowNumber}' Missing QR Code");
} }
$plant = Plant::where('code', $plantCode)->first(); $plant = Plant::where('code', $plantCode)->first();
if (!$plant) { if (! $plant) {
throw new \Exception("Invalid plant code : '{$plantCode}'"); throw new \Exception("Invalid plant code : '{$plantCode}'");
} }
if (!empty($DocDate)) { if (! empty($DocDate)) {
if (preg_match('/^\d{2}[-\/]\d{2}[-\/]\d{4}$/', $DocDate)) { if (preg_match('/^\d{2}[-\/]\d{2}[-\/]\d{4}$/', $DocDate)) {
[$day, $month, $year] = preg_split('/[-\/]/', $DocDate); [$day, $month, $year] = preg_split('/[-\/]/', $DocDate);
$formattedDate = "{$year}-{$month}-{$day}"; $formattedDate = "{$year}-{$month}-{$day}";
@@ -503,26 +517,26 @@ class InvoiceDataValidationResource extends Resource
if ($record) { if ($record) {
$record->update([ $record->update([
'distribution_channel_desc' => $DisChaDesc, 'distribution_channel_desc' => $DisChaDesc,
'customer_code' => $CustomerCode, 'customer_code' => $CustomerCode,
'document_date' => $formattedDate, 'document_date' => $formattedDate,
'customer_trade_name' => $CusTradeName, 'customer_trade_name' => $CusTradeName,
'customer_location' => $CusLocation, 'customer_location' => $CusLocation,
'location' => $Location, 'location' => $Location,
'updated_by' => $operatorName 'updated_by' => $operatorName,
]); ]);
$inserted = $record; $inserted = $record;
} else { } else {
// Record does not exist, create with 'created_by' // Record does not exist, create with 'created_by'
$inserted = InvoiceDataValidation::create([ $inserted = InvoiceDataValidation::create([
'plant_id' => $plant->id, 'plant_id' => $plant->id,
'document_number' => $DocNo, 'document_number' => $DocNo,
'distribution_channel_desc' => $DisChaDesc, 'distribution_channel_desc' => $DisChaDesc,
'customer_code' => $CustomerCode, 'customer_code' => $CustomerCode,
'document_date' => $formattedDate, 'document_date' => $formattedDate,
'customer_trade_name' => $CusTradeName, 'customer_trade_name' => $CusTradeName,
'customer_location' => $CusLocation, 'customer_location' => $CusLocation,
'location' => $Location, 'location' => $Location,
'created_by' => $operatorName 'created_by' => $operatorName,
]); ]);
} }
// $inserted = InvoiceDataValidation::create([ // $inserted = InvoiceDataValidation::create([
@@ -536,7 +550,7 @@ class InvoiceDataValidationResource extends Resource
// 'created_by' => $operatorName // 'created_by' => $operatorName
// ]); // ]);
if (!$inserted) { if (! $inserted) {
throw new \Exception("{$curStat} failed for Document Number : {$DocNo}"); throw new \Exception("{$curStat} failed for Document Number : {$DocNo}");
} }
@@ -545,7 +559,7 @@ class InvoiceDataValidationResource extends Resource
$failedRecords[] = [ $failedRecords[] = [
'row' => $rowNumber, 'row' => $rowNumber,
'document_number' => $DocNo ?? null, 'document_number' => $DocNo ?? null,
'error' => $e->getMessage() 'error' => $e->getMessage(),
]; ];
} }
} }
@@ -554,13 +568,13 @@ class InvoiceDataValidationResource extends Resource
if (count($failedRecords) > 0) { if (count($failedRecords) > 0) {
$failedSummary = collect($failedRecords) $failedSummary = collect($failedRecords)
->map(fn($f) => "Row {$f['row']} ({$f['document_number']}) : {$f['error']}") ->map(fn ($f) => "Row {$f['row']} ({$f['document_number']}) : {$f['error']}")
->take(5) // limit preview to first 5 errors ->take(5) // limit preview to first 5 errors
->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("'{$successCount}' records inserted. ".count($failedRecords)." failed.\n\n{$failedSummary}")
->warning() ->warning()
->send(); ->send();
} else { } else {
@@ -570,9 +584,7 @@ class InvoiceDataValidationResource extends Resource
->success() ->success()
->send(); ->send();
} }
} } catch (\Exception $e) {
catch (\Exception $e)
{
DB::rollBack(); DB::rollBack();
Notification::make() Notification::make()
->title('Import Failed') ->title('Import Failed')
@@ -582,14 +594,14 @@ class InvoiceDataValidationResource extends Resource
} }
} }
}) })
->visible(function() { ->visible(function () {
return Filament::auth()->user()->can('view import invoice data validation'); return Filament::auth()->user()->can('view import invoice data validation');
}), }),
ExportAction::make() ExportAction::make()
->label('Export Invoice Data') ->label('Export Invoice Data')
->color('warning') ->color('warning')
->exporter(InvoiceDataValidationExporter::class) ->exporter(InvoiceDataValidationExporter::class)
->visible(function() { ->visible(function () {
return Filament::auth()->user()->can('view export invoice data validation'); return Filament::auth()->user()->can('view export invoice data validation');
}), }),
]); ]);

View File

@@ -4,27 +4,25 @@ namespace App\Filament\Resources;
use App\Filament\Exports\InvoiceOutValidationExporter; use App\Filament\Exports\InvoiceOutValidationExporter;
use App\Filament\Resources\InvoiceOutValidationResource\Pages; use App\Filament\Resources\InvoiceOutValidationResource\Pages;
use App\Filament\Resources\InvoiceOutValidationResource\RelationManagers;
use App\Models\InvoiceOutValidation; use App\Models\InvoiceOutValidation;
use App\Models\Plant; use App\Models\Plant;
use App\Models\User;
use Carbon\Carbon; use Carbon\Carbon;
use DB; use DB;
use Filament\Facades\Filament;
use Filament\Forms; use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Section;
use Filament\Forms\Form; use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource; use Filament\Resources\Resource;
use Filament\Tables; use Filament\Tables;
use Filament\Tables\Actions\ExportAction;
use Filament\Tables\Table; use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope; 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 Maatwebsite\Excel\Facades\Excel;
use Storage;
use Filament\Tables\Actions\ExportAction;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate; use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
use Storage;
class InvoiceOutValidationResource extends Resource class InvoiceOutValidationResource extends Resource
{ {
@@ -39,24 +37,25 @@ class InvoiceOutValidationResource extends Resource
return $form return $form
->schema([ ->schema([
Section::make('') Section::make('')
->schema([ ->schema([
Forms\Components\Select::make('plant_id') Forms\Components\Select::make('plant_id')
->label('Plant') ->label('Plant')
->relationship('plant', 'name') ->relationship('plant', 'name')
->required(), ->required(),
Forms\Components\TextInput::make('qr_code') Forms\Components\TextInput::make('qr_code')
->label('QR Code'), ->label('QR Code'),
Forms\Components\DateTimePicker::make('scanned_at') Forms\Components\DateTimePicker::make('scanned_at')
->label('Scanned At'), ->label('Scanned At'),
Forms\Components\TextInput::make('scanned_by') Forms\Components\TextInput::make('scanned_by')
->label('Scanned By'), ->label('Scanned By'),
Forms\Components\Hidden::make('created_by') Forms\Components\Hidden::make('created_by')
->label('Created By') ->label('Created By')
->default(Filament::auth()->user()?->name), ->default(Filament::auth()->user()?->name),
Forms\Components\Hidden::make('updated_by') Forms\Components\Hidden::make('updated_by')
->default(Filament::auth()->user()?->name), ->label('Updated By')
]) ->default(Filament::auth()->user()?->name),
->columns(4), ])
->columns(4),
]); ]);
} }
@@ -66,15 +65,18 @@ class InvoiceOutValidationResource extends Resource
->columns([ ->columns([
Tables\Columns\TextColumn::make('No.') Tables\Columns\TextColumn::make('No.')
->label('No.') ->label('No.')
->alignCenter()
->getStateUsing(function ($record, $livewire, $column, $rowLoop) { ->getStateUsing(function ($record, $livewire, $column, $rowLoop) {
$paginator = $livewire->getTableRecords(); $paginator = $livewire->getTableRecords();
$perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10;
$currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1;
return ($currentPage - 1) * $perPage + $rowLoop->iteration; return ($currentPage - 1) * $perPage + $rowLoop->iteration;
}), }),
Tables\Columns\TextColumn::make('plant.code') Tables\Columns\TextColumn::make('plant.code')
->label('Plant') ->label('Plant')
->alignCenter() ->alignCenter()
->searchable()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('qr_code') Tables\Columns\TextColumn::make('qr_code')
->label('QR Code') ->label('QR Code')
@@ -93,20 +95,24 @@ class InvoiceOutValidationResource extends Resource
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('created_at') Tables\Columns\TextColumn::make('created_at')
->searchable() ->label('Created At')
->dateTime() ->dateTime()
->searchable()
->alignCenter()
->sortable(), ->sortable(),
//->toggleable(isToggledHiddenByDefault: true), // ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('created_by') Tables\Columns\TextColumn::make('created_by')
->label('Created By') ->label('Created By')
->searchable() ->searchable()
->alignCenter() ->alignCenter()
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('updated_at') Tables\Columns\TextColumn::make('updated_at')
->label('Updated At')
->dateTime() ->dateTime()
->searchable() ->searchable()
->alignCenter()
->sortable(), ->sortable(),
// ->toggleable(isToggledHiddenByDefault: true), // ->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_by') Tables\Columns\TextColumn::make('updated_by')
->label('Updated By') ->label('Updated By')
->searchable() ->searchable()
@@ -114,6 +120,7 @@ class InvoiceOutValidationResource extends Resource
->sortable(), ->sortable(),
Tables\Columns\TextColumn::make('deleted_at') Tables\Columns\TextColumn::make('deleted_at')
->dateTime() ->dateTime()
->alignCenter()
->sortable() ->sortable()
->toggleable(isToggledHiddenByDefault: true), ->toggleable(isToggledHiddenByDefault: true),
]) ])
@@ -143,7 +150,7 @@ class InvoiceOutValidationResource extends Resource
->reactive() ->reactive()
->required() ->required()
->disk('local') ->disk('local')
//->visible(fn (Get $get) => !empty($get('plant_id'))) // ->visible(fn (Get $get) => !empty($get('plant_id')))
->directory('uploads/temp'), ->directory('uploads/temp'),
]) ])
->action(function (array $data) { ->action(function (array $data) {
@@ -165,12 +172,10 @@ class InvoiceOutValidationResource extends Resource
$fullPath = Storage::disk('local')->path($path); $fullPath = Storage::disk('local')->path($path);
if ($fullPath && file_exists($fullPath)) if ($fullPath && file_exists($fullPath)) {
{
$rows = Excel::toArray(null, $fullPath)[0]; $rows = Excel::toArray(null, $fullPath)[0];
if ((count($rows) - 1) <= 0) if ((count($rows) - 1) <= 0) {
{
Notification::make() Notification::make()
->title('Records Not Found') ->title('Records Not Found')
->body("Import the valid 'Invoice Data' file to proceed..!") ->body("Import the valid 'Invoice Data' file to proceed..!")
@@ -180,6 +185,7 @@ class InvoiceOutValidationResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
@@ -192,27 +198,34 @@ class InvoiceOutValidationResource extends Resource
$userNotFound = []; $userNotFound = [];
$seenPlantQr = []; $seenPlantQr = [];
$duplicateQrExcel = []; $duplicateQrExcel = [];
//$duplicateQrDb = []; // $duplicateQrDb = [];
foreach ($rows as $index => $row) foreach ($rows as $index => $row) {
{ if ($index == 0) {
if ($index == 0) continue; continue;
}
$qrCode = trim($row[1]); $qrCode = trim($row[1]);
$plantCode = trim($row[2]); $plantCode = trim($row[2]);
$scannedAt = trim($row[3]); $scannedAt = trim($row[3]);
$scannedby = trim($row[4]); $scannedby = trim($row[4]);
if (empty($plantCode)) $invalidPlantCode[] = "Row {$index}"; if (empty($plantCode)) {
if (empty($qrCode)) $invalidqrCode[] = "Row {$index}"; $invalidPlantCode[] = "Row {$index}";
if (empty($scannedAt)) $invalidScannedAt[] = "Row {$index}"; }
if (empty($scannedby)) $invalidScannedBy[] = "Row {$index}"; if (empty($qrCode)) {
$invalidqrCode[] = "Row {$index}";
}
if (empty($scannedAt)) {
$invalidScannedAt[] = "Row {$index}";
}
if (empty($scannedby)) {
$invalidScannedBy[] = "Row {$index}";
}
if (strlen($plantCode) < 4) { if (strlen($plantCode) < 4) {
$invalidPlantCode[] = $plantCode; $invalidPlantCode[] = $plantCode;
} } elseif (! Plant::where('code', $plantCode)->first()) {
else if(!Plant::where('code', $plantCode)->first())
{
$invalidPlaCoFound[] = $plantCode; $invalidPlaCoFound[] = $plantCode;
} }
@@ -220,7 +233,7 @@ class InvoiceOutValidationResource extends Resource
$plantId = $plant->id; $plantId = $plant->id;
$uniqueKey = $plantCode . '_' . $qrCode; $uniqueKey = $plantCode.'_'.$qrCode;
if (in_array($uniqueKey, $seenPlantQr)) { if (in_array($uniqueKey, $seenPlantQr)) {
$duplicateQrExcel[] = "Duplicate in file at Row {$index}: Document Number '{$qrCode}' already exists for Plant '{$plant->name}'"; $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 = ''; $errorMsg = '';
if (!empty($invalidqrCode)) $errorMsg .= 'Missing Qr code in rows: '.implode(', ', $invalidqrCode) . '<br>'; if (! empty($invalidqrCode)) {
if (!empty($invalidScannedAt)) $errorMsg .= 'Missing Scanned At in rows: '.implode(', ', $invalidScannedAt) . '<br>'; $errorMsg .= 'Missing Qr code in rows: '.implode(', ', $invalidqrCode).'<br>';
if (!empty($invalidScannedBy)) $errorMsg .= 'Missing Scanned By in rows: '.implode(', ', $invalidScannedBy) . '<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() Notification::make()
->title('Missing Mandatory Fields') ->title('Missing Mandatory Fields')
@@ -254,53 +272,56 @@ class InvoiceOutValidationResource extends Resource
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
if (!empty($invalidPlantCode)) { if (! empty($invalidPlantCode)) {
$invalidPlantCode = array_unique($invalidPlantCode); $invalidPlantCode = array_unique($invalidPlantCode);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
->body('The following plant codes should contain minimum 4 digits:<br>' . implode(', ', $invalidPlantCode)) ->body('The following plant codes should contain minimum 4 digits:<br>'.implode(', ', $invalidPlantCode))
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
if (!empty($invalidPlaCoFound)) { if (! empty($invalidPlaCoFound)) {
$invalidPlaCoFound = array_unique($invalidPlaCoFound); $invalidPlaCoFound = array_unique($invalidPlaCoFound);
Notification::make() Notification::make()
->title('Invalid Plant Codes') ->title('Invalid Plant Codes')
->body('The following plant codes not found in plants:<br>' . implode(', ', $invalidPlaCoFound)) ->body('The following plant codes not found in plants:<br>'.implode(', ', $invalidPlaCoFound))
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
if (!empty($userNotFound)) { if (! empty($userNotFound)) {
$userNotFound = array_unique($userNotFound); $userNotFound = array_unique($userNotFound);
Notification::make() Notification::make()
->title('Invalid User') ->title('Invalid User')
->body('The following user not found:<br>' . implode(', ', $userNotFound)) ->body('The following user not found:<br>'.implode(', ', $userNotFound))
->danger() ->danger()
->send(); ->send();
if ($disk->exists($path)) { if ($disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
return; return;
} }
if (!empty($duplicateQrExcel)) if (! empty($duplicateQrExcel)) {
{
$duplicateGroupedByPlantQr = []; $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)) { if (preg_match("/Document Number '([^']+)' already exists for Plant '([^']+)'/", $message, $matches)) {
$qrCode = $matches[1]; $qrCode = $matches[1];
$plantCode = $matches[2]; $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>"; $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : {$count} Document Numbers already exist in uploaded file<br>";
} else { } else {
$errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : '" $errorMsg .= "Duplicate Document Numbers for Plant <b>{$plantCode}</b> : '"
. implode(', ', $uniqueQrCodes) .implode(', ', $uniqueQrCodes)
. "' already exist in uploaded file<br>"; ."' already exist in uploaded file<br>";
} }
} }
Notification::make() Notification::make()
//->title('Duplicate Document Number in Uploaded File') // ->title('Duplicate Document Number in Uploaded File')
->body($errorMsg) ->body($errorMsg)
->danger() ->danger()
->send(); ->send();
@@ -380,15 +401,16 @@ class InvoiceOutValidationResource extends Resource
DB::beginTransaction(); DB::beginTransaction();
try try {
{
foreach ($rows as $index => $row) { foreach ($rows as $index => $row) {
if ($index == 0) continue; if ($index == 0) {
continue;
}
$rowNumber = $index + 1; $rowNumber = $index + 1;
try { try {
$qrcode = trim($row[1]); $qrcode = trim($row[1]);
$plantCode = trim($row[2]); $plantCode = trim($row[2]);
$scannedAt = trim($row[3]); $scannedAt = trim($row[3]);
$scannedBy = trim($row[4]); $scannedBy = trim($row[4]);
@@ -398,12 +420,12 @@ class InvoiceOutValidationResource extends Resource
} }
$plant = Plant::where('code', $plantCode)->first(); $plant = Plant::where('code', $plantCode)->first();
if (!$plant) { if (! $plant) {
throw new \Exception("Invalid plant code : '{$plantCode}'"); throw new \Exception("Invalid plant code : '{$plantCode}'");
} }
$formattedDate = null; $formattedDate = null;
if (!empty($scannedAt)) { if (! empty($scannedAt)) {
try { try {
// $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt) // $formattedDate = Carbon::createFromFormat('d-m-Y H:i:s', $scannedAt)
// ->format('Y-m-d H:i:s'); // ->format('Y-m-d H:i:s');
@@ -430,7 +452,7 @@ class InvoiceOutValidationResource extends Resource
$record->update([ $record->update([
'scanned_at' => $formattedDate, 'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy, 'scanned_by' => $scannedBy,
'updated_by' => $operatorName 'updated_by' => $operatorName,
]); ]);
$inserted = $record; $inserted = $record;
} else { } else {
@@ -440,7 +462,7 @@ class InvoiceOutValidationResource extends Resource
'qr_code' => $qrcode, 'qr_code' => $qrcode,
'scanned_at' => $formattedDate, 'scanned_at' => $formattedDate,
'scanned_by' => $scannedBy, 'scanned_by' => $scannedBy,
'created_by' => $operatorName 'created_by' => $operatorName,
]); ]);
} }
// $inserted = InvoiceOutValidation::create([ // $inserted = InvoiceOutValidation::create([
@@ -451,7 +473,7 @@ class InvoiceOutValidationResource extends Resource
// 'created_by' => $operatorName // 'created_by' => $operatorName
// ]); // ]);
if (!$inserted) { if (! $inserted) {
throw new \Exception("{$curStat} failed for QR : {$qrcode}"); throw new \Exception("{$curStat} failed for QR : {$qrcode}");
} }
@@ -460,7 +482,7 @@ class InvoiceOutValidationResource extends Resource
$failedRecords[] = [ $failedRecords[] = [
'row' => $rowNumber, 'row' => $rowNumber,
'qrcode' => $qrcode ?? null, 'qrcode' => $qrcode ?? null,
'error' => $e->getMessage() 'error' => $e->getMessage(),
]; ];
} }
} }
@@ -469,13 +491,13 @@ class InvoiceOutValidationResource extends Resource
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) // limit preview to first 5 errors
->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("'{$successCount}' records inserted. ".count($failedRecords)." failed.\n\n{$failedSummary}")
->warning() ->warning()
->send(); ->send();
} else { } else {
@@ -485,9 +507,7 @@ class InvoiceOutValidationResource extends Resource
->success() ->success()
->send(); ->send();
} }
} } catch (\Exception $e) {
catch (\Exception $e)
{
DB::rollBack(); DB::rollBack();
Notification::make() Notification::make()
->title('Import Failed') ->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'); return Filament::auth()->user()->can('view import invoice out validation');
}), }),
ExportAction::make() ExportAction::make()
->label('Export Invoice Out Data') ->label('Export Invoice Out Data')
->color('warning') ->color('warning')
->exporter(InvoiceOutValidationExporter::class) ->exporter(InvoiceOutValidationExporter::class)
->visible(function() { ->visible(function () {
return Filament::auth()->user()->can('view export invoice out validation'); return Filament::auth()->user()->can('view export invoice out validation');
}), }),
]); ]);