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

138 lines
4.9 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 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()
{
// Expiring Assets
$expiring_assets = Asset::getExpiringWarrantee(Setting::getSettings()->alert_interval);
$this->info(count($expiring_assets).' expiring assets');
$asset_data['count'] = count($expiring_assets);
$asset_data['email_content'] ='';
$now = date("Y-m-d");
foreach ($expiring_assets as $asset) {
$expires = $asset->present()->warrantee_expires();
2016-06-22 12:27:41 -07:00
$difference = round(abs(strtotime($expires) - strtotime($now))/86400);
if ($difference > 30) {
$asset_data['email_content'] .= '<tr style="background-color: #fcffa3;">';
} else {
$asset_data['email_content'] .= '<tr style="background-color:#d9534f;">';
}
$asset_data['email_content'] .= '<td><a href="'.config('app.url').'/hardware/'.e($asset->id).'/view">';
$asset_data['email_content'] .= $asset->present()->name().'</a></td><td>'.e($asset->asset_tag).'</td>';
$asset_data['email_content'] .= '<td>'.e($asset->present()->warrantee_expires()).'</td>';
$asset_data['email_content'] .= '<td>'.$difference.' '.trans('mail.days').'</td>';
2016-04-23 04:02:40 -07:00
$asset_data['email_content'] .= '<td>'.($asset->supplier ? e($asset->supplier->name) : '').'</td>';
2016-12-27 16:24:41 -08:00
$asset_data['email_content'] .= '<td>'.($asset->assignedTo ? e($asset->assignedTo->present()->name()) : '').'</td>';
2016-06-22 12:27:41 -07:00
$asset_data['email_content'] .= '</tr>';
}
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
// Expiring licenses
$expiring_licenses = License::getExpiringLicenses(Setting::getSettings()->alert_interval);
$this->info(count($expiring_licenses).' expiring licenses');
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
$license_data['count'] = count($expiring_licenses);
$license_data['email_content'] = '';
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
foreach ($expiring_licenses as $license) {
$expires = $license->expiration_date;
$difference = round(abs(strtotime($expires) - strtotime($now))/86400);
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
if ($difference > 30) {
$license_data['email_content'] .= '<tr style="background-color: #fcffa3;">';
} else {
$license_data['email_content'] .= '<tr style="background-color:#d9534f;">';
}
Discussion: Moving to policies for controller based authorization (#3080) * Make delete routes work. We put a little form in the modal that spoofs the delete field. * Fix route on creating a user. * Fix redundant id parameter. * Port acceptance tests to new urls. * Initial work on migrating to model based policies instead of global gates. Will allow for much more detailed permissions bits in the future. * This needs to stay for the dashboard checks. * Add user states for permissions to build tests. * Build up unit tests for gates/permissions. Move accessories/consumables/assets to policies instead of in authserviceprovider * Migrate various locations to new syntax. Update test to be more specific * Fix functional tests. Add an artisan command for installing a settings setup on travis-ci * Try a different id... Need to come up with a better way of passing the id for tests that need an existing one. * Try to fix travis * Update urls to use routes and not hardcode old paths. Also fix some migration errors found along the way.: * Add a environment for travis functional tests. * Adjust config file to make travis use it. * Use redirect()->route instead of redirect()-to * Dump all failures in the output directory if travis fails. * Cleanups and minor fixes. * Adjust the supplier modelfactory to comply with new validation restrictions. * Some test fixes. * Locales can be longer than 5 characters according to faker... fex gez_ET. Increase lenght in mysql and add a validation * Update test database dump to latest migrations.
2016-12-19 11:04:28 -08:00
$license_data['email_content'] .= '<td><a href="'.route('licenses.show', $license->id).'">';
2016-06-22 12:27:41 -07:00
$license_data['email_content'] .= $license->name.'</a></td>';
$license_data['email_content'] .= '<td>'.$license->expiration_date.'</td>';
$license_data['email_content'] .= '<td>'.$difference.' days</td>';
$license_data['email_content'] .= '</tr>';
}
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
if ((Setting::getSettings()->alert_email!='') && (Setting::getSettings()->alerts_enabled==1)) {
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
if (count($expiring_assets) > 0) {
2016-09-20 07:20:10 -07:00
$this->info('Report sent to '.Setting::getSettings()->alert_email);
2016-06-22 12:27:41 -07:00
\Mail::send('emails.expiring-assets-report', $asset_data, function ($m) {
$m->to(explode(',', Setting::getSettings()->alert_email), Setting::getSettings()->site_name);
2016-09-20 07:20:10 -07:00
$m->replyTo(config('mail.reply_to.address'), config('mail.reply_to.name'));
$m->subject(trans('mail.Expiring_Assets_Report'));
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
if (count($expiring_licenses) > 0) {
2016-09-20 07:20:10 -07:00
$this->info('Report sent to '.Setting::getSettings()->alert_email);
2016-06-22 12:27:41 -07:00
\Mail::send('emails.expiring-licenses-report', $license_data, function ($m) {
$m->to(explode(',', Setting::getSettings()->alert_email), Setting::getSettings()->site_name);
2016-09-20 07:20:10 -07:00
$m->replyTo(config('mail.reply_to.address'), config('mail.reply_to.name'));
$m->subject(trans('mail.Expiring_Licenses_Report'));
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
} else {
2016-03-25 01:18:05 -07:00
2016-06-22 12:27:41 -07:00
if (Setting::getSettings()->alert_email=='') {
echo "Could not send email. No alert email configured in settings. \n";
} elseif (Setting::getSettings()->alerts_enabled!=1) {
echo "Alerts are disabled in the settings. No mail will be sent. \n";
}
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
}