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;
|
|
|
|
use App\Models\Setting;
|
2017-02-08 18:24:55 -08:00
|
|
|
|
2017-01-13 11:38:25 -08:00
|
|
|
|
|
|
|
class AssetsTransformer
|
|
|
|
{
|
|
|
|
|
|
|
|
public function transformAssets (Collection $assets, $total)
|
|
|
|
{
|
|
|
|
$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-01-13 11:38:25 -08:00
|
|
|
public function transformAsset (Asset $asset)
|
|
|
|
{
|
2017-03-03 17:29:41 -08:00
|
|
|
$settings = Setting::getSettings();
|
2017-01-13 11:38:25 -08:00
|
|
|
$array = [
|
|
|
|
'id' => $asset->id,
|
2017-02-03 02:04:17 -08:00
|
|
|
'name' => e($asset->name),
|
|
|
|
'asset_tag' => e($asset->asset_tag),
|
|
|
|
'serial' => e($asset->serial),
|
|
|
|
'model' => ($asset->model) ? ['id' => $asset->model->id,'name'=> e($asset->model->name)] : '',
|
2017-03-03 17:29:41 -08:00
|
|
|
'model_number' => ($asset->model_number) ? e($asset->model_number) : null,
|
2017-02-03 02:04:17 -08:00
|
|
|
'status_label' => ($asset->assetstatus) ? ['id' => $asset->assetstatus->id,'name'=> e($asset->assetstatus->name)] : null,
|
|
|
|
'category' => ($asset->model->category) ? ['id' => $asset->model->category->id,'name'=> e($asset->model->category->name)] : null,
|
|
|
|
'manufacturer' => ($asset->model->manufacturer) ? ['id' => $asset->model->manufacturer->id,'name'=> e($asset->model->manufacturer->name)] : null,
|
2017-01-13 11:38:25 -08:00
|
|
|
'notes' => $asset->notes,
|
|
|
|
'order_number' => $asset->order_number,
|
2017-02-03 02:04:17 -08:00
|
|
|
'company' => ($asset->company) ? ['id' => $asset->company->id,'name'=> e($asset->company->name)] : null,
|
|
|
|
'location' => ($asset->assetLoc) ? ['id' => $asset->assetLoc->id,'name'=> e($asset->assetLoc->name)] : null,
|
2017-02-08 03:31:42 -08:00
|
|
|
'rtd_location' => ($asset->defaultLoc) ? ['id' => $asset->defaultLoc->id,'name'=> e($asset->defaultLoc->name)] : null,
|
2017-01-24 22:46:07 -08:00
|
|
|
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
|
2017-02-23 17:00:50 -08:00
|
|
|
'assigned_to' => ($asset->assigneduser) ? ['id' => $asset->assigneduser->id, 'name' => $asset->assigneduser->getFullNameAttribute(), 'first_name'=> e( $asset->assigneduser->first_name), 'last_name'=> e( $asset->assigneduser->last_name)] : null,
|
2017-02-03 02:04:17 -08:00
|
|
|
'warranty' => ($asset->warranty_months > 0) ? e($asset->warranty_months).' '.trans('admin/hardware/form.months') : null,
|
2017-02-03 01:45:12 -08:00
|
|
|
'warranty_expires' => ($asset->warranty_months > 0) ? $asset->present()->warrantee_expires() : 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'),
|
|
|
|
'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-01-13 11:38:25 -08:00
|
|
|
'purchase_cost' => $asset->purchase_cost,
|
2017-02-08 08:48:41 -08:00
|
|
|
'can_checkout' => $asset->availableForCheckout(),
|
2017-01-13 11:38:25 -08:00
|
|
|
|
|
|
|
];
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
|
|
|
'checkout' => Gate::allows('checkout', Asset::class) ? true : false,
|
|
|
|
'checkin' => Gate::allows('checkin', Asset::class) ? true : false,
|
|
|
|
'update' => Gate::allows('update', Asset::class) ? true : false,
|
|
|
|
'delete' => Gate::allows('delete', Asset::class) ? true : false,
|
|
|
|
];
|
|
|
|
|
|
|
|
$array += $permissions_array;
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-25 21:01:00 -08:00
|
|
|
if ($asset->model->fieldset) {
|
|
|
|
|
|
|
|
foreach($asset->model->fieldset->fields as $field) {
|
|
|
|
$fields_array = [$field->name => $asset->{$field->convertUnicodeDbSlug()}];
|
|
|
|
$array += $fields_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
|
2017-01-13 11:38:25 -08:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:57:21 -08:00
|
|
|
public function transformAssetsDatatable($assets) {
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($assets);
|
2017-01-13 11:38:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|