snipe-it/app/Console/Commands/SendExpirationAlerts.php

91 lines
2.3 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
namespace App\Console\Commands;
2016-06-22 12:27:41 -07:00
2016-03-25 01:18:05 -07:00
use App\Models\Asset;
use App\Models\License;
use App\Models\Setting;
use DB;
use App\Notifications\ExpiringLicenseNotification;
use App\Notifications\ExpiringAssetsNotification;
2016-03-25 01:18:05 -07:00
use Illuminate\Console\Command;
2016-06-22 12:27:41 -07:00
class SendExpirationAlerts extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'snipeit:expiring-alerts';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check for expiring warrantees and service agreements, and sends out an alert email.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$settings = Setting::getSettings();
$threshold = $settings->alert_interval;
2016-06-22 12:27:41 -07:00
// Expiring Assets
$assets = Asset::getExpiringWarrantee(Setting::getSettings()->alert_interval);
$this->info(trans_choice('mail.assets_warrantee_alert', $assets->count(), ['count'=>$assets->count(), 'threshold' => $threshold]));
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
// Expiring licenses
$licenses = License::getExpiringLicenses($threshold);
2016-03-25 01:18:05 -07:00
$this->info(trans_choice('mail.license_expiring_alert', $licenses->count(), ['count'=>$licenses->count(), 'threshold' => $threshold]));
2016-03-25 01:18:05 -07:00
$recipient = new \App\Models\Recipients\AlertRecipient();
2016-03-25 01:18:05 -07:00
if ((Setting::getSettings()->alert_email!='') && ($settings->alerts_enabled==1)) {
2016-03-25 01:18:05 -07:00
if ($assets->count() > 0) {
// Send a rollup to the admin, if settings dictate
$recipient->notify(new ExpiringAssetsNotification($assets, $threshold));
2016-06-22 12:27:41 -07:00
}
2016-03-25 01:18:05 -07:00
if ($licenses->count() > 0) {
$recipient->notify(new ExpiringLicenseNotification($licenses, $threshold));
2016-06-22 12:27:41 -07:00
}
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
} else {
2016-03-25 01:18:05 -07:00
if ($settings->alert_email=='') {
$this->error('Could not send email. No alert email configured in settings');
} elseif ($settings->alerts_enabled!=1) {
$this->info('Alerts are disabled in the settings. No mail will be sent');
2016-06-22 12:27:41 -07:00
}
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
}
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
}
2016-03-25 01:18:05 -07:00
}