refactor(inventory): record stock operations consistently
This commit is contained in:
@@ -64,6 +64,7 @@ class CatalogInventoryService
|
||||
$selection->loadMissing('variants.inventory');
|
||||
|
||||
return $selection->variants
|
||||
->each(fn (Variant $variant) => $variant->setRelation('catalogItem', $selection))
|
||||
->filter(fn (Variant $variant): bool => $variant->isSellable())
|
||||
->unique(fn (Variant $variant): string => $variant->inventory_id === null
|
||||
? 'object:'.spl_object_id($variant->inventory)
|
||||
@@ -153,7 +154,7 @@ class CatalogInventoryService
|
||||
|
||||
if ($operation === 'commit'
|
||||
&& $requirement['tracks_inventory']
|
||||
&& $inventory->real_stock - $inventory->entry_reserved_stock < $requiredQuantity) {
|
||||
&& $inventory->availableStock() < 0) {
|
||||
throw new \InvalidArgumentException('No hay suficiente stock real para confirmar la compra.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -370,9 +370,12 @@ class CatalogService
|
||||
|
||||
private function createInventory(int $realStock): Inventory
|
||||
{
|
||||
return Inventory::query()->create([
|
||||
$inventory = Inventory::query()->create([
|
||||
'real_stock' => $realStock,
|
||||
]);
|
||||
$inventory->recordInitialization();
|
||||
|
||||
return $inventory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -225,7 +225,7 @@ class StockReservationService
|
||||
$inventory = $inventories->get($line->inventory_id)
|
||||
?? throw new \InvalidArgumentException('No se encontró el inventario reservado.');
|
||||
if ($inventory->reserved_stock < $line->quantity
|
||||
|| ($line->tracks_inventory && $inventory->real_stock - $inventory->entry_reserved_stock < $line->quantity)) {
|
||||
|| ($line->tracks_inventory && $inventory->availableStock() < 0)) {
|
||||
throw new \InvalidArgumentException('La reserva de stock no alcanza para confirmar la compra.');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,6 +194,7 @@ class VariantReplacementService
|
||||
'entry_reserved_stock' => $sourceInventory->entry_reserved_stock,
|
||||
'real_stock' => $sourceInventory->real_stock,
|
||||
]);
|
||||
$replacementInventory->recordTransferInitialization();
|
||||
|
||||
if ($activeLines->isNotEmpty()) {
|
||||
StockReservationLine::query()
|
||||
@@ -202,7 +203,10 @@ class VariantReplacementService
|
||||
}
|
||||
EntryReservation::query()->where('inventory_id', $sourceInventory->id)
|
||||
->update(['inventory_id' => $replacementInventory->id]);
|
||||
$sourceInventory->update(['reserved_stock' => 0, 'entry_reserved_stock' => 0]);
|
||||
$sourceInventory->transferCounters([
|
||||
'reserved_stock' => -$sourceInventory->reserved_stock,
|
||||
'entry_reserved_stock' => -$sourceInventory->entry_reserved_stock,
|
||||
]);
|
||||
|
||||
return $replacementInventory;
|
||||
}
|
||||
@@ -236,13 +240,14 @@ class VariantReplacementService
|
||||
throw new \LogicException('El inventario reservado de la variante es inconsistente.');
|
||||
}
|
||||
|
||||
$destinationInventory->update([
|
||||
'real_stock' => $destinationInventory->real_stock + $sourceInventory->real_stock,
|
||||
'reserved_stock' => $destinationInventory->reserved_stock + $sourceInventory->reserved_stock,
|
||||
'entry_reserved_stock' => $destinationInventory->entry_reserved_stock + $sourceInventory->entry_reserved_stock,
|
||||
'sold_units' => $destinationInventory->sold_units + $sourceInventory->sold_units,
|
||||
'refunded_units' => $destinationInventory->refunded_units + $sourceInventory->refunded_units,
|
||||
]);
|
||||
$transferredCounters = [
|
||||
'real_stock' => $sourceInventory->real_stock,
|
||||
'reserved_stock' => $sourceInventory->reserved_stock,
|
||||
'entry_reserved_stock' => $sourceInventory->entry_reserved_stock,
|
||||
'sold_units' => $sourceInventory->sold_units,
|
||||
'refunded_units' => $sourceInventory->refunded_units,
|
||||
];
|
||||
$destinationInventory->transferCounters($transferredCounters);
|
||||
if ($activeLines->isNotEmpty()) {
|
||||
StockReservationLine::query()
|
||||
->whereKey($activeLines->modelKeys())
|
||||
@@ -250,13 +255,10 @@ class VariantReplacementService
|
||||
}
|
||||
EntryReservation::query()->where('inventory_id', $sourceInventory->id)
|
||||
->update(['inventory_id' => $destinationInventory->id]);
|
||||
$sourceInventory->update([
|
||||
'real_stock' => 0,
|
||||
'reserved_stock' => 0,
|
||||
'entry_reserved_stock' => 0,
|
||||
'sold_units' => 0,
|
||||
'refunded_units' => 0,
|
||||
]);
|
||||
$sourceInventory->transferCounters(array_map(
|
||||
fn (int $value): int => -$value,
|
||||
$transferredCounters,
|
||||
));
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
|
||||
Reference in New Issue
Block a user