115 lines
3.2 KiB
PHP
115 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Purchase\Resources;
|
|
|
|
use App\Domains\Cart\Models\CartItem;
|
|
use App\Domains\Purchase\Models\PurchaseItem;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
/**
|
|
* @mixin \App\Domains\Purchase\Models\PurchaseItem|\App\Domains\Cart\Models\CartItem
|
|
*/
|
|
class PurchaseItemResource extends JsonResource
|
|
{
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
$variant = $this->variant;
|
|
$product = $variant?->product;
|
|
$quantity = (int) ($this->cantidad ?? 0);
|
|
$unitPrice = $this->resolveUnitPrice();
|
|
$lineTotal = $this->resolveLineTotal($unitPrice, $quantity);
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'quantity' => $quantity,
|
|
'unit_price' => $this->formatMoney($unitPrice),
|
|
'line_total' => $this->formatMoney($lineTotal),
|
|
'product' => $product === null ? null : [
|
|
'id' => $product->id,
|
|
'nombre' => $product->nombre,
|
|
'slug' => $product->slug,
|
|
'imagen' => $this->resolveImageUrl(),
|
|
],
|
|
'variant' => $variant === null ? null : [
|
|
'id' => $variant->id,
|
|
'attributes' => $this->resolveAttributes(),
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function resolveUnitPrice(): float
|
|
{
|
|
if ($this->resource instanceof PurchaseItem) {
|
|
return (float) ($this->precio_unitario ?? 0);
|
|
}
|
|
|
|
if ($this->resource instanceof CartItem) {
|
|
return (float) ($this->variant?->product?->precio ?? 0);
|
|
}
|
|
|
|
return 0.0;
|
|
}
|
|
|
|
protected function resolveLineTotal(float $unitPrice, int $quantity): float
|
|
{
|
|
if ($this->resource instanceof PurchaseItem) {
|
|
return (float) ($this->total ?? 0);
|
|
}
|
|
|
|
return $unitPrice * $quantity;
|
|
}
|
|
|
|
protected function resolveImageUrl(): ?string
|
|
{
|
|
$variant = $this->variant;
|
|
|
|
if ($variant === null || ! $variant->relationLoaded('attachments')) {
|
|
return null;
|
|
}
|
|
|
|
$attachment = $variant->attachments->first();
|
|
|
|
if ($attachment === null) {
|
|
return null;
|
|
}
|
|
|
|
return $attachment->getTemporaryUrl(1440);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array{name: string, value: mixed}>
|
|
*/
|
|
protected function resolveAttributes(): array
|
|
{
|
|
$variant = $this->variant;
|
|
|
|
if ($variant === null || ! $variant->relationLoaded('definitions')) {
|
|
return [];
|
|
}
|
|
|
|
return $variant->definitions
|
|
->map(function ($definition): array {
|
|
return [
|
|
'name' => (string) ($definition->productAttribute?->attribute?->nombre ?? ''),
|
|
'value' => $definition->value,
|
|
];
|
|
})
|
|
->filter(fn (array $attribute): bool => $attribute['name'] !== '' || $attribute['value'] !== null)
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
protected function formatMoney(float|int|string|null $amount): ?string
|
|
{
|
|
if ($amount === null) {
|
|
return null;
|
|
}
|
|
|
|
return number_format((float) $amount, 2, '.', '');
|
|
}
|
|
}
|