ifnex/04_Laravel/app/Filament/Pages/ImportRatesPage.php
Kazem Alghasi e9a0c731c2 feat(admin): implement currency management and rate import system
Introduce a new currency management module including a dedicated
resource, database migration, and a custom Filament page for importing
exchange rates. Additionally, enhance the ShipmentResource table with
improved column visibility, sorting, and route display, and integrate
shipment update notifications.
2026-08-24 01:18:34 +03:30

115 lines
3.5 KiB
PHP

<?php
namespace App\Filament\Pages;
use App\Imports\ShippingRatesImport;
use Filament\Actions\Action;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Section;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Maatwebsite\Excel\Facades\Excel;
class ImportRatesPage extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationIcon = 'heroicon-o-arrow-up-tray';
protected static ?string $navigationLabel = 'آپلود نرخ‌ها';
protected static ?string $navigationGroup = 'تنظیمات';
protected static ?int $navigationSort = 10;
protected static string $view = 'filament.pages.import-rates';
public ?array $data = [];
public function mount(): void
{
$this->form->fill();
}
public function getFormSchema(): array
{
return [
Section::make('فایل اکسل نرخ‌ها')
->description('فایل اکسل حاوی شیت‌های Export Rate, Import Rate و DocEco را آپلود کنید.')
->schema([
FileUpload::make('file')
->label('فایل اکسل')
->acceptedFileTypes([
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
])
->maxSize(5120)
->required()
->disk('local')
->directory('imports')
->preserveFilenames(),
]),
];
}
public function import(): void
{
$data = $this->form->getState();
if (empty($data['file'])) {
Notification::make()
->title('خطا')
->body('لطفاً فایل اکسل را انتخاب کنید.')
->danger()
->send();
return;
}
$filePath = storage_path('app/' . $data['file']);
if (!file_exists($filePath)) {
Notification::make()
->title('خطا')
->body('فایل پیدا نشد.')
->danger()
->send();
return;
}
try {
$import = new ShippingRatesImport();
Excel::import($import, $filePath);
$stats = [];
foreach ($import->sheets() as $name => $sheet) {
if (method_exists($sheet, 'getStats')) {
$s = $sheet->getStats();
$stats[] = "{$name}: {$s['imported']} imported, {$s['skipped']} skipped";
}
}
Notification::make()
->title('ایمپورت موفق')
->body(implode(' | ', $stats))
->success()
->send();
$this->form->fill();
} catch (\Throwable $e) {
Notification::make()
->title('خطا در ایمپورت')
->body($e->getMessage())
->danger()
->send();
}
}
protected function getFormActions(): array
{
return [
Action::make('import')
->label('شروع ایمپورت')
->icon('heroicon-o-arrow-up-tray')
->color('success')
->action('import'),
];
}
}