2017-01-26 19:16:06 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 19:16:06 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Component;
|
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 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
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-01-26 19:16:06 -08:00
|
|
|
foreach ($components as $component) {
|
|
|
|
$array[] = self::transformComponent($component);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-26 19:16:06 -08:00
|
|
|
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),
|
2018-09-29 21:33:52 -07:00
|
|
|
'image' => ($component->image) ? Storage::disk('public')->url('components/'.e($component->image)) : null,
|
2017-12-05 00:40:30 -08:00
|
|
|
'serial' => ($component->serial) ? e($component->serial) : null,
|
2017-03-14 08:37:39 -07:00
|
|
|
'location' => ($component->location) ? [
|
|
|
|
'id' => (int) $component->location->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => e($component->location->name),
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
2021-06-10 13:15:52 -07:00
|
|
|
'qty' => ($component->qty != '') ? (int) $component->qty : null,
|
|
|
|
'min_amt' => ($component->min_amt != '') ? (int) $component->min_amt : null,
|
2017-03-14 08:37:39 -07:00
|
|
|
'category' => ($component->category) ? [
|
|
|
|
'id' => (int) $component->category->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => e($component->category->name),
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
2023-04-13 11:10:51 -07:00
|
|
|
'supplier' => ($component->supplier) ? ['id' => $component->supplier->id, 'name'=> e($component->supplier->name)] : null,
|
2024-10-23 09:50:22 -07:00
|
|
|
'manufacturer' => ($component->manufacturer) ? ['id' => $component->manufacturer->id, 'name'=> e($component->manufacturer->name)] : null,
|
|
|
|
'model_number' => ($component->model_number) ? e($component->model_number) : 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,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => e($component->company->name),
|
2017-03-14 08:37:39 -07:00
|
|
|
] : null,
|
2023-07-11 03:41:58 -07:00
|
|
|
'notes' => ($component->notes) ? Helper::parseEscapedMarkedownInline($component->notes) : null,
|
2024-09-17 14:16:41 -07:00
|
|
|
'created_by' => ($component->adminuser) ? [
|
|
|
|
'id' => (int) $component->adminuser->id,
|
|
|
|
'name'=> e($component->adminuser->present()->fullName()),
|
|
|
|
] : 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-07-29 16:13:17 -07:00
|
|
|
'user_can_checkout' => ($component->numRemaining() > 0) ? 1 : 0,
|
2017-01-26 19:16:06 -08:00
|
|
|
];
|
2017-02-08 18:24:55 -08:00
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
2020-05-23 10:36:02 -07:00
|
|
|
'checkout' => Gate::allows('checkout', Component::class),
|
|
|
|
'checkin' => Gate::allows('checkin', Component::class),
|
|
|
|
'update' => Gate::allows('update', Component::class),
|
|
|
|
'delete' => 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-10-07 06:56:18 -07:00
|
|
|
public function transformCheckedoutComponents(Collection $components_assets, $total)
|
2017-01-26 19:16:06 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-10-07 06:56:18 -07:00
|
|
|
foreach ($components_assets as $asset) {
|
|
|
|
$array[] = [
|
|
|
|
'assigned_pivot_id' => $asset->pivot->id,
|
|
|
|
'id' => (int) $asset->id,
|
2021-06-10 13:15:52 -07:00
|
|
|
'name' => e($asset->model->present()->name).' '.e($asset->present()->name),
|
2017-10-07 06:56:18 -07:00
|
|
|
'qty' => $asset->pivot->assigned_qty,
|
2022-08-10 15:04:34 -07:00
|
|
|
'note' => $asset->pivot->note,
|
2017-10-07 06:56:18 -07:00
|
|
|
'type' => 'asset',
|
|
|
|
'created_at' => Helper::getFormattedDateObject($asset->pivot->created_at, 'datetime'),
|
2021-06-10 13:15:52 -07:00
|
|
|
'available_actions' => ['checkin' => true],
|
2017-10-07 06:56:18 -07:00
|
|
|
];
|
2017-01-26 19:16:06 -08:00
|
|
|
}
|
2017-10-07 06:56:18 -07:00
|
|
|
|
2017-01-26 19:16:06 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
}
|