*/ public function toArray(Request $request): array { $items = $this->resource->relationLoaded('items') ? $this->resource->getRelation('items') : collect(); $subtotal = $items->reduce( fn (float $carry, $item): float => $carry + ((float) $item->precio_unitario * $item->cantidad), 0.0, ); $total = $items->reduce( fn (float $carry, $item): float => $carry + (float) $item->total, 0.0, ); return [ 'id' => $this->id, 'tenant_codigo' => $this->tenant_codigo, 'user_id' => $this->user_id, 'status' => $this->status, 'payment_status' => $this->payment_status, 'payment_method' => $this->payment_method, 'items' => PurchaseItemResource::collection($items), 'subtotal' => $this->formatMoney($subtotal), 'total' => $this->formatMoney($total), ]; } protected function formatMoney(float|int|string|null $amount): string { return number_format((float) ($amount ?? 0), 2, '.', ''); } }