feat: update sales query to use cart quantity when purchase items are absent and enhance related tests
This commit is contained in:
@@ -151,7 +151,8 @@ class AdminAppSaleService
|
||||
$filters['status'] ?? null,
|
||||
fn (Builder $query, string $status): Builder => $query->where('status', $status)
|
||||
)
|
||||
->withSum('items as quantity', 'cantidad')
|
||||
->select('compras.*')
|
||||
->selectRaw($this->quantityExpression())
|
||||
->withCount('tickets')
|
||||
->orderBy($sortColumns[$sortBy], $sortDirection)
|
||||
->when($sortBy !== 'id', fn (Builder $query): Builder => $query->orderByDesc('id'));
|
||||
@@ -177,8 +178,26 @@ class AdminAppSaleService
|
||||
|
||||
protected function saleForResponse(Purchase $sale): Purchase
|
||||
{
|
||||
return $sale->refresh()
|
||||
->loadSum('items as quantity', 'cantidad')
|
||||
->loadCount('tickets');
|
||||
/** @var Purchase */
|
||||
return Purchase::query()
|
||||
->select('compras.*')
|
||||
->selectRaw($this->quantityExpression())
|
||||
->withCount('tickets')
|
||||
->findOrFail($sale->getKey());
|
||||
}
|
||||
|
||||
protected function quantityExpression(): string
|
||||
{
|
||||
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