2017-01-13 11:38:25 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-13 11:38:25 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-01-13 11:38:25 -08:00
|
|
|
use App\Models\Asset;
|
2022-03-16 11:02:07 -07:00
|
|
|
use App\Models\Setting;
|
2023-03-18 11:58:09 -07:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2019-03-13 20:12:03 -07:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2023-10-22 09:16:49 -07:00
|
|
|
use Carbon\Carbon;
|
2024-05-29 04:38:15 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
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
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-01-13 11:38:25 -08:00
|
|
|
foreach ($assets as $asset) {
|
|
|
|
$array[] = self::transformAsset($asset);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-13 11:38:25 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2017-03-14 08:37:39 -07:00
|
|
|
public function transformAsset(Asset $asset)
|
2017-01-13 11:38:25 -08:00
|
|
|
{
|
2022-03-16 11:02:07 -07:00
|
|
|
// This uses the getSettings() method so we're pulling from the cache versus querying the settings on single asset
|
|
|
|
$setting = Setting::getSettings();
|
|
|
|
|
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,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($asset->model->name),
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
2023-01-18 13:04:02 -08:00
|
|
|
'byod' => ($asset->byod ? true : false),
|
2024-02-28 06:59:55 -08:00
|
|
|
'requestable' => ($asset->requestable ? true : false),
|
2023-01-18 13:04:02 -08:00
|
|
|
|
2017-10-28 15:17:36 -07:00
|
|
|
'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null,
|
2023-10-22 09:16:49 -07:00
|
|
|
'eol' => (($asset->asset_eol_date != '') && ($asset->purchase_date != '')) ? Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date).' months' : null,
|
2023-01-22 00:56:28 -08:00
|
|
|
'asset_eol_date' => ($asset->asset_eol_date != '') ? Helper::getFormattedDateObject($asset->asset_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,
|
2021-06-10 13:15:52 -07:00
|
|
|
'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,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($asset->model->manufacturer->name),
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
2017-04-22 17:14:28 -07:00
|
|
|
'supplier' => ($asset->supplier) ? [
|
|
|
|
'id' => (int) $asset->supplier->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($asset->supplier->name),
|
|
|
|
] : null,
|
2023-07-11 03:41:58 -07:00
|
|
|
'notes' => ($asset->notes) ? Helper::parseEscapedMarkedownInline($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,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($asset->company->name),
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
2017-10-28 01:49:13 -07:00
|
|
|
'location' => ($asset->location) ? [
|
|
|
|
'id' => (int) $asset->location->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($asset->location->name),
|
|
|
|
] : null,
|
2017-03-14 08:37:39 -07:00
|
|
|
'rtd_location' => ($asset->defaultLoc) ? [
|
|
|
|
'id' => (int) $asset->defaultLoc->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name'=> e($asset->defaultLoc->name),
|
|
|
|
] : null,
|
2017-01-24 22:46:07 -08:00
|
|
|
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
|
2022-03-16 11:02:07 -07:00
|
|
|
'qr' => ($setting->qr_code=='1') ? config('app.url').'/uploads/barcodes/qr-'.str_slug($asset->asset_tag).'-'.str_slug($asset->id).'.png' : null,
|
|
|
|
'alt_barcode' => ($setting->alt_barcode_enabled=='1') ? config('app.url').'/uploads/barcodes/'.str_slug($setting->alt_barcode).'-'.str_slug($asset->asset_tag).'.png' : null,
|
2017-09-05 17:54:58 -07:00
|
|
|
'assigned_to' => $this->transformAssignedTo($asset),
|
2021-06-10 13:15:52 -07:00
|
|
|
'warranty_months' => ($asset->warranty_months > 0) ? e($asset->warranty_months.' '.trans('admin/hardware/form.months')) : null,
|
|
|
|
'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'),
|
2023-03-08 16:47:54 -08:00
|
|
|
'age' => $asset->purchase_date ? $asset->purchase_date->diffForHumans() : '',
|
2017-03-03 17:29:41 -08:00
|
|
|
'last_checkout' => Helper::getFormattedDateObject($asset->last_checkout, 'datetime'),
|
2024-02-13 13:52:12 -08:00
|
|
|
'last_checkin' => Helper::getFormattedDateObject($asset->last_checkin, 'datetime'),
|
2017-03-03 17:29:41 -08:00
|
|
|
'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'date'),
|
2017-03-14 08:37:39 -07:00
|
|
|
'purchase_cost' => Helper::formatCurrencyOutput($asset->purchase_cost),
|
2018-05-16 19:20:43 -07:00
|
|
|
'checkin_counter' => (int) $asset->checkin_counter,
|
|
|
|
'checkout_counter' => (int) $asset->checkout_counter,
|
|
|
|
'requests_counter' => (int) $asset->requests_counter,
|
2017-03-14 08:37:39 -07:00
|
|
|
'user_can_checkout' => (bool) $asset->availableForCheckout(),
|
2023-06-22 14:00:42 -07:00
|
|
|
'book_value' => Helper::formatCurrencyOutput($asset->getLinearDepreciatedValue()),
|
2017-01-13 11:38:25 -08:00
|
|
|
];
|
|
|
|
|
2017-07-08 17:04:40 -07:00
|
|
|
|
2018-04-29 06:10:49 -07:00
|
|
|
if (($asset->model) && ($asset->model->fieldset) && ($asset->model->fieldset->fields->count() > 0)) {
|
2021-06-10 13:15:52 -07:00
|
|
|
$fields_array = [];
|
2017-09-26 16:01:23 -07:00
|
|
|
|
2017-07-08 17:04:40 -07:00
|
|
|
foreach ($asset->model->fieldset->fields as $field) {
|
2022-06-27 14:17:07 -07:00
|
|
|
if ($field->isFieldDecryptable($asset->{$field->db_column})) {
|
|
|
|
$decrypted = Helper::gracefulDecrypt($field, $asset->{$field->db_column});
|
2023-07-19 18:52:03 -07:00
|
|
|
$value = (Gate::allows('assets.view.encrypted_custom_fields')) ? $decrypted : strtoupper(trans('admin/custom_fields/general.encrypted'));
|
2017-07-08 17:04:40 -07:00
|
|
|
|
2022-06-07 16:00:46 -07:00
|
|
|
if ($field->format == 'DATE'){
|
2023-07-19 18:52:03 -07:00
|
|
|
if (Gate::allows('assets.view.encrypted_custom_fields')){
|
2022-06-20 17:58:51 -07:00
|
|
|
$value = Helper::getFormattedDateObject($value, 'date', false);
|
2022-06-07 16:00:46 -07:00
|
|
|
} else {
|
|
|
|
$value = strtoupper(trans('admin/custom_fields/general.encrypted'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-08 18:44:28 -07:00
|
|
|
$fields_array[$field->name] = [
|
2022-06-27 14:17:07 -07:00
|
|
|
'field' => e($field->db_column),
|
2021-11-15 20:31:01 -08:00
|
|
|
'value' => e($value),
|
2017-11-21 22:34:53 -08:00
|
|
|
'field_format' => $field->format,
|
2022-06-09 11:02:31 -07:00
|
|
|
'element' => $field->element,
|
2017-07-08 18:44:28 -07:00
|
|
|
];
|
2017-07-08 17:04:40 -07:00
|
|
|
|
|
|
|
} else {
|
2022-06-27 14:17:07 -07:00
|
|
|
$value = $asset->{$field->db_column};
|
2022-06-07 16:00:46 -07:00
|
|
|
|
2022-06-15 13:42:30 -07:00
|
|
|
if (($field->format == 'DATE') && (!is_null($value)) && ($value!='')){
|
2022-06-20 17:58:51 -07:00
|
|
|
$value = Helper::getFormattedDateObject($value, 'date', false);
|
2022-06-07 16:00:46 -07:00
|
|
|
}
|
2022-06-13 23:37:15 -07:00
|
|
|
|
2017-07-08 18:44:28 -07:00
|
|
|
$fields_array[$field->name] = [
|
2022-06-27 14:17:07 -07:00
|
|
|
'field' => e($field->db_column),
|
2022-06-07 16:00:46 -07:00
|
|
|
'value' => e($value),
|
2017-11-21 22:34:53 -08:00
|
|
|
'field_format' => $field->format,
|
2022-06-09 11:02:31 -07:00
|
|
|
'element' => $field->element,
|
2017-07-08 18:44:28 -07:00
|
|
|
];
|
2017-07-08 17:04:40 -07:00
|
|
|
}
|
2022-06-07 16:00:46 -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 {
|
2022-11-15 09:04:06 -08:00
|
|
|
$array['custom_fields'] = new \stdClass; // HACK to force generation of empty object instead of empty list
|
2017-07-08 17:04:40 -07:00
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$permissions_array['available_actions'] = [
|
2021-10-25 15:34:22 -07:00
|
|
|
'checkout' => ($asset->deleted_at=='' && Gate::allows('checkout', Asset::class)) ? true : false,
|
|
|
|
'checkin' => ($asset->deleted_at=='' && Gate::allows('checkin', Asset::class)) ? true : false,
|
|
|
|
'clone' => Gate::allows('create', Asset::class) ? true : false,
|
|
|
|
'restore' => ($asset->deleted_at!='' && Gate::allows('create', Asset::class)) ? true : false,
|
|
|
|
'update' => ($asset->deleted_at=='' && Gate::allows('update', Asset::class)) ? true : false,
|
2023-11-22 10:04:24 -08:00
|
|
|
'delete' => ($asset->deleted_at=='' && $asset->assigned_to =='' && Gate::allows('delete', Asset::class) && ($asset->deleted_at == '')) ? true : false,
|
2021-11-15 19:24:38 -08:00
|
|
|
];
|
2017-11-22 10:35:24 -08:00
|
|
|
|
|
|
|
|
2021-09-23 17:23:53 -07:00
|
|
|
if (request('components')=='true') {
|
|
|
|
|
|
|
|
if ($asset->components) {
|
|
|
|
$array['components'] = [];
|
|
|
|
|
|
|
|
foreach ($asset->components as $component) {
|
|
|
|
$array['components'][] = [
|
2021-10-06 10:38:13 -07:00
|
|
|
|
2021-09-23 17:23:53 -07:00
|
|
|
'id' => $component->id,
|
2021-09-30 15:51:08 -07:00
|
|
|
'pivot_id' => $component->pivot->id,
|
2021-11-15 20:32:59 -08:00
|
|
|
'name' => e($component->name),
|
2021-09-23 17:23:53 -07:00
|
|
|
'qty' => $component->pivot->assigned_qty,
|
|
|
|
'price_cost' => $component->purchase_cost,
|
|
|
|
'purchase_total' => $component->purchase_cost * $component->pivot->assigned_qty,
|
|
|
|
'checkout_date' => Helper::getFormattedDateObject($component->pivot->created_at, 'datetime') ,
|
2021-10-06 10:38:13 -07:00
|
|
|
|
2021-09-23 17:23:53 -07:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$array += $permissions_array;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
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,
|
2022-12-09 16:07:17 -08:00
|
|
|
'email'=> ($asset->assigned->email) ? e($asset->assigned->email) : null,
|
2017-10-28 15:17:36 -07:00
|
|
|
'employee_number' => ($asset->assigned->employee_num) ? e($asset->assigned->employee_num) : null,
|
2021-06-10 13:15:52 -07:00
|
|
|
'type' => 'user',
|
2017-09-05 17:54:58 -07:00
|
|
|
] : null;
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-10-28 03:50:02 -07:00
|
|
|
return $asset->assigned ? [
|
|
|
|
'id' => $asset->assigned->id,
|
2021-12-13 12:03:03 -08:00
|
|
|
'name' => e($asset->assigned->display_name),
|
2017-09-05 17:54:58 -07:00
|
|
|
'type' => $asset->assignedType()
|
|
|
|
] : null;
|
|
|
|
}
|
2018-04-04 17:33:02 -07:00
|
|
|
|
|
|
|
|
|
|
|
public function transformRequestedAssets(Collection $assets, $total)
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2018-04-04 17:33:02 -07:00
|
|
|
foreach ($assets as $asset) {
|
|
|
|
$array[] = self::transformRequestedAsset($asset);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformRequestedAsset(Asset $asset)
|
|
|
|
{
|
2018-04-04 17:33:02 -07:00
|
|
|
$array = [
|
|
|
|
'id' => (int) $asset->id,
|
|
|
|
'name' => e($asset->name),
|
|
|
|
'asset_tag' => e($asset->asset_tag),
|
|
|
|
'serial' => e($asset->serial),
|
|
|
|
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
|
|
|
|
'model' => ($asset->model) ? e($asset->model->name) : null,
|
|
|
|
'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null,
|
|
|
|
'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'date'),
|
|
|
|
'location' => ($asset->location) ? e($asset->location->name) : null,
|
|
|
|
'status'=> ($asset->assetstatus) ? $asset->present()->statusMeta : null,
|
2024-07-04 12:49:22 -07:00
|
|
|
'assigned_to_self' => ($asset->assigned_to == auth()->id()),
|
2018-04-04 17:33:02 -07:00
|
|
|
];
|
|
|
|
|
2023-10-25 09:28:14 -07:00
|
|
|
if (($asset->model) && ($asset->model->fieldset) && ($asset->model->fieldset->fields->count() > 0)) {
|
|
|
|
$fields_array = [];
|
|
|
|
|
|
|
|
foreach ($asset->model->fieldset->fields as $field) {
|
|
|
|
|
|
|
|
// Only display this if it's allowed via the custom field setting
|
2023-10-25 11:43:09 -07:00
|
|
|
if (($field->field_encrypted=='0') && ($field->show_in_requestable_list=='1')) {
|
2023-10-25 09:28:14 -07:00
|
|
|
|
|
|
|
$value = $asset->{$field->db_column};
|
|
|
|
if (($field->format == 'DATE') && (!is_null($value)) && ($value != '')) {
|
|
|
|
$value = Helper::getFormattedDateObject($value, 'date', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields_array[$field->db_column] = e($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$array['custom_fields'] = $fields_array;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$array['custom_fields'] = new \stdClass; // HACK to force generation of empty object instead of empty list
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
$permissions_array['available_actions'] = [
|
2024-07-04 12:49:22 -07:00
|
|
|
'cancel' => ($asset->isRequestedBy(auth()->user())) ? true : false,
|
|
|
|
'request' => ($asset->isRequestedBy(auth()->user())) ? false : true,
|
2018-04-04 17:33:02 -07:00
|
|
|
];
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$array += $permissions_array;
|
|
|
|
return $array;
|
2018-04-04 17:33:02 -07:00
|
|
|
|
2021-10-20 17:26:41 -07:00
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
}
|
2017-01-13 11:38:25 -08:00
|
|
|
}
|