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