Asset.php
Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
7 use Config;
10 use Log;
11 use Parsedown;
13 use DateTime;
15 
21 class Asset extends Depreciable
22 {
23  use SoftDeletes;
24 
30  protected $table = 'assets';
31 
39  protected $injectUniqueIdentifier = true;
40  use ValidatingTrait;
41 
42  protected $rules = [
43  'name' => 'min:2|max:255',
44  'model_id' => 'required|integer',
45  'status_id' => 'required|integer',
46  'company_id' => 'integer',
47  'warranty_months' => 'integer|min:0|max:240',
48  'physical' => 'integer',
49  'checkout_date' => 'date|max:10|min:10',
50  'checkin_date' => 'date|max:10|min:10',
51  'supplier_id' => 'integer',
52  'asset_tag' => 'required|min:2|max:255|unique:assets,asset_tag,NULL,deleted_at',
53  'status' => 'integer',
54  ];
55 
56 
62  protected $fillable = ['name','model_id','status_id','asset_tag'];
63 
64 
65  public function company()
66  {
67  return $this->belongsTo('\App\Models\Company', 'company_id');
68  }
69 
70 
74  public function checkOutToUser($user, $admin, $checkout_at = null, $expected_checkin = null, $note = null, $name = null)
75  {
76 
77  if ($expected_checkin) {
78  $this->expected_checkin = $expected_checkin ;
79  }
80 
81  $this->last_checkout = $checkout_at;
82 
83  $this->assigneduser()->associate($user);
84  $this->name = $name;
85 
86  $settings = Setting::getSettings();
87 
88  if ($this->requireAcceptance()) {
89  $this->accepted="pending";
90  }
91 
92  if (!$user) {
93  return false;
94  }
95 
96  if ($this->save()) {
97 
98  $log_id = $this->createCheckoutLog($checkout_at, $admin, $user, $expected_checkin, $note);
99 
100  if ((($this->requireAcceptance()=='1') || ($this->getEula())) && ($user->email!='')) {
101  $this->checkOutNotifyMail($log_id, $user, $checkout_at, $expected_checkin, $note);
102  }
103 
104  if ($settings->slack_endpoint) {
105  $this->checkOutNotifySlack($settings, $admin, $note);
106  }
107  return true;
108 
109  }
110  return false;
111 
112  }
113 
114  public function checkOutNotifyMail($log_id, $user, $checkout_at, $expected_checkin, $note)
115  {
116 
117  $data['log_id'] = $log_id;
118  $data['eula'] = $this->getEula();
119  $data['first_name'] = $user->first_name;
120  $data['item_name'] = $this->showAssetName();
121  $data['checkout_date'] = $checkout_at;
122  $data['expected_checkin'] = $expected_checkin;
123  $data['item_tag'] = $this->asset_tag;
124  $data['note'] = $note;
125  $data['item_serial'] = $this->serial;
126  $data['require_acceptance'] = $this->requireAcceptance();
127 
128  if ((($this->requireAcceptance()=='1') || ($this->getEula())) && (!config('app.lock_passwords'))) {
129 
130  \Mail::send('emails.accept-asset', $data, function ($m) use ($user) {
131  $m->to($user->email, $user->first_name . ' ' . $user->last_name);
132  $m->subject('Confirm asset delivery');
133  });
134  }
135 
136  }
137 
138  public function checkOutNotifySlack($settings, $admin, $note = null)
139  {
140 
141  if ($settings->slack_endpoint) {
142 
143  $slack_settings = [
144  'username' => $settings->botname,
145  'channel' => $settings->slack_channel,
146  'link_names' => true
147  ];
148 
149  $client = new \Maknz\Slack\Client($settings->slack_endpoint, $slack_settings);
150 
151  try {
152  $client->attach([
153  'color' => 'good',
154  'fields' => [
155  [
156  'title' => 'Checked Out:',
157  'value' => 'HARDWARE asset <'.config('app.url').'/hardware/'.$this->id.'/view'.'|'.$this->showAssetName().'> checked out to <'.config('app.url').'/admin/users/'.$this->assigned_to.'/view|'.$this->assigneduser->fullName().'> by <'.config('app.url').'/hardware/'.$this->id.'/view'.'|'.$admin->fullName().'>.'
158  ],
159  [
160  'title' => 'Note:',
161  'value' => e($note)
162  ],
163  ]
164  ])->send('Asset Checked Out');
165 
166  } catch (Exception $e) {
167  print_r($e);
168  }
169  }
170 
171  }
172 
173 
174  public function validationRules($id = '0')
175  {
176  return $this->rules;
177  }
178 
179  public function createCheckoutLog($checkout_at = null, $admin, $user, $expected_checkin = null, $note = null)
180  {
181 
182  $logaction = new \App\Models\Actionlog();
183  $logaction->asset_id = $this->id;
184  $logaction->checkedout_to = $this->assigned_to;
185  $logaction->asset_type = 'hardware';
186  if ($user) {
187  $logaction->location_id = $user->location_id;
188  }
189 
190  $logaction->adminlog()->associate($admin);
191  $logaction->note = $note;
192  if ($checkout_at) {
193  $logaction->created_at = $checkout_at;
194  }
195  $log = $logaction->logaction('checkout');
196  return $logaction->id;
197  }
198 
199 
203  public function depreciation()
204  {
205  return $this->model->belongsTo('\App\Models\Depreciation', 'depreciation_id');
206  }
207 
211  public function get_depreciation()
212  {
213  return $this->model->depreciation;
214  }
215 
219  public function uploads()
220  {
221 
222  return $this->hasMany('\App\Models\Actionlog', 'asset_id')
223  ->where('asset_type', '=', 'hardware')
224  ->where('action_type', '=', 'uploaded')
225  ->whereNotNull('filename')
226  ->orderBy('created_at', 'desc');
227  }
228 
229  public function assigneduser()
230  {
231  return $this->belongsTo('\App\Models\User', 'assigned_to')
232  ->withTrashed();
233  }
234 
238  public function assetloc()
239  {
240  if ($this->assigneduser) {
241  return $this->assigneduser->userloc();
242  } else {
243  return $this->belongsTo('\App\Models\Location', 'rtd_location_id');
244  }
245  }
246 
250  public function defaultLoc()
251  {
252  return $this->belongsTo('\App\Models\Location', 'rtd_location_id');
253  }
254 
258  public function assetlog()
259  {
260  return $this->hasMany('\App\Models\Actionlog', 'asset_id')
261  ->where('asset_type', '=', 'hardware')
262  ->orderBy('created_at', 'desc')
263  ->withTrashed();
264  }
265 
266 
275  public function assetmaintenances()
276  {
277 
278  return $this->hasMany('\App\Models\AssetMaintenance', 'asset_id')
279  ->orderBy('created_at', 'desc')
280  ->withTrashed();
281  }
282 
286  public function adminuser()
287  {
288  return $this->belongsTo('\App\Models\User', 'user_id');
289  }
290 
294  public static function assetcount()
295  {
296 
297  return Asset::where('physical', '=', '1')
298  ->whereNull('deleted_at', 'and')
299  ->count();
300  }
301 
305  public static function availassetcount()
306  {
307 
308  return Asset::RTD()
309  ->whereNull('deleted_at')
310  ->count();
311 
312  }
313 
317  public static function getRequestable()
318  {
319 
320  return Asset::Requestable()
321  ->whereNull('deleted_at')
322  ->count();
323 
324  }
325 
329  public function assetstatus()
330  {
331  return $this->belongsTo('\App\Models\Statuslabel', 'status_id');
332  }
333 
337  public function showAssetName()
338  {
339 
340  if ($this->name == '') {
341  return $this->model->name;
342  } else {
343  return $this->name;
344  }
345  }
346 
347  public function warrantee_expires()
348  {
349  $date = date_create($this->purchase_date);
350  date_add($date, date_interval_create_from_date_string($this->warranty_months . ' months'));
351  return date_format($date, 'Y-m-d');
352  }
353 
354 
355  public function model()
356  {
357  return $this->belongsTo('\App\Models\AssetModel', 'model_id')->withTrashed();
358  }
359 
360  public static function getExpiringWarrantee($days = 30)
361  {
362 
363  return Asset::where('archived', '=', '0')
364  ->whereNotNull('warranty_months')
365  ->whereNotNull('purchase_date')
366  ->whereNull('deleted_at')
367  ->whereRaw(\DB::raw('DATE_ADD(`purchase_date`,INTERVAL `warranty_months` MONTH) <= DATE(NOW() + INTERVAL '
368  . $days
369  . ' DAY) AND DATE_ADD(`purchase_date`,INTERVAL `warranty_months` MONTH) > NOW()'))
370  ->orderBy('purchase_date', 'ASC')
371  ->get();
372  }
373 
377  public function licenses()
378  {
379  return $this->belongsToMany('\App\Models\License', 'license_seats', 'asset_id', 'license_id');
380  }
381 
382  public function licenseseats()
383  {
384  return $this->hasMany('\App\Models\LicenseSeat', 'asset_id');
385  }
386 
387  public function supplier()
388  {
389  return $this->belongsTo('\App\Models\Supplier', 'supplier_id');
390  }
391 
392  public function months_until_eol()
393  {
394 
395  $today = date("Y-m-d");
396  $d1 = new DateTime($today);
397  $d2 = new DateTime($this->eol_date());
398 
399  if ($this->eol_date() > $today) {
400  $interval = $d2->diff($d1);
401  } else {
402  $interval = null;
403  }
404 
405  return $interval;
406  }
407 
408  public function eol_date()
409  {
410 
411  if (( $this->purchase_date ) && ( $this->model )) {
412  $date = date_create($this->purchase_date);
413  date_add($date, date_interval_create_from_date_string($this->model->eol . ' months'));
414  return date_format($date, 'Y-m-d');
415  }
416 
417  }
418 
422  public static function autoincrement_asset()
423  {
424 
425  $settings = \App\Models\Setting::getSettings();
426 
427  if ($settings->auto_increment_assets == '1') {
428  $asset_tag = \DB::table('assets')
429  ->where('physical', '=', '1')
430  ->max('id');
431  return $settings->auto_increment_prefix.($asset_tag + 1);
432  } else {
433  return false;
434  }
435  }
436 
437  public function checkin_email()
438  {
439  return $this->model->category->checkin_email;
440  }
441 
442  public function requireAcceptance()
443  {
444  return $this->model->category->require_acceptance;
445  }
446 
447  public function getEula()
448  {
449 
450  $Parsedown = new \Parsedown();
451 
452  if ($this->model->category->eula_text) {
453  return $Parsedown->text(e($this->model->category->eula_text));
454  } elseif ($this->model->category->use_default_eula == '1') {
455  return $Parsedown->text(e(Setting::getSettings()->default_eula_text));
456  } else {
457  return null;
458  }
459 
460  }
461 
462 
477  public function scopeHardware($query)
478  {
479 
480  return $query->where('physical', '=', '1');
481  }
482 
491  public function scopePending($query)
492  {
493 
494  return $query->whereHas('assetstatus', function ($query) {
495 
496  $query->where('deployable', '=', 0)
497  ->where('pending', '=', 1)
498  ->where('archived', '=', 0);
499  });
500  }
501 
510  public function scopeRTD($query)
511  {
512 
513  return $query->whereNULL('assigned_to')
514  ->whereHas('assetstatus', function ($query) {
515 
516  $query->where('deployable', '=', 1)
517  ->where('pending', '=', 0)
518  ->where('archived', '=', 0);
519  });
520  }
521 
530  public function scopeUndeployable($query)
531  {
532 
533  return $query->whereHas('assetstatus', function ($query) {
534 
535  $query->where('deployable', '=', 0)
536  ->where('pending', '=', 0)
537  ->where('archived', '=', 0);
538  });
539  }
540 
549  public function scopeArchived($query)
550  {
551 
552  return $query->whereHas('assetstatus', function ($query) {
553 
554  $query->where('deployable', '=', 0)
555  ->where('pending', '=', 0)
556  ->where('archived', '=', 1);
557  });
558  }
559 
568  public function scopeDeployed($query)
569  {
570 
571  return $query->where('assigned_to', '>', '0');
572  }
573 
582  public function scopeRequestableAssets($query)
583  {
584 
585  return $query->where('requestable', '=', 1)
586  ->whereHas('assetstatus', function ($query) {
587 
588  $query->where('deployable', '=', 1)
589  ->where('pending', '=', 0)
590  ->where('archived', '=', 0);
591  });
592  }
593 
594 
603  public function scopeDeleted($query)
604  {
605  return $query->whereNotNull('deleted_at');
606  }
607 
619  public function scopeInModelList($query, array $modelIdListing)
620  {
621  return $query->whereIn('model_id', $modelIdListing);
622  }
623 
631  public function scopeNotYetAccepted($query)
632  {
633  return $query->where("accepted", "=", "pending");
634  }
635 
643  public function scopeRejected($query)
644  {
645  return $query->where("accepted", "=", "rejected");
646  }
647 
648 
656  public function scopeAccepted($query)
657  {
658  return $uery->where("accepted", "=", "accepted");
659  }
660 
661 
670  public function scopeTextSearch($query, $search)
671  {
672  $search = explode(' OR ', $search);
673 
674  return $query->where(function ($query) use ($search) {
675 
676  foreach ($search as $search) {
677  $query->whereHas('model', function ($query) use ($search) {
678  $query->whereHas('category', function ($query) use ($search) {
679  $query->where(function ($query) use ($search) {
680  $query->where('categories.name', 'LIKE', '%'.$search.'%')
681  ->orWhere('models.name', 'LIKE', '%'.$search.'%');
682  });
683  });
684  })->orWhere(function ($query) use ($search) {
685  $query->whereHas('assetstatus', function ($query) use ($search) {
686  $query->where('status_labels.name', 'LIKE', '%'.$search.'%');
687  });
688  })->orWhere(function ($query) use ($search) {
689  $query->whereHas('company', function ($query) use ($search) {
690  $query->where('companies.name', 'LIKE', '%'.$search.'%');
691  });
692  })->orWhere(function ($query) use ($search) {
693  $query->whereHas('defaultLoc', function ($query) use ($search) {
694  $query->where('locations.name', 'LIKE', '%'.$search.'%');
695  });
696  })->orWhere(function ($query) use ($search) {
697  $query->whereHas('assigneduser', function ($query) use ($search) {
698  $query->where(function ($query) use ($search) {
699  $query->where('users.first_name', 'LIKE', '%'.$search.'%')
700  ->orWhere('users.last_name', 'LIKE', '%'.$search.'%')
701  ->orWhere(function ($query) use ($search) {
702  $query->whereHas('userloc', function ($query) use ($search) {
703  $query->where('locations.name', 'LIKE', '%'.$search.'%');
704  });
705  });
706  });
707  });
708  })->orWhere('assets.name', 'LIKE', '%'.$search.'%')
709  ->orWhere('assets.asset_tag', 'LIKE', '%'.$search.'%')
710  ->orWhere('assets.serial', 'LIKE', '%'.$search.'%')
711  ->orWhere('assets.order_number', 'LIKE', '%'.$search.'%')
712  ->orWhere('assets.notes', 'LIKE', '%'.$search.'%');
713  }
714  foreach (CustomField::all() as $field) {
715  $query->orWhere($field->db_column_name(), 'LIKE', "%$search%");
716  }
717  });
718  }
719 
728  public function scopeOrderModels($query, $order)
729  {
730  return $query->join('models', 'assets.model_id', '=', 'models.id')->orderBy('models.name', $order);
731  }
732 
733 
742  public function scopeOrderAssigned($query, $order)
743  {
744  return $query->join('users', 'assets.assigned_to', '=', 'users.id')->orderBy('users.first_name', $order)->orderBy('users.last_name', $order);
745  }
746 
755  public function scopeOrderStatus($query, $order)
756  {
757  return $query->join('status_labels', 'assets.status_id', '=', 'status_labels.id')->orderBy('status_labels.name', $order);
758  }
759 
768  public function scopeOrderCompany($query, $order)
769  {
770  return $query->leftJoin('companies', 'assets.company_id', '=', 'companies.id')->orderBy('companies.name', $order);
771  }
772 
781  public function scopeOrderCategory($query, $order)
782  {
783  return $query->join('models', 'assets.model_id', '=', 'models.id')
784  ->join('categories', 'models.category_id', '=', 'categories.id')
785  ->orderBy('categories.name', $order);
786  }
787 
798  public function scopeOrderLocation($query, $order)
799  {
800  return $query->join('locations', 'locations.id', '=', 'assets.rtd_location_id')->orderBy('locations.name', $order);
801  }
802 }
defaultLoc()
Get the asset&#39;s location based on default RTD location.
Definition: Asset.php:250
scopeOrderStatus($query, $order)
Query builder scope to order on status.
Definition: Asset.php:755
scopeHardware($query)
BEGIN QUERY SCOPES
Definition: Asset.php:477
scopeUndeployable($query)
Query builder scope for Undeployable assets.
Definition: Asset.php:530
scopeOrderCompany($query, $order)
Query builder scope to order on company.
Definition: Asset.php:768
scopeRTD($query)
Query builder scope for RTD assets.
Definition: Asset.php:510
assetstatus()
Get total assets.
Definition: Asset.php:329
get_depreciation()
Get depreciation attribute from associated asset model.
Definition: Asset.php:211
uploads()
Get uploads for this asset.
Definition: Asset.php:219
createCheckoutLog($checkout_at=null, $admin, $user, $expected_checkin=null, $note=null)
Definition: Asset.php:179
validationRules($id= '0')
Definition: Asset.php:174
checkOutToUser($user, $admin, $checkout_at=null, $expected_checkin=null, $note=null, $name=null)
Checkout asset.
Definition: Asset.php:74
static assetcount()
Get total assets.
Definition: Asset.php:294
static autoincrement_asset()
Get auto-increment.
Definition: Asset.php:422
adminuser()
Get action logs for this asset.
Definition: Asset.php:286
scopeNotYetAccepted($query)
Query builder scope to get not-yet-accepted assets.
Definition: Asset.php:631
showAssetName()
Get name for EULA.
Definition: Asset.php:337
static getExpiringWarrantee($days=30)
Definition: Asset.php:360
Model for Assets.
Definition: Asset.php:21
$injectUniqueIdentifier
Definition: Asset.php:39
scopeArchived($query)
Query builder scope for Archived assets.
Definition: Asset.php:549
scopeDeployed($query)
Query builder scope for Deployed assets.
Definition: Asset.php:568
assetmaintenances()
assetmaintenances Get improvements for this asset
Definition: Asset.php:275
static getSettings()
Definition: Setting.php:33
scopeDeleted($query)
Query builder scope for Deleted assets.
Definition: Asset.php:603
scopeAccepted($query)
Query builder scope to get accepted assets.
Definition: Asset.php:656
static availassetcount()
Get total assets not checked out.
Definition: Asset.php:305
scopeRejected($query)
Query builder scope to get rejected assets.
Definition: Asset.php:643
scopePending($query)
Query builder scope for pending assets.
Definition: Asset.php:491
scopeOrderModels($query, $order)
Query builder scope to order on model.
Definition: Asset.php:728
checkOutNotifySlack($settings, $admin, $note=null)
Definition: Asset.php:138
assetlog()
Get action logs for this asset.
Definition: Asset.php:258
scopeTextSearch($query, $search)
Query builder scope to search on text for complex Bootstrap Tables API.
Definition: Asset.php:670
checkOutNotifyMail($log_id, $user, $checkout_at, $expected_checkin, $note)
Definition: Asset.php:114
depreciation()
Set depreciation relationship.
Definition: Asset.php:203
scopeOrderCategory($query, $order)
Query builder scope to order on category.
Definition: Asset.php:781
scopeInModelList($query, array $modelIdListing)
scopeInModelList Get all assets in the provided listing of model ids
Definition: Asset.php:619
static getRequestable()
Get requestable assets.
Definition: Asset.php:317
licenses()
Get the license seat information.
Definition: Asset.php:377
scopeRequestableAssets($query)
Query builder scope for Requestable assets.
Definition: Asset.php:582
assetloc()
Get the asset&#39;s location based on the assigned user.
Definition: Asset.php:238
scopeOrderAssigned($query, $order)
Query builder scope to order on assigned user.
Definition: Asset.php:742
scopeOrderLocation($query, $order)
Query builder scope to order on model.
Definition: Asset.php:798