2017-01-13 11:38:25 -08:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2017-01-24 21:04:38 -08:00
|
|
|
use App\Http\Transformers\UsersTransformer;
|
2017-02-08 18:24:55 -08:00
|
|
|
use Gate;
|
2017-03-03 17:29:41 -08:00
|
|
|
use App\Helpers\Helper;
|
2017-02-08 18:24:55 -08:00
|
|
|
|
2017-01-13 11:38:25 -08:00
|
|
|
class AssetsTransformer
|
|
|
|
{
|
2017-03-14 08:37:39 -07:00
|
|
|
public function transformAssets(Collection $assets, $total)
|
2017-01-13 11:38:25 -08:00
|
|
|
{
|
|
|
|
$array = array();
|
|
|
|
foreach ($assets as $asset) {
|
|
|
|
$array[] = self::transformAsset($asset);
|
|
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
|
2017-03-14 08:37:39 -07:00
|
|
|
public function transformAsset(Asset $asset)
|
2017-01-13 11:38:25 -08:00
|
|
|
{
|
|
|
|
$array = [
|
2017-03-14 08:37:39 -07:00
|
|
|
'id' => (int) $asset->id,
|
2017-02-03 02:04:17 -08:00
|
|
|
'name' => e($asset->name),
|
|
|
|
'asset_tag' => e($asset->asset_tag),
|
|
|
|
'serial' => e($asset->serial),
|
2017-03-14 08:37:39 -07:00
|
|
|
'model' => ($asset->model) ? [
|
|
|
|
'id' => (int) $asset->model->id,
|
|
|
|
'name'=> e($asset->model->name)
|
|
|
|
] : null,
|
2017-10-28 15:17:36 -07:00
|
|
|
'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null,
|
2017-12-05 13:03:11 -08:00
|
|
|
'eol' => ($asset->purchase_date!='') ? Helper::getFormattedDateObject($asset->present()->eol_date(), 'date') : null ,
|
2017-03-14 08:37:39 -07:00
|
|
|
'status_label' => ($asset->assetstatus) ? [
|
|
|
|
'id' => (int) $asset->assetstatus->id,
|
2018-01-15 21:03:26 -08:00
|
|
|
'name'=> e($asset->assetstatus->name),
|
|
|
|
'status_type'=> e($asset->assetstatus->getStatuslabelType()),
|
2018-01-23 18:08:54 -08:00
|
|
|
'status_meta' => e($asset->present()->statusMeta),
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
2018-01-24 08:16:05 -08:00
|
|
|
'category' => (($asset->model) && ($asset->model->category)) ? [
|
2017-03-14 08:37:39 -07:00
|
|
|
'id' => (int) $asset->model->category->id,
|
|
|
|
'name'=> e($asset->model->category->name)
|
|
|
|
] : null,
|
2018-01-24 08:16:05 -08:00
|
|
|
'manufacturer' => (($asset->model) && ($asset->model->manufacturer)) ? [
|
2017-03-14 08:37:39 -07:00
|
|
|
'id' => (int) $asset->model->manufacturer->id,
|
|
|
|
'name'=> e($asset->model->manufacturer->name)
|
|
|
|
] : null,
|
2017-04-22 17:14:28 -07:00
|
|
|
'supplier' => ($asset->supplier) ? [
|
|
|
|
'id' => (int) $asset->supplier->id,
|
|
|
|
'name'=> e($asset->supplier->name)
|
|
|
|
] : null,
|
2017-10-28 07:01:06 -07:00
|
|
|
'notes' => ($asset->notes) ? e($asset->notes) : null,
|
2017-10-28 15:53:22 -07:00
|
|
|
'order_number' => ($asset->order_number) ? e($asset->order_number) : null,
|
2017-03-14 08:37:39 -07:00
|
|
|
'company' => ($asset->company) ? [
|
|
|
|
'id' => (int) $asset->company->id,
|
|
|
|
'name'=> e($asset->company->name)
|
|
|
|
] : null,
|
2017-10-28 01:49:13 -07:00
|
|
|
'location' => ($asset->location) ? [
|
|
|
|
'id' => (int) $asset->location->id,
|
|
|
|
'name'=> e($asset->location->name)
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
|
|
|
'rtd_location' => ($asset->defaultLoc) ? [
|
|
|
|
'id' => (int) $asset->defaultLoc->id,
|
|
|
|
'name'=> e($asset->defaultLoc->name)
|
|
|
|
] : null,
|
2017-01-24 22:46:07 -08:00
|
|
|
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
|
2017-09-05 17:54:58 -07:00
|
|
|
'assigned_to' => $this->transformAssignedTo($asset),
|
2017-10-31 07:05:15 -07:00
|
|
|
'warranty_months' => ($asset->warranty_months > 0) ? e($asset->warranty_months . ' ' . trans('admin/hardware/form.months')) : null,
|
2017-03-14 08:37:39 -07:00
|
|
|
'warranty_expires' => ($asset->warranty_months > 0) ? Helper::getFormattedDateObject($asset->warranty_expires, 'date') : null,
|
2017-03-03 17:29:41 -08:00
|
|
|
'created_at' => Helper::getFormattedDateObject($asset->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($asset->updated_at, 'datetime'),
|
2017-12-12 03:03:43 -08:00
|
|
|
'last_audit_date' => Helper::getFormattedDateObject($asset->last_audit_date, 'datetime'),
|
|
|
|
'next_audit_date' => Helper::getFormattedDateObject($asset->next_audit_date, 'date'),
|
2017-10-28 02:31:13 -07:00
|
|
|
'deleted_at' => Helper::getFormattedDateObject($asset->deleted_at, 'datetime'),
|
2017-03-03 17:29:41 -08:00
|
|
|
'purchase_date' => Helper::getFormattedDateObject($asset->purchase_date, 'date'),
|
|
|
|
'last_checkout' => Helper::getFormattedDateObject($asset->last_checkout, 'datetime'),
|
|
|
|
'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'date'),
|
2017-03-14 08:37:39 -07:00
|
|
|
'purchase_cost' => Helper::formatCurrencyOutput($asset->purchase_cost),
|
|
|
|
'user_can_checkout' => (bool) $asset->availableForCheckout(),
|
2017-01-13 11:38:25 -08:00
|
|
|
];
|
|
|
|
|
2017-07-08 17:04:40 -07:00
|
|
|
|
2018-01-25 03:00:54 -08:00
|
|
|
if (($asset->model) && ($asset->model->fieldset) && (count($asset->model->fieldset->fields)> 0)) {
|
2017-07-08 17:04:40 -07:00
|
|
|
$fields_array = array();
|
2017-09-26 16:01:23 -07:00
|
|
|
|
2017-07-08 17:04:40 -07:00
|
|
|
foreach ($asset->model->fieldset->fields as $field) {
|
|
|
|
|
|
|
|
if ($field->isFieldDecryptable($asset->{$field->convertUnicodeDbSlug()})) {
|
|
|
|
$decrypted = \App\Helpers\Helper::gracefulDecrypt($field,$asset->{$field->convertUnicodeDbSlug()});
|
|
|
|
$value = (Gate::allows('superadmin')) ? $decrypted : strtoupper(trans('admin/custom_fields/general.encrypted'));
|
|
|
|
|
2017-07-08 18:44:28 -07:00
|
|
|
$fields_array[$field->name] = [
|
|
|
|
'field' => $field->convertUnicodeDbSlug(),
|
2017-11-21 22:34:53 -08:00
|
|
|
'value' => $value,
|
|
|
|
'field_format' => $field->format,
|
2017-07-08 18:44:28 -07:00
|
|
|
];
|
2017-07-08 17:04:40 -07:00
|
|
|
|
|
|
|
} else {
|
2017-07-08 18:44:28 -07:00
|
|
|
$fields_array[$field->name] = [
|
|
|
|
'field' => $field->convertUnicodeDbSlug(),
|
2017-11-21 22:34:53 -08:00
|
|
|
'value' => $asset->{$field->convertUnicodeDbSlug()},
|
|
|
|
'field_format' => $field->format,
|
2017-07-08 18:44:28 -07:00
|
|
|
];
|
2017-07-08 17:04:40 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2017-07-08 18:44:28 -07:00
|
|
|
$array['custom_fields'] = $fields_array;
|
2017-07-08 17:04:40 -07:00
|
|
|
}
|
2017-08-23 03:28:13 -07:00
|
|
|
} else {
|
|
|
|
$array['custom_fields'] = array();
|
2017-07-08 17:04:40 -07:00
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$permissions_array['available_actions'] = [
|
2017-03-14 08:37:39 -07:00
|
|
|
'checkout' => (bool) Gate::allows('checkout', Asset::class),
|
|
|
|
'checkin' => (bool) Gate::allows('checkin', Asset::class),
|
2017-07-07 18:45:49 -07:00
|
|
|
'clone' => Gate::allows('create', Asset::class) ? true : false,
|
2017-11-22 10:35:24 -08:00
|
|
|
'restore' => false,
|
2017-03-14 08:37:39 -07:00
|
|
|
'update' => (bool) Gate::allows('update', Asset::class),
|
|
|
|
'delete' => (bool) Gate::allows('delete', Asset::class),
|
2017-02-08 18:24:55 -08:00
|
|
|
];
|
|
|
|
|
2017-11-22 10:35:24 -08:00
|
|
|
if ($asset->deleted_at!='') {
|
|
|
|
$permissions_array['available_actions'] = [
|
|
|
|
'checkout' => true,
|
|
|
|
'checkin' => false,
|
|
|
|
'clone' => Gate::allows('create', Asset::class) ? true : false,
|
|
|
|
'restore' => Gate::allows('create', Asset::class) ? true : false,
|
|
|
|
'update' => false,
|
|
|
|
'delete' => false,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$array += $permissions_array;
|
2017-01-13 11:38:25 -08:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2017-03-14 08:37:39 -07:00
|
|
|
public function transformAssetsDatatable($assets)
|
|
|
|
{
|
2017-01-24 18:57:21 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($assets);
|
2017-01-13 11:38:25 -08:00
|
|
|
}
|
2017-09-05 17:54:58 -07:00
|
|
|
|
|
|
|
public function transformAssignedTo($asset)
|
|
|
|
{
|
|
|
|
if ($asset->checkedOutToUser()) {
|
2017-10-28 03:50:02 -07:00
|
|
|
return $asset->assigned ? [
|
|
|
|
'id' => (int) $asset->assigned->id,
|
|
|
|
'username' => e($asset->assigned->username),
|
|
|
|
'name' => e($asset->assigned->getFullNameAttribute()),
|
|
|
|
'first_name'=> e($asset->assigned->first_name),
|
2017-10-28 15:17:36 -07:00
|
|
|
'last_name'=> ($asset->assigned->last_name) ? e($asset->assigned->last_name) : null,
|
|
|
|
'employee_number' => ($asset->assigned->employee_num) ? e($asset->assigned->employee_num) : null,
|
2017-09-05 17:54:58 -07:00
|
|
|
'type' => 'user'
|
|
|
|
] : null;
|
|
|
|
}
|
2017-10-28 03:50:02 -07:00
|
|
|
return $asset->assigned ? [
|
|
|
|
'id' => $asset->assigned->id,
|
|
|
|
'name' => $asset->assigned->display_name,
|
2017-09-05 17:54:58 -07:00
|
|
|
'type' => $asset->assignedType()
|
|
|
|
] : null;
|
|
|
|
}
|
2017-01-13 11:38:25 -08:00
|
|
|
}
|