mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/shipping-portal.git
synced 2026-08-19 20:44:19 +00:00
127 lines
3.9 KiB
PHP
127 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Transformers;
|
|
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\DocumentType;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Models\Transaction;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use League\Fractal\Resource\Item;
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
class GroupTransformer extends TransformerAbstract
|
|
{
|
|
/**
|
|
* List of resources to automatically include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $defaultIncludes = [
|
|
'original_currency',
|
|
'currency',
|
|
'documents'
|
|
];
|
|
|
|
/**
|
|
* List of resources possible to include
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $availableIncludes = [
|
|
//
|
|
];
|
|
|
|
/**
|
|
* A Fractal transformer.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function transform(Transaction $transaction): array
|
|
{
|
|
return [
|
|
'id' => $transaction->id,
|
|
'original_amount' => (float)$transaction->original_amount,
|
|
'issuer_name' => $transaction->issuerCompany->name,
|
|
'issuer_id' => $transaction->issuerCompany->id,
|
|
'amount' => (float)$transaction->amount,
|
|
'service_charge' => (float)$transaction->amount,
|
|
'created_at' => Carbon::parse($transaction->created_at)->format('d-m-Y h:i:s A'),
|
|
'currency_rate' => (float)$transaction->currency_rate,
|
|
'transactions' => $this->transactions($transaction),
|
|
'complete_transactions' => $this->complete_transactions($transaction),
|
|
'documents' => [
|
|
'currency_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->first()),
|
|
'purchase_order' => new DocumentResource($this->documents()->where('document_type', DocumentType::BULK_PURCHASE_ORDER)->first())
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Include Original Currency
|
|
*
|
|
* @param Transaction $transaction
|
|
* @return Item
|
|
*/
|
|
public function includeOriginalCurrency(Transaction $transaction): Item
|
|
{
|
|
return $this->item($transaction->original_currency, new CurrencyTransformer());
|
|
}
|
|
|
|
/**
|
|
* Include Currency
|
|
*
|
|
* @param Transaction $transaction
|
|
* @return Item
|
|
*/
|
|
public function includeCurrency(Transaction $transaction): Item
|
|
{
|
|
return $this->item($transaction->currency, new CurrencyTransformer());
|
|
}
|
|
|
|
/**
|
|
* Get Transactions
|
|
* @param Transaction $transaction
|
|
* @return Collection
|
|
*/
|
|
protected function transactions(Transaction $transaction): Collection
|
|
{
|
|
return $transaction->transactions()->get()->pluck('owner.owner.marking');
|
|
}
|
|
|
|
/**
|
|
* complete Transactions
|
|
* @param Transaction $transaction
|
|
* @return Collection
|
|
*/
|
|
protected function complete_transactions(Transaction $transaction): Collection
|
|
{
|
|
return $transaction->transactions()->whereHasMorph('owner', [Transaction::class], function ($query) {
|
|
return $query->whereHas('booking', function ($query) {
|
|
return $query->whereHas('transactions', function ($query) {
|
|
return $query->where('type', TransactionType::PURCHASE_ORDER)->where('status', '=', ApprovalStatus::APPROVED);
|
|
});
|
|
});
|
|
})->get()->pluck('owner.owner.marking');
|
|
|
|
}
|
|
|
|
/**
|
|
* Include Documents
|
|
*
|
|
* @param Transaction $transaction
|
|
* @return array
|
|
*/
|
|
public function includeDocuments(Transaction $transaction): array
|
|
{
|
|
return [
|
|
'currency_order' => $this->item($transaction->documents()->where('document_type', DocumentType::CURRENCY_VENDOR_ORDER)->first(), new DocumentTransformer()),
|
|
'purchase_order' => $this->item($transaction->documents()->where('document_type', DocumentType::BULK_PURCHASE_ORDER)->first(), new DocumentTransformer())
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|