2016-12-23 17:52:00 -08:00
|
|
|
<?php
|
|
|
|
namespace App\Presenters;
|
2016-12-29 14:02:18 -08:00
|
|
|
|
2016-12-23 17:52:00 -08:00
|
|
|
use App\Helpers\Helper;
|
|
|
|
use App\Models\SnipeModel;
|
|
|
|
use DateTime;
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AssetPresenter
|
|
|
|
* @package App\Presenters
|
|
|
|
*/
|
|
|
|
class AssetPresenter extends Presenter
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bootstrap Table Bits
|
|
|
|
* @param array $all_custom_fields Preloaded cache of custom fields
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function forDataTable($all_custom_fields)
|
|
|
|
{
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
$inout = '';
|
|
|
|
$actions = '<div style="white-space: nowrap;">';
|
|
|
|
if ($this->model->deleted_at=='') {
|
|
|
|
if (Gate::allows('create', $this->model)) {
|
|
|
|
$actions .= Helper::generateDatatableButton('clone', route('clone/hardware', $this->model->id));
|
|
|
|
}
|
|
|
|
if (Gate::allows('update', $this->model)) {
|
|
|
|
$actions .= Helper::generateDatatableButton('edit', route('hardware.edit', $this->model->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Gate::allows('delete', $this->model)) {
|
|
|
|
$actions .= Helper::generateDatatableButton(
|
|
|
|
'delete',
|
|
|
|
route('hardware.destroy', $this->model->id),
|
|
|
|
true, /*enabled*/
|
|
|
|
trans('admin/hardware/message.delete.confirm'),
|
|
|
|
$this->model->asset_tag
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} elseif ($this->model->model->deleted_at=='') {
|
|
|
|
$actions .= Helper::generateDatatableButton('restore', route('restore/hardware', $this->model->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
$actions .= '</div>';
|
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
if (($this->model->availableForCheckout())) {
|
2016-12-23 17:52:00 -08:00
|
|
|
if (Gate::allows('checkout', $this->model)) {
|
2016-12-29 14:02:18 -08:00
|
|
|
$inout = '<a href="' . route(
|
|
|
|
'checkout/hardware',
|
|
|
|
$this->model->id
|
|
|
|
) . '" class="btn btn-info btn-sm" title="Checkout this asset to a user" data-toggle="tooltip">' . trans('general.checkout') . '</a>';
|
2016-12-23 17:52:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2016-12-26 15:19:04 -08:00
|
|
|
if (!empty($this->model->assigned_to)) {
|
|
|
|
if (Gate::allows('checkin', $this->model)) {
|
2016-12-29 14:02:18 -08:00
|
|
|
$inout = '<a href="' . route(
|
|
|
|
'checkin/hardware',
|
|
|
|
$this->model->id
|
|
|
|
) . '" class="btn btn-primary btn-sm" title="Checkin this asset" data-toggle="tooltip">' . trans('general.checkin') . '</a>';
|
2016-12-26 15:19:04 -08:00
|
|
|
}
|
2016-12-23 17:52:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
$results['checkbox'] = '<div class="text-center"><input type="checkbox" name="edit_asset['.$this->id.']" class="one_required"></div>';
|
|
|
|
$results['id'] = $this->id;
|
|
|
|
|
|
|
|
$results['name'] = $this->nameUrl();
|
|
|
|
$results['asset_tag'] = $this->assetTagUrl();
|
|
|
|
$results['serial'] = $this->serial;
|
|
|
|
$results['image'] = $this->imageUrl();
|
|
|
|
// Presets for when conditionals fail.
|
2016-12-23 19:23:07 -08:00
|
|
|
$results['model'] = $this->modelUrl();
|
|
|
|
$results['model_number'] = $this->model->model_number;
|
|
|
|
$results['category'] = $this->categoryUrl();
|
|
|
|
$results['manufacturer'] = $this->manufacturerUrl();
|
2016-12-23 17:52:00 -08:00
|
|
|
$results['status_label'] = '';
|
|
|
|
$results['assigned_to'] = '';
|
2017-01-03 16:55:43 -08:00
|
|
|
if ($assigned = $this->model->assignedTo) {
|
2016-12-29 18:12:27 -08:00
|
|
|
$results['assigned_to'] = $assigned->present()->glyph() . ' ' . $assigned->present()->nameUrl();
|
2016-12-23 17:52:00 -08:00
|
|
|
}
|
2017-01-03 16:55:43 -08:00
|
|
|
$results['status_label'] = $this->statusText();
|
2016-12-23 17:52:00 -08:00
|
|
|
$results['location'] = '';
|
2016-12-27 16:24:41 -08:00
|
|
|
if (isset($assigned) and !empty($assignedLoc = $this->model->assetLoc)) {
|
2016-12-23 17:52:00 -08:00
|
|
|
$results['location'] = $assignedLoc->present()->nameUrl();
|
2016-12-29 14:02:18 -08:00
|
|
|
} elseif (!empty($this->model->defaultLoc)) {
|
2016-12-23 17:52:00 -08:00
|
|
|
$results['location'] = $this->model->defaultLoc->present()->nameUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
$results['eol'] = $this->eol_date() ?: '';
|
|
|
|
$results['purchase_cost'] = Helper::formatCurrencyOutput($this->purchase_cost);
|
|
|
|
$results['purchase_date'] = $this->purchase_date ?: '';
|
2016-12-23 19:23:07 -08:00
|
|
|
$results['notes'] = $this->notes;
|
2017-01-11 18:25:53 -08:00
|
|
|
$results['order_number'] = $this->order_number;
|
2016-12-29 14:02:18 -08:00
|
|
|
if (!empty($this->order_number)) {
|
2016-12-23 17:52:00 -08:00
|
|
|
$results['order_number'] = link_to_route('hardware.index', $this->order_number, ['order_number' => $this->order_number]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$results['last_checkout'] = $this->last_checkout ?: '';
|
|
|
|
$results['expected_checkin'] = $this->expected_checkin ?: '';
|
|
|
|
$results['created_at'] = '';
|
2016-12-29 14:02:18 -08:00
|
|
|
if (!empty($this->created_at)) {
|
2016-12-23 17:52:00 -08:00
|
|
|
$results['created_at'] = $this->created_at->format('F j, Y h:iA');
|
|
|
|
}
|
2016-12-23 19:23:07 -08:00
|
|
|
$results['companyName'] = $this->companyUrl();
|
|
|
|
$results['actions'] = $actions;
|
|
|
|
$results['change'] = $inout;
|
2016-12-23 17:52:00 -08:00
|
|
|
|
|
|
|
|
|
|
|
// Custom Field bits
|
|
|
|
foreach ($all_custom_fields as $field) {
|
|
|
|
$column_name = $field->db_column_name();
|
|
|
|
|
|
|
|
if ($field->isFieldDecryptable($this->model->{$column_name})) {
|
|
|
|
|
|
|
|
if (Gate::allows('admin')) {
|
|
|
|
if (($field->format=='URL') && ($this->model->{$column_name}!='')) {
|
|
|
|
$row[$column_name] = '<a href="'.Helper::gracefulDecrypt($field, $this->model->{$column_name}).'" target="_blank">'.Helper::gracefulDecrypt($field, $this->model->{$column_name}).'</a>';
|
|
|
|
} else {
|
|
|
|
$row[$column_name] = Helper::gracefulDecrypt($field, $this->model->{$column_name});
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$row[$field->db_column_name()] = strtoupper(trans('admin/custom_fields/general.encrypted'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (($field->format=='URL') && ($this->model->{$field->db_column_name()}!='')) {
|
|
|
|
$row[$field->db_column_name()] = '<a href="'.$this->model->{$field->db_column_name()}.'" target="_blank">'.$this->model->{$field->db_column_name()}.'</a>';
|
|
|
|
} else {
|
|
|
|
$row[$field->db_column_name()] = e($this->model->{$field->db_column_name()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate html link to this items asset tag
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function assetTagUrl()
|
|
|
|
{
|
|
|
|
return (string) link_to_route('hardware.show', e($this->asset_tag), $this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate html link to this items name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function nameUrl()
|
|
|
|
{
|
|
|
|
return (string) link_to_route('hardware.show', e($this->name), $this->id);
|
|
|
|
}
|
|
|
|
|
2016-12-23 19:23:07 -08:00
|
|
|
public function modelUrl()
|
|
|
|
{
|
2016-12-29 14:02:18 -08:00
|
|
|
if ($this->model->model) {
|
2016-12-23 19:23:07 -08:00
|
|
|
return $this->model->model->present()->nameUrl();
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-12-23 17:52:00 -08:00
|
|
|
/**
|
|
|
|
* Generate img tag to this items image.
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
|
|
|
public function imageUrl()
|
|
|
|
{
|
|
|
|
$imagePath = '';
|
2016-12-29 14:02:18 -08:00
|
|
|
if ($this->image && !empty($this->image)) {
|
2016-12-23 17:52:00 -08:00
|
|
|
$imagePath = $this->image;
|
2016-12-29 14:02:18 -08:00
|
|
|
} elseif ($this->model && !empty($this->model->image)) {
|
2016-12-23 17:52:00 -08:00
|
|
|
$imagePath = $this->model->image;
|
|
|
|
}
|
|
|
|
$url = config('app.url');
|
2016-12-29 14:02:18 -08:00
|
|
|
if (!empty($imagePath)) {
|
2016-12-23 17:52:00 -08:00
|
|
|
$imagePath = "<img src='{$url}/uploads/assets/{$imagePath}' height=50 width=50>";
|
|
|
|
}
|
|
|
|
return $imagePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Displayable Name
|
|
|
|
* @return string
|
|
|
|
**/
|
|
|
|
public function name()
|
|
|
|
{
|
|
|
|
if (empty($this->name)) {
|
|
|
|
if (isset($this->model)) {
|
|
|
|
return $this->model->name.' ('.$this->asset_tag.')';
|
|
|
|
}
|
|
|
|
return $this->asset_tag;
|
|
|
|
} else {
|
2016-12-27 12:03:47 -08:00
|
|
|
return $this->name.' ('.$this->asset_tag.')';
|
2016-12-23 17:52:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:19:04 -08:00
|
|
|
/**
|
|
|
|
* Helper for notification polymorphism.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function fullName()
|
|
|
|
{
|
|
|
|
return $this->name();
|
|
|
|
}
|
2016-12-23 17:52:00 -08:00
|
|
|
/**
|
|
|
|
* Returns the date this item hits EOL.
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
public function eol_date()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (( $this->purchase_date ) && ( $this->model )) {
|
|
|
|
$date = date_create($this->purchase_date);
|
|
|
|
date_add($date, date_interval_create_from_date_string($this->model->model->eol . ' months'));
|
|
|
|
return date_format($date, 'Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How many months until this asset hits EOL.
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function months_until_eol()
|
|
|
|
{
|
|
|
|
|
|
|
|
$today = date("Y-m-d");
|
|
|
|
$d1 = new DateTime($today);
|
|
|
|
$d2 = new DateTime($this->eol_date());
|
|
|
|
|
|
|
|
if ($this->eol_date() > $today) {
|
|
|
|
$interval = $d2->diff($d1);
|
|
|
|
} else {
|
|
|
|
$interval = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $interval;
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:55:43 -08:00
|
|
|
public function statusText()
|
|
|
|
{
|
|
|
|
if ($this->model->assignedTo) {
|
|
|
|
return trans('general.deployed');
|
|
|
|
}
|
|
|
|
return $this->model->assetstatus->name;
|
|
|
|
}
|
2016-12-23 17:52:00 -08:00
|
|
|
/**
|
|
|
|
* Date the warantee expires.
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
public function warrantee_expires()
|
|
|
|
{
|
|
|
|
$date = date_create($this->purchase_date);
|
|
|
|
date_add($date, date_interval_create_from_date_string($this->warranty_months . ' months'));
|
|
|
|
return date_format($date, 'Y-m-d');
|
|
|
|
}
|
2016-12-26 15:19:04 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Url to view this item.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function viewUrl()
|
|
|
|
{
|
|
|
|
return route('hardware.show', $this->id);
|
|
|
|
}
|
2016-12-29 18:12:27 -08:00
|
|
|
|
|
|
|
public function glyph()
|
|
|
|
{
|
|
|
|
return '<i class="fa fa-barcode"></i>';
|
|
|
|
}
|
2016-12-23 17:52:00 -08:00
|
|
|
}
|