refactor(checkout): materialize purchase item snapshots at start
This commit is contained in:
@@ -2,12 +2,10 @@
|
||||
|
||||
namespace App\Domains\Sale\Resources\AdminApp;
|
||||
|
||||
use App\Domains\Cart\Models\CartItem;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Models\PurchaseItem;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/** @mixin Purchase */
|
||||
class SaleDetailResource extends JsonResource
|
||||
@@ -15,44 +13,23 @@ class SaleDetailResource extends JsonResource
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$items = $this->saleItems();
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'items' => $items->map(fn (PurchaseItem|CartItem $item): array => [
|
||||
'items' => $this->items->map(fn (PurchaseItem $item): array => [
|
||||
'id' => $item->id,
|
||||
'product' => $item instanceof PurchaseItem
|
||||
? $item->item_nombre
|
||||
: $item->selectedItem()?->getName(),
|
||||
'product' => $item->item_nombre,
|
||||
'event_dates' => $this->eventDates($item),
|
||||
'quantity' => (int) $item->cantidad,
|
||||
'unit_price' => $this->formatMoney($this->unitPrice($item)),
|
||||
'total' => $this->formatMoney($this->lineTotal($item)),
|
||||
'unit_price' => $this->formatMoney($item->precio_unitario),
|
||||
'total' => $this->formatMoney($item->total),
|
||||
])->values(),
|
||||
'total' => $this->formatMoney($this->total),
|
||||
];
|
||||
}
|
||||
|
||||
/** @return Collection<int, PurchaseItem|CartItem> */
|
||||
private function saleItems(): Collection
|
||||
{
|
||||
if ($this->items->isNotEmpty()) {
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
return $this->cart?->items ?? collect();
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function eventDates(PurchaseItem|CartItem $item): array
|
||||
private function eventDates(PurchaseItem $item): array
|
||||
{
|
||||
if ($item instanceof CartItem) {
|
||||
return $item->variant?->selectedEventDates()
|
||||
->map(fn ($eventDate): string => $eventDate->date->format('Y-m-d'))
|
||||
->values()
|
||||
->all() ?? [];
|
||||
}
|
||||
|
||||
return collect($item->variant_attributes ?? [])
|
||||
->filter(fn (mixed $attribute): bool => is_array($attribute)
|
||||
&& mb_strtolower(trim((string) ($attribute['name'] ?? ''))) === 'fecha')
|
||||
@@ -66,20 +43,6 @@ class SaleDetailResource extends JsonResource
|
||||
->all();
|
||||
}
|
||||
|
||||
private function unitPrice(PurchaseItem|CartItem $item): float|int|string|null
|
||||
{
|
||||
return $item instanceof PurchaseItem
|
||||
? $item->precio_unitario
|
||||
: $item->selectedItem()?->getPrice();
|
||||
}
|
||||
|
||||
private function lineTotal(PurchaseItem|CartItem $item): float|int|string|null
|
||||
{
|
||||
return $item instanceof PurchaseItem
|
||||
? $item->total
|
||||
: ($item->selectedItem()?->getPrice() ?? 0) * $item->cantidad;
|
||||
}
|
||||
|
||||
private function formatMoney(float|int|string|null $amount): string
|
||||
{
|
||||
return number_format((float) ($amount ?? 0), 2, '.', '');
|
||||
|
||||
@@ -51,14 +51,7 @@ class AdminAppSaleService
|
||||
{
|
||||
return Purchase::query()
|
||||
->where('tenant_codigo', $tenant->codigo)
|
||||
->with([
|
||||
'items',
|
||||
'cart' => fn ($query) => $query->withTrashed(),
|
||||
'cart.items.catalogItem',
|
||||
'cart.items.variant.catalogItem',
|
||||
'cart.items.variant.eventDates',
|
||||
'cart.items.variant.eventDate',
|
||||
])
|
||||
->with('items')
|
||||
->findOrFail($saleId);
|
||||
}
|
||||
|
||||
@@ -154,15 +147,9 @@ class AdminAppSaleService
|
||||
)
|
||||
->select('compras.*')
|
||||
->selectRaw(
|
||||
'CASE WHEN compras.status IN (?, ?) '
|
||||
.'THEN (SELECT COALESCE(SUM(cart_items.cantidad), 0) FROM carrito_items AS cart_items '
|
||||
.'WHERE cart_items.cart_id = compras.cart_id) '
|
||||
.'ELSE (SELECT COALESCE(SUM(purchase_items.cantidad), 0) FROM compra_items AS purchase_items '
|
||||
.'WHERE purchase_items.compra_id = compras.id) END AS quantity',
|
||||
[
|
||||
Purchase::STATUS_CREATED,
|
||||
Purchase::STATUS_PENDING_PAYMENT,
|
||||
],
|
||||
'(SELECT COALESCE(SUM(purchase_items.cantidad), 0) '
|
||||
.'FROM compra_items AS purchase_items '
|
||||
.'WHERE purchase_items.compra_id = compras.id) AS quantity',
|
||||
)
|
||||
->withCount('tickets')
|
||||
->orderBy($sortColumns[$sortBy], $sortDirection)
|
||||
@@ -199,16 +186,11 @@ class AdminAppSaleService
|
||||
|
||||
protected function quantityExpression(): string
|
||||
{
|
||||
// El fallback se resuelve en SQL para poder ordenar por cantidad antes de paginar;
|
||||
// los ítems consolidados de la compra tienen prioridad sobre los del carrito de origen.
|
||||
return <<<'SQL'
|
||||
COALESCE(
|
||||
(SELECT SUM(compra_items.cantidad)
|
||||
FROM compra_items
|
||||
WHERE compra_items.compra_id = compras.id),
|
||||
(SELECT SUM(carrito_items.cantidad)
|
||||
FROM carrito_items
|
||||
WHERE carrito_items.cart_id = compras.cart_id),
|
||||
0
|
||||
) AS quantity
|
||||
SQL;
|
||||
|
||||
Reference in New Issue
Block a user