feat(food): add endpoint to update historical stock and separate current and historical food variants
This commit is contained in:
@@ -11,6 +11,8 @@ use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Catalog\Models\ItemAttribute;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Event\Enums\EventDateStatus;
|
||||
use App\Domains\Event\Models\EventDate;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -36,8 +38,10 @@ class FoodService
|
||||
->with([
|
||||
'variants.catalogItem',
|
||||
'variants.inventory',
|
||||
'variants.eventDate',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate.rescheduledTo',
|
||||
'variants.eventDate.changeHistory.destinationEventDate',
|
||||
'variants.eventDates.rescheduledTo',
|
||||
'variants.eventDates.changeHistory.destinationEventDate',
|
||||
'variants.definitions.itemAttribute.attribute',
|
||||
])
|
||||
->first();
|
||||
@@ -55,11 +59,16 @@ class FoodService
|
||||
|
||||
$food->variants()->whereNull('precio')->update(['precio' => $food->precio]);
|
||||
$existingVariants = $food->variants()
|
||||
->whereNull('sales_disabled_at')
|
||||
->whereNull('replaced_by_variant_id')
|
||||
->with(['inventory', 'eventDate', 'eventDates', 'definitions.itemAttribute.attribute'])
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
->get()
|
||||
->filter(fn (Variant $variant): bool => $this->hasCurrentDate($variant))
|
||||
->values();
|
||||
$resolvedVariants = $this->resolveVariants($variants, $attributes);
|
||||
|
||||
$this->validateCurrentEventDates($tenant, $resolvedVariants);
|
||||
$this->validateCombinations($resolvedVariants, $existingVariants);
|
||||
|
||||
foreach ($resolvedVariants as $index => $data) {
|
||||
@@ -80,7 +89,13 @@ class FoodService
|
||||
}
|
||||
}
|
||||
|
||||
$minimumPrice = $food->variants()->min('precio');
|
||||
$minimumPrice = $food->variants()
|
||||
->whereNull('sales_disabled_at')
|
||||
->whereNull('replaced_by_variant_id')
|
||||
->with(['eventDate', 'eventDates'])
|
||||
->get()
|
||||
->filter(fn (Variant $variant): bool => $this->hasCurrentDate($variant))
|
||||
->min('precio');
|
||||
if ($minimumPrice !== null) {
|
||||
$food->update(['precio' => $minimumPrice]);
|
||||
}
|
||||
@@ -88,22 +103,81 @@ class FoodService
|
||||
return $food->fresh()->load([
|
||||
'variants.catalogItem',
|
||||
'variants.inventory',
|
||||
'variants.eventDate',
|
||||
'variants.eventDates',
|
||||
'variants.eventDate.rescheduledTo',
|
||||
'variants.eventDate.changeHistory.destinationEventDate',
|
||||
'variants.eventDates.rescheduledTo',
|
||||
'variants.eventDates.changeHistory.destinationEventDate',
|
||||
'variants.definitions.itemAttribute.attribute',
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{id: int, stock: int}> $variants
|
||||
*/
|
||||
public function updateHistoricalStock(Tenant $tenant, array $variants): CatalogItem
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $variants): CatalogItem {
|
||||
$food = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('slug', 'comida')
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
$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()
|
||||
->filter(fn (Variant $variant): bool => $this->hasHistoricalDate($variant))
|
||||
->keyBy('id');
|
||||
|
||||
foreach ($variants as $index => $data) {
|
||||
$variant = $historicalVariants->get((int) $data['id']);
|
||||
if ($variant === null) {
|
||||
throw ValidationException::withMessages([
|
||||
"variants.{$index}.id" => [
|
||||
'La variante no pertenece al historial de Comida.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$stock = (int) $data['stock'];
|
||||
$inventory = Inventory::query()
|
||||
->whereKey($variant->inventory_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
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]);
|
||||
}
|
||||
|
||||
return $this->current($tenant) ?? $food;
|
||||
});
|
||||
}
|
||||
|
||||
public function delete(Tenant $tenant, int $foodId): void
|
||||
{
|
||||
$variant = Variant::query()
|
||||
->whereKey($foodId)
|
||||
->whereNull('sales_disabled_at')
|
||||
->whereNull('replaced_by_variant_id')
|
||||
->with(['eventDate', 'eventDates'])
|
||||
->whereHas('catalogItem', fn ($query) => $query
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('slug', 'comida'))
|
||||
->firstOrFail();
|
||||
|
||||
abort_unless($this->hasCurrentDate($variant), 404);
|
||||
|
||||
$this->catalogService->deleteVariant($variant);
|
||||
}
|
||||
|
||||
@@ -225,6 +299,30 @@ class FoodService
|
||||
return $option;
|
||||
}
|
||||
|
||||
/** @param array<int, array<string, mixed>> $variants */
|
||||
private function validateCurrentEventDates(Tenant $tenant, array $variants): void
|
||||
{
|
||||
$eventDates = EventDate::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->whereIn('id', collect($variants)->pluck('event_date_id')->unique())
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
foreach ($variants as $index => $variant) {
|
||||
$eventDate = $eventDates->get($variant['event_date_id']);
|
||||
|
||||
if ($eventDate !== null && $this->isCurrentStatus($eventDate->status)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
"variants.{$index}.event_date_id" => [
|
||||
'La fecha seleccionada ya no está disponible.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $incoming
|
||||
* @param Collection<int, Variant> $existing
|
||||
@@ -329,4 +427,25 @@ class FoodService
|
||||
mb_strtolower(trim($service)),
|
||||
]);
|
||||
}
|
||||
|
||||
private function hasCurrentDate(Variant $variant): bool
|
||||
{
|
||||
$status = $variant->selectedEventDates()->first()?->status;
|
||||
|
||||
return $status === null || $this->isCurrentStatus($status);
|
||||
}
|
||||
|
||||
private function hasHistoricalDate(Variant $variant): bool
|
||||
{
|
||||
return in_array(
|
||||
$variant->selectedEventDates()->first()?->status,
|
||||
[EventDateStatus::Rescheduled, EventDateStatus::Suspended, EventDateStatus::Completed],
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
private function isCurrentStatus(EventDateStatus $status): bool
|
||||
{
|
||||
return in_array($status, [EventDateStatus::Scheduled, EventDateStatus::InProgress], true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user