81 lines
4.0 KiB
PHP
81 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Resources\Api\Service\Order;
|
|
|
|
use App\Enums\Invoice\InvoiceStatus;
|
|
use App\Enums\Order\OrderStatus;
|
|
use App\Models\InvoiceAttachment;
|
|
use App\Services\InvoicePaymentService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class OrderDetailResource extends JsonResource
|
|
{
|
|
use ResolvesPaymentMethod;
|
|
|
|
private const INVOICE_TYPE_LABELS = [1 => 'Đơn mới', 2 => 'Gia hạn', 3 => 'Nâng cấp'];
|
|
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'order_num' => $this->order_num,
|
|
'status' => $this->status,
|
|
'status_text' => OrderStatus::from((int) $this->status)->label(),
|
|
'total' => (float) $this->total,
|
|
'created_at' => $this->created_at?->toISOString(),
|
|
'customer' => $this->whenLoaded('customer', fn () => [
|
|
'id' => $this->customer->id,
|
|
'name' => $this->customer->fullname,
|
|
'email' => $this->customer->email,
|
|
]),
|
|
'invoice' => $this->whenLoaded('invoice', fn () => [
|
|
'id' => $this->invoice->id,
|
|
'invoice_number' => $this->invoice->invoice_number,
|
|
'status' => $this->invoice->status,
|
|
'status_text' => InvoiceStatus::tryFrom((int) $this->invoice->status)?->label() ?? '-',
|
|
'sub_total' => (float) $this->invoice->sub_total,
|
|
'tax' => (float) $this->invoice->tax,
|
|
'total' => (float) $this->invoice->total,
|
|
'due_date' => $this->invoice->due_date?->toISOString(),
|
|
'paid_at' => $this->invoice->paid_at?->toISOString(),
|
|
'payment_method' => $this->resolvePaymentMethod($this->invoice),
|
|
]),
|
|
'invoices' => $this->whenLoaded('invoices', fn () => $this->invoices
|
|
->sortByDesc('id')
|
|
->values()
|
|
->map(fn ($invoice) => [
|
|
'id' => $invoice->id,
|
|
'invoice_number' => $invoice->invoice_number,
|
|
'invoice_type' => (int) $invoice->invoice_type,
|
|
'type_text' => self::INVOICE_TYPE_LABELS[(int) $invoice->invoice_type] ?? 'Hóa đơn',
|
|
'status' => (int) $invoice->status,
|
|
'status_text' => InvoiceStatus::tryFrom((int) $invoice->status)?->label() ?? '-',
|
|
'total' => (float) $invoice->total,
|
|
'amount_paid' => (float) $invoice->amount_paid,
|
|
'due_date' => $invoice->due_date?->toISOString(),
|
|
'paid_at' => $invoice->paid_at?->toISOString(),
|
|
'billing_period_start' => $invoice->billing_period_start,
|
|
'billing_period_end' => $invoice->billing_period_end,
|
|
'payable' => in_array((int) $invoice->status, InvoicePaymentService::PAYABLE_STATUSES, true),
|
|
'payment_url' => $invoice->setRelation('order', $this->resource)->clientPath(),
|
|
])),
|
|
'contracts' => $this->whenLoaded('invoices', fn () => $this->invoices
|
|
->flatMap(fn ($invoice) => $invoice->relationLoaded('attachments')
|
|
? $invoice->attachments
|
|
->where('type', InvoiceAttachment::TYPE_CONTRACT)
|
|
->map(fn ($attachment) => [
|
|
'id' => $attachment->id,
|
|
'filename' => $attachment->filename,
|
|
'mime_type' => $attachment->mime_type,
|
|
'size' => (int) $attachment->size,
|
|
'invoice_number' => $invoice->invoice_number,
|
|
'uploaded_at' => $attachment->created_at?->toISOString(),
|
|
])
|
|
: collect())
|
|
->values()),
|
|
'items' => $this->whenLoaded('orderItems') ? OrderItemResource::collection($this->orderItems) : [],
|
|
];
|
|
}
|
|
}
|