added datagrid table
This commit is contained in:
@@ -3,10 +3,14 @@
|
||||
namespace App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||
|
||||
use App\Filament\Resources\InvoiceValidationResource;
|
||||
use App\Livewire\InvoiceDataTable;
|
||||
use App\Models\InvoiceValidation;
|
||||
use App\Models\StickerMaster;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Filament\Notifications\Notification;
|
||||
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Livewire\Livewire;
|
||||
|
||||
class CreateInvoiceValidation extends CreateRecord
|
||||
{
|
||||
@@ -18,103 +22,151 @@ class CreateInvoiceValidation extends CreateRecord
|
||||
|
||||
public $invoice_data;
|
||||
|
||||
public $invoiceData = [];
|
||||
|
||||
public $scanned_quantity;
|
||||
|
||||
public $total_quantity;
|
||||
|
||||
public string $invoiceNumber;
|
||||
|
||||
|
||||
public $excel_file;
|
||||
|
||||
public function getFormActions(): array
|
||||
{
|
||||
return parent::getFormActions();
|
||||
}
|
||||
|
||||
public function processInvoice($invoiceNumber)
|
||||
{
|
||||
// Get plant_id selected in the form
|
||||
$plantId = $this->form->getState()['plant_id'];
|
||||
|
||||
$this->invoice_number = $invoiceNumber;
|
||||
|
||||
// Check if the file is uploaded
|
||||
if (!$this->excel_file) {
|
||||
Notification::make()
|
||||
->title('No File Uploaded')
|
||||
->body("Please upload an Excel file to proceed with invoice: {$invoiceNumber}.")
|
||||
->danger()
|
||||
->persistent()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
$localPath = $this->excel_file->getPath();
|
||||
|
||||
// Check if file exists
|
||||
if (!file_exists($localPath)) {
|
||||
Notification::make()
|
||||
->title('File Not Found')
|
||||
->body("No Excel file found for invoice: {$invoiceNumber}. Please ensure the file exists in your Downloads folder.")
|
||||
->danger()
|
||||
->persistent()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$rows = \Maatwebsite\Excel\Facades\Excel::toArray([], $localPath)[0] ?? [];
|
||||
array_shift($rows);
|
||||
|
||||
if (empty($rows)) {
|
||||
Notification::make()
|
||||
->title('Empty File')
|
||||
->body('The Excel file contains no data rows')
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
$validRecords = [];
|
||||
$invalidMaterials = [];
|
||||
|
||||
foreach ($rows as $row) {
|
||||
$materialCode = $row[0] ?? null;
|
||||
$serialNumber = $row[1] ?? null;
|
||||
|
||||
if (!\App\Models\StickerMaster::where('item_id', $materialCode)->exists()) {
|
||||
$invalidMaterials[] = $materialCode;
|
||||
continue;
|
||||
}
|
||||
|
||||
$validRecords[] = [
|
||||
'material_code' => $materialCode,
|
||||
'serial_number' => $serialNumber,
|
||||
'invoice_number' => $invoiceNumber,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
if (!empty($invalidMaterials)) {
|
||||
Notification::make()
|
||||
->title('Invalid Materials')
|
||||
->body('These codes not found: ' . implode(', ', array_unique($invalidMaterials)))
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
\DB::table('invoice_validations')->insert($validRecords);
|
||||
|
||||
$this->invoice_data = $validRecords;
|
||||
$this->scanned_quantity = count($validRecords);
|
||||
$this->total_quantity = count($validRecords);
|
||||
$filePath = session('uploaded_invoice_path');
|
||||
|
||||
if (!file_exists($filePath))
|
||||
{
|
||||
Notification::make()
|
||||
->title('Success')
|
||||
->body(count($validRecords) . ' records inserted successfully')
|
||||
->success()
|
||||
->send();
|
||||
} catch (\Exception $e) {
|
||||
Notification::make()
|
||||
->title('Error')
|
||||
->body($e->getMessage())
|
||||
->title('Excel File not found.')
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract filename without extension (e.g., "3RA0018732")
|
||||
$uploadedFilename = pathinfo($filePath, PATHINFO_FILENAME);
|
||||
|
||||
// Compare with invoice number
|
||||
if ($uploadedFilename !== $invoiceNumber) {
|
||||
Notification::make()
|
||||
->title("Uploaded file name does not match the invoice number.")
|
||||
// ->body("Expected: {$invoiceNumber}.xlsx, but got: {$uploadedFilename}.xlsx")
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
if ($filePath && file_exists($filePath))
|
||||
{
|
||||
// Now you can read/process the file here
|
||||
$rows = Excel::toArray(null, $filePath)[0]; //0 means excel first sheet
|
||||
|
||||
$materialCodes = [];
|
||||
$serialNumbers = [];
|
||||
|
||||
foreach ($rows as $index => $row)
|
||||
{
|
||||
if ($index === 0) continue; // Skip header
|
||||
|
||||
$materialCode = trim($row[0]);
|
||||
$serialNumber = trim($row[1]);
|
||||
|
||||
if (!empty($materialCode)) {
|
||||
$materialCodes[] = $materialCode;
|
||||
}
|
||||
if (!empty($serialNumber)) {
|
||||
$serialNumbers[] = $serialNumber;
|
||||
}
|
||||
}
|
||||
|
||||
$existingSerialNumbers = InvoiceValidation::whereIn('serial_number', $serialNumbers)->pluck('serial_number')->toArray();
|
||||
|
||||
// If there are duplicates, notify and stop the process
|
||||
if (!empty($existingSerialNumbers)) {
|
||||
Notification::make()
|
||||
->title('Duplicate Serial Numbers Found')
|
||||
->body('The following serial numbers already exist: ' . implode(', ', $existingSerialNumbers))
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
$uniqueCodes = array_unique($materialCodes);
|
||||
|
||||
$existingCodes = StickerMaster::with('item')
|
||||
->get()
|
||||
->pluck('item.code')
|
||||
->toArray();
|
||||
|
||||
$missingCodes = array_diff($uniqueCodes, $existingCodes);
|
||||
|
||||
if (!empty($missingCodes)) {
|
||||
Notification::make()
|
||||
->title('Material codes do not exist in Sticker Master')
|
||||
->body('Missing: ' . implode(', ', $missingCodes))
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
|
||||
$inserted = 0;
|
||||
foreach ($rows as $index => $row)
|
||||
{
|
||||
if ($index === 0) continue;
|
||||
|
||||
$materialCode = trim($row[0]);
|
||||
$serialNumber = trim($row[1]);
|
||||
|
||||
if (in_array($serialNumber, $existingSerialNumbers)) {
|
||||
continue; // here duplicate serial numbers are skipped and new serial numbers are inserted
|
||||
}
|
||||
|
||||
$sticker = StickerMaster::whereHas('item', function ($query) use ($materialCode) {
|
||||
$query->where('code', $materialCode); //Check if item.code matches Excel's materialCode
|
||||
})->first();
|
||||
|
||||
if ($sticker) {
|
||||
InvoiceValidation::create([
|
||||
'sticker_master_id' => $sticker->id,
|
||||
'serial_number' => $serialNumber,
|
||||
'plant_id' => $plantId,
|
||||
'invoice_number' => $invoiceNumber,
|
||||
]);
|
||||
$inserted++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($inserted > 0)
|
||||
{
|
||||
Notification::make()
|
||||
->title("Import Successful")
|
||||
->body("$inserted records were inserted.")
|
||||
->success()
|
||||
->send();
|
||||
// Dispatch the event to refresh the Livewire component
|
||||
$this->dispatch('refreshInvoiceData', invoiceNumber: $invoiceNumber);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Notification::make()
|
||||
->title("Import Failed")
|
||||
->body("No new records were inserted.")
|
||||
->danger()
|
||||
->send();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,9 +175,5 @@ class CreateInvoiceValidation extends CreateRecord
|
||||
return 'Scan Invoice Validation';
|
||||
}
|
||||
|
||||
// public function render(): View
|
||||
// {
|
||||
// return view('filament.resources.invoice-validation-resource.pages.create-invoice-validation');
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace App\Filament\Resources\InvoiceValidationResource\Pages;
|
||||
|
||||
use App\Filament\Resources\InvoiceValidationResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListInvoiceValidations extends ListRecords
|
||||
@@ -16,4 +18,12 @@ class ListInvoiceValidations extends ListRecords
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
//to hide the data from the table
|
||||
protected function getTableQuery(): \Illuminate\Database\Eloquent\Builder
|
||||
{
|
||||
return static::getResource()::getEloquentQuery()->whereRaw('1 = 0');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user