/
DATKAI
/
ITCUP
Обзор
Документация
Войти
/
DATKAI
/
ITCUP
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
app/Console/Commands/LicenseExpiryCheckCommand.php
98 строк
4 KB
DATKAI
feat(licenses): уведомления, email, тип 1С:Предприятие/ИТС, продление с историей
02 июл 2026, 11:04
02 июл 2026, 11:04
ce115fc
Код
Авторство
О чём код?
<?php namespace App\Console\Commands; use App\Models\License; use App\Models\User; use App\Notifications\LicenseExpiring; use Filament\Notifications\Actions\Action; use Filament\Notifications\Notification as FilamentNotification; use Illuminate\Console\Command; use Illuminate\Support\Facades\Notification as MailNotification; class LicenseExpiryCheckCommand extends Command { protected $signature = 'itcup:license-expiry-check'; protected $description = 'Проверяет лицензии, шлёт уведомления об истечении и проставляет статус expired'; public function handle(): int { $today = now()->startOfDay(); $licenses = License::query() ->with(['type', 'owner']) ->where('is_perpetual', false) ->whereNotNull('expires_at') ->whereIn('status', ['active', 'expired']) ->get(); $admins = User::whereHas('roles', fn ($q) => $q->where('name', 'admin'))->get(); $notifiedSoon = 0; $notifiedExpired = 0; foreach ($licenses as $license) { $threshold = $license->type?->notify_days_before ?? 30; // Истекла if ($license->isExpired()) { if ($license->status !== 'expired') { $license->status = 'expired'; $license->saveQuietly(); } if (!$license->notified_expired_at) { $this->notify($license, 'expired', $admins); $license->notified_expired_at = $today; $license->saveQuietly(); $notifiedExpired++; } continue; } // Скоро истекает $days = $license->daysUntilExpiry(); if ($days !== null && $days <= $threshold) { if (!$license->notified_expiring_at) { $this->notify($license, 'soon', $admins); $license->notified_expiring_at = $today; $license->saveQuietly(); $notifiedSoon++; } } } $this->info("Уведомлений: скоро истекают — {$notifiedSoon}, истекли — {$notifiedExpired}"); return self::SUCCESS; } /** Колокольчик всем админам + email владельцу */ protected function notify(License $license, string $stage, $admins): void { $days = $license->daysUntilExpiry(); $title = $stage === 'expired' ? "Лицензия истекла: {$license->name}" : "Скоро истекает: {$license->name}"; $body = $stage === 'expired' ? "«{$license->name}» ({$license->type?->name}) истекла {$license->expires_at?->format('d.m.Y')}." : "«{$license->name}» ({$license->type?->name}) истекает через {$days} дн. ({$license->expires_at?->format('d.m.Y')})."; $url = \App\Filament\Resources\Licenses\LicenseResource::getUrl('edit', ['record' => $license->id]); foreach ($admins as $admin) { FilamentNotification::make() ->title($title) ->body($body) ->icon($stage === 'expired' ? 'heroicon-o-exclamation-triangle' : 'heroicon-o-clock') ->color($stage === 'expired' ? 'danger' : 'warning') ->actions([ Action::make('open')->label('Открыть')->url($url), ]) ->sendToDatabase($admin); } // Email владельцу, если указан и есть адрес if ($license->owner && filled($license->owner->email)) { MailNotification::send($license->owner, new LicenseExpiring($license, $stage)); } } }