Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 64 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ClearDecidedApprovals | |
0.00% |
0 / 64 |
|
0.00% |
0 / 1 |
1560 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 64 |
|
0.00% |
0 / 1 |
1560 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Services\ResultCache; |
| 6 | use Illuminate\Console\Command; |
| 7 | use Illuminate\Support\Facades\DB; |
| 8 | |
| 9 | /** |
| 10 | * Clear the pending-approval flags on budgets whose approval circuit is already |
| 11 | * decided — "approved/rejected but still showing as pending". |
| 12 | * |
| 13 | * Same rule the controller applies on every approve/reject |
| 14 | * (Quotations::clearApprovalIfDecided), matched per dimension: |
| 15 | * - any rejection ends the circuit |
| 16 | * - technical (1) needs the V1 slot, financial (2) needs V2, dual (3) both |
| 17 | * - unclassified (no approval_type) accepts either slot |
| 18 | * |
| 19 | * CROSS-SLOT rows are reported but NOT cleared unless --include-cross-slot: the |
| 20 | * required dimension is unapproved while the OTHER slot holds an approval. That |
| 21 | * shape has two very different causes and only a human can tell them apart: |
| 22 | * (a) legacy — an approval recorded against the wrong dimension before the |
| 23 | * authorization guard existed, so the budget has been stuck ever since; |
| 24 | * (b) current and CORRECT — since FIRE-1572 an edit re-requests only the |
| 25 | * dimension whose inputs changed and deliberately keeps the other |
| 26 | * dimension's approval (margin edit → technical only, Finance's approval |
| 27 | * for the unchanged amount stands). Clearing those would cancel a |
| 28 | * technical approval nobody has given. |
| 29 | * |
| 30 | * Unlike `approvals:reconcile` this does NOT need the matrix engine and never |
| 31 | * re-classifies anything — it only clears rows that are already decided. |
| 32 | * |
| 33 | * SAFETY: preview by default; only writes with --apply. |
| 34 | */ |
| 35 | class ClearDecidedApprovals extends Command |
| 36 | { |
| 37 | protected $signature = 'approvals:clear-decided |
| 38 | {--apply : Actually write the changes (default is a dry-run preview)} |
| 39 | {--include-cross-slot : Also clear rows whose approval sits in the other dimension\'s slot (review the preview first)} |
| 40 | {--company= : Limit to a single company_id} |
| 41 | {--id= : Limit to a single quotation id}'; |
| 42 | |
| 43 | protected $description = 'Clear for_approval/approval_type on budgets whose approval circuit is already decided.'; |
| 44 | |
| 45 | public function handle(): int |
| 46 | { |
| 47 | $apply = (bool) $this->option('apply'); |
| 48 | |
| 49 | $query = DB::table('tbl_quotations') |
| 50 | ->whereNotNull('for_approval') |
| 51 | ->where('for_approval', '!=', 0); |
| 52 | |
| 53 | if ($this->option('company') !== null) { |
| 54 | $query->where('company_id', (int) $this->option('company')); |
| 55 | } |
| 56 | if ($this->option('id') !== null) { |
| 57 | $query->where('id', (int) $this->option('id')); |
| 58 | } |
| 59 | |
| 60 | $budgets = $query->get([ |
| 61 | 'id', 'quote_id', 'company_id', 'for_approval', 'approval_type', |
| 62 | 'approved_by', 'approved_by_v2', 'rejected_by', 'rejected_by_v2', |
| 63 | ]); |
| 64 | |
| 65 | $this->info(($apply ? 'APPLYING' : 'DRY-RUN — no changes written').' · '.$budgets->count().' pending budget(s) examined'.PHP_EOL); |
| 66 | |
| 67 | $includeCrossSlot = (bool) $this->option('include-cross-slot'); |
| 68 | |
| 69 | $rows = []; |
| 70 | $ids = []; |
| 71 | $crossSlot = 0; |
| 72 | |
| 73 | foreach ($budgets as $b) { |
| 74 | $type = $b->approval_type !== null ? (int) $b->approval_type : (int) $b->for_approval; |
| 75 | |
| 76 | $approvedTech = $b->approved_by !== null && $b->approved_by !== ''; |
| 77 | $approvedFin = $b->approved_by_v2 !== null && $b->approved_by_v2 !== ''; |
| 78 | $rejected = ($b->rejected_by !== null && $b->rejected_by !== '') |
| 79 | || ($b->rejected_by_v2 !== null && $b->rejected_by_v2 !== ''); |
| 80 | |
| 81 | // Strict per-dimension satisfaction — mirrors clearApprovalIfDecided(). |
| 82 | $decided = $rejected || match ($type) { |
| 83 | 1 => $approvedTech, |
| 84 | 2 => $approvedFin, |
| 85 | 3 => $approvedTech && $approvedFin, |
| 86 | default => $approvedTech || $approvedFin, |
| 87 | }; |
| 88 | |
| 89 | // The required dimension is unapproved, but the other slot holds an |
| 90 | // approval. Could be a legacy wrong-slot record OR a deliberate |
| 91 | // FIRE-1572 preserved approval — surfaced for a human, cleared only |
| 92 | // with --include-cross-slot. |
| 93 | $isCrossSlot = ! $decided && ! $rejected && ( |
| 94 | ($type === 1 && $approvedFin) || ($type === 2 && $approvedTech) |
| 95 | ); |
| 96 | |
| 97 | if (! $decided && ! $isCrossSlot) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if ($isCrossSlot) { |
| 102 | $crossSlot++; |
| 103 | } |
| 104 | |
| 105 | if ($decided || $includeCrossSlot) { |
| 106 | $ids[] = $b->id; |
| 107 | } |
| 108 | |
| 109 | $rows[] = [ |
| 110 | $b->id, |
| 111 | $b->quote_id, |
| 112 | $b->company_id, |
| 113 | 'for='.$b->for_approval.' type='.($b->approval_type ?? 'null'), |
| 114 | $rejected ? 'rejected' : ($approvedTech && $approvedFin ? 'appr(v1+v2)' : ($approvedTech ? 'appr(v1)' : 'appr(v2)')), |
| 115 | $isCrossSlot ? ($includeCrossSlot ? 'CROSS-SLOT → clear' : 'CROSS-SLOT → review, kept') : 'decided → clear', |
| 116 | ]; |
| 117 | } |
| 118 | |
| 119 | if ($rows) { |
| 120 | $this->table(['id', 'quote_id', 'co', 'current', 'decision', 'action'], $rows); |
| 121 | } |
| 122 | |
| 123 | $this->info(count($ids).' budget(s) to clear.'); |
| 124 | |
| 125 | if ($crossSlot > 0 && ! $includeCrossSlot) { |
| 126 | $this->warn($crossSlot.' CROSS-SLOT budget(s) left untouched: the required dimension is unapproved and the approval sits in the other slot.'); |
| 127 | $this->warn('Those are either a legacy wrong-slot record (stuck — should be cleared) or a deliberately preserved approval from an edit that re-requested only one dimension (must NOT be cleared).'); |
| 128 | $this->warn('Review the rows above, then re-run with --include-cross-slot to clear them too.'); |
| 129 | } |
| 130 | |
| 131 | if (! $ids) { |
| 132 | return self::SUCCESS; |
| 133 | } |
| 134 | |
| 135 | if (! $apply) { |
| 136 | $this->comment('Dry-run only. Re-run with --apply to clear them.'); |
| 137 | |
| 138 | return self::SUCCESS; |
| 139 | } |
| 140 | |
| 141 | DB::table('tbl_quotations') |
| 142 | ->whereIn('id', $ids) |
| 143 | ->update(['for_approval' => null, 'approval_type' => null, 'updated_at' => date('Y-m-d H:i:s')]); |
| 144 | |
| 145 | // Approval state feeds list_quotations + the approver counters. |
| 146 | ResultCache::forgetDomain(['quotations', 'users']); |
| 147 | |
| 148 | $this->info('Cleared. Caches flushed. No emails or notifications were sent.'); |
| 149 | |
| 150 | return self::SUCCESS; |
| 151 | } |
| 152 | } |