SendExpirationAlerts.php
Go to the documentation of this file.
1 <?php
2 
3 namespace App\Console\Commands;
7 use DB;
8 
10 
11 class SendExpirationAlerts extends Command {
12 
18  protected $name = 'alerts:expiring';
19 
25  protected $description = 'Check for expiring warrantees and service agreements, and sends out an alert email.';
26 
32  public function __construct()
33  {
34  parent::__construct();
35  }
36 
42  public function fire()
43  {
44 
45  // Expiring Assets
46  $expiring_assets = Asset::getExpiringWarrantee(60);
47  $this->info(count($expiring_assets).' expiring assets');
48 
49  $asset_data['count'] = count($expiring_assets);
50  $asset_data['email_content'] ='';
51  $now = date("Y-m-d");
52 
53 
54  foreach ($expiring_assets as $asset) {
55 
56  $expires = $asset->warrantee_expires();
57  $difference = round(abs(strtotime($expires) - strtotime($now))/86400);
58 
59  if ($difference > 30) {
60  $asset_data['email_content'] .= '<tr style="background-color: #fcffa3;">';
61  } else {
62  $asset_data['email_content'] .= '<tr style="background-color:#d9534f;">';
63  }
64  $asset_data['email_content'] .= '<td><a href="'.config('app.url').'/hardware/'.$asset->id.'/view">';
65  $asset_data['email_content'] .= $asset->showAssetName().'</a></td><td>'.$asset->asset_tag.'</td>';
66  $asset_data['email_content'] .= '<td>'.$asset->warrantee_expires().'</td>';
67  $asset_data['email_content'] .= '<td>'.$difference.' days</td>';
68  $asset_data['email_content'] .= '</tr>';
69  }
70 
71  // Expiring licenses
72  $expiring_licenses = License::getExpiringLicenses(60);
73  $this->info(count($expiring_licenses).' expiring licenses');
74 
75 
76  $license_data['count'] = count($expiring_licenses);
77  $license_data['email_content'] = '';
78 
79  foreach ($expiring_licenses as $license) {
80  $expires = $license->expiration_date;
81  $difference = round(abs(strtotime($expires) - strtotime($now))/86400);
82 
83  if ($difference > 30) {
84  $license_data['email_content'] .= '<tr style="background-color: #fcffa3;">';
85  } else {
86  $license_data['email_content'] .= '<tr style="background-color:#d9534f;">';
87  }
88  $license_data['email_content'] .= '<td><a href="'.config('app.url').'/admin/licenses/'.$license->id.'/view">';
89  $license_data['email_content'] .= $license->name.'</a></td>';
90  $license_data['email_content'] .= '<td>'.$license->expiration_date.'</td>';
91  $license_data['email_content'] .= '<td>'.$difference.' days</td>';
92  $license_data['email_content'] .= '</tr>';
93  }
94 
95  if ((Setting::getSettings()->alert_email!='') && (Setting::getSettings()->alerts_enabled==1)) {
96 
97 
98  if (count($expiring_assets) > 0) {
99  Mail::send('emails.expiring-assets-report', $asset_data, function ($m) {
100  $m->to(explode(',',Setting::getSettings()->alert_email), Setting::getSettings()->site_name);
101  $m->subject('Expiring Assets Report');
102  });
103 
104  }
105 
106  if (count($expiring_licenses) > 0) {
107  Mail::send('emails.expiring-licenses-report', $license_data, function ($m) {
108  $m->to(explode(',',Setting::getSettings()->alert_email), Setting::getSettings()->site_name);
109  $m->subject('Expiring Licenses Report');
110  });
111 
112  }
113 
114 
115  } else {
116 
117  if (Setting::getSettings()->alert_email=='') {
118  echo "Could not send email. No alert email configured in settings. \n";
119  } elseif (Setting::getSettings()->alerts_enabled!=1) {
120  echo "Alerts are disabled in the settings. No mail will be sent. \n";
121  }
122 
123  }
124 
125 
126 
127 
128  }
129 
130 
131 
132 
133 
134 }
static getExpiringWarrantee($days=30)
Definition: Asset.php:360
__construct()
Create a new command instance.
static getSettings()
Definition: Setting.php:33
static getExpiringLicenses($days=60)
Definition: License.php:182