ifnex/04_Laravel/app/Console/Commands/ImportShippingRates.php
Kazem Alghasi f330d13620 refactor(import): enhance shipping rates import and refine PDF layouts
Improve the reliability and usability of the shipping rates import
process and update PDF document templates for better visual fidelity.

- Enhance `ImportShippingRates` command with `--clear` and `--dry-run`
  options to prevent accidental data loss and allow safe testing.
- Implement error handling and statistics tracking in `ShippingRatesImport`
  to capture skipped rows and import errors.
- Refactor AWB and Label PDF templates to use A4 standard dimensions
  and improved CSS layouts.
- Integrate company logo into PDF headers.
- Add a new pricing inquiry page and navigation link.
- Clean up obsolete test scripts.
2026-08-04 09:47:22 +03:30

66 lines
1.9 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Imports\ShippingRatesImport;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Throwable;
class ImportShippingRates extends Command
{
protected $signature = 'ifnex:import:rates
{path : Path to the Excel file}
{--clear : Clear existing rates before import}
{--dry-run : Show what would be imported without actually importing}';
protected $description = 'Import shipping rates from Excel into the shipping_rates table';
public function handle(): int
{
$path = $this->argument('path');
if (!file_exists($path)) {
$this->error("File not found: $path");
return Command::FAILURE;
}
if ($this->option('dry-run')) {
$this->info('DRY RUN - No data will be imported.');
}
if ($this->option('clear') && !$this->option('dry-run')) {
if (!$this->confirm('This will delete ALL existing shipping rates. Are you sure?')) {
$this->info('Operation cancelled.');
return Command::SUCCESS;
}
DB::table('shipping_rates')->truncate();
$this->info('All existing rates cleared.');
}
$this->info('Starting shipping rates import...');
$this->info('File: ' . realpath($path));
try {
if (!$this->option('dry-run')) {
Excel::import(new ShippingRatesImport(), $path);
} else {
$this->info('Dry run - skipping actual import.');
}
$this->info('Shipping rates import completed successfully.');
} catch (Throwable $e) {
$this->error('Import failed: ' . $e->getMessage());
$this->line($e->getTraceAsString());
return Command::FAILURE;
}
return Command::SUCCESS;
}
}