feat(admin-stock): apply idempotent availability deltas
This commit is contained in:
@@ -28,6 +28,7 @@ class AccommodationController extends Controller
|
||||
$this->accommodationService->upsertMany(
|
||||
$tenant,
|
||||
$request->validated('variants'),
|
||||
$request->validated('stock_adjustment_id'),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ class EntryController extends Controller
|
||||
$entries = $this->entryService->upsertMany(
|
||||
$tenant,
|
||||
$request->validated('entries'),
|
||||
$request->validated('stock_adjustment_id'),
|
||||
);
|
||||
|
||||
return EntryResource::collection($entries)
|
||||
|
||||
@@ -29,6 +29,7 @@ class FoodController extends Controller
|
||||
$this->foodService->upsertMany(
|
||||
$tenant,
|
||||
$request->validated('variants'),
|
||||
$request->validated('stock_adjustment_id'),
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -39,6 +40,7 @@ class FoodController extends Controller
|
||||
$this->foodService->updateHistoricalStock(
|
||||
$request->user()->tenant()->firstOrFail(),
|
||||
$request->validated('variants'),
|
||||
$request->validated('stock_adjustment_id'),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ class MerchandiseController extends Controller
|
||||
$items = $this->merchandiseService->upsertMany(
|
||||
$tenant,
|
||||
$request->validated('items'),
|
||||
$request->validated('stock_adjustment_id'),
|
||||
);
|
||||
|
||||
return MerchandiseResource::collection($items)
|
||||
|
||||
@@ -16,9 +16,11 @@ class UpdateHistoricalFoodStockRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'variants' => ['required', 'array', 'min:1', 'max:500'],
|
||||
'variants.*' => ['required', 'array:id,stock'],
|
||||
'stock_adjustment_id' => ['nullable', 'uuid'],
|
||||
'variants.*' => ['required', 'array:id,stock,stock_difference'],
|
||||
'variants.*.id' => ['required', 'integer', 'distinct'],
|
||||
'variants.*.stock' => ['required', 'integer', 'min:0'],
|
||||
'variants.*.stock_difference' => ['nullable', 'integer'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@ class UpsertAccommodationVariantsRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'variants' => ['required', 'array', 'min:1', 'max:500'],
|
||||
'variants.*' => ['required', 'array:id,title,description,stock,price'],
|
||||
'stock_adjustment_id' => ['nullable', 'uuid'],
|
||||
'variants.*' => ['required', 'array:id,title,description,stock,stock_difference,price'],
|
||||
'variants.*.id' => ['sometimes', 'nullable', 'integer', 'distinct'],
|
||||
'variants.*.title' => ['required', 'string', 'max:255'],
|
||||
'variants.*.description' => ['sometimes', 'nullable', 'string'],
|
||||
'variants.*.stock' => ['required', 'integer', 'min:0'],
|
||||
'variants.*.stock_difference' => ['nullable', 'integer'],
|
||||
'variants.*.price' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@ class UpsertEntriesRequest extends FormRequest
|
||||
|
||||
return [
|
||||
'entries' => ['required', 'array', 'min:1', 'max:100'],
|
||||
'entries.*' => ['required', 'array:id,title,description,event_date_ids,stock,price'],
|
||||
'stock_adjustment_id' => ['nullable', 'uuid'],
|
||||
'entries.*' => ['required', 'array:id,title,description,event_date_ids,stock,stock_difference,price'],
|
||||
'entries.*.id' => [
|
||||
'sometimes',
|
||||
'nullable',
|
||||
@@ -47,6 +48,7 @@ class UpsertEntriesRequest extends FormRequest
|
||||
),
|
||||
],
|
||||
'entries.*.stock' => ['required', 'integer', 'min:0'],
|
||||
'entries.*.stock_difference' => ['nullable', 'integer'],
|
||||
'entries.*.price' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@ class UpsertFoodVariantsRequest extends FormRequest
|
||||
|
||||
return [
|
||||
'variants' => ['required', 'array', 'min:1', 'max:500'],
|
||||
'variants.*' => ['required', 'array:id,event_date_id,schedule,service,description,stock,price'],
|
||||
'stock_adjustment_id' => ['nullable', 'uuid'],
|
||||
'variants.*' => ['required', 'array:id,event_date_id,schedule,service,description,stock,stock_difference,price'],
|
||||
'variants.*.id' => ['sometimes', 'nullable', 'integer', 'distinct'],
|
||||
'variants.*.event_date_id' => [
|
||||
'required',
|
||||
@@ -33,6 +34,7 @@ class UpsertFoodVariantsRequest extends FormRequest
|
||||
'variants.*.service' => ['required', 'string', 'max:255'],
|
||||
'variants.*.description' => ['sometimes', 'nullable', 'string'],
|
||||
'variants.*.stock' => ['required', 'integer', 'min:0'],
|
||||
'variants.*.stock_difference' => ['nullable', 'integer'],
|
||||
'variants.*.price' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -39,11 +39,13 @@ class UpsertMerchandiseRequest extends FormRequest
|
||||
'items.*.description' => ['sometimes', 'nullable', 'string'],
|
||||
'items.*.max_units_per_user' => ['required', 'integer', 'min:1'],
|
||||
'items.*.variants' => ['required', 'array', 'min:1', 'max:500'],
|
||||
'items.*.variants.*' => ['required', 'array:id,color,size,stock,price'],
|
||||
'stock_adjustment_id' => ['nullable', 'uuid'],
|
||||
'items.*.variants.*' => ['required', 'array:id,color,size,stock,stock_difference,price'],
|
||||
'items.*.variants.*.id' => ['sometimes', 'nullable', 'integer', 'distinct'],
|
||||
'items.*.variants.*.color' => ['required', 'string', 'max:255'],
|
||||
'items.*.variants.*.size' => ['required', 'string', 'max:255'],
|
||||
'items.*.variants.*.stock' => ['required', 'integer', 'min:0'],
|
||||
'items.*.variants.*.stock_difference' => ['nullable', 'integer'],
|
||||
'items.*.variants.*.price' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class AccommodationResource extends JsonResource
|
||||
'title' => $options->get($value)?->label ?? $value,
|
||||
'value' => $value,
|
||||
'description' => $variant->descripcion,
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'stock' => $variant->inventory->availableStock(),
|
||||
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
||||
];
|
||||
})->values(),
|
||||
|
||||
@@ -33,7 +33,7 @@ class EntryResource extends JsonResource
|
||||
'title' => $this->nombre,
|
||||
'description' => $this->descripcion,
|
||||
'event_date_ids' => $variant->selectedEventDates()->pluck('id')->values(),
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'stock' => $variant->inventory->availableStock(),
|
||||
'price' => $this->precio,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ class FoodResource extends JsonResource
|
||||
'schedule' => $values->get('horario'),
|
||||
'service' => $values->get('servicio'),
|
||||
'description' => $variant->descripcion,
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'stock' => $variant->inventory->availableStock(),
|
||||
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class MerchandiseResource extends JsonResource
|
||||
'color_value' => $colorValue,
|
||||
'size' => $sizeOptions->get($sizeValue)?->label ?? $sizeValue,
|
||||
'size_value' => $sizeValue,
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'stock' => $variant->inventory->availableStock(),
|
||||
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
||||
];
|
||||
})->values(),
|
||||
|
||||
@@ -39,9 +39,9 @@ class AccommodationService
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $variants
|
||||
*/
|
||||
public function upsertMany(Tenant $tenant, array $variants): CatalogItem
|
||||
public function upsertMany(Tenant $tenant, array $variants, ?string $stockAdjustmentId = null): CatalogItem
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $variants): CatalogItem {
|
||||
return DB::transaction(function () use ($tenant, $variants, $stockAdjustmentId): CatalogItem {
|
||||
$attribute = $this->attribute($tenant);
|
||||
$accommodation = $this->accommodation($tenant, $variants);
|
||||
$itemAttribute = $accommodation->itemAttributes()->firstOrCreate(
|
||||
@@ -70,7 +70,7 @@ class AccommodationService
|
||||
if ($variant === null) {
|
||||
$this->createVariant($attribute, $accommodation, $itemAttribute, $data);
|
||||
} else {
|
||||
$this->updateVariant($attribute, $variant, $itemAttribute, $data, $index);
|
||||
$this->updateVariant($attribute, $variant, $itemAttribute, $data, $index, $stockAdjustmentId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,6 +212,7 @@ class AccommodationService
|
||||
$this->createOption($attribute, $data['value'], $data['title']);
|
||||
|
||||
$inventory = Inventory::query()->create(['real_stock' => $data['stock']]);
|
||||
$inventory->recordInitialization();
|
||||
$variant = $accommodation->variants()->create([
|
||||
'inventory_id' => $inventory->id,
|
||||
'descripcion' => $data['description'],
|
||||
@@ -230,20 +231,13 @@ class AccommodationService
|
||||
ItemAttribute $itemAttribute,
|
||||
array $data,
|
||||
int $index,
|
||||
?string $stockAdjustmentId,
|
||||
): void {
|
||||
$inventory = Inventory::query()
|
||||
->whereKey($variant->inventory_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
if ($data['stock'] < $inventory->reserved_stock) {
|
||||
throw ValidationException::withMessages([
|
||||
"variants.{$index}.stock" => [
|
||||
'El stock no puede ser menor que la cantidad actualmente reservada.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$definition = $variant->definitions
|
||||
->firstWhere('item_attribute_id', $itemAttribute->id);
|
||||
$option = $definition === null
|
||||
@@ -263,7 +257,8 @@ class AccommodationService
|
||||
'descripcion' => $data['description'],
|
||||
'precio' => $data['price'],
|
||||
]);
|
||||
$inventory->update(['real_stock' => $data['stock']]);
|
||||
$stockDifference = (int) ($data['stock_difference'] ?? ($data['stock'] - $inventory->availableStock()));
|
||||
$inventory->adjustAvailableStock($stockDifference, idempotencyKey: $stockAdjustmentId);
|
||||
$variant->definitions()->updateOrCreate(
|
||||
['item_attribute_id' => $itemAttribute->id],
|
||||
['value' => $data['value']],
|
||||
|
||||
@@ -43,18 +43,18 @@ class EntryService
|
||||
* @param array<int, array<string, mixed>> $entries
|
||||
* @return Collection<int, CatalogItem>
|
||||
*/
|
||||
public function upsertMany(Tenant $tenant, array $entries): Collection
|
||||
public function upsertMany(Tenant $tenant, array $entries, ?string $stockAdjustmentId = null): Collection
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $entries): Collection {
|
||||
return DB::transaction(function () use ($tenant, $entries, $stockAdjustmentId): Collection {
|
||||
$reservedSlugs = [];
|
||||
$category = Category::query()->firstOrCreate([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'nombre' => 'Entradas',
|
||||
]);
|
||||
|
||||
return collect($entries)->map(function (array $entry, int $index) use ($tenant, $category, &$reservedSlugs): CatalogItem {
|
||||
return collect($entries)->map(function (array $entry, int $index) use ($tenant, $category, $stockAdjustmentId, &$reservedSlugs): CatalogItem {
|
||||
if (isset($entry['id'])) {
|
||||
return $this->update($tenant, $category, $entry, $index);
|
||||
return $this->update($tenant, $category, $entry, $index, $stockAdjustmentId);
|
||||
}
|
||||
|
||||
$slug = $this->uniqueSlug($tenant, $entry['title'], $reservedSlugs);
|
||||
@@ -92,7 +92,7 @@ class EntryService
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $entry */
|
||||
private function update(Tenant $tenant, Category $category, array $entry, int $index): CatalogItem
|
||||
private function update(Tenant $tenant, Category $category, array $entry, int $index, ?string $stockAdjustmentId): CatalogItem
|
||||
{
|
||||
$catalogItem = CatalogItem::query()
|
||||
->whereKey($entry['id'])
|
||||
@@ -121,14 +121,6 @@ class EntryService
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
if ((int) $entry['stock'] < $inventory->reserved_stock) {
|
||||
throw ValidationException::withMessages([
|
||||
"entries.{$index}.stock" => [
|
||||
'El stock no puede ser menor que la cantidad actualmente reservada.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$eventDateIds = collect($entry['event_date_ids'])
|
||||
->map(fn ($id): int => (int) $id)
|
||||
->unique()
|
||||
@@ -149,7 +141,8 @@ class EntryService
|
||||
$catalogItem->itemAttributes()
|
||||
->whereHas('attribute', fn ($query) => $query->where('codigo', 'event_date'))
|
||||
->update(['allow_multi_select' => true]);
|
||||
$inventory->update(['real_stock' => $entry['stock']]);
|
||||
$stockDifference = (int) ($entry['stock_difference'] ?? ((int) $entry['stock'] - $inventory->availableStock()));
|
||||
$inventory->adjustAvailableStock($stockDifference, idempotencyKey: $stockAdjustmentId);
|
||||
|
||||
return $catalogItem->load([
|
||||
'variants.inventory',
|
||||
|
||||
@@ -11,9 +11,9 @@ use App\Domains\Commerce\Catalog\Models\Inventory;
|
||||
use App\Domains\Commerce\Catalog\Models\ItemAttribute;
|
||||
use App\Domains\Commerce\Catalog\Models\Variant;
|
||||
use App\Domains\Commerce\Catalog\Services\CatalogService;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Event\Enums\EventDateStatus;
|
||||
use App\Domains\Ticketing\Event\Models\EventDate;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
@@ -50,9 +50,9 @@ class FoodService
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $variants
|
||||
*/
|
||||
public function upsertMany(Tenant $tenant, array $variants): CatalogItem
|
||||
public function upsertMany(Tenant $tenant, array $variants, ?string $stockAdjustmentId = null): CatalogItem
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $variants): CatalogItem {
|
||||
return DB::transaction(function () use ($tenant, $variants, $stockAdjustmentId): CatalogItem {
|
||||
$attributes = $this->attributes($tenant);
|
||||
$food = $this->food($tenant, $variants);
|
||||
$itemAttributes = $this->itemAttributes($food, $attributes);
|
||||
@@ -85,7 +85,7 @@ class FoodService
|
||||
if ($variant === null) {
|
||||
$this->createVariant($food, $itemAttributes, $data);
|
||||
} else {
|
||||
$this->updateVariant($variant, $itemAttributes, $data, $index);
|
||||
$this->updateVariant($variant, $itemAttributes, $data, $index, $stockAdjustmentId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,9 +115,9 @@ class FoodService
|
||||
/**
|
||||
* @param array<int, array{id: int, stock: int}> $variants
|
||||
*/
|
||||
public function updateHistoricalStock(Tenant $tenant, array $variants): CatalogItem
|
||||
public function updateHistoricalStock(Tenant $tenant, array $variants, ?string $stockAdjustmentId = null): CatalogItem
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $variants): CatalogItem {
|
||||
return DB::transaction(function () use ($tenant, $variants, $stockAdjustmentId): CatalogItem {
|
||||
$food = CatalogItem::query()
|
||||
->forTenantCatalog($tenant)
|
||||
->where('slug', 'comida')
|
||||
@@ -142,17 +142,9 @@ class FoodService
|
||||
]);
|
||||
}
|
||||
|
||||
$stock = (int) $data['stock'];
|
||||
$inventory = $this->inventoryForHistoricalStockUpdate($variant);
|
||||
if ($stock < $inventory->reserved_stock) {
|
||||
throw ValidationException::withMessages([
|
||||
"variants.{$index}.stock" => [
|
||||
'El stock no puede ser menor que la cantidad actualmente reservada.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$inventory->update(['real_stock' => $stock]);
|
||||
$stockDifference = (int) ($data['stock_difference'] ?? ((int) $data['stock'] - $inventory->availableStock()));
|
||||
$inventory->adjustAvailableStock($stockDifference, idempotencyKey: $stockAdjustmentId);
|
||||
}
|
||||
|
||||
return $this->current($tenant) ?? $food;
|
||||
@@ -181,6 +173,7 @@ class FoodService
|
||||
'reserved_stock' => 0,
|
||||
'real_stock' => $inventory->real_stock,
|
||||
]);
|
||||
$historicalInventory->recordTransferInitialization();
|
||||
$variant->update(['inventory_id' => $historicalInventory->getKey()]);
|
||||
|
||||
return $historicalInventory;
|
||||
@@ -384,6 +377,7 @@ class FoodService
|
||||
private function createVariant(CatalogItem $food, Collection $itemAttributes, array $data): void
|
||||
{
|
||||
$inventory = Inventory::query()->create(['real_stock' => $data['stock']]);
|
||||
$inventory->recordInitialization();
|
||||
$variant = $food->variants()->create([
|
||||
'event_date_id' => $data['event_date_id'],
|
||||
'inventory_id' => $inventory->id,
|
||||
@@ -400,27 +394,21 @@ class FoodService
|
||||
Collection $itemAttributes,
|
||||
array $data,
|
||||
int $index,
|
||||
?string $stockAdjustmentId,
|
||||
): void {
|
||||
$inventory = Inventory::query()
|
||||
->whereKey($variant->inventory_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
if ($data['stock'] < $inventory->reserved_stock) {
|
||||
throw ValidationException::withMessages([
|
||||
"variants.{$index}.stock" => [
|
||||
'El stock no puede ser menor que la cantidad actualmente reservada.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$variant->update([
|
||||
'event_date_id' => $data['event_date_id'],
|
||||
'descripcion' => $data['description'],
|
||||
'precio' => $data['price'],
|
||||
]);
|
||||
$variant->eventDates()->sync([$data['event_date_id']]);
|
||||
$inventory->update(['real_stock' => $data['stock']]);
|
||||
$stockDifference = (int) ($data['stock_difference'] ?? ($data['stock'] - $inventory->availableStock()));
|
||||
$inventory->adjustAvailableStock($stockDifference, idempotencyKey: $stockAdjustmentId);
|
||||
$this->syncDefinitions($variant, $itemAttributes, $data);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,9 +43,9 @@ class MerchandiseService
|
||||
* @param array<int, array<string, mixed>> $items
|
||||
* @return Collection<int, CatalogItem>
|
||||
*/
|
||||
public function upsertMany(Tenant $tenant, array $items): Collection
|
||||
public function upsertMany(Tenant $tenant, array $items, ?string $stockAdjustmentId = null): Collection
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $items): Collection {
|
||||
return DB::transaction(function () use ($tenant, $items, $stockAdjustmentId): Collection {
|
||||
$attributes = $this->attributes($tenant);
|
||||
$category = Category::query()->firstOrCreate([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
@@ -57,6 +57,7 @@ class MerchandiseService
|
||||
$tenant,
|
||||
$attributes,
|
||||
$category,
|
||||
$stockAdjustmentId,
|
||||
&$reservedSlugs,
|
||||
): CatalogItem {
|
||||
$item = isset($data['id'])
|
||||
@@ -101,7 +102,7 @@ class MerchandiseService
|
||||
if ($variant === null) {
|
||||
$this->createVariant($item, $itemAttributes, $variantData);
|
||||
} else {
|
||||
$this->updateVariant($variant, $itemAttributes, $variantData, $index, $variantIndex);
|
||||
$this->updateVariant($variant, $itemAttributes, $variantData, $index, $variantIndex, $stockAdjustmentId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -342,6 +343,7 @@ 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'],
|
||||
@@ -359,22 +361,16 @@ 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->update(['real_stock' => $data['stock']]);
|
||||
$stockDifference = (int) ($data['stock_difference'] ?? ($data['stock'] - $inventory->availableStock()));
|
||||
$inventory->adjustAvailableStock($stockDifference, idempotencyKey: $stockAdjustmentId);
|
||||
$this->syncDefinitions($variant, $itemAttributes, $data);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user