70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Contract;
|
|
use App\Models\User;
|
|
use App\Mail\ContractExpiringNotification;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class SendContractAlerts extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'contracts:check-expirations';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Check for expiring contracts and notify admins';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('Checking for expiring contracts...');
|
|
|
|
$expiringContracts = Contract::where('status', '!=', 'expired') // Assuming 'active' or 'pending'
|
|
->where('end_date', '<=', now()->addDays(30))
|
|
->where('end_date', '>=', now())
|
|
->get();
|
|
|
|
if ($expiringContracts->isEmpty()) {
|
|
$this->info('No contracts expiring soon.');
|
|
return;
|
|
}
|
|
|
|
$admins = User::where('role', 'admin')->get();
|
|
|
|
if ($admins->isEmpty()) {
|
|
$this->warn('No admins found to notify.');
|
|
return;
|
|
}
|
|
|
|
foreach ($admins as $admin) {
|
|
// In a real app, you might group these or send one email per contract/batch
|
|
// Here we just simulate sending a notification for the batch
|
|
// Or send individual emails
|
|
|
|
// Simplification: Send one email with list
|
|
try {
|
|
Mail::to($admin->email)->send(new ContractExpiringNotification($expiringContracts));
|
|
$this->info("Notification sent to {$admin->email}");
|
|
} catch (\Exception $e) {
|
|
$this->error("Failed to send email to {$admin->email}: " . $e->getMessage());
|
|
Log::error($e);
|
|
}
|
|
}
|
|
|
|
$this->info('Done.');
|
|
}
|
|
}
|