mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 05:34:06 -08:00
987536930c
* Fix some n+1 problems * Use route in notification dropdown to make sure we link to correct page * Work on better UI support for checkout to non-user. Fix links on index bootstrap table, work towards eliminating assignedUser * Remove Asset::assigneduser() relationship. Instead add a checkedOutToUser() method and/or port to using assignedTo() * Adjust string to fit new reality * Fix #3780. Move the consumables getDataView method to the ApiController. Not entirely RESTful, but it's a weird method that probably doesn't need its own controller and the functionality would be strange to stack on the userscontroller... * Fix file uploads to assets and restore the delete route. * Add asset maintence edit action to index. * Suppliers asset list should link to the related asset, not to the supplier with same ID. * Asset models page should use polymorphic formatter on assigned to to better handle assorted item types. * Comment out more assigneduser fallacy until we figure out the query builder approach to searching for location text.
361 lines
12 KiB
PHP
361 lines
12 KiB
PHP
<?php
|
|
namespace App\Presenters;
|
|
|
|
use App\Models\CustomField;
|
|
use DateTime;
|
|
|
|
/**
|
|
* Class AssetPresenter
|
|
* @package App\Presenters
|
|
*/
|
|
class AssetPresenter extends Presenter
|
|
{
|
|
|
|
/**
|
|
* Json Column Layout for bootstrap table
|
|
* @return string
|
|
*/
|
|
public static function dataTableLayout()
|
|
{
|
|
$layout = [
|
|
[
|
|
"field" => "checkbox",
|
|
"checkbox" => true
|
|
], [
|
|
"field" => "id",
|
|
"searchable" => false,
|
|
"sortable" => true,
|
|
"switchable" => true,
|
|
"title" => trans('general.id'),
|
|
"visible" => false
|
|
], [
|
|
"field" => "company",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"switchable" => true,
|
|
"title" => trans('general.company'),
|
|
"visible" => false,
|
|
"formatter" => 'assetCompanyObjFilterFormatter'
|
|
], [
|
|
"field" => "name",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/hardware/form.name'),
|
|
"visible" => true,
|
|
"formatter" => "hardwareLinkFormatter"
|
|
], [
|
|
"field" => "image",
|
|
"searchable" => false,
|
|
"sortable" => true,
|
|
"switchable" => true,
|
|
"title" => trans('admin/hardware/table.image'),
|
|
"visible" => true,
|
|
"formatter" => "imageFormatter"
|
|
], [
|
|
"field" => "asset_tag",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/hardware/table.asset_tag'),
|
|
"visible" => true,
|
|
"formatter" => "hardwareLinkFormatter"
|
|
], [
|
|
"field" => "serial",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/hardware/form.serial'),
|
|
"visible" => true,
|
|
"formatter" => "hardwareLinkFormatter"
|
|
], [
|
|
"field" => "model",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/hardware/form.model'),
|
|
"visible" => true,
|
|
"formatter" => "modelsLinkObjFormatter"
|
|
], [
|
|
"field" => "model_number",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/models/table.modelnumber'),
|
|
"visible" => false
|
|
], [
|
|
"field" => "category",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('general.category'),
|
|
"visible" => true,
|
|
"formatter" => "categoriesLinkObjFormatter"
|
|
], [
|
|
"field" => "status_label",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/hardware/table.status'),
|
|
"visible" => true,
|
|
"formatter" => "statuslabelsLinkObjFormatter"
|
|
], [
|
|
"field" => "assigned_to",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/hardware/form.checkedout_to'),
|
|
"visible" => true,
|
|
"formatter" => "polymorphicItemFormatter"
|
|
], [
|
|
"field" => "employee_number",
|
|
"searchable" => false,
|
|
"sortable" => false,
|
|
"title" => trans('admin/users/table.employee_num'),
|
|
"visible" => false,
|
|
"formatter" => "employeeNumFormatter"
|
|
],[
|
|
"field" => "location",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('admin/hardware/table.location'),
|
|
"visible" => true,
|
|
"formatter" => "locationsLinkObjFormatter"
|
|
], [
|
|
"field" => "manufacturer",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('general.manufacturer'),
|
|
"visible" => false,
|
|
"formatter" => "manufacturersLinkObjFormatter"
|
|
],[
|
|
"field" => "supplier",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('general.supplier'),
|
|
"visible" => false,
|
|
"formatter" => "suppliersLinkObjFormatter"
|
|
], [
|
|
"field" => "purchase_date",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"visible" => false,
|
|
"title" => trans('general.purchase_date'),
|
|
"formatter" => "dateDisplayFormatter"
|
|
], [
|
|
"field" => "purchase_cost",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"title" => trans('general.purchase_cost'),
|
|
], [
|
|
"field" => "order_number",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"visible" => false,
|
|
"title" => trans('general.order_number'),
|
|
'formatter' => "orderNumberObjFilterFormatter"
|
|
], [
|
|
"field" => "notes",
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"visible" => false,
|
|
"title" => trans('general.notes'),
|
|
], [
|
|
"field" => "created_at",
|
|
"searchable" => false,
|
|
"sortable" => true,
|
|
"visible" => false,
|
|
"title" => trans('general.created_at'),
|
|
"formatter" => "dateDisplayFormatter"
|
|
], [
|
|
"field" => "updated_at",
|
|
"searchable" => false,
|
|
"sortable" => true,
|
|
"visible" => false,
|
|
"title" => trans('general.updated_at'),
|
|
"formatter" => "dateDisplayFormatter"
|
|
], [
|
|
"field" => "last_checkout",
|
|
"searchable" => false,
|
|
"sortable" => true,
|
|
"visible" => false,
|
|
"title" => trans('admin/hardware/table.checkout_date'),
|
|
"formatter" => "dateDisplayFormatter"
|
|
], [
|
|
"field" => "expected_checkin",
|
|
"searchable" => false,
|
|
"sortable" => true,
|
|
"visible" => false,
|
|
"title" => trans('admin/hardware/form.expected_checkin'),
|
|
"formatter" => "dateDisplayFormatter"
|
|
],
|
|
];
|
|
|
|
// This looks complicated, but we have to confirm that the custom fields exist in custom fieldsets
|
|
// *and* those fieldsets are associated with models, otherwise we'll trigger
|
|
// javascript errors on the bootstrap tables side of things, since we're asking for properties
|
|
// on fields that will never be passed through the REST API since they're not associated with
|
|
// models. We only pass the fieldsets that pertain to each asset (via their model) so that we
|
|
// don't junk up the REST API with tons of custom fields that don't apply
|
|
|
|
$fields = CustomField::whereHas('fieldset', function ($query) {
|
|
$query->whereHas('models');
|
|
})->get();
|
|
|
|
foreach ($fields as $field) {
|
|
$layout[] = [
|
|
"field" => 'custom_fields.'.$field->convertUnicodeDbSlug(),
|
|
"searchable" => true,
|
|
"sortable" => true,
|
|
"switchable" => true,
|
|
"title" => ($field->field_encrypted=='1') ?'<i class="fa fa-lock"></i> '.e($field->name) : e($field->name),
|
|
"formatter" => "customFieldsFormatter"
|
|
];
|
|
|
|
}
|
|
|
|
$layout[] = [
|
|
"field" => "checkincheckout",
|
|
"searchable" => false,
|
|
"sortable" => false,
|
|
"switchable" => true,
|
|
"title" => 'Checkin/Checkout',
|
|
"visible" => true,
|
|
"formatter" => "hardwareInOutFormatter",
|
|
];
|
|
|
|
$layout[] = [
|
|
"field" => "actions",
|
|
"searchable" => false,
|
|
"sortable" => false,
|
|
"switchable" => false,
|
|
"title" => trans('table.actions'),
|
|
"formatter" => "hardwareActionsFormatter",
|
|
];
|
|
|
|
return json_encode($layout);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Generate html link to this items name.
|
|
* @return string
|
|
*/
|
|
public function nameUrl()
|
|
{
|
|
return (string) link_to_route('hardware.show', e($this->name), $this->id);
|
|
}
|
|
|
|
public function modelUrl()
|
|
{
|
|
if ($this->model->model) {
|
|
return $this->model->model->present()->nameUrl();
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Generate img tag to this items image.
|
|
* @return mixed|string
|
|
*/
|
|
public function imageUrl()
|
|
{
|
|
$imagePath = '';
|
|
if ($this->image && !empty($this->image)) {
|
|
$imagePath = $this->image;
|
|
} elseif ($this->model && !empty($this->model->image)) {
|
|
$imagePath = $this->model->image;
|
|
}
|
|
$url = config('app.url');
|
|
if (!empty($imagePath)) {
|
|
$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 {
|
|
return $this->name.' ('.$this->asset_tag.')';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Helper for notification polymorphism.
|
|
* @return mixed
|
|
*/
|
|
public function fullName()
|
|
{
|
|
return $this->name();
|
|
}
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
public function statusText()
|
|
{
|
|
if ($this->model->assignedTo) {
|
|
return trans('general.deployed');
|
|
}
|
|
return $this->model->assetstatus->name;
|
|
}
|
|
/**
|
|
* 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');
|
|
}
|
|
|
|
/**
|
|
* Url to view this item.
|
|
* @return string
|
|
*/
|
|
public function viewUrl()
|
|
{
|
|
return route('hardware.show', $this->id);
|
|
}
|
|
|
|
public function glyph()
|
|
{
|
|
return '<i class="fa fa-barcode"></i>';
|
|
}
|
|
}
|
|
|