snipe-it/app/Http/Transformers/LicensesTransformer.php

75 lines
3.3 KiB
PHP
Raw Normal View History

2017-01-24 18:57:33 -08:00
<?php
2017-01-24 18:57:33 -08:00
namespace App\Http\Transformers;
use App\Helpers\Helper;
2017-01-24 18:57:33 -08:00
use App\Models\License;
use Gate;
2017-01-24 18:57:33 -08:00
use Illuminate\Database\Eloquent\Collection;
class LicensesTransformer
{
public function transformLicenses(Collection $licenses, $total)
2017-01-24 18:57:33 -08:00
{
$array = [];
2017-01-24 18:57:33 -08:00
foreach ($licenses as $license) {
$array[] = self::transformLicense($license);
}
2017-01-24 18:57:33 -08:00
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformLicense(License $license)
2017-01-24 18:57:33 -08:00
{
$array = [
'id' => (int) $license->id,
2017-01-24 19:24:47 -08:00
'name' => e($license->name),
'company' => ($license->company) ? ['id' => (int) $license->company->id, 'name'=> e($license->company->name)] : null,
'manufacturer' => ($license->manufacturer) ? ['id' => (int) $license->manufacturer->id, 'name'=> e($license->manufacturer->name)] : null,
2017-12-05 16:26:23 -08:00
'product_key' => (Gate::allows('viewKeys', License::class)) ? e($license->serial) : '------------',
2017-02-10 18:43:30 -08:00
'order_number' => e($license->order_number),
'purchase_order' => e($license->purchase_order),
2017-03-03 17:53:42 -08:00
'purchase_date' => Helper::getFormattedDateObject($license->purchase_date, 'date'),
'termination_date' => Helper::getFormattedDateObject($license->termination_date, 'date'),
'depreciation' => ($license->depreciation) ? ['id' => (int) $license->depreciation->id,'name'=> e($license->depreciation->name)] : null,
'purchase_cost' => Helper::formatCurrencyOutput($license->purchase_cost),
'purchase_cost_numeric' => $license->purchase_cost,
2017-01-24 19:24:47 -08:00
'notes' => e($license->notes),
2017-03-03 17:53:42 -08:00
'expiration_date' => Helper::getFormattedDateObject($license->expiration_date, 'date'),
'seats' => (int) $license->seats,
'free_seats_count' => (int) $license->free_seats_count,
2017-03-11 02:52:50 -08:00
'license_name' => e($license->license_name),
2017-01-24 19:24:47 -08:00
'license_email' => e($license->license_email),
'reassignable' => ($license->reassignable == 1) ? true : false,
2017-01-24 19:24:47 -08:00
'maintained' => ($license->maintained == 1) ? true : false,
'supplier' => ($license->supplier) ? ['id' => (int) $license->supplier->id, 'name'=> e($license->supplier->name)] : null,
'category' => ($license->category) ? ['id' => (int) $license->category->id, 'name'=> e($license->category->name)] : null,
2017-03-03 17:53:42 -08:00
'created_at' => Helper::getFormattedDateObject($license->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($license->updated_at, 'datetime'),
'deleted_at' => Helper::getFormattedDateObject($license->deleted_at, 'datetime'),
'user_can_checkout' => (bool) ($license->free_seats_count > 0),
2017-01-24 18:57:33 -08:00
];
$permissions_array['available_actions'] = [
'checkout' => Gate::allows('checkout', License::class),
'checkin' => Gate::allows('checkin', License::class),
'clone' => Gate::allows('create', License::class),
'update' => Gate::allows('update', License::class),
'delete' => Gate::allows('delete', License::class),
];
$array += $permissions_array;
2017-01-24 18:57:33 -08:00
return $array;
}
public function transformAssetsDatatable($licenses)
{
2017-01-24 18:57:33 -08:00
return (new DatatablesTransformer)->transformDatatables($licenses);
}
}