Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
PricesImport
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\PriceImportService;
6use Illuminate\Console\Command;
7use Illuminate\Support\Facades\Log;
8
9class PricesImport extends Command
10{
11    protected $signature = 'prices:import
12        {file : Path to the CSV file}
13        {--region=Madrid : Region name (must match tbl_companies.region)}
14        {--dry-run : Parse and validate but do not write to DB}
15        {--truncate : Delete existing data for this region before import}
16        {--force : Required to confirm --truncate}';
17
18    protected $description = 'Import price data from a region-specific CSV into tbl_price_* tables';
19
20    public function handle(PriceImportService $service): int
21    {
22        $file = $this->argument('file');
23        $region = $this->option('region');
24        $dryRun = (bool) $this->option('dry-run');
25        $truncate = (bool) $this->option('truncate');
26
27        if (!file_exists($file)) {
28            $this->error("File not found: {$file}");
29
30            return 1;
31        }
32
33        if ($truncate && $dryRun) {
34            $this->error('--truncate and --dry-run are mutually exclusive.');
35
36            return 1;
37        }
38
39        if ($truncate && !$this->option('force')) {
40            $this->error('--truncate requires --force to confirm destructive operation.');
41
42            return 1;
43        }
44
45        $this->info("Importing prices for region: {$region}" . ($dryRun ? ' [DRY RUN]' : ''));
46
47        try {
48            if ($truncate) {
49                $deletedZones = $service->truncateRegion($region);
50                $this->warn("Truncated {$deletedZones} zones for region {$region} (cascaded to postal codes and prices)");
51                Log::channel('third-party')->info("[prices:import] Truncated region {$region}{$deletedZones} zones");
52            }
53
54            $stats = $service->import($file, $region, $dryRun);
55
56            $summary = sprintf(
57                '[prices:import] region=%s zones=%d postal_codes=%d prices=%d skipped=%d',
58                $region,
59                $stats['zones'],
60                $stats['postal_codes'],
61                $stats['prices'],
62                $stats['skipped']
63            );
64            $this->info($summary);
65            Log::channel('third-party')->info($summary, $stats);
66
67            return 0;
68        } catch (\Exception $e) {
69            $this->error("Import failed: {$e->getMessage()}");
70            Log::channel('third-party')->error('[prices:import] ' . $e->getMessage());
71
72            return 1;
73        }
74    }
75}