Added import panel box in invoice validation page #1497
@@ -11,6 +11,7 @@ use App\Filament\Imports\SapInvoiceValidationImporter;
|
|||||||
use App\Filament\Resources\InvoiceValidationResource\Pages;
|
use App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||||
use App\Mail\InvoiceNotification;
|
use App\Mail\InvoiceNotification;
|
||||||
use App\Models\InvoiceValidation;
|
use App\Models\InvoiceValidation;
|
||||||
|
use App\Models\PanelBoxValidation;
|
||||||
use App\Models\Item;
|
use App\Models\Item;
|
||||||
use App\Models\Plant;
|
use App\Models\Plant;
|
||||||
use App\Models\StickerMaster;
|
use App\Models\StickerMaster;
|
||||||
@@ -41,6 +42,7 @@ use Illuminate\Support\Facades\Storage;
|
|||||||
use Livewire\Livewire;
|
use Livewire\Livewire;
|
||||||
use Maatwebsite\Excel\Facades\Excel;
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
use Str;
|
use Str;
|
||||||
|
use Filament\Tables\Actions\ActionGroup;
|
||||||
|
|
||||||
class InvoiceValidationResource extends Resource
|
class InvoiceValidationResource extends Resource
|
||||||
{
|
{
|
||||||
@@ -388,6 +390,7 @@ class InvoiceValidationResource extends Resource
|
|||||||
// ->required()
|
// ->required()
|
||||||
// ->reactive()
|
// ->reactive()
|
||||||
// ]),
|
// ]),
|
||||||
|
// ActionGroup::make([
|
||||||
|
|
||||||
Tables\Actions\Action::make('Import Serial Number')
|
Tables\Actions\Action::make('Import Serial Number')
|
||||||
->label('Import Serial Invoice')
|
->label('Import Serial Invoice')
|
||||||
@@ -739,6 +742,408 @@ class InvoiceValidationResource extends Resource
|
|||||||
->visible(function () {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view import serial invoice');
|
return Filament::auth()->user()->can('view import serial invoice');
|
||||||
}),
|
}),
|
||||||
|
Tables\Actions\Action::make('Import Panel Box')
|
||||||
|
->label('Import Panel Box')
|
||||||
|
->form([
|
||||||
|
Select::make('plant_id')
|
||||||
|
// ->options(Plant::pluck('name', 'id')->toArray()) // Fetch plant names and IDs
|
||||||
|
->options(function (callable $get) {
|
||||||
|
$userHas = Filament::auth()->user()->plant_id;
|
||||||
|
|
||||||
|
return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::orderBy('code')->pluck('name', 'id')->toArray();
|
||||||
|
})
|
||||||
|
->searchable()
|
||||||
|
->label('Search by Plant Name')
|
||||||
|
->required()
|
||||||
|
->default(function () {
|
||||||
|
return optional(InvoiceValidation::latest()->first())->plant_id;
|
||||||
|
})
|
||||||
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
||||||
|
$set('invoice_panel_serial_number', null);
|
||||||
|
$plantId = $get('plant_id');
|
||||||
|
if (! $plantId) {
|
||||||
|
$set('invoice_panel_serial_number', null);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$plantCode = Plant::find($plantId)?->code ?? null;
|
||||||
|
|
||||||
|
$directory = "uploads/temp/{$plantCode}";
|
||||||
|
if ($plantId && ! Storage::disk('local')->exists($directory)) {
|
||||||
|
Storage::disk('local')->makeDirectory($directory);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->reactive(),
|
||||||
|
|
||||||
|
FileUpload::make('invoice_panel_serial_number')
|
||||||
|
->label('Choose Panel Serial Invoice')
|
||||||
|
->required()
|
||||||
|
->acceptedFileTypes([
|
||||||
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||||
|
// 'application/vnd.ms-excel', // Legacy .xls fallback if needed
|
||||||
|
])
|
||||||
|
->rules(['mimes:xlsx', 'max:50']) // Laravel validation: extension check
|
||||||
|
->maxSize(50)
|
||||||
|
->preserveFilenames() // <- this keeps the original filename
|
||||||
|
->reactive()
|
||||||
|
->storeFiles(false) // prevent auto-storing, we will store manually
|
||||||
|
->disk('local') // 'local' refers to the local storage disk defined in config/filesystems.php, typically pointing to storage/app.
|
||||||
|
->visible(fn (Get $get) => ! empty($get('plant_id')))
|
||||||
|
->directory(function (callable $get) {
|
||||||
|
$plant = Plant::find($get('plant_id'));
|
||||||
|
$plantCode = $plant?->code ?? null;
|
||||||
|
|
||||||
|
return "uploads/temp/{$plantCode}";
|
||||||
|
})
|
||||||
|
->uploadingMessage('Uploading...')
|
||||||
|
->helperText('Only .xlsx files are allowed (Excel files).'),
|
||||||
|
])
|
||||||
|
->action(function (array $data) {
|
||||||
|
$uploadedFile = $data['invoice_panel_serial_number'];
|
||||||
|
|
||||||
|
$disk = Storage::disk('local');
|
||||||
|
|
||||||
|
$plantId = $data['plant_id'];
|
||||||
|
|
||||||
|
$plant = Plant::find($plantId);
|
||||||
|
$plantCode = $plant?->code ?? null;
|
||||||
|
|
||||||
|
// Get original filename
|
||||||
|
$originalName = $uploadedFile->getClientOriginalName(); // e.g. 3RA0018732.xlsx
|
||||||
|
|
||||||
|
$extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
|
||||||
|
if ($extension != 'xlsx') {
|
||||||
|
throw new \Exception('Only .xlsx files allowed.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$originalNameOnly = strtoupper(pathinfo($originalName, PATHINFO_FILENAME));
|
||||||
|
|
||||||
|
$originalName = "{$originalNameOnly}.xlsx";
|
||||||
|
|
||||||
|
// Store manually using storeAs to keep original name
|
||||||
|
$path = $uploadedFile->storeAs("uploads/temp/{$plantCode}", $originalName, 'local'); // returns relative path
|
||||||
|
// uploads/temp/{$plantCode}/3RA0018735.xlsx
|
||||||
|
|
||||||
|
if (strlen($originalNameOnly) < 8 || ! ctype_alnum($originalNameOnly)) {
|
||||||
|
Notification::make()
|
||||||
|
->title("Panel Serial invoice number : '$originalNameOnly' should contain minimum 8 digit alpha numeric values!")
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$fullPath = Storage::disk('local')->path($path);
|
||||||
|
// /home/iot-dev/projects/pds/storage/app/private/uploads/temp/{$plantCode}/3RA0018735.xlsx
|
||||||
|
|
||||||
|
$totQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->count();
|
||||||
|
if ($totQuan > 0) {
|
||||||
|
$scanSQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->count();
|
||||||
|
if ($totQuan == $scanSQuan) {
|
||||||
|
$invoiceFirst = InvoiceValidation::with('plant')->where('invoice_number', $originalNameOnly)->first();
|
||||||
|
$plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null;
|
||||||
|
|
||||||
|
Notification::make()
|
||||||
|
->title("Panel Serial invoice number : '$originalNameOnly' already completed the scanning process for plant : '$plantName'.")
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
$invoiceFirst = InvoiceValidation::with('plant')->where('invoice_number', $originalNameOnly)->first();
|
||||||
|
// $plantCode = $invoiceFirst ? (String)$invoiceFirst->plant->code : null;
|
||||||
|
$plantName = $invoiceFirst ? (string) $invoiceFirst->plant->name : null;
|
||||||
|
$invoicePlantId = $invoiceFirst->plant_id;
|
||||||
|
if ($plantId != $invoicePlantId) {
|
||||||
|
Notification::make()
|
||||||
|
->title("Panel Serial invoice number : '$originalNameOnly' already exists for plant : '$plantName'.<br>Choose the valid 'Plant' to proceed!")
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$totQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->where('plant_id', $plantId)->count();
|
||||||
|
$scanSQuan = InvoiceValidation::where('invoice_number', $originalNameOnly)->where('scanned_status', 'Scanned')->where('plant_id', $plantId)->count();
|
||||||
|
|
||||||
|
if ($totQuan > 0 && $totQuan == $scanSQuan) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Panel Serial invoice already completed the scanning process for selected plant.')
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($fullPath && file_exists($fullPath)) {
|
||||||
|
$rows = Excel::toArray(null, $fullPath)[0];
|
||||||
|
|
||||||
|
if ((count($rows) - 1) <= 0) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Records Not Found')
|
||||||
|
->body("Import the valid 'Panel Serial Invoice' file to proceed..!")
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$invalidMatCodes = [];
|
||||||
|
$invalidSerialCodes = [];
|
||||||
|
$materialCodes = [];
|
||||||
|
$missingSerials = [];
|
||||||
|
$duplicateSerials = [];
|
||||||
|
$seenSerialNumbers = [];
|
||||||
|
$validRowsFound = false;
|
||||||
|
|
||||||
|
foreach ($rows as $index => $row) {
|
||||||
|
if ($index == 0) {
|
||||||
|
continue;
|
||||||
|
} // Skip header
|
||||||
|
|
||||||
|
$materialCode = trim($row[0]);
|
||||||
|
$serialNumber = trim($row[1]);
|
||||||
|
|
||||||
|
if (empty($materialCode) && empty($serialNumber)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($materialCode)) {
|
||||||
|
if (Str::length($materialCode) < 6 || ! ctype_alnum($materialCode)) {
|
||||||
|
$invalidMatCodes[] = $materialCode;
|
||||||
|
} else {
|
||||||
|
if (empty($serialNumber)) {
|
||||||
|
$missingSerials[] = $materialCode;
|
||||||
|
|
||||||
|
} elseif (Str::length($serialNumber) < 9 || ! ctype_alnum($serialNumber)) {
|
||||||
|
$invalidSerialCodes[] = $serialNumber;
|
||||||
|
} else {
|
||||||
|
if (in_array($serialNumber, $seenSerialNumbers)) {
|
||||||
|
$duplicateSerials[] = $serialNumber;
|
||||||
|
} else {
|
||||||
|
$seenSerialNumbers[] = $serialNumber;
|
||||||
|
$materialCodes[] = $materialCode;
|
||||||
|
$validRowsFound = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$uniqueInvalidCodes = array_unique($invalidMatCodes);
|
||||||
|
|
||||||
|
$uniqueMissingSerials = array_unique($missingSerials);
|
||||||
|
|
||||||
|
$uniqueSerialCodes = array_unique($invalidSerialCodes);
|
||||||
|
|
||||||
|
$duplicateSerialCodes = array_unique($duplicateSerials);
|
||||||
|
|
||||||
|
// Collect all unique serial numbers from the uploaded Excel
|
||||||
|
$serialNumbers = array_unique($seenSerialNumbers);
|
||||||
|
|
||||||
|
$formattedSerialNumbers = [];
|
||||||
|
|
||||||
|
$invalidPanelSerials = [];
|
||||||
|
|
||||||
|
foreach ($serialNumbers as $serialNumber) {
|
||||||
|
|
||||||
|
$serialNumber = trim($serialNumber);
|
||||||
|
|
||||||
|
// Get supplier number (first 7 digits)
|
||||||
|
$supplier = substr($serialNumber, 0, -10);
|
||||||
|
|
||||||
|
// Get remaining 10 digits
|
||||||
|
$serialPart = substr($serialNumber, -10);
|
||||||
|
|
||||||
|
// Split serial number
|
||||||
|
$formattedSerial = substr($serialPart, 0, 4) . '/'
|
||||||
|
. substr($serialPart, 4, 6);
|
||||||
|
|
||||||
|
// Check panel box validation
|
||||||
|
$panelBox = PanelBoxValidation::where('plant_id', $plantId)
|
||||||
|
->where('panel_box_serial_number', $formattedSerial)
|
||||||
|
->where('panel_box_supplier', $supplier)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($panelBox && $panelBox->status != 'Ok') {
|
||||||
|
$invalidPanelSerials[] = $formattedSerial;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (!empty($invalidPanelSerials)) {
|
||||||
|
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid Panel Box Serial Numbers')
|
||||||
|
->body(
|
||||||
|
'The following serial numbers are not OK in Panel Box validation:<br>'
|
||||||
|
. implode(', ', $invalidPanelSerials)
|
||||||
|
)
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($uniqueInvalidCodes)) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid Item Codes')
|
||||||
|
->body('The following item codes should contain minimum 6 digit alpha numeric values:<br>'.implode(', ', $uniqueInvalidCodes))
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} elseif (! empty($uniqueMissingSerials)) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Missing Serial Numbers')
|
||||||
|
->body("The following item codes doesn't have valid serial number:<br>".implode(', ', $uniqueMissingSerials))
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} elseif (! empty($uniqueSerialCodes)) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid Serial Number')
|
||||||
|
->body('The following serial numbers should contain minimum 9 digit alpha numeric values:<br>'.implode(', ', $uniqueSerialCodes))
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} elseif (! empty($duplicateSerialCodes)) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Duplicate Serial Numbers')
|
||||||
|
->body('The following serial numbers are already exist in imported excel:<br>'.implode(', ', $duplicateSerialCodes))
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} elseif (! $validRowsFound) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid Panel Serial Invoice')
|
||||||
|
->danger() // This makes the notification red to indicate an error
|
||||||
|
->body('Uploaded Excel sheet is empty or<br>contains no valid data.')
|
||||||
|
->send();
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$uniqueCodes = array_unique($materialCodes);
|
||||||
|
|
||||||
|
$matchedItems = StickerMaster::with('item')->where('plant_id', $plantId)
|
||||||
|
->whereHas('item', function ($query) use ($uniqueCodes, $plantId) {
|
||||||
|
$query->whereIn('code', $uniqueCodes)->where('plant_id', $plantId);
|
||||||
|
})
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$matchedCodes = $matchedItems->pluck('item.code')->toArray();
|
||||||
|
|
||||||
|
$missingCodes = array_diff($uniqueCodes, $matchedCodes);
|
||||||
|
|
||||||
|
if (! empty($missingCodes)) {
|
||||||
|
$missingCount = count($missingCodes);
|
||||||
|
|
||||||
|
$message = $missingCount > 10 ? "'$missingCount' item codes are not found in sticker master." : 'The following item codes are not found in sticker master:<br>'.implode(', ', $missingCodes);
|
||||||
|
|
||||||
|
Notification::make()
|
||||||
|
->title('Unknown Item Codes')
|
||||||
|
->body($message)
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check which codes have a material_type set (not null)
|
||||||
|
$invalidCodes = $matchedItems
|
||||||
|
->filter(fn ($sticker) => ! empty($sticker->material_type)) // filter invalid
|
||||||
|
->pluck('item.code')
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
if (count($invalidCodes) > 10) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid item codes found')
|
||||||
|
->body(''.count($invalidCodes).' item codes have material type.')
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} elseif (count($invalidCodes) > 0) {
|
||||||
|
Notification::make()
|
||||||
|
->title('Invalid item codes found')
|
||||||
|
->body('Material invoice Item Codes found : '.implode(', ', $invalidCodes))
|
||||||
|
->danger()
|
||||||
|
->send();
|
||||||
|
|
||||||
|
if ($disk->exists($path)) {
|
||||||
|
$disk->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
// Save full file path to session
|
||||||
|
session(['uploaded_invoice_path' => $fullPath]);
|
||||||
|
Notification::make()
|
||||||
|
->title('Panel Serial invoice imported successfully.')
|
||||||
|
->success()
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->visible(function () {
|
||||||
|
return Filament::auth()->user()->can('view import panel serial invoice');
|
||||||
|
}),
|
||||||
Tables\Actions\Action::make('Import Invoice Material')
|
Tables\Actions\Action::make('Import Invoice Material')
|
||||||
->label('Import Material Invoice')
|
->label('Import Material Invoice')
|
||||||
->form([
|
->form([
|
||||||
@@ -1151,6 +1556,7 @@ class InvoiceValidationResource extends Resource
|
|||||||
->visible(function () {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view import material invoice');
|
return Filament::auth()->user()->can('view import material invoice');
|
||||||
}),
|
}),
|
||||||
|
|
||||||
ImportAction::make('invoice_import')
|
ImportAction::make('invoice_import')
|
||||||
->label('Import Invoices')
|
->label('Import Invoices')
|
||||||
->color('warning')
|
->color('warning')
|
||||||
@@ -1165,6 +1571,11 @@ class InvoiceValidationResource extends Resource
|
|||||||
->visible(function () {
|
->visible(function () {
|
||||||
return Filament::auth()->user()->can('view import sap invoice');
|
return Filament::auth()->user()->can('view import sap invoice');
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
// ])
|
||||||
|
// ->label('Imports')
|
||||||
|
// ->icon('heroicon-m-document-text')
|
||||||
|
// ->button(),
|
||||||
ExportAction::make('serial_invoice_export')
|
ExportAction::make('serial_invoice_export')
|
||||||
->label('Export Serial Invoices')
|
->label('Export Serial Invoices')
|
||||||
->color('warning')
|
->color('warning')
|
||||||
|
|||||||
Reference in New Issue
Block a user