Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuotationsSync
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 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     * The PresupuestoService instance.
27     *
28     * @var PresupuestosService
29     */
30    protected $presupuestoService;
31
32    /**
33     * @var PresupuestosService
34     */
35    private $presupuestosService;
36
37    /**
38     * Create a new command instance.
39     *
40     * @return void
41     */
42    public function __construct(PresupuestosService $presupuestosService)
43    {
44        parent::__construct();
45        $this->presupuestosService = $presupuestosService;
46    }
47
48    /**
49     * Execute the console command.
50     *
51     * @return int
52     */
53    public function handle()
54    {
55        $date = $this->argument('date') ?? date('Y-m-d');
56        $name = $this->argument('name') ?? 'System';
57        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'Andalucía', 'Baleares'];
58
59        $hasFailure = false;
60
61        foreach ($regions as $region) {
62            try {
63                $this->presupuestosService->syncByDate($date, $name, $region);
64            } catch (\Exception $e) {
65                Log::channel('g3w')->error("Synchronization failed for region: $region, date: $date, Error: ".$e->getMessage());
66                $hasFailure = true;
67                // Continúa con la siguiente región
68            }
69        }
70
71        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
72    }
73}