2017-02-21 14:26:46 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-21 14:26:46 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
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;
|
2017-02-21 14:26:46 -08:00
|
|
|
|
|
|
|
class ComponentsAssetsTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAssets(Collection $assets, $total)
|
2017-02-21 14:26:46 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-02-21 14:26:46 -08:00
|
|
|
foreach ($assets as $asset) {
|
|
|
|
$array[] = self::transformAsset($asset);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-21 14:26:46 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAsset(Asset $asset)
|
2017-02-21 14:26:46 -08:00
|
|
|
{
|
|
|
|
$array = [
|
|
|
|
'id' => $asset->id,
|
|
|
|
'name' => e($asset->name),
|
|
|
|
'created_at' => $asset->created_at->format('Y-m-d'),
|
|
|
|
'qty' => $asset->components()->count(),
|
2017-03-11 12:14:10 -08:00
|
|
|
'user_can_checkout' => $asset->availableForCheckout(),
|
2022-08-10 15:04:59 -07:00
|
|
|
'note' => e($asset->note),
|
2017-02-21 14:26:46 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
2020-05-23 10:36:02 -07:00
|
|
|
'checkout' => Gate::allows('checkout', Asset::class),
|
|
|
|
'checkin' => Gate::allows('checkin', Asset::class),
|
|
|
|
'update' => Gate::allows('update', Asset::class),
|
|
|
|
'delete' => Gate::allows('delete', Asset::class),
|
2017-02-21 14:26:46 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
$array += $permissions_array;
|
|
|
|
|
|
|
|
if ($asset->model->fieldset) {
|
|
|
|
foreach ($asset->model->fieldset->fields as $field) {
|
2022-06-27 14:17:07 -07:00
|
|
|
$fields_array = [$field->name => $asset->{$field->db_column}];
|
2017-02-21 14:26:46 -08:00
|
|
|
$array += $fields_array;
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|
2017-02-21 14:26:46 -08:00
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAssetsDatatable($assets)
|
|
|
|
{
|
2017-02-21 14:26:46 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($assets);
|
|
|
|
}
|
|
|
|
}
|