132 lines
3.8 KiB
PHP
132 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\InvoiceValidationResource\Pages;
|
|
|
|
use App\Filament\Resources\InvoiceValidationResource;
|
|
use Illuminate\Contracts\View\View;
|
|
use Filament\Resources\Pages\CreateRecord;
|
|
use Filament\Notifications\Notification;
|
|
|
|
|
|
class CreateInvoiceValidation extends CreateRecord
|
|
{
|
|
protected static string $resource = InvoiceValidationResource::class;
|
|
|
|
protected static string $view = 'filament.resources.invoice-validation-resource.pages.create-invoice-validation';
|
|
|
|
public $invoice_number;
|
|
|
|
public $invoice_data;
|
|
|
|
public $scanned_quantity;
|
|
|
|
public $total_quantity;
|
|
|
|
|
|
public $excel_file;
|
|
|
|
public function processInvoice($invoiceNumber)
|
|
{
|
|
|
|
$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);
|
|
|
|
Notification::make()
|
|
->title('Success')
|
|
->body(count($validRecords) . ' records inserted successfully')
|
|
->success()
|
|
->send();
|
|
} catch (\Exception $e) {
|
|
Notification::make()
|
|
->title('Error')
|
|
->body($e->getMessage())
|
|
->danger()
|
|
->send();
|
|
}
|
|
}
|
|
|
|
public function getHeading(): string
|
|
{
|
|
return 'Scan Invoice Validation';
|
|
}
|
|
|
|
// public function render(): View
|
|
// {
|
|
// return view('filament.resources.invoice-validation-resource.pages.create-invoice-validation');
|
|
// }
|
|
|
|
}
|