Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
BackfillMarginFlag
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\Quotations;
6use App\Models\TblQuotations;
7use Illuminate\Console\Command;
8use Illuminate\Support\Facades\DB;
9
10/**
11 * Turn budget_margin_enabled on for quotes that have real margin data.
12 *
13 * The G3W sync wrote costs and margins but never set the flag, so the quotes
14 * list (which used to show margins only when the flag was on) left "Margen en
15 * factura" / "Margen para la empresa" blank for ~13,500 quotes, e.g. PC24053594.
16 * The form auto-ticks the box (FIRE-1387), so the flag now matches what the
17 * form already shows.
18 *
19 * Rule (same as PresupuestosService::hasMarginData): flag 0 or NULL, and a
20 * non-zero numeric invoice_margin or a total_cost_of_job above zero.
21 *
22 *   php artisan quotations:backfill-margin-flag --ticket=FIRE-1234            # dry run
23 *   php artisan quotations:backfill-margin-flag --ticket=FIRE-1234 --apply
24 */
25class BackfillMarginFlag extends Command
26{
27    protected $signature = 'quotations:backfill-margin-flag
28        {--ticket= : Ticket number for updated_by, e.g. FIRE-1234 (required)}
29        {--apply : Write the changes; without it nothing changes}';
30
31    protected $description = 'Set budget_margin_enabled = 1 on quotes that have real margins but the flag off';
32
33    public function handle(Quotations $quotations): int
34    {
35        $ticket = trim((string) $this->option('ticket'));
36        if (! preg_match('/^FIRE-\d+$/', $ticket)) {
37            $this->error('Pass the ticket number: --ticket=FIRE-1234');
38
39            return self::INVALID;
40        }
41        $apply = (bool) $this->option('apply');
42        $updatedBy = "{$ticket} backfill";
43
44        $base = fn () => DB::table('tbl_quotations')
45            ->where(fn ($q) => $q->where('budget_margin_enabled', 0)->orWhereNull('budget_margin_enabled'))
46            ->where(function ($q): void {
47                $q->whereRaw("invoice_margin RLIKE '^-?[0-9.]+$' AND invoice_margin + 0 <> 0")
48                    ->orWhereRaw("total_cost_of_job RLIKE '^-?[0-9.]+$' AND total_cost_of_job + 0 > 0");
49            });
50
51        $total = $base()->count();
52        $bySource = $base()->selectRaw('IFNULL(sync_import, 0) g3w, COUNT(*) n')->groupBy('g3w')->pluck('n', 'g3w');
53        $this->info(sprintf('%d quotes with margin data and the flag off (G3W imports: %d, others: %d)%s',
54            $total, $bySource[1] ?? 0, $bySource[0] ?? 0, $apply ? '' : '  [DRY RUN]'));
55
56        if (! $apply || $total === 0) {
57            return self::SUCCESS;
58        }
59
60        $updated = 0;
61        $base()->select('id')->orderBy('id')->chunkById(2000, function ($rows) use (&$updated, $updatedBy, $quotations): void {
62            $ids = $rows->pluck('id')->all();
63            $updated += TblQuotations::whereIn('id', $ids)
64                ->where(fn ($q) => $q->where('budget_margin_enabled', 0)->orWhereNull('budget_margin_enabled'))
65                ->update(['budget_margin_enabled' => 1, 'updated_by' => $updatedBy]);
66            foreach ($ids as $id) {
67                $quotations->addUpdateLog($id, $updatedBy, 'budget_margin_enabled', 0, 1, 4);
68            }
69        });
70
71        $this->info("Updated {$updated} quotes (updated_by = '{$updatedBy}').");
72
73        return self::SUCCESS;
74    }
75}