Merge branch 'refactor/inventory_stock' of https://gitea.quo.ar/tbianchini/shopit-back into refactor/inventory_stock

This commit is contained in:
2026-09-29 08:23:09 -03:00
parent c5aeb19735
commit 96378a6c2e
596 changed files with 239 additions and 7622 deletions

View File

@@ -43,9 +43,9 @@ class MerchandiseService
* @param array<int, array<string, mixed>> $items
* @return Collection<int, CatalogItem>
*/
public function upsertMany(Tenant $tenant, array $items, ?string $stockAdjustmentId = null): Collection
public function upsertMany(Tenant $tenant, array $items): Collection
{
return DB::transaction(function () use ($tenant, $items, $stockAdjustmentId): Collection {
return DB::transaction(function () use ($tenant, $items): Collection {
$attributes = $this->attributes($tenant);
$category = Category::query()->firstOrCreate([
'tenant_code' => $tenant->codigo,
@@ -57,7 +57,6 @@ class MerchandiseService
$tenant,
$attributes,
$category,
$stockAdjustmentId,
&$reservedSlugs,
): CatalogItem {
$item = isset($data['id'])
@@ -102,7 +101,7 @@ class MerchandiseService
if ($variant === null) {
$this->createVariant($item, $itemAttributes, $variantData);
} else {
$this->updateVariant($variant, $itemAttributes, $variantData, $index, $variantIndex, $stockAdjustmentId);
$this->updateVariant($variant, $itemAttributes, $variantData, $index, $variantIndex);
}
}
@@ -245,7 +244,7 @@ class MerchandiseService
...$variant,
'color' => $color->value,
'size' => $size->value,
...(array_key_exists('stock', $variant) ? ['stock' => (int) $variant['stock']] : []),
'stock' => (int) $variant['stock'],
];
})->all();
}
@@ -343,7 +342,6 @@ class MerchandiseService
array $data,
): void {
$inventory = Inventory::query()->create(['real_stock' => $data['stock']]);
$inventory->recordInitialization();
$variant = $item->variants()->create([
'inventory_id' => $inventory->id,
'precio' => $data['price'],
@@ -361,18 +359,22 @@ class MerchandiseService
array $data,
int $itemIndex,
int $variantIndex,
?string $stockAdjustmentId,
): void {
$inventory = Inventory::query()
->whereKey($variant->inventory_id)
->lockForUpdate()
->firstOrFail();
if ($data['stock'] < $inventory->reserved_stock) {
throw ValidationException::withMessages([
"items.{$itemIndex}.variants.{$variantIndex}.stock" => [
'El stock no puede ser menor que la cantidad actualmente reservada.',
],
]);
}
$variant->update(['precio' => $data['price']]);
$inventory->adjustAvailableStock(
(int) $data['stock_difference'],
idempotencyKey: $stockAdjustmentId,
);
$inventory->update(['real_stock' => $data['stock']]);
$this->syncDefinitions($variant, $itemAttributes, $data);
}