Premier commit
This commit is contained in:
69
app/Console/Commands/SendContractAlerts.php
Normal file
69
app/Console/Commands/SendContractAlerts.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?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.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user