mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 12:34:18 +00:00
121 lines
3.6 KiB
PHP
121 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 $defaultIncludes = [
|
|
'transport',
|
|
'receive_packing_list',
|
|
'packages',
|
|
'shipping_transaction',
|
|
'suspended_invoice'
|
|
];
|
|
|
|
/**
|
|
* List of resources possible to include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $availableIncludes = [
|
|
'order'
|
|
];
|
|
|
|
/**
|
|
* 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());
|
|
}
|
|
|
|
}
|