<?php

namespace Modules\Inventory\Transformers\Purchase;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Modules\Accounts\Transformers\CoaResource;
use Modules\Approval\Transformers\ApprovalRequestSummaryResource;

class PurchasePaymentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'purchase_invoice_id' =>$this->purchase_invoice_id,
            'purchase_invoice' => $this->whenLoaded('purchaseInvoice', function () {
                return $this->purchaseInvoice;
            }),
            'account_id' =>$this->account_id,
            'account' => new CoaResource($this->whenLoaded('account')),
            'voucher_id' =>$this->voucher_id,
            'voucher' => $this->whenLoaded('voucher', function () {
                return $this->voucher;
            }),
            'payment_date' =>$this->payment_date,
            'amount' =>(float) $this->amount,
            'payment_method' =>$this->payment_method,
            'reference' =>$this->reference,
            'note' =>$this->note,
            'status' =>$this->status,
            'can_be_posted' => $this->isEligibleForPosting(),
            'is_posted' =>(bool) $this->is_posted,
            'posted_at' =>$this->posted_at,
            'created_at' => $this->created_at,
            'approval_request' => new ApprovalRequestSummaryResource(
                $this->whenLoaded('approvalRequest')
            )
        ];
    }
}
