Files
shipping-portal/app/Http/Resources/PackageBaseResource.php
T
2025-06-26 18:29:15 +08:00

61 lines
2.4 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Models\PackingList;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Crypt;
class PackageBaseResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
// $exceptionUsers = in_array(Auth()->user()->type, [RoleTypes::SHADOW_ADMIN, RoleTypes::SUPER_ADMIN]);
$userType = Auth() && Auth()->user() ? Auth()->user()->type : RoleTypes::USER;
$exceptionUsers = in_array($userType, [RoleTypes::SHADOW_ADMIN, RoleTypes::SUPER_ADMIN]);
$data = [
'id' => $this->id,
'type' => $this->type,
'description' => $this->description,
'width' => floatval($this->width),
'height' => floatval($this->height),
'length' => floatval($this->length),
'weight' => $this->weight,
'quantity' => $this->quantity,
'cbm' => (($this->width / 100) * ($this->height / 100) * ($this->length / 100)) * $this->quantity,
'status' => $this->status,
// 'container' => new ContainerResource($originalPackingList->containers()->first()),
// 'transport' => new TransportResource($originalPackingList->transports()->first())
];
if($this->include_order || $this->include_transport || $this->include_container) {
$originalPackingList = $this->packingList->owner instanceof PackingList ? $this->packingList->owner : $this->packingList;
if($this->include_order) {
$data['order'] = new OrderResource($originalPackingList->owner);
}
if($this->include_transport) {
$data['transport'] = new TransportResource($originalPackingList->transports()->first());
}
if($this->include_container) {
$data['container'] = new ContainerResource($originalPackingList->containers()->first());
}
}
if($this->include_packing_list_reference){
$data['reference'] = $exceptionUsers ? $this->packingList->reference : '';
$data['reference_hash'] = hash('sha256', $this->packingList->reference);
$data['reference_encrypted'] = Crypt::encryptString($this->packingList->reference);
}
return $data;
}
}