2017-01-26 19:16:06 -08:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Models\Component;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use App\Helpers\Helper;
|
2017-02-08 18:24:55 -08:00
|
|
|
use Gate;
|
2017-01-26 19:16:06 -08:00
|
|
|
|
|
|
|
class ComponentsTransformer
|
|
|
|
{
|
2017-03-14 08:37:39 -07:00
|
|
|
public function transformComponents(Collection $components, $total)
|
2017-01-26 19:16:06 -08:00
|
|
|
{
|
|
|
|
$array = array();
|
|
|
|
foreach ($components as $component) {
|
|
|
|
$array[] = self::transformComponent($component);
|
|
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2017-03-14 08:37:39 -07:00
|
|
|
public function transformComponent(Component $component)
|
2017-01-26 19:16:06 -08:00
|
|
|
{
|
2017-02-08 18:24:55 -08:00
|
|
|
$array = [
|
2017-03-14 08:37:39 -07:00
|
|
|
'id' => (int) $component->id,
|
|
|
|
'name' => e($component->name),
|
2017-01-26 19:16:06 -08:00
|
|
|
'serial_number' => e($component->serial),
|
2017-03-14 08:37:39 -07:00
|
|
|
'location' => ($component->location) ? [
|
|
|
|
'id' => (int) $component->location->id,
|
|
|
|
'name' => e($component->location->name)
|
|
|
|
] : null,
|
|
|
|
'qty' => number_format($component->qty),
|
|
|
|
'min_amt' => e($component->min_amt),
|
|
|
|
'category' => ($component->category) ? [
|
|
|
|
'id' => (int) $component->category->id,
|
|
|
|
'name' => e($component->category->name)
|
|
|
|
] : null,
|
2017-01-26 19:16:06 -08:00
|
|
|
'order_number' => e($component->order_number),
|
2017-03-03 17:53:42 -08:00
|
|
|
'purchase_date' => Helper::getFormattedDateObject($component->purchase_date, 'date'),
|
2017-01-26 19:16:06 -08:00
|
|
|
'purchase_cost' => Helper::formatCurrencyOutput($component->purchase_cost),
|
2017-03-14 08:37:39 -07:00
|
|
|
'remaining' => (int) $component->numRemaining(),
|
|
|
|
'company' => ($component->company) ? [
|
|
|
|
'id' => (int) $component->company->id,
|
|
|
|
'name' => e($component->company->name)
|
|
|
|
] : null,
|
2017-03-03 17:53:42 -08:00
|
|
|
'created_at' => Helper::getFormattedDateObject($component->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($component->updated_at, 'datetime'),
|
2017-01-26 19:16:06 -08:00
|
|
|
];
|
2017-02-08 18:24:55 -08:00
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
2017-03-14 08:37:39 -07:00
|
|
|
'checkout' => (bool) Gate::allows('checkout', Component::class),
|
|
|
|
'checkin' => (bool) Gate::allows('checkin', Component::class),
|
|
|
|
'update' => (bool) Gate::allows('update', Component::class),
|
|
|
|
'delete' => (bool) Gate::allows('delete', Component::class),
|
2017-02-08 18:24:55 -08:00
|
|
|
];
|
|
|
|
$array += $permissions_array;
|
|
|
|
|
|
|
|
return $array;
|
2017-01-26 19:16:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-14 08:37:39 -07:00
|
|
|
public function transformCheckedoutComponents(Collection $components_users, $total)
|
2017-01-26 19:16:06 -08:00
|
|
|
{
|
|
|
|
$array = array();
|
|
|
|
foreach ($components_users as $user) {
|
|
|
|
$array[] = (new UsersTransformer)->transformUser($user);
|
|
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
}
|