<?php

namespace Modules\Inventory\Transformers\Purchase;

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

class PurchaseRequisitionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'requisition_no' => $this->requisition_no,
            'requisition_date' => $this->requisition_date,
            'status' => $this->status,
            'create_purchase' => $this->isEligibleForPurchaseInvoice(),
            'subtotal' => (float) $this->subtotal,
            'total_discount' => (float) $this->total_discount,
            'taxable_amount' => (float) $this->taxable_amount,
            'total_vat' => (float) $this->total_vat,
            'grand_total' => (float) $this->grand_total,
            'note' => $this->note,
            'submitted_at' => $this->submitted_at,
            'approved_at' => $this->approved_at,
            'rejected_at' => $this->rejected_at,
            'closed_at' => $this->closed_at,
            'items' => PurchaseRequisitionItemResource::collection(
                $this->whenLoaded('items')
            ),
            'created_at' => $this->created_at,
            'approval_request' => new ApprovalRequestSummaryResource(
                $this->whenLoaded('approvalRequest')
            ),
        ];
    }
}
