2017-01-26 18:46:18 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 18:46:18 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Consumable;
|
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;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2017-01-26 18:46:18 -08:00
|
|
|
|
|
|
|
class ConsumablesTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformConsumables(Collection $consumables, $total)
|
2017-01-26 18:46:18 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-01-26 18:46:18 -08:00
|
|
|
foreach ($consumables as $consumable) {
|
|
|
|
$array[] = self::transformConsumable($consumable);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 18:46:18 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformConsumable(Consumable $consumable)
|
2017-01-26 18:46:18 -08:00
|
|
|
{
|
2017-02-08 18:24:55 -08:00
|
|
|
$array = [
|
2017-07-25 23:40:30 -07:00
|
|
|
'id' => (int) $consumable->id,
|
2017-10-25 22:35:58 -07:00
|
|
|
'name' => e($consumable->name),
|
2018-09-29 21:33:52 -07:00
|
|
|
'image' => ($consumable->image) ? Storage::disk('public')->url('consumables/'.e($consumable->image)) : null,
|
2017-07-25 23:40:30 -07:00
|
|
|
'category' => ($consumable->category) ? ['id' => $consumable->category->id, 'name' => e($consumable->category->name)] : null,
|
|
|
|
'company' => ($consumable->company) ? ['id' => (int) $consumable->company->id, 'name' => e($consumable->company->name)] : null,
|
|
|
|
'item_no' => e($consumable->item_no),
|
|
|
|
'location' => ($consumable->location) ? ['id' => (int) $consumable->location->id, 'name' => e($consumable->location->name)] : null,
|
|
|
|
'manufacturer' => ($consumable->manufacturer) ? ['id' => (int) $consumable->manufacturer->id, 'name' => e($consumable->manufacturer->name)] : null,
|
2023-04-13 11:10:51 -07:00
|
|
|
'supplier' => ($consumable->supplier) ? ['id' => $consumable->supplier->id, 'name'=> e($consumable->supplier->name)] : null,
|
2017-07-25 23:40:30 -07:00
|
|
|
'min_amt' => (int) $consumable->min_amt,
|
2021-06-10 13:15:52 -07:00
|
|
|
'model_number' => ($consumable->model_number != '') ? e($consumable->model_number) : null,
|
2017-01-26 19:16:06 -08:00
|
|
|
'remaining' => $consumable->numRemaining(),
|
2017-07-25 23:40:30 -07:00
|
|
|
'order_number' => e($consumable->order_number),
|
2017-01-26 18:46:18 -08:00
|
|
|
'purchase_cost' => Helper::formatCurrencyOutput($consumable->purchase_cost),
|
2017-03-03 17:53:42 -08:00
|
|
|
'purchase_date' => Helper::getFormattedDateObject($consumable->purchase_date, 'date'),
|
2017-07-25 23:40:30 -07:00
|
|
|
'qty' => (int) $consumable->qty,
|
2023-07-11 03:41:58 -07:00
|
|
|
'notes' => ($consumable->notes) ? Helper::parseEscapedMarkedownInline($consumable->notes) : null,
|
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
|
|
|
];
|
2017-02-08 18:24:55 -08:00
|
|
|
|
2017-03-11 12:14:10 -08:00
|
|
|
$permissions_array['user_can_checkout'] = false;
|
|
|
|
|
|
|
|
if ($consumable->numRemaining() > 0) {
|
|
|
|
$permissions_array['user_can_checkout'] = true;
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$permissions_array['available_actions'] = [
|
2020-05-23 10:36:02 -07:00
|
|
|
'checkout' => Gate::allows('checkout', Consumable::class),
|
|
|
|
'checkin' => Gate::allows('checkin', Consumable::class),
|
|
|
|
'update' => Gate::allows('update', Consumable::class),
|
|
|
|
'delete' => Gate::allows('delete', Consumable::class),
|
2017-02-08 18:24:55 -08:00
|
|
|
];
|
|
|
|
$array += $permissions_array;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
return $array;
|
2017-01-26 18:46:18 -08:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformCheckedoutConsumables(Collection $consumables_users, $total)
|
2017-01-26 18:46:18 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-01-26 18:46:18 -08:00
|
|
|
foreach ($consumables_users as $user) {
|
|
|
|
$array[] = (new UsersTransformer)->transformUser($user);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 18:46:18 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
}
|