Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InvoicesSync
91.67% covered (success)
91.67%
11 / 12
50.00% covered (danger)
50.00%
1 / 2
4.01
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
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3namespace App\Console\Commands;
4
5use App\Jobs\SyncG3WInvoicesForRegion;
6use App\Services\FacturasService;
7use Illuminate\Console\Command;
8use Illuminate\Support\Facades\Log;
9
10class InvoicesSync extends Command
11{
12    /**
13     * The name and signature of the console command.
14     *
15     * @var string
16     */
17    protected $signature = 'invoices:send-reminder {region?}';
18
19    /**
20     * The console command description.
21     *
22     * @var string
23     */
24    protected $description = 'Automatic sending of invoice reminders';
25
26    /**
27     * Create a new command instance.
28     */
29    public function __construct(protected FacturasService $facturasService)
30    {
31        parent::__construct();
32    }
33
34    /**
35     * Execute the console command.
36     */
37    public function handle(): int
38    {
39        // FIRE-1293 (Berta, 2026-07): dispatch to every Gestiona region so the
40        // KPI can display "potential" everywhere. FacturasService gates the
41        // actual SendGrid call on tbl_companies.invoice_reminder_active; the
42        // other regions log region_inactive skips (read-only mode).
43        $regions = $this->argument('region')
44            ? explode(',', $this->argument('region'))
45            : ['Cataluña', 'Madrid', 'Comunidad Valenciana', 'Andalucía', 'Baleares', 'Castellón'];
46
47        // FIRE-1151: dispatch one job per region (parallel under redis driver,
48        // serial inline under sync driver — see QuotationsSync for details).
49        $cronStart = microtime(true);
50
51        foreach ($regions as $region) {
52            SyncG3WInvoicesForRegion::dispatch($region)->onQueue('g3w');
53        }
54
55        Log::channel('g3w_invoices')->info('invoices:sync dispatched', [
56            'regions' => count($regions),
57            'dispatch_ms' => (int) round((microtime(true) - $cronStart) * 1000),
58        ]);
59
60        return Command::SUCCESS;
61    }
62}