|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services; |
| 4 | + |
| 5 | +use App\Models\AlertEvent; |
| 6 | +use App\Models\AlertRule; |
| 7 | +use App\Modules\Finance\Models\Invoice; |
| 8 | +use App\Modules\Inventory\Models\Product; |
| 9 | +use App\Modules\HelpDesk\Models\Ticket; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | + |
| 12 | +class AlertEvaluatorService |
| 13 | +{ |
| 14 | + public function evaluate(AlertRule $rule): array |
| 15 | + { |
| 16 | + return match ($rule->type) { |
| 17 | + 'overdue_invoice' => $this->checkOverdueInvoices($rule), |
| 18 | + 'low_stock' => $this->checkLowStock($rule), |
| 19 | + 'high_receivables' => $this->checkHighReceivables($rule), |
| 20 | + 'unresolved_ticket' => $this->checkUnresolvedTickets($rule), |
| 21 | + default => [], |
| 22 | + }; |
| 23 | + } |
| 24 | + |
| 25 | + private function checkOverdueInvoices(AlertRule $rule): array |
| 26 | + { |
| 27 | + $days = $rule->conditions['days'] ?? 30; |
| 28 | + $threshold = now()->subDays($days); |
| 29 | + |
| 30 | + $invoices = Invoice::withoutGlobalScopes() |
| 31 | + ->where('tenant_id', $rule->tenant_id) |
| 32 | + ->where('status', 'sent') |
| 33 | + ->where('due_date', '<', $threshold) |
| 34 | + ->get(['id', 'number', 'total', 'due_date', 'contact_id']); |
| 35 | + |
| 36 | + if ($invoices->isEmpty()) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + return [[ |
| 41 | + 'message' => "{$invoices->count()} invoice(s) overdue by more than {$days} days", |
| 42 | + 'context' => [ |
| 43 | + 'count' => $invoices->count(), |
| 44 | + 'ids' => $invoices->pluck('id')->toArray(), |
| 45 | + 'numbers' => $invoices->pluck('number')->toArray(), |
| 46 | + 'total_outstanding' => $invoices->sum('total'), |
| 47 | + ], |
| 48 | + ]]; |
| 49 | + } |
| 50 | + |
| 51 | + private function checkLowStock(AlertRule $rule): array |
| 52 | + { |
| 53 | + $threshold = $rule->conditions['below'] ?? 10; |
| 54 | + |
| 55 | + $products = Product::withoutGlobalScopes() |
| 56 | + ->where('tenant_id', $rule->tenant_id) |
| 57 | + ->where('stock_quantity', '<', $threshold) |
| 58 | + ->where('reorder_point', '>', 0) |
| 59 | + ->get(['id', 'name', 'sku', 'stock_quantity']); |
| 60 | + |
| 61 | + if ($products->isEmpty()) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + return [[ |
| 66 | + 'message' => "{$products->count()} product(s) with stock below {$threshold}", |
| 67 | + 'context' => [ |
| 68 | + 'count' => $products->count(), |
| 69 | + 'products' => $products->map(fn ($p) => [ |
| 70 | + 'id' => $p->id, |
| 71 | + 'name' => $p->name, |
| 72 | + 'sku' => $p->sku, |
| 73 | + 'quantity' => $p->stock_quantity, |
| 74 | + ])->toArray(), |
| 75 | + ], |
| 76 | + ]]; |
| 77 | + } |
| 78 | + |
| 79 | + private function checkHighReceivables(AlertRule $rule): array |
| 80 | + { |
| 81 | + $threshold = $rule->conditions['amount'] ?? 10000; |
| 82 | + |
| 83 | + $total = Invoice::withoutGlobalScopes() |
| 84 | + ->where('tenant_id', $rule->tenant_id) |
| 85 | + ->whereNotIn('status', ['paid', 'cancelled']) |
| 86 | + ->sum('total'); |
| 87 | + |
| 88 | + if ($total < $threshold) { |
| 89 | + return []; |
| 90 | + } |
| 91 | + |
| 92 | + return [[ |
| 93 | + 'message' => "Outstanding receivables (\${$total}) exceed threshold of \${$threshold}", |
| 94 | + 'context' => ['total_outstanding' => $total, 'threshold' => $threshold], |
| 95 | + ]]; |
| 96 | + } |
| 97 | + |
| 98 | + private function checkUnresolvedTickets(AlertRule $rule): array |
| 99 | + { |
| 100 | + $hours = $rule->conditions['hours'] ?? 48; |
| 101 | + $cutoff = now()->subHours($hours); |
| 102 | + |
| 103 | + try { |
| 104 | + $count = Ticket::withoutGlobalScopes() |
| 105 | + ->where('tenant_id', $rule->tenant_id) |
| 106 | + ->whereNotIn('status', ['resolved', 'closed']) |
| 107 | + ->where('created_at', '<', $cutoff) |
| 108 | + ->count(); |
| 109 | + |
| 110 | + if ($count === 0) { |
| 111 | + return []; |
| 112 | + } |
| 113 | + |
| 114 | + return [[ |
| 115 | + 'message' => "{$count} helpdesk ticket(s) unresolved for more than {$hours} hours", |
| 116 | + 'context' => ['count' => $count, 'hours' => $hours], |
| 117 | + ]]; |
| 118 | + } catch (\Throwable) { |
| 119 | + return []; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public function fire(AlertRule $rule, array $triggered): void |
| 124 | + { |
| 125 | + foreach ($triggered as $alert) { |
| 126 | + AlertEvent::create([ |
| 127 | + 'alert_rule_id' => $rule->id, |
| 128 | + 'message' => $alert['message'], |
| 129 | + 'context' => $alert['context'] ?? null, |
| 130 | + 'triggered_at' => now(), |
| 131 | + ]); |
| 132 | + } |
| 133 | + |
| 134 | + $rule->update(['last_triggered_at' => now()]); |
| 135 | + |
| 136 | + // Send in-app notifications |
| 137 | + foreach ($rule->notification_targets as $target) { |
| 138 | + if (is_int($target)) { |
| 139 | + NotificationService::send( |
| 140 | + $rule->tenant_id, |
| 141 | + $target, |
| 142 | + 'alert', |
| 143 | + "Alert: {$rule->name}", |
| 144 | + $triggered[0]['message'] ?? '', |
| 145 | + ['rule_id' => $rule->id] |
| 146 | + ); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments