feat(variants): implement inventory cloning for historical stock updates and adjust related tests
This commit is contained in:
@@ -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