feat: enhance purchase detail handling to support cart items and improve resource structure
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
|
||||
namespace App\Domains\Purchase\Resources;
|
||||
|
||||
use App\Domains\Catalog\Resources\ProductVariantDefinitionResource;
|
||||
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
|
||||
* @mixin \App\Domains\Purchase\Models\PurchaseItem|\App\Domains\Cart\Models\CartItem
|
||||
*/
|
||||
class PurchaseItemResource extends JsonResource
|
||||
{
|
||||
@@ -18,24 +19,90 @@ class PurchaseItemResource extends JsonResource
|
||||
{
|
||||
$variant = $this->variant;
|
||||
$product = $variant?->product;
|
||||
$quantity = (int) ($this->cantidad ?? 0);
|
||||
$unitPrice = $this->resolveUnitPrice();
|
||||
$lineTotal = $this->resolveLineTotal($unitPrice, $quantity);
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'cantidad' => $this->cantidad,
|
||||
'precio_unitario' => $this->formatMoney($this->precio_unitario),
|
||||
'total' => $this->formatMoney($this->total),
|
||||
'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,
|
||||
'definitions' => ProductVariantDefinitionResource::collection($variant->definitions),
|
||||
'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) {
|
||||
|
||||
Reference in New Issue
Block a user