Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.26% covered (danger)
5.26%
1 / 19
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BulkSyncRun
5.26% covered (danger)
5.26%
1 / 19
50.00% covered (danger)
50.00%
1 / 2
5.40
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 / 18
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\PresupuestosService;
6use Illuminate\Console\Command;
7
8/**
9 * FIRE-1170: CLI wrapper around `PresupuestosService::bulkSyncByFilters` so the
10 * `bulkSync` HTTP controller can fork it into a background process via exec().
11 *
12 * This is the same pattern `PresupuestosController::syncByDate` uses for the
13 * daily cron sync — `QUEUE_CONNECTION=sync` would otherwise block the HTTP
14 * POST for 20+ minutes on a real run, and `dispatchAfterResponse` doesn't
15 * actually flush the response early under Apache mod_php.
16 *
17 * Invocation contract:
18 *   php artisan bulk-sync:run {runId} {region} {dateFrom} {dateTo}
19 *   {fields=client,amount,...} {suppressOps=0|1} {suppressFin=0|1}
20 */
21class BulkSyncRun extends Command
22{
23    protected $signature = 'bulk-sync:run
24        {runId : tbl_g3w_bulk_sync_runs.id}
25        {region : Region name (matches tbl_companies.region)}
26        {dateFrom : YYYY-MM-DD}
27        {dateTo : YYYY-MM-DD}
28        {fields : Comma-separated UI field names}
29        {suppressOps=0 : 1 if ops approval emails should be suppressed}
30        {suppressFin=0 : 1 if financial approval cascade should be suppressed}';
31
32    protected $description = 'FIRE-1170 bulk-sync worker — invoked by the controller via exec()';
33
34    public function __construct(private readonly PresupuestosService $presupuestos)
35    {
36        parent::__construct();
37    }
38
39    public function handle(): int
40    {
41        // The job can run for 20+ minutes. We're already in a CLI context so
42        // PHP's default time limit is 0, but be explicit in case php.ini was
43        // overridden in this image.
44        @set_time_limit(0);
45
46        $runId = (int) $this->argument('runId');
47        $region = (string) $this->argument('region');
48        $dateFrom = (string) $this->argument('dateFrom');
49        $dateTo = (string) $this->argument('dateTo');
50        $fields = array_values(array_filter(array_map('trim', explode(',', (string) $this->argument('fields')))));
51        $suppressOps = (bool) (int) $this->argument('suppressOps');
52        $suppressFin = (bool) (int) $this->argument('suppressFin');
53
54        $this->presupuestos->bulkSyncByFilters(
55            $runId,
56            $region,
57            $dateFrom,
58            $dateTo,
59            $fields,
60            $suppressOps,
61            $suppressFin,
62        );
63
64        return self::SUCCESS;
65    }
66}