feat(cart): implement restoration and reservation of source cart on expired checkout modification
This commit is contained in:
@@ -118,6 +118,37 @@ class StockReservationService
|
||||
]);
|
||||
}
|
||||
|
||||
public function restore(CartItem $cartItem, CatalogItem|Variant $selection): void
|
||||
{
|
||||
DB::transaction(function () use ($cartItem, $selection): void {
|
||||
$requirements = $this->inventory->requirementsFor(
|
||||
$selection,
|
||||
(int) $cartItem->cantidad,
|
||||
);
|
||||
$activeReservations = StockReservation::query()
|
||||
->where('cart_item_id', $cartItem->getKey())
|
||||
->where('status', StockReservation::STATUS_ACTIVE)
|
||||
->lockForUpdate()
|
||||
->get()
|
||||
->keyBy('inventory_id');
|
||||
|
||||
$hasCompleteReservation = collect($requirements)->every(
|
||||
fn (int $quantity, int $inventoryId): bool => (int) ($activeReservations->get($inventoryId)?->quantity ?? 0) === $quantity,
|
||||
);
|
||||
|
||||
if ($hasCompleteReservation) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($activeReservations->isNotEmpty()) {
|
||||
throw new \InvalidArgumentException('La reserva de stock del carrito es inconsistente.');
|
||||
}
|
||||
|
||||
$this->inventory->reserve($selection, (int) $cartItem->cantidad);
|
||||
$this->recordIncrease($cartItem, $selection, (int) $cartItem->cantidad);
|
||||
});
|
||||
}
|
||||
|
||||
public function syncPurchaseExpiration(Purchase $purchase): void
|
||||
{
|
||||
StockReservation::query()
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace App\Domains\Purchase\Services\Checkout;
|
||||
use App\Domains\Catalog\Models\StockReservation;
|
||||
use App\Domains\Catalog\Services\StockReservationService;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Purchase\Services\PurchaseStateGuard;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -72,7 +71,7 @@ class ReleaseCheckoutService
|
||||
return DB::transaction(function () use ($purchase, $targetStatus, $restoreCart): Purchase {
|
||||
$purchase = $this->lockPurchase($purchase);
|
||||
|
||||
if ($targetStatus !== Purchase::STATUS_EXPIRED) {
|
||||
if ($targetStatus !== Purchase::STATUS_EXPIRED && ! $restoreCart) {
|
||||
$this->purchaseState->assertNotExpired($purchase);
|
||||
}
|
||||
|
||||
@@ -86,7 +85,7 @@ class ReleaseCheckoutService
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->isAlreadyReleased($purchase)) {
|
||||
if ($this->isAlreadyReleased($purchase) && ! ($restoreCart && $purchase->status === Purchase::STATUS_EXPIRED)) {
|
||||
return $this->loadPurchase($purchase);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,8 @@ class SourceCartService
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->restoreReservations($sourceCart);
|
||||
|
||||
/** @var Cart|null $activeCart */
|
||||
$activeCart = Cart::query()
|
||||
->where('tenant_codigo', $purchase->tenant_codigo)
|
||||
@@ -62,6 +64,25 @@ class SourceCartService
|
||||
return true;
|
||||
}
|
||||
|
||||
private function restoreReservations(Cart $cart): void
|
||||
{
|
||||
$items = $cart->items()->orderBy('id')->lockForUpdate()->get();
|
||||
$items->load([
|
||||
'catalogItem.inventory',
|
||||
'catalogItem.bundleComponents.catalogItem.inventory',
|
||||
'catalogItem.bundleComponents.variant.inventory',
|
||||
'variant.inventory',
|
||||
'variant.catalogItem',
|
||||
]);
|
||||
|
||||
foreach ($items as $item) {
|
||||
$selection = $item->selectedItem();
|
||||
if ($selection !== null) {
|
||||
$this->reservations->restore($item, $selection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function syncItemQuantity(
|
||||
Purchase $purchase,
|
||||
PurchaseItem $purchaseItem,
|
||||
|
||||
Reference in New Issue
Block a user