2017-01-25 02:19:26 -08:00
|
|
|
<?php
|
|
|
|
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;
|
2017-02-08 18:24:55 -08:00
|
|
|
use 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
|
|
|
|
{
|
|
|
|
|
|
|
|
public function transformAccessories (Collection $accessories, $total)
|
|
|
|
{
|
|
|
|
$array = array();
|
|
|
|
foreach ($accessories as $accessory) {
|
|
|
|
$array[] = self::transformAccessory($accessory);
|
|
|
|
}
|
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function transformAccessory (Accessory $accessory)
|
|
|
|
{
|
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,
|
2017-07-25 23:40:30 -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,
|
2017-10-19 16:25:24 -07:00
|
|
|
'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,
|
2017-07-25 23:40:30 -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,
|
2017-01-25 02:19:26 -08:00
|
|
|
'notes' => ($accessory->notes) ? e($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,
|
2017-01-25 02:19:26 -08:00
|
|
|
'remaining_qty' => $accessory->numRemaining(),
|
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,
|
2020-05-23 10:36:02 -07:00
|
|
|
'update' => Gate::allows('update', Accessory::class) ,
|
|
|
|
'delete' => Gate::allows('delete', Accessory::class),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-31 16:00:38 -07:00
|
|
|
public function transformCheckedoutAccessory ($accessory, $accessory_users, $total)
|
2017-01-25 02:19:26 -08:00
|
|
|
{
|
|
|
|
|
2017-09-28 21:18:00 -07:00
|
|
|
|
2017-01-25 02:19:26 -08:00
|
|
|
$array = array();
|
2017-10-03 21:03:00 -07:00
|
|
|
foreach ($accessory_users as $user) {
|
2017-09-28 21:18:00 -07:00
|
|
|
$array[] = [
|
|
|
|
'assigned_pivot_id' => $user->pivot->id,
|
|
|
|
'id' => (int) $user->id,
|
|
|
|
'username' => e($user->username),
|
|
|
|
'name' => e($user->getFullNameAttribute()),
|
|
|
|
'first_name'=> e($user->first_name),
|
|
|
|
'last_name'=> e($user->last_name),
|
|
|
|
'employee_number' => e($user->employee_num),
|
2018-07-31 16:00:38 -07:00
|
|
|
'checkout_notes' => $accessory->lastCheckoutArray[0]['note'],
|
2017-09-28 21:18:00 -07:00
|
|
|
'type' => 'user',
|
|
|
|
'available_actions' => ['checkin' => true]
|
|
|
|
];
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|