feat(variants): implement inventory cloning for historical stock updates and adjust related tests
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
namespace App\Domains\Catalog\Services;
|
||||
|
||||
use App\Domains\Catalog\Models\BundleComponent;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\StockReservation;
|
||||
use App\Domains\Catalog\Models\StockReservationLine;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Event\Models\EventDate;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -92,11 +95,14 @@ class VariantReplacementService
|
||||
/** @param Collection<int, int> $eventDateIds */
|
||||
private function cloneWithDates(Variant $source, Collection $eventDateIds): Variant
|
||||
{
|
||||
$replacementInventory = $this->cloneInventory($source);
|
||||
$replacement = $source->replicate([
|
||||
'event_date_id',
|
||||
'inventory_id',
|
||||
'replaced_by_variant_id',
|
||||
'sales_disabled_at',
|
||||
]);
|
||||
$replacement->inventory_id = $replacementInventory->getKey();
|
||||
$replacement->event_date_id = $eventDateIds->count() === 1
|
||||
? $eventDateIds->first()
|
||||
: null;
|
||||
@@ -125,6 +131,41 @@ class VariantReplacementService
|
||||
return $replacement->load(['eventDates', 'eventDate', 'definitions', 'allAttachments']);
|
||||
}
|
||||
|
||||
private function cloneInventory(Variant $source): Inventory
|
||||
{
|
||||
$activeLines = StockReservationLine::query()
|
||||
->where('inventory_id', $source->inventory_id)
|
||||
->whereHas('reservation', fn ($reservation) => $reservation
|
||||
->where('status', StockReservation::STATUS_ACTIVE))
|
||||
->orderBy('id')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
$sourceInventory = Inventory::query()
|
||||
->whereKey($source->inventory_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
$reservedStock = (int) $activeLines->sum('quantity');
|
||||
|
||||
if ($sourceInventory->reserved_stock !== $reservedStock) {
|
||||
throw new \LogicException('El inventario reservado de la variante es inconsistente.');
|
||||
}
|
||||
|
||||
$replacementInventory = Inventory::query()->create([
|
||||
'sold_units' => $sourceInventory->sold_units,
|
||||
'reserved_stock' => $reservedStock,
|
||||
'real_stock' => $sourceInventory->real_stock,
|
||||
]);
|
||||
|
||||
if ($activeLines->isNotEmpty()) {
|
||||
StockReservationLine::query()
|
||||
->whereKey($activeLines->modelKeys())
|
||||
->update(['inventory_id' => $replacementInventory->getKey()]);
|
||||
}
|
||||
$sourceInventory->update(['reserved_stock' => 0]);
|
||||
|
||||
return $replacementInventory;
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
private function definitionSignature(Variant $variant): array
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Domains\FiestaFutbolInfantil\Controllers;
|
||||
|
||||
use App\Domains\FiestaFutbolInfantil\Requests\UpsertFoodVariantsRequest;
|
||||
use App\Domains\FiestaFutbolInfantil\Requests\UpdateHistoricalFoodStockRequest;
|
||||
use App\Domains\FiestaFutbolInfantil\Requests\UpsertFoodVariantsRequest;
|
||||
use App\Domains\FiestaFutbolInfantil\Resources\FoodResource;
|
||||
use App\Domains\FiestaFutbolInfantil\Services\FoodService;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
@@ -126,8 +126,6 @@ class FoodService
|
||||
$variantIds = collect($variants)->pluck('id')->map(fn ($id): int => (int) $id);
|
||||
$historicalVariants = $food->variants()
|
||||
->whereIn('id', $variantIds)
|
||||
->whereNull('sales_disabled_at')
|
||||
->whereNull('replaced_by_variant_id')
|
||||
->with(['inventory', 'eventDate', 'eventDates'])
|
||||
->lockForUpdate()
|
||||
->get()
|
||||
@@ -145,10 +143,7 @@ class FoodService
|
||||
}
|
||||
|
||||
$stock = (int) $data['stock'];
|
||||
$inventory = Inventory::query()
|
||||
->whereKey($variant->inventory_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
$inventory = $this->inventoryForHistoricalStockUpdate($variant);
|
||||
if ($stock < $inventory->reserved_stock) {
|
||||
throw ValidationException::withMessages([
|
||||
"variants.{$index}.stock" => [
|
||||
@@ -164,6 +159,32 @@ class FoodService
|
||||
});
|
||||
}
|
||||
|
||||
private function inventoryForHistoricalStockUpdate(Variant $variant): Inventory
|
||||
{
|
||||
$inventory = Inventory::query()
|
||||
->whereKey($variant->inventory_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
$variantsSharingInventory = Variant::query()
|
||||
->where('inventory_id', $inventory->getKey())
|
||||
->orderBy('id')
|
||||
->lockForUpdate()
|
||||
->get(['id']);
|
||||
|
||||
if ($variantsSharingInventory->count() === 1) {
|
||||
return $inventory;
|
||||
}
|
||||
|
||||
$historicalInventory = Inventory::query()->create([
|
||||
'sold_units' => $inventory->sold_units,
|
||||
'reserved_stock' => 0,
|
||||
'real_stock' => $inventory->real_stock,
|
||||
]);
|
||||
$variant->update(['inventory_id' => $historicalInventory->getKey()]);
|
||||
|
||||
return $historicalInventory;
|
||||
}
|
||||
|
||||
public function delete(Tenant $tenant, int $foodId): void
|
||||
{
|
||||
$variant = Variant::query()
|
||||
|
||||
Reference in New Issue
Block a user