- Fix 'File not found' error in ImportRates page by using Storage facade to resolve the actual path on the 'local' disk (which points to storage/app/private, not storage/app). - Deprecate the old ImportRatesPage (2-zone format) — hide from navigation and redirect anyone hitting its URL to /admin/rate-import. - Update the template download route to serve the new 4-quadrant IFNEX_Rate_Template.xlsx from public/templates/ instead of the old ShippingRatesTemplateExport (2-zone format). - Update rate-import.blade.php to use the named route for the download link. - Add public/templates/IFNEX_Rate_Template.xlsx for direct web access.
111 lines
3.6 KiB
PHP
111 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Pages;
|
||
|
||
use App\Exports\ShippingRatesTemplateExport;
|
||
use App\Imports\ShippingRatesImport;
|
||
use Filament\Forms\Components\FileUpload;
|
||
use Filament\Forms\Concerns\InteractsWithForms;
|
||
use Filament\Forms\Contracts\HasForms;
|
||
use Filament\Forms\Form;
|
||
use Filament\Notifications\Notification;
|
||
use Filament\Pages\Page;
|
||
use Maatwebsite\Excel\Facades\Excel;
|
||
|
||
/**
|
||
* ⚠️ DEPRECATED — این صفحه قدیمی برای فرمت ۲-زونی است.
|
||
*
|
||
* با معماری ۴-رُبعی جدید، ایمپورت نرخها از طریق Resource جدید انجام میشود:
|
||
* App\Filament\Resources\RateImportResource → URL: /admin/rate-import
|
||
*
|
||
* این صفحه فقط برای backward-compatibility باقی مانده و در منوی ناوبری
|
||
* نمایش داده نمیشود. اگر کاربر مستقیم URL قدیمی را باز کرد، به صفحه جدید
|
||
* ریدایرکت میشود.
|
||
*/
|
||
class ImportRatesPage extends Page implements HasForms
|
||
{
|
||
use InteractsWithForms;
|
||
|
||
protected static ?string $navigationIcon = 'heroicon-o-arrow-up-on-square';
|
||
protected static ?string $navigationLabel = 'ایمپورت زون ها (قدیمی)';
|
||
protected static ?int $navigationSort = 8;
|
||
protected static string $view = 'filament.pages.import-rates';
|
||
|
||
/** ❌ این صفحه از منوی ناوبری پنهان است — از /admin/rate-import استفاده کنید. */
|
||
protected static bool $shouldRegisterNavigation = false;
|
||
|
||
public ?array $formData = [];
|
||
|
||
public function mount(): void
|
||
{
|
||
// ریدایرکت به صفحه جدید ایمپورت نرخها (۴-رُبعی)
|
||
redirect()->to('/admin/rate-import')->send();
|
||
exit;
|
||
}
|
||
|
||
public function form(Form $form): Form
|
||
{
|
||
return $form
|
||
->schema([
|
||
FileUpload::make('file')
|
||
->label('فایل اکسل نرخها')
|
||
->acceptedFileTypes([
|
||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||
'application/vnd.ms-excel',
|
||
])
|
||
->maxSize(5120)
|
||
->required()
|
||
->disk('local')
|
||
->directory('imports'),
|
||
])
|
||
->statePath('formData');
|
||
}
|
||
|
||
public function downloadTemplate(): void
|
||
{
|
||
(new ShippingRatesTemplateExport)->download('IFNEX_Rate_Template.xlsx');
|
||
}
|
||
|
||
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 {
|
||
Excel::import(new ShippingRatesImport, $filePath);
|
||
|
||
Notification::make()
|
||
->title('موفق')
|
||
->body('نرخها با موفقیت ایمپورت شدند.')
|
||
->success()
|
||
->send();
|
||
|
||
$this->form->fill();
|
||
} catch (\Exception $e) {
|
||
Notification::make()
|
||
->title('خطا در ایمپورت')
|
||
->body($e->getMessage())
|
||
->danger()
|
||
->send();
|
||
}
|
||
}
|
||
} |