2017-01-25 02:19:26 -08:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-25 02:19:26 -08:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-01-25 02:19:26 -08:00
|
|
|
use App\Models\Accessory;
|
2023-03-07 13:41:48 -08:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2017-01-25 02:19:26 -08:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2018-09-29 21:33:52 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2017-01-25 02:19:26 -08:00
|
|
|
|
|
|
|
class AccessoriesTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAccessories(Collection $accessories, $total)
|
2017-01-25 02:19:26 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-01-25 02:19:26 -08:00
|
|
|
foreach ($accessories as $accessory) {
|
|
|
|
$array[] = self::transformAccessory($accessory);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-01-25 02:19:26 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformAccessory(Accessory $accessory)
|
2017-01-25 02:19:26 -08:00
|
|
|
{
|
2017-02-08 18:24:55 -08:00
|
|
|
$array = [
|
2017-01-25 02:19:26 -08:00
|
|
|
'id' => $accessory->id,
|
|
|
|
'name' => e($accessory->name),
|
2020-08-03 11:17:56 -07:00
|
|
|
'image' => ($accessory->image) ? Storage::disk('public')->url('accessories/'.e($accessory->image)) : null,
|
2021-06-10 13:15:52 -07:00
|
|
|
'company' => ($accessory->company) ? ['id' => $accessory->company->id, 'name'=> e($accessory->company->name)] : null,
|
|
|
|
'manufacturer' => ($accessory->manufacturer) ? ['id' => $accessory->manufacturer->id, 'name'=> e($accessory->manufacturer->name)] : null,
|
|
|
|
'supplier' => ($accessory->supplier) ? ['id' => $accessory->supplier->id, 'name'=> e($accessory->supplier->name)] : null,
|
2017-01-25 02:19:26 -08:00
|
|
|
'model_number' => ($accessory->model_number) ? e($accessory->model_number) : null,
|
2021-06-10 13:15:52 -07:00
|
|
|
'category' => ($accessory->category) ? ['id' => $accessory->category->id, 'name'=> e($accessory->category->name)] : null,
|
|
|
|
'location' => ($accessory->location) ? ['id' => $accessory->location->id, 'name'=> e($accessory->location->name)] : null,
|
2023-07-11 03:41:58 -07:00
|
|
|
'notes' => ($accessory->notes) ? Helper::parseEscapedMarkedownInline($accessory->notes) : null,
|
2017-07-25 23:40:30 -07:00
|
|
|
'qty' => ($accessory->qty) ? (int) $accessory->qty : null,
|
2017-03-11 02:49:24 -08:00
|
|
|
'purchase_date' => ($accessory->purchase_date) ? Helper::getFormattedDateObject($accessory->purchase_date, 'date') : null,
|
2018-02-24 19:01:34 -08:00
|
|
|
'purchase_cost' => Helper::formatCurrencyOutput($accessory->purchase_cost),
|
2017-01-25 02:19:26 -08:00
|
|
|
'order_number' => ($accessory->order_number) ? e($accessory->order_number) : null,
|
2017-07-25 23:40:30 -07:00
|
|
|
'min_qty' => ($accessory->min_amt) ? (int) $accessory->min_amt : null,
|
2023-01-21 14:40:35 -08:00
|
|
|
'remaining_qty' => (int) $accessory->numRemaining(),
|
2024-07-29 01:54:53 -07:00
|
|
|
'checkouts_count' => $accessory->checkouts_count,
|
2018-09-29 21:33:52 -07:00
|
|
|
|
2017-03-03 17:53:42 -08:00
|
|
|
'created_at' => Helper::getFormattedDateObject($accessory->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($accessory->updated_at, 'datetime'),
|
2017-01-25 02:19:26 -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', Accessory::class),
|
2017-03-11 12:14:10 -08:00
|
|
|
'checkin' => false,
|
2021-06-10 13:15:52 -07:00
|
|
|
'update' => Gate::allows('update', Accessory::class),
|
2020-05-23 10:36:02 -07:00
|
|
|
'delete' => Gate::allows('delete', Accessory::class),
|
2023-03-21 12:48:53 -07:00
|
|
|
'clone' => Gate::allows('create', Accessory::class),
|
2023-03-15 10:48:22 -07:00
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
];
|
|
|
|
|
2017-03-11 12:14:10 -08:00
|
|
|
$permissions_array['user_can_checkout'] = false;
|
|
|
|
|
|
|
|
if ($accessory->numRemaining() > 0) {
|
|
|
|
$permissions_array['user_can_checkout'] = true;
|
|
|
|
}
|
|
|
|
|
2017-02-08 18:24:55 -08:00
|
|
|
$array += $permissions_array;
|
|
|
|
|
|
|
|
return $array;
|
2017-01-25 02:19:26 -08:00
|
|
|
}
|
|
|
|
|
2024-07-29 01:54:53 -07:00
|
|
|
public function transformCheckedoutAccessory($accessory, $accessory_checkouts, $total)
|
2017-01-25 02:19:26 -08:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2020-10-23 00:44:26 -07:00
|
|
|
|
2024-07-29 01:54:53 -07:00
|
|
|
foreach ($accessory_checkouts as $checkout) {
|
2017-09-28 21:18:00 -07:00
|
|
|
$array[] = [
|
2024-07-29 01:54:53 -07:00
|
|
|
'id' => $checkout->id,
|
|
|
|
'assigned_to' => $this->transformAssignedTo($checkout),
|
|
|
|
'checkout_notes' => e($checkout->note),
|
|
|
|
'last_checkout' => Helper::getFormattedDateObject($checkout->created_at, 'datetime'),
|
2021-06-10 13:15:52 -07:00
|
|
|
'available_actions' => ['checkin' => true],
|
2017-09-28 21:18:00 -07:00
|
|
|
];
|
2017-01-25 02:19:26 -08:00
|
|
|
}
|
2017-09-28 21:18:00 -07:00
|
|
|
|
2017-01-25 02:19:26 -08:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
2024-07-29 01:54:53 -07:00
|
|
|
|
|
|
|
public function transformAssignedTo($accessoryCheckout)
|
|
|
|
{
|
|
|
|
if ($accessoryCheckout->checkedOutToUser()) {
|
|
|
|
return [
|
|
|
|
'id' => (int) $accessoryCheckout->assigned->id,
|
|
|
|
'username' => e($accessoryCheckout->assigned->username),
|
|
|
|
'name' => e($accessoryCheckout->assigned->getFullNameAttribute()),
|
|
|
|
'first_name'=> e($accessoryCheckout->assigned->first_name),
|
|
|
|
'last_name'=> ($accessoryCheckout->assigned->last_name) ? e($accessoryCheckout->assigned->last_name) : null,
|
|
|
|
'email'=> ($accessoryCheckout->assigned->email) ? e($accessoryCheckout->assigned->email) : null,
|
|
|
|
'employee_number' => ($accessoryCheckout->assigned->employee_num) ? e($accessoryCheckout->assigned->employee_num) : null,
|
|
|
|
'type' => 'user',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $accessoryCheckout->assigned ? [
|
|
|
|
'id' => $accessoryCheckout->assigned->id,
|
|
|
|
'name' => e($accessoryCheckout->assigned->display_name),
|
|
|
|
'type' => $accessoryCheckout->assignedType(),
|
|
|
|
] : null;
|
|
|
|
}
|
2017-01-25 02:19:26 -08:00
|
|
|
}
|