Files
mykopkb-be/Modules/Investment/Transformers/InvestmentResource.php

100 lines
4.7 KiB
PHP

<?php
namespace Modules\Investment\Transformers;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class InvestmentResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'reference_no' => $this->reference_no,
'user_id' => $this->user_id,
'investment_account_id' => $this->investment_account_id,
'investment_type_id' => $this->investment_type_id,
'amount' => $this->amount,
'source' => $this->source?->value ?? $this->source,
'status' => $this->status?->value ?? $this->status,
'remarks' => $this->remarks,
'created_by' => $this->created_by,
'approved_by' => $this->approved_by,
'approved_at' => $this->approved_at?->toISOString(),
'completed_at' => $this->completed_at?->toISOString(),
'cancelled_at' => $this->cancelled_at?->toISOString(),
'created_at' => $this->created_at?->toISOString(),
'updated_at' => $this->updated_at?->toISOString(),
'user' => $this->whenLoaded('user', fn () => $this->user ? [
'id' => $this->user->id,
'name' => $this->user->name,
'email' => $this->user->email,
'ic_number' => $this->user->ic_number,
'member_number' => $this->user->member_number,
] : null),
'account' => $this->whenLoaded('account', fn () => [
'id' => $this->account->id,
'current_balance' => $this->account->current_balance,
'pending_balance' => $this->account->pending_balance,
'status' => $this->account->status?->value ?? $this->account->status,
]),
'investment_type' => $this->whenLoaded('investmentType', fn () => [
'id' => $this->investmentType->id,
'code' => $this->investmentType->code,
'name' => $this->investmentType->name,
]),
'created_by_user' => $this->whenLoaded('createdBy', fn () => $this->createdBy ? [
'id' => $this->createdBy->id,
'name' => $this->createdBy->name,
'email' => $this->createdBy->email,
] : null),
'approved_by_user' => $this->whenLoaded('approvedBy', fn () => $this->approvedBy ? [
'id' => $this->approvedBy->id,
'name' => $this->approvedBy->name,
'email' => $this->approvedBy->email,
] : null),
'ledger_entries' => $this->whenLoaded('ledgerEntries', function () {
return $this->ledgerEntries->map(fn ($entry) => [
'id' => $entry->id,
'transaction_type' => $entry->transaction_type?->value ?? $entry->transaction_type,
'credit' => $entry->credit,
'debit' => $entry->debit,
'balance_after' => $entry->balance_after,
'created_at' => $entry->created_at?->toISOString(),
]);
}),
'payment' => $this->whenLoaded('payment', function () {
if (! $this->payment) {
return null;
}
$billplz = null;
if ($this->payment->relationLoaded('billplzTransaction') && $this->payment->billplzTransaction) {
$billplz = [
'bill_id' => $this->payment->billplzTransaction->bill_id,
'bill_url' => $this->payment->billplzTransaction->bill_url,
'state' => $this->payment->billplzTransaction->state,
'transaction_status' => $this->payment->billplzTransaction->transaction_status?->value
?? $this->payment->billplzTransaction->transaction_status,
'signature_verified' => (bool) $this->payment->billplzTransaction->signature_verified,
];
}
return [
'id' => $this->payment->id,
'gateway' => $this->payment->gateway?->value ?? $this->payment->gateway,
'amount' => $this->payment->amount,
'currency' => $this->payment->currency,
'status' => $this->payment->status?->value ?? $this->payment->status,
'external_reference' => $this->payment->external_reference,
'paid_at' => $this->payment->paid_at?->toISOString(),
'billplz' => $billplz,
];
}),
'documents' => InvestmentDocumentResource::collection($this->whenLoaded('documents')),
];
}
}