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
QuotationsWorkSync
9.09% covered (danger)
9.09%
1 / 11
50.00% covered (danger)
50.00%
1 / 2
43.81
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
42
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\PresupuestosService;
6use Illuminate\Console\Command;
7use Illuminate\Support\Facades\Log;
8
9class QuotationsWorkSync extends Command
10{
11    /**
12     * The name and signature of the console command.
13     *
14     * @var string
15     */
16    protected $signature = 'quotations:sync-work
17                            {name?}
18                            {region?}
19                            {--limit= : Max quotations to process per region (omit for full sweep)}';
20
21    /**
22     * The console command description.
23     *
24     * @var string
25     */
26    protected $description = 'Synchronizes works related to the orders';
27
28    /**
29     * @var PresupuestosService
30     */
31    protected $presupuestoService;
32
33    /**
34     * Create a new command instance.
35     */
36    public function __construct(private readonly PresupuestosService $presupuestosService)
37    {
38        parent::__construct();
39    }
40
41    /**
42     * Execute the console command.
43     */
44    public function handle(): int
45    {
46        $name = $this->argument('name') ?? 'System';
47        $regions = $this->argument('region') ? explode(',', $this->argument('region')) : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'AndalucĂ­a', 'Baleares'];
48        $limit = $this->option('limit') !== null ? max(1, (int) $this->option('limit')) : null;
49
50        $hasFailure = false;
51
52        foreach ($regions as $region) {
53            try {
54                $this->presupuestosService->syncBudgetsWorks($name, $region, $limit);
55            } catch (\Exception $e) {
56                Log::channel('g3w')->error("Synchronization failed for region: $region, Error: ".$e->getMessage());
57                $hasFailure = true;
58            }
59        }
60
61        return $hasFailure ? Command::FAILURE : Command::SUCCESS;
62    }
63}