Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
3.23% covered (danger)
3.23%
1 / 31
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InvoicesBackfillPaymentDates
3.23% covered (danger)
3.23%
1 / 31
50.00% covered (danger)
50.00%
1 / 2
27.66
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 / 30
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace App\Console\Commands;
4
5use App\Services\FacturasService;
6use Illuminate\Console\Command;
7use Illuminate\Support\Facades\Log;
8
9class InvoicesBackfillPaymentDates extends Command
10{
11    /**
12     * FIRE-1293 — one-shot backfill of tbl_invoice_reminders.collection_date
13     * by polling G3W's factura/{id} for every reminder with a NULL
14     * collection_date in the requested window. Idempotent: rows already
15     * holding a date are skipped at the SQL level (see syncPaymentDateForInvoice).
16     *
17     * Run-by-region is the default behaviour so an operator can throttle
18     * the G3W load (one region at a time) on a busy day.
19     */
20    protected $signature = 'invoices:backfill-payment-dates {region?} {--days=365}';
21
22    protected $description = 'FIRE-1293: backfill collection_date on tbl_invoice_reminders from G3W';
23
24    public function __construct(protected FacturasService $facturasService)
25    {
26        parent::__construct();
27    }
28
29    public function handle(): int
30    {
31        $regions = $this->argument('region')
32            ? explode(',', (string) $this->argument('region'))
33            : ['Cataluña', 'Comunidad Valenciana', 'Madrid'];
34
35        $daysBack = (int) $this->option('days');
36
37        $totals = ['checked' => 0, 'resolved' => 0, 'unpaid' => 0, 'errors' => 0];
38
39        foreach ($regions as $region) {
40            $this->info("Backfilling region={$region} days_back={$daysBack}");
41            $result = $this->facturasService->syncPaymentDatesForRegion($region, $daysBack);
42
43            foreach ($totals as $key => $_) {
44                $totals[$key] += $result[$key];
45            }
46
47            $this->line(sprintf(
48                '  %s: checked=%d resolved=%d unpaid=%d errors=%d',
49                $region,
50                $result['checked'],
51                $result['resolved'],
52                $result['unpaid'],
53                $result['errors'],
54            ));
55        }
56
57        Log::channel('g3w_invoices')->info('invoices:backfill-payment-dates finished', $totals + [
58            'regions' => $regions,
59            'days_back' => $daysBack,
60        ]);
61
62        $this->info(sprintf(
63            'Backfill complete: checked=%d resolved=%d unpaid=%d errors=%d',
64            $totals['checked'],
65            $totals['resolved'],
66            $totals['unpaid'],
67            $totals['errors'],
68        ));
69
70        return self::SUCCESS;
71    }
72}