refactor(inventory): update stock validation rules and enforce stock_difference requirement

This commit is contained in:
2026-09-25 15:28:49 -03:00
parent 717f02ce16
commit a05d15d17f
13 changed files with 141 additions and 31 deletions

View File

@@ -113,7 +113,7 @@ class FoodService
}
/**
* @param array<int, array{id: int, stock: int}> $variants
* @param array<int, array{id: int, stock_difference: int}> $variants
*/
public function updateHistoricalStock(Tenant $tenant, array $variants, ?string $stockAdjustmentId = null): CatalogItem
{
@@ -143,8 +143,10 @@ class FoodService
}
$inventory = $this->inventoryForHistoricalStockUpdate($variant);
$stockDifference = (int) ($data['stock_difference'] ?? ((int) $data['stock'] - $inventory->availableStock()));
$inventory->adjustAvailableStock($stockDifference, idempotencyKey: $stockAdjustmentId);
$inventory->adjustAvailableStock(
(int) $data['stock_difference'],
idempotencyKey: $stockAdjustmentId,
);
}
return $this->current($tenant) ?? $food;
@@ -294,7 +296,7 @@ class FoodService
'schedule' => $schedule->value,
'service' => $service->value,
'description' => (string) ($variant['description'] ?? ''),
'stock' => (int) $variant['stock'],
...(array_key_exists('stock', $variant) ? ['stock' => (int) $variant['stock']] : []),
];
})->all();
}
@@ -407,8 +409,10 @@ class FoodService
'precio' => $data['price'],
]);
$variant->eventDates()->sync([$data['event_date_id']]);
$stockDifference = (int) ($data['stock_difference'] ?? ($data['stock'] - $inventory->availableStock()));
$inventory->adjustAvailableStock($stockDifference, idempotencyKey: $stockAdjustmentId);
$inventory->adjustAvailableStock(
(int) $data['stock_difference'],
idempotencyKey: $stockAdjustmentId,
);
$this->syncDefinitions($variant, $itemAttributes, $data);
}