Files
2023-05-26 16:56:21 +03:00

123 lines
3.6 KiB
PHP

<?php
namespace App\Transformers;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\PackingList;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;
class PackageListTransformer extends TransformerAbstract
{
/**
* List of resources to automatically include
*
* @var array
*/
protected array $defaultIncludes = [
'transport',
'packages',
];
/**
* List of resources possible to include
*
* @var array
*/
protected array $availableIncludes = [
'order',
'receive_packing_list',
'shipping_transaction',
'suspended_invoice'
];
/**
* A Fractal transformer.
*
* @param PackingList $packingList
* @return array
*/
public function transform(PackingList $packingList): array
{
return [
'id' => $packingList->id,
'claimant_id' => $packingList->claimant_id,
'reference' => $packingList->reference,
'status' => $packingList->status,
'type' => $packingList->type,
];
}
/**
* include Transport
* @param PackingList $packingList
* @return Item
*/
public function includeTransport(PackingList $packingList): Item
{
return $this->item($packingList->transport()->first(), new TransportTransformer);
}
/**
* Include Receive Packing List
* @param PackingList $packingList
* @return Item
*/
public function includeReceivePackageList(PackingList $packingList): Item
{
//$this->when($this->type === PackingListType::SHIPPING_PACKING_LIST, new PackingListResource(PackingList::where('reference', $this->reference)->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->first())),
return $this->item($packingList->receivePackageList()->first(), new PackageListTransformer);
}
/**
* Include Packages
* @param PackingList $packingList
* @return Collection
*/
public function includePackages(PackingList $packingList): Collection
{
$exceptionUsers = in_array(Auth()->user()->type, [RoleTypes::SHADOW_ADMIN, RoleTypes::SUPER_ADMIN]);
$packages = !$packingList->packingLists()->exists() || $exceptionUsers ? $packingList->packages : $packingList->packingLists->first()->packages;
return $this->collection($packages, new PackageTransformer);
}
/**
* include Order
* @param PackingList $packingList
* @return Item
*/
public function includeOrder(PackingList $packingList): Item
{
return $this->item($packingList->owner, new OrderTransformer);
}
/**
* include Shipping Transaction
* @param PackingList $packingList
* @return Item
*/
public function includeShippingTransaction(PackingList $packingList): Item
{
return $this->item($packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereNotIn('status', [ApprovalStatus::SUSPENDED, ApprovalStatus::EXPIRED])->first(), new TransactionTransformer());
}
/**
* include Suspended Invoice
* @param PackingList $packingList
* @return Item
*/
public function includeSuspendedInvoice(PackingList $packingList): Item
{
return $this->item($packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::SUSPENDED])->first(), new TransactionTransformer());
}
}