mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-21 13:34:00 +00:00
63 lines
2.5 KiB
PHP
63 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\PackingListType;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Models\Order;
|
|
use App\Models\PackingList;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class PackingListPaidInvoiceResource extends JsonResource
|
|
{
|
|
/**
|
|
* Transform the resource into an array.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return array
|
|
*/
|
|
public function toArray($request)
|
|
{
|
|
|
|
//The Base
|
|
$data = [
|
|
'id' => $this->id,
|
|
'claimant_id' => $this->claimant_id,
|
|
'reference' => $this->reference,
|
|
'status' => $this->status,
|
|
'transport' => new TransportResource($this->transports()->first()),
|
|
'type' => $this->type,
|
|
'container' => new ContainerResource($this->containers->first()),
|
|
$this->mergeWhen($this->owner instanceof Order, [
|
|
'order' => New OrderResource($this->owner)
|
|
]),
|
|
];
|
|
|
|
//option 1: include_packages
|
|
$data['packages'] = PackageBaseResource::collection([]);
|
|
|
|
//option 4: include_suspended_invoice
|
|
$data['suspended_invoice'] = new TransactionResource($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::SUSPENDED])->first());
|
|
|
|
|
|
//option 5: include_shipping_transaction WITH warehouse storage
|
|
$shippingTransaction = $this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereNotIn('status', [ApprovalStatus::SUSPENDED, ApprovalStatus::EXPIRED])->first();
|
|
$transactions = $this->transactions()->whereNotIn('transactions.status', [0, 1])->whereIn('transactions.type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE])->get();
|
|
$storages = [];
|
|
|
|
if($this->storages){
|
|
$transactionId = $shippingTransaction->id;
|
|
$filteredStorages = array_filter($this->storages, function ($storage) use ($transactionId) {
|
|
return isset($storage['parentInvoiceId']) && $storage['parentInvoiceId'] == $transactionId;
|
|
});
|
|
$storages = $filteredStorages;
|
|
}
|
|
$data['shipping_transaction'] = count($storages) == 0 ? new TransactionWithStorageResource($shippingTransaction): null;
|
|
$data['storages'] = $storages;
|
|
$data['invoices'] = TransactionWithStorageResource::collection($transactions);
|
|
|
|
return $data;
|
|
}
|
|
}
|