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

72 lines
2.9 KiB
PHP
Raw Normal View History

2017-01-26 18:46:18 -08:00
<?php
namespace App\Http\Transformers;
use App\Models\Consumable;
use Illuminate\Database\Eloquent\Collection;
use App\Helpers\Helper;
use Gate;
2017-01-26 18:46:18 -08:00
class ConsumablesTransformer
{
public function transformConsumables (Collection $consumables, $total)
{
$array = array();
foreach ($consumables as $consumable) {
$array[] = self::transformConsumable($consumable);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
public function transformConsumable (Consumable $consumable)
{
$array = [
2017-01-26 18:46:18 -08:00
'category' => ($consumable->category) ? ['id' => $consumable->category->id, 'name' => $consumable->category->name] : null,
'company' => ($consumable->company) ? ['id' => $consumable->company->id, 'name' => $consumable->company->name] : null,
'id' => $consumable->id,
'item_no' => $consumable->item_no,
'location' => ($consumable->location) ? ['id' => $consumable->location->id, 'name' => $consumable->location->name] : null,
'manufacturer' => ($consumable->manufacturer) ? ['id' => $consumable->manufacturer->id, 'name' => $consumable->manufacturer->name] : null,
'min_amt' => $consumable->min_amt,
'model_number' => $consumable->model_number,
'name' => $consumable->name,
2017-01-26 19:16:06 -08:00
'remaining' => $consumable->numRemaining(),
2017-01-26 18:46:18 -08:00
'order_number' => $consumable->order_number,
'purchase_cost' => Helper::formatCurrencyOutput($consumable->purchase_cost),
2017-03-03 17:53:42 -08:00
'purchase_date' => Helper::getFormattedDateObject($consumable->purchase_date, 'date'),
2017-01-26 18:46:18 -08:00
'qty' => $consumable->qty,
2017-03-03 17:53:42 -08:00
'created_at' => Helper::getFormattedDateObject($consumable->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($consumable->updated_at, 'datetime'),
2017-01-26 18:46:18 -08:00
];
$permissions_array['user_can_checkout'] = false;
if ($consumable->numRemaining() > 0) {
$permissions_array['user_can_checkout'] = true;
}
$permissions_array['available_actions'] = [
'checkout' => Gate::allows('checkout', Consumable::class) ? true : false,
'checkin' => Gate::allows('checkin', Consumable::class) ? true : false,
'update' => Gate::allows('update', Consumable::class) ? true : false,
'delete' => Gate::allows('delete', Consumable::class) ? true : false,
];
$array += $permissions_array;
return $array;
2017-01-26 18:46:18 -08:00
}
public function transformCheckedoutConsumables (Collection $consumables_users, $total)
{
$array = array();
foreach ($consumables_users as $user) {
$array[] = (new UsersTransformer)->transformUser($user);
}
return (new DatatablesTransformer)->transformDatatables($array, $total);
}
}