Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Fire2040EmailStatusBackfill
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
462
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 64
0.00% covered (danger)
0.00%
0 / 1
462
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\Quotations;
6use App\Models\TblQuotations;
7use App\Services\ResultCache;
8use Illuminate\Console\Command;
9
10/**
11 * FIRE-2040 backfill: quotes stuck in "Correo erroneo" (22) with a valid email.
12 *
13 * Most were imported from Gestiona with a trailing space on the address, which
14 * the import's email check treated as invalid. The lists below are the ticket's
15 * review of 2026-10-05, decided per address against the real SendGrid events:
16 *
17 *   → 2  sent: delivered to the client and not bounced afterwards
18 *   → 1  new: approval still pending (approval flags are kept)
19 *   → 11 ready to send: everything else that can move
20 *   stay in 22: a hard failure for the client's own address (not in here)
21 *
22 * Quote numbers repeat across companies, so every entry is company + quote_id.
23 * A quote is only moved if it is STILL in 22 (anything else changed it since
24 * the review). The stored email is trimmed on the same write. Nothing is sent.
25 *
26 *   php artisan quotations:fire2040-backfill            # dry run
27 *   php artisan quotations:fire2040-backfill --apply
28 */
29class Fire2040EmailStatusBackfill extends Command
30{
31    protected $signature = 'quotations:fire2040-backfill {--apply : Write the changes; without it nothing changes}';
32
33    protected $description = 'FIRE-2040: move quotes wrongly held in "Correo erroneo" (22) to their reviewed status';
34
35    private const UPDATED_BY = 'FIRE-2040 backfill';
36
37    private const CORREO_ERRONEO = 22;
38
39    /** Expected sizes from the ticket — guards against a typo in the lists. */
40    private const EXPECTED = [2 => 1, 1 => 3, 11 => 275];
41
42    /** @var array<int, array<int, list<string>>> target status => company => quote_ids */
43    private const TARGETS = [
44        2 => [
45            19 => ['PC24054114'],
46        ],
47        1 => [
48            18 => ['PC24036787'],
49            19 => ['PC24057554'],
50            21 => ['3369'],
51        ],
52        11 => [
53            9 => ['58371'],
54            18 => [
55                '748f62a2-66ec-4a34-9828-dc163da7c5a2', 'PC24032164', 'PC24032199', 'PC24032227', 'PC24032373',
56                'PC24032388', 'PC24024494', 'PC24032842', 'PC24033301', 'PC24033302', 'PC24024799', 'PC24024803',
57                'PC24024822', 'PC24033965', 'PC24034004', 'PC24024913', 'PC24024920', 'PC24034382', 'PC24035064',
58                'PC24035720', 'PC24035794', 'PC24036720', 'PC24036998', 'PC24037454', 'PC24037641', 'PC24037793',
59                'PC24037861', 'PC24037891', 'PC24037901', 'PC24038114',
60            ],
61            19 => [
62                'PC24052884', 'PC24052909', 'PC24052990', 'PC24053013', 'PC24053052', 'PC24053102', 'PC24053356',
63                'PC24053480', 'PC24053481', 'PC24053613', 'PC24053968', 'PC24054005', 'PC24054100', 'PC24054467',
64                'PC24054663', 'PC24054679', 'PC24055053', 'PC24056358', 'PC24057755', 'PC24058127',
65            ],
66            21 => ['2106', '2128', '2191', '2496', '2532', '2592', '2601', '3449'],
67            22 => [
68                'PC24025816', 'PC24025818', 'PC24025820', 'PC24025823', 'PC24025827', 'PC24025831', 'PC24025836',
69                'PC24025842', 'PC24025847', 'PC24025852', 'PC24025857', 'PC24025863', 'PC24025869', 'PC24025875',
70                'PC24025882', 'PC24025890', 'PC24025899', 'PC24025909', 'PC24025919', 'PC24025929', 'PC24025939',
71                'PC24025949', 'PC24025960', 'PC24025971', 'PC24025982', 'PC24025993', 'PC24026004', 'PC24026015',
72                'PC24026026', 'PC24026037', 'PC24026048', 'PC24026059', 'PC24026070', 'PC24026081', 'PC24026092',
73                'PC24026103', 'PC24026115', 'PC24026127', 'PC24026139', 'PC24026151', 'PC24026163', 'PC24026175',
74                'PC24026187', 'PC24026199', 'PC24026211', 'PC24026223', 'PC24026237', 'PC24026250', 'PC24026263',
75                'PC24026276', 'PC24026289', 'PC24026302', 'PC24026315', 'PC24026328', 'PC24026341', 'PC24026354',
76                'PC24026367', 'PC24026380', 'PC24026393', 'PC24026406', 'PC24026419', 'PC24026432', 'PC24026445',
77                'PC24026458', 'PC24026475', 'PC24026490', 'PC24026503', 'PC24026516', 'PC24026529', 'PC24026542',
78                'PC24026555', 'PC24026568', 'PC24026581', 'PC24026594', 'PC24026607', 'PC24026620', 'PC24026633',
79                'PC24026646', 'PC24026659', 'PC24026672', 'PC24026685', 'PC24026698', 'PC24026711', 'PC24026724',
80                'PC24026737', 'PC24026750', 'PC24026763', 'PC24026776', 'PC24026790', 'PC24026804', 'PC24026818',
81                'PC24026832', 'PC24026846', 'PC24026860', 'PC24026874', 'PC24026888', 'PC24026902', 'PC24026916',
82                'PC24026930', 'PC24026944', 'PC24026958', 'PC24026972', 'PC24026986', 'PC24027000', 'PC24027014',
83                'PC24027028', 'PC24027042', 'PC24027056', 'PC24027070', 'PC24027084', 'PC24027098', 'PC24027108',
84                'PC24027122', 'PC24027136', 'PC24027150', 'PC24027164', 'PC24027178', 'PC24027192', 'PC24027206',
85                'PC24027220', 'PC24027234', 'PC24027248', 'PC24027262', 'PC24027276', 'PC24027290', 'PC24027304',
86                'PC24027318', 'PC24027332', 'PC24027346', 'PC24027360', 'PC24027374', 'PC24027388', 'PC24027402',
87                'PC24027416', 'PC24027430', 'PC24027444', 'PC24027458', 'PC24027472', 'PC24027486', 'PC24027500',
88                'PC24027514', 'PC24027528', 'PC24027542', 'PC24027556', 'PC24027570', 'PC24027584', 'PC24027598',
89                'PC24027612', 'PC24027626', 'PC24027640', 'PC24027654', 'PC24027668', 'PC24027682', 'PC24027696',
90                'PC24027710', 'PC24027724', 'PC24027738', 'PC24027752', 'PC24027766', 'PC24027780', 'PC24027794',
91                'PC24027808', 'PC24027822', 'PC24027836', 'PC24027850', 'PC24027864', 'PC24027878', 'PC24027892',
92                'PC24027906', 'PC24027920', 'PC24028204',
93            ],
94            23 => ['3491', '3513', '3626'],
95            30 => [
96                '44886', '44934', '45205', '46110', '46112', '46806', '46820', '47662', '47675', '47756', '48255',
97                '48316', '48569', '48589', '48737', '48997', '49010',
98            ],
99            33 => ['483'],
100            34 => [
101                '78', '88', '91', '103', '129', '141', '142', '160', '161', '166', '168', '169', '185', '189', '388',
102                '420', '421', '429', '442', '458', '485', '505', '571', '604',
103            ],
104        ],
105    ];
106
107    public function handle(Quotations $quotations): int
108    {
109        $apply = (bool) $this->option('apply');
110
111        foreach (self::EXPECTED as $status => $expected) {
112            $count = array_sum(array_map('count', self::TARGETS[$status]));
113            if ($count !== $expected) {
114                $this->error("List for status {$status} has {$count} quotes, the ticket says {$expected}. Aborting.");
115
116                return self::FAILURE;
117            }
118        }
119
120        $this->info($apply ? 'Applying FIRE-2040 backfill.' : 'DRY RUN, nothing is written. Add --apply to write.');
121
122        $moved = [2 => 0, 1 => 0, 11 => 0];
123        $trimmed = 0;
124        $notFound = [];
125        $notIn22 = [];
126        $approvalHeld = [];
127        $companies = [];
128
129        foreach (self::TARGETS as $target => $byCompany) {
130            foreach ($byCompany as $companyId => $quoteIds) {
131                foreach ($quoteIds as $quoteId) {
132                    $row = TblQuotations::where('company_id', $companyId)->where('quote_id', $quoteId)->first();
133                    $label = "{$companyId}/{$quoteId}";
134
135                    if (! $row) {
136                        $notFound[] = $label;
137
138                        continue;
139                    }
140                    if ((int) $row->budget_status_id !== self::CORREO_ERRONEO) {
141                        $notIn22[] = "{$label} (now {$row->budget_status_id})";
142
143                        continue;
144                    }
145
146                    // Never let a quote skip approval: a pending one goes to
147                    // 1 (new) whatever list it is in. for_approval 0 = none.
148                    $newStatus = $target;
149                    if ($target !== 1 && in_array((int) $row->for_approval, [1, 2, 3], true)) {
150                        $newStatus = 1;
151                        $approvalHeld[] = $label;
152                    }
153
154                    $update = [
155                        'budget_status_id' => $newStatus,
156                        'updated_by' => self::UPDATED_BY,
157                        'updated_at' => now(),
158                    ];
159                    $cleanEmail = $row->email === null ? null : trim((string) $row->email);
160                    if ($cleanEmail !== $row->email) {
161                        $update['email'] = $cleanEmail;
162                        $trimmed++;
163                    }
164
165                    $moved[$newStatus]++;
166                    $companies[$companyId] = true;
167
168                    if (! $apply) {
169                        continue;
170                    }
171
172                    // Query-builder update: for_approval / approval_type are not
173                    // in the payload, so approval state stays exactly as it was.
174                    TblQuotations::where('id', $row->id)->where('budget_status_id', self::CORREO_ERRONEO)->update($update);
175                    $quotations->addUpdateLog($row->id, self::UPDATED_BY, 'budget_status_id', self::CORREO_ERRONEO, $newStatus, 4);
176                    if (isset($update['email'])) {
177                        $quotations->addUpdateLog($row->id, self::UPDATED_BY, 'email', $row->email, $cleanEmail, 4);
178                    }
179                }
180            }
181        }
182
183        if ($apply) {
184            foreach (array_keys($companies) as $companyId) {
185                try {
186                    $quotations->update_commercial_numbers($companyId);
187                } catch (\Throwable $e) {
188                    $this->warn("update_commercial_numbers failed for company {$companyId}: {$e->getMessage()}");
189                }
190            }
191            ResultCache::forgetDomain('quotations');
192        }
193
194        $verb = $apply ? 'Moved' : 'Would move';
195        $this->table(['Result', 'Quotes'], [
196            ["{$verb} to 2 (Enviado)", $moved[2]],
197            ["{$verb} to 1 (Nuevo, approval pending)", $moved[1]],
198            ["{$verb} to 11 (Listo para enviar)", $moved[11]],
199            ['Emails trimmed', $trimmed],
200            ['Skipped: no longer in 22', count($notIn22)],
201            ['Skipped: not found', count($notFound)],
202        ]);
203
204        foreach (['Sent to 1 instead (approval pending)' => $approvalHeld, 'No longer in 22' => $notIn22, 'Not found' => $notFound] as $title => $list) {
205            if ($list !== []) {
206                $this->line("{$title}: ".implode(', ', $list));
207            }
208        }
209
210        return self::SUCCESS;
211    }
212}