2017-01-24 22:25:17 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-24 22:25:17 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-01-24 22:25:17 -08:00
|
|
|
use App\Models\AssetModel;
|
2023-02-07 11:02:02 -08:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2017-01-24 22:25:17 -08:00
|
|
|
|
|
|
|
class AssetModelsTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAssetModels(Collection $assetmodels, $total)
|
2017-01-24 22:25:17 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-01-24 22:25:17 -08:00
|
|
|
foreach ($assetmodels as $assetmodel) {
|
|
|
|
$array[] = self::transformAssetModel($assetmodel);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-24 22:25:17 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAssetModel(AssetModel $assetmodel)
|
2017-01-24 22:25:17 -08:00
|
|
|
{
|
2023-01-05 13:12:54 -08:00
|
|
|
|
|
|
|
$default_field_values = array();
|
|
|
|
|
|
|
|
// Reach into the custom fields and models_custom_fields pivot table to find the default values for this model
|
|
|
|
if ($assetmodel->fieldset) {
|
|
|
|
foreach($assetmodel->fieldset->fields AS $field) {
|
|
|
|
$default_field_values[] = [
|
|
|
|
'name' => e($field->name),
|
|
|
|
'db_column_name' => e($field->db_column_name()),
|
|
|
|
'default_value' => ($field->defaultValue($assetmodel->id)) ? e($field->defaultValue($assetmodel->id)) : null,
|
2023-01-05 13:15:30 -08:00
|
|
|
'format' => e($field->format),
|
2023-01-05 13:12:54 -08:00
|
|
|
'required' => ($field->pivot->required == '1') ? true : false,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$array = [
|
2017-07-25 23:40:30 -07:00
|
|
|
'id' => (int) $assetmodel->id,
|
2017-01-24 22:25:17 -08:00
|
|
|
'name' => e($assetmodel->name),
|
2017-07-29 17:01:03 -07:00
|
|
|
'manufacturer' => ($assetmodel->manufacturer) ? [
|
|
|
|
'id' => (int) $assetmodel->manufacturer->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($assetmodel->manufacturer->name),
|
|
|
|
] : null,
|
|
|
|
'image' => ($assetmodel->image != '') ? Storage::disk('public')->url('models/'.e($assetmodel->image)) : null,
|
2017-01-24 22:25:17 -08:00
|
|
|
'model_number' => e($assetmodel->model_number),
|
2023-08-21 12:07:57 -07:00
|
|
|
'min_amt' => ($assetmodel->min_amt) ? (int) $assetmodel->min_amt : null,
|
2017-07-29 17:01:03 -07:00
|
|
|
'depreciation' => ($assetmodel->depreciation) ? [
|
|
|
|
'id' => (int) $assetmodel->depreciation->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($assetmodel->depreciation->name),
|
|
|
|
] : null,
|
2018-02-25 12:10:02 -08:00
|
|
|
'assets_count' => (int) $assetmodel->assets_count,
|
2017-07-29 17:01:03 -07:00
|
|
|
'category' => ($assetmodel->category) ? [
|
|
|
|
'id' => (int) $assetmodel->category->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($assetmodel->category->name),
|
|
|
|
] : null,
|
2017-07-29 17:01:03 -07:00
|
|
|
'fieldset' => ($assetmodel->fieldset) ? [
|
|
|
|
'id' => (int) $assetmodel->fieldset->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($assetmodel->fieldset->name),
|
|
|
|
] : null,
|
2023-01-05 13:12:54 -08:00
|
|
|
'default_fieldset_values' => $default_field_values,
|
2021-06-10 13:15:52 -07:00
|
|
|
'eol' => ($assetmodel->eol > 0) ? $assetmodel->eol.' months' : 'None',
|
|
|
|
'requestable' => ($assetmodel->requestable == '1') ? true : false,
|
2023-07-13 05:14:10 -07:00
|
|
|
'notes' => Helper::parseEscapedMarkedownInline($assetmodel->notes),
|
2017-07-29 17:01:03 -07:00
|
|
|
'created_at' => Helper::getFormattedDateObject($assetmodel->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($assetmodel->updated_at, 'datetime'),
|
2017-10-03 17:53:08 -07:00
|
|
|
'deleted_at' => Helper::getFormattedDateObject($assetmodel->deleted_at, 'datetime'),
|
2017-01-24 22:25:17 -08:00
|
|
|
|
|
|
|
];
|
2017-02-08 18:24:55 -08:00
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
2021-06-10 13:15:52 -07:00
|
|
|
'update' => (Gate::allows('update', AssetModel::class) && ($assetmodel->deleted_at == '')),
|
2023-11-22 10:04:24 -08:00
|
|
|
'delete' => $assetmodel->isDeletable(),
|
2021-06-10 13:15:52 -07:00
|
|
|
'clone' => (Gate::allows('create', AssetModel::class) && ($assetmodel->deleted_at == '')),
|
|
|
|
'restore' => (Gate::allows('create', AssetModel::class) && ($assetmodel->deleted_at != '')),
|
2017-02-08 18:24:55 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
$array += $permissions_array;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
return $array;
|
2017-01-24 22:25:17 -08:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAssetModelsDatatable($assetmodels)
|
|
|
|
{
|
2017-01-24 22:25:17 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($assetmodels);
|
|
|
|
}
|
|
|
|
}
|