Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
9.09% covered (danger)
9.09%
1 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuotationsSync
9.09% covered (danger)
9.09%
1 / 11
50.00% covered (danger)
50.00%
1 / 2
33.05
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
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\PresupuestosService;
6use Illuminate\Console\Command;
7use Illuminate\Support\Facades\Log;
8
9class QuotationsSync extends Command
10{
11    /**
12     * The name and signature of the console command.
13     *
14     * @var string
15     */
16    protected $signature = 'quotations:sync {date?} {name?} {region?}';
17
18    /**
19     * The console command description.
20     *
21     * @var string
22     */
23    protected $description = 'Synchronizes budgets for the specified date';
24
25    /**
26     * @var PresupuestosService
27     */
28    protected $presupuestoService;
29
30    /**
31     * Create a new command instance.
32     */
33    public function __construct(private readonly PresupuestosService $presupuestosService)
34    {
35        parent::__construct();
36    }
37
38    /**
39     * Execute the console command.
40     */
41    public function handle(): int
42    {
43        $date = $this->argument('date') ?? date('Y-m-d');
44        $name = $this->argument('name') ?? 'System';
45        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'Andalucía', 'Baleares'];
46
47        $hasFailure = false;
48
49        foreach ($regions as $region) {
50            try {
51                $this->presupuestosService->syncByDate($date, $name, $region);
52            } catch (\Exception $e) {
53                Log::channel('g3w')->error("Synchronization failed for region: $region, date: $date, Error: ".$e->getMessage());
54                $hasFailure = true;
55                // Continúa con la siguiente región
56            }
57        }
58
59        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
60    }
61}