Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.19% covered (warning)
85.19%
23 / 27
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientsSyncRange
85.19% covered (warning)
85.19%
23 / 27
50.00% covered (danger)
50.00%
1 / 2
8.21
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 handle
84.62% covered (warning)
84.62%
22 / 26
0.00% covered (danger)
0.00%
0 / 1
7.18
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\GestionaClientSyncService;
6use Carbon\CarbonPeriod;
7use Illuminate\Console\Command;
8
9class ClientsSyncRange extends Command
10{
11    protected $signature = 'clients:sync-range {from} {to?} {region?}';
12
13    protected $description = 'Sync clients modified within a date range from Gestiona (G3W). Creates or updates local client records.';
14
15    public function __construct(private readonly GestionaClientSyncService $syncService)
16    {
17        parent::__construct();
18    }
19
20    public function handle(): int
21    {
22        $from = $this->argument('from');
23        $to = $this->argument('to') ?? date('Y-m-d');
24        $regions = $this->argument('region')
25            ? explode(',', $this->argument('region'))
26            : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'AndalucĂ­a', 'Baleares'];
27
28        $dates = CarbonPeriod::create($from, $to);
29
30        $this->info("Collecting client IDs from {$from} to {$to}...");
31
32        $hasFailure = false;
33        $totals = ['created' => 0, 'updated' => 0, 'failed' => 0, 'skipped' => 0];
34
35        foreach ($regions as $region) {
36            try {
37                $this->info("Region {$region}: collecting IDs...");
38
39                $allIds = [];
40                foreach ($dates as $date) {
41                    $ids = $this->syncService->fetchClientIds($date->format('Y-m-d'), $region);
42                    $allIds = array_merge($allIds, $ids);
43                }
44
45                $uniqueIds = array_unique(array_filter($allIds));
46                $this->info("  Region {$region}" . count($allIds) . " total IDs, " . count($uniqueIds) . " unique. Syncing...");
47
48                $stats = $this->syncService->syncClientIds($uniqueIds, $region);
49
50                foreach ($stats as $key => $value) {
51                    $totals[$key] += $value;
52                }
53
54                $this->info("  Region {$region}: created={$stats['created']}, updated={$stats['updated']}, failed={$stats['failed']}, skipped={$stats['skipped']}");
55            } catch (\Exception $e) {
56                $this->error("Region {$region} failed: " . $e->getMessage());
57                $hasFailure = true;
58            }
59        }
60
61        $this->info("Totals: created={$totals['created']}, updated={$totals['updated']}, failed={$totals['failed']}, skipped={$totals['skipped']}");
62
63        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
64    }
65}