ifnex/04_Laravel/app/Console/Commands/ImportTrackingData.php
Kazem Alghasi f8557a0f20 feat(console): add tracking data import functionality and utility scripts
Implement a new Artisan command and Excel import logic to handle tracking data ingestion. This includes registering new commands in the application bootstrap and providing several utility scripts for debugging and testing the import process.

- Add `ImportTrackingData` command and `TrackingDataImport` class
- Register `ImportShippingRates` and `ImportTrackingData` in `app.php`
- Add `ifnex:import:tracking` closure command in `console.php`
- Include various debugging and testing scripts for Excel and shipment verification
2026-08-04 05:38:35 +03:30

78 lines
2.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Imports\TrackingDataImport;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;
class ImportTrackingData extends Command
{
protected $signature = 'ifnex:import:tracking {path : Path to Excel file}';
protected $description = 'Import tracking events from Excel file';
public function handle(): int
{
$path = $this->argument('path');
if (!file_exists($path)) {
$this->error("File not found: {$path}");
return 1;
}
$this->info("Starting tracking data import from: {$path}");
$this->newLine();
try {
$import = new TrackingDataImport();
Excel::import($import, $path);
$imported = $import->getImportedEvents();
$skipped = $import->getSkippedEvents();
$errors = $import->getErrors();
$createdShipments = $import->getCreatedShipments();
$this->info("Import completed successfully!");
$this->newLine();
$this->table(
['Metric', 'Count'],
[
['Imported Events', $imported],
['Skipped Events', $skipped],
['Created Shipments', $createdShipments],
['Errors', count($errors)],
]
);
if ($errors) {
$this->newLine();
$this->warn('Errors:');
foreach (array_slice($errors, 0, 20) as $error) {
$this->error(" - {$error}");
}
if (count($errors) > 20) {
$this->warn(" ... and " . (count($errors) - 20) . " more errors");
}
}
Log::info('Tracking import command completed', [
'path' => $path,
'imported' => $imported,
'skipped' => $skipped,
'errors' => count($errors),
]);
return 0;
} catch (\Throwable $e) {
$this->error("Import failed: " . $e->getMessage());
Log::error('Tracking import command failed', [
'path' => $path,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return 1;
}
}
}