feat(cart): implement restoration and reservation of source cart on expired checkout modification
This commit is contained in:
@@ -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