2017-05-31 06:52:37 -07:00
|
|
|
<?php
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-31 06:52:37 -07:00
|
|
|
namespace App\Http\Transformers;
|
|
|
|
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-05-31 06:52:37 -07:00
|
|
|
use App\Models\Supplier;
|
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-05-31 06:52:37 -07:00
|
|
|
|
|
|
|
class SuppliersTransformer
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformSuppliers(Collection $suppliers, $total)
|
2017-05-31 06:52:37 -07:00
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
$array = [];
|
2017-05-31 06:52:37 -07:00
|
|
|
foreach ($suppliers as $supplier) {
|
|
|
|
$array[] = self::transformSupplier($supplier);
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-05-31 06:52:37 -07:00
|
|
|
return (new DatatablesTransformer)->transformDatatables($array, $total);
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
public function transformSupplier(Supplier $supplier = null)
|
2017-05-31 06:52:37 -07:00
|
|
|
{
|
|
|
|
if ($supplier) {
|
|
|
|
$array = [
|
2017-07-25 23:40:30 -07:00
|
|
|
'id' => (int) $supplier->id,
|
2017-05-31 06:52:37 -07:00
|
|
|
'name' => e($supplier->name),
|
2018-09-29 21:33:52 -07:00
|
|
|
'image' => ($supplier->image) ? Storage::disk('public')->url('suppliers/'.e($supplier->image)) : null,
|
2018-08-01 17:37:58 -07:00
|
|
|
'url' => e($supplier->url),
|
2020-05-23 11:58:44 -07:00
|
|
|
'address' => e($supplier->address),
|
|
|
|
'address2' => e($supplier->address2),
|
|
|
|
'city' => e($supplier->city),
|
|
|
|
'state' => e($supplier->state),
|
|
|
|
'country' => e($supplier->country),
|
|
|
|
'zip' => e($supplier->zip),
|
|
|
|
'fax' => e($supplier->fax),
|
|
|
|
'phone' => e($supplier->phone),
|
|
|
|
'email' => e($supplier->email),
|
|
|
|
'contact' => e($supplier->contact),
|
2017-07-25 23:40:30 -07:00
|
|
|
'assets_count' => (int) $supplier->assets_count,
|
2017-10-19 16:25:24 -07:00
|
|
|
'accessories_count' => (int) $supplier->accessories_count,
|
2017-07-25 23:40:30 -07:00
|
|
|
'licenses_count' => (int) $supplier->licenses_count,
|
2023-04-13 11:10:32 -07:00
|
|
|
'consumables_count' => (int) $supplier->consumables_count,
|
|
|
|
'components_count' => (int) $supplier->components_count,
|
2017-08-22 14:18:21 -07:00
|
|
|
'notes' => ($supplier->notes) ? e($supplier->notes) : null,
|
2017-05-31 06:52:37 -07:00
|
|
|
'created_at' => Helper::getFormattedDateObject($supplier->created_at, 'datetime'),
|
|
|
|
'updated_at' => Helper::getFormattedDateObject($supplier->updated_at, 'datetime'),
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$permissions_array['available_actions'] = [
|
2020-05-23 10:36:02 -07:00
|
|
|
'update' => Gate::allows('update', Supplier::class),
|
2021-06-10 13:15:52 -07:00
|
|
|
'delete' => (Gate::allows('delete', Supplier::class) && ($supplier->assets_count == 0) && ($supplier->licenses_count == 0) && ($supplier->accessories_count == 0)),
|
2017-05-31 06:52:37 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
$array += $permissions_array;
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|