Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuotationsSyncMissing
0.00% covered (danger)
0.00%
0 / 19
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 / 17
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\GestionaController;
6use Illuminate\Console\Command;
7use Illuminate\Http\Request;
8use Illuminate\Support\Facades\Log;
9
10class QuotationsSyncMissing extends Command
11{
12    /**
13     * The name and signature of the console command.
14     *
15     * @var string
16     */
17    protected $signature = 'quotations:sync-missing {region?} {day?}';
18
19    /**
20     * The console command description.
21     *
22     * @var string
23     */
24    protected $description = 'Synchronizes missing quotations';
25
26    /**
27     * The PresupuestoService instance.
28     *
29     * @var GestionaController
30     */
31    protected $gestionaController;
32
33    /**
34     * Create a new command instance.
35     *
36     * @return void
37     */
38    public function __construct(GestionaController $gestionaController)
39    {
40        parent::__construct();
41        $this->gestionaController = $gestionaController;
42    }
43
44    /**
45     * Execute the console command.
46     *
47     * @return int
48     */
49    public function handle()
50    {
51        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'AndalucĂ­a', 'Baleares'];
52        $day = now()->subDay()->format('Y-m-d');
53
54        $this->info("Starting synchronization for day: {$day}");
55
56        $hasFailure = false;
57
58        foreach ($regions as $region) {
59            try {
60                $this->info("Synchronizing missing quotations for region: {$region}");
61
62                $request = Request::create('/', 'GET', ['day' => $day]);
63                $request->server->set('HTTP_REGION', $region);
64                $request->headers->set('Region', $region);
65
66                $this->info("Sending region: {$region}");
67
68                $response = $this->gestionaController->syncAllBudgetMonitor($request);
69
70                $this->info("Completed for region: {$region}");
71            } catch (\Exception $e) {
72                Log::channel('g3w')->error("Synchronization failed for region: $region, Error: ".$e->getMessage());
73                $hasFailure = true;
74            }
75        }
76
77        $this->info('All regions processed!');
78
79        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
80    }
81}