- Updated the FeaturedGroupService to include 'variants.eventDates' in the items query. - Introduced a BelongsToMany relationship in EventDate for selected variants. - Modified PurchaseItemResource and PurchaseItemSnapshotFactory to handle multiple event dates for variants. - Enhanced StartCheckoutService to load event dates for variants. - Updated TicketGeneratorService to accommodate event dates in ticket generation. - Created migrations to support multi-value event dates and allow multiple variant values per attribute. - Adjusted seeders to reflect new event date handling and added new attributes. - Added tests to ensure correct functionality for multi-date variants and their integration with ticket generation.
258 lines
8.3 KiB
PHP
258 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Ticket\Services;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Catalog\Models\Variant;
|
|
use App\Domains\Event\Models\EventDate;
|
|
use App\Domains\Ticket\Enums\ValidityTimeType;
|
|
use App\Domains\Ticket\Exceptions\TicketGenerationException;
|
|
use App\Domains\Ticket\Models\Ticket;
|
|
use App\Domains\Ticket\Models\ValidityTime;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class TicketGeneratorService
|
|
{
|
|
/**
|
|
* @return Collection<int, Ticket>
|
|
*/
|
|
public function generate(
|
|
CatalogItem $catalogItem,
|
|
User $user,
|
|
int $quantity = 1,
|
|
?int $sourceVariantId = null,
|
|
?int $sourcePurchaseId = null,
|
|
): Collection {
|
|
if ($quantity < 1) {
|
|
throw TicketGenerationException::invalidQuantity();
|
|
}
|
|
|
|
return DB::transaction(function () use ($catalogItem, $user, $quantity, $sourceVariantId, $sourcePurchaseId): Collection {
|
|
$targets = $this->resolveTargets(
|
|
$catalogItem,
|
|
$quantity,
|
|
$sourceVariantId,
|
|
);
|
|
$fixedValidityTimes = [];
|
|
|
|
return $targets->map(function (array $target) use (
|
|
&$fixedValidityTimes,
|
|
$sourcePurchaseId,
|
|
$user,
|
|
): Ticket {
|
|
$item = $target['catalog_item'];
|
|
$variant = $target['variant'];
|
|
$eventDate = $target['event_date'];
|
|
$validityTime = $this->resolveValidityTime($item, $variant);
|
|
$validityTime = $this->materializeEventDateValidityTime(
|
|
$variant,
|
|
$eventDate,
|
|
$validityTime,
|
|
$fixedValidityTimes,
|
|
);
|
|
|
|
return Ticket::query()->create([
|
|
'tenant_code' => $item->tenant_code,
|
|
'ticket' => (string) Str::uuid(),
|
|
'name' => $item->nombre,
|
|
'description' => (string) ($item->descripcion ?? ''),
|
|
'source_purchase_id' => $sourcePurchaseId,
|
|
'source_catalog_item_id' => $item->getKey(),
|
|
'source_variant_id' => $variant?->getKey(),
|
|
'validity_time_id' => $validityTime?->getKey(),
|
|
'used_at' => null,
|
|
'user_id' => $user->getKey(),
|
|
]);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, array{catalog_item: CatalogItem, variant: Variant|null, event_date: EventDate|null}>
|
|
*/
|
|
private function resolveTargets(
|
|
CatalogItem $catalogItem,
|
|
int $quantity,
|
|
?int $sourceVariantId,
|
|
): Collection {
|
|
if (! $catalogItem->isBundle()) {
|
|
$variant = $this->resolveVariant($catalogItem, $sourceVariantId);
|
|
$this->validateTarget($catalogItem, $variant);
|
|
|
|
return $this->targetsForVariant($catalogItem, $variant, $quantity);
|
|
}
|
|
|
|
$catalogItem->loadMissing([
|
|
'bundleComponents.catalogItem',
|
|
'bundleComponents.variant.catalogItem',
|
|
]);
|
|
|
|
if ($catalogItem->bundleComponents->isEmpty()) {
|
|
throw TicketGenerationException::emptyBundle($catalogItem);
|
|
}
|
|
|
|
return $catalogItem->bundleComponents
|
|
->flatMap(function ($component) use ($quantity): Collection {
|
|
$componentItem = $component->catalogItem;
|
|
$variant = $component->variant;
|
|
$this->validateTarget($componentItem, $variant);
|
|
|
|
return $this->targetsForVariant(
|
|
$componentItem,
|
|
$variant,
|
|
$quantity * $component->quantity,
|
|
);
|
|
})
|
|
->values();
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, array{catalog_item: CatalogItem, variant: Variant|null, event_date: EventDate|null}>
|
|
*/
|
|
private function targetsForVariant(
|
|
CatalogItem $catalogItem,
|
|
?Variant $variant,
|
|
int $quantity,
|
|
): Collection {
|
|
if ($variant === null) {
|
|
return Collection::times($quantity, fn (): array => [
|
|
'catalog_item' => $catalogItem,
|
|
'variant' => null,
|
|
'event_date' => null,
|
|
]);
|
|
}
|
|
|
|
$variant->loadMissing(['eventDates', 'eventDate']);
|
|
$selectedEventDates = $variant->selectedEventDates();
|
|
$eventDates = $selectedEventDates->isEmpty()
|
|
? collect([null])
|
|
: $selectedEventDates;
|
|
|
|
return Collection::times($quantity)
|
|
->flatMap(fn () => $eventDates->map(fn (?EventDate $eventDate): array => [
|
|
'catalog_item' => $catalogItem,
|
|
'variant' => $variant,
|
|
'event_date' => $eventDate,
|
|
]))
|
|
->values();
|
|
}
|
|
|
|
private function resolveVariant(
|
|
CatalogItem $catalogItem,
|
|
?int $sourceVariantId,
|
|
): ?Variant {
|
|
if ($sourceVariantId === null) {
|
|
return null;
|
|
}
|
|
|
|
$variant = $catalogItem->variants()
|
|
->whereKey($sourceVariantId)
|
|
->first();
|
|
|
|
if ($variant === null) {
|
|
throw TicketGenerationException::variantNotFound(
|
|
$catalogItem,
|
|
$sourceVariantId,
|
|
);
|
|
}
|
|
|
|
$variant->setRelation('catalogItem', $catalogItem);
|
|
|
|
return $variant;
|
|
}
|
|
|
|
private function validateTarget(
|
|
CatalogItem $catalogItem,
|
|
?Variant $variant,
|
|
): void {
|
|
if (! $catalogItem->has_tickets) {
|
|
throw TicketGenerationException::ticketsDisabled($catalogItem);
|
|
}
|
|
|
|
}
|
|
|
|
private function resolveValidityTime(
|
|
CatalogItem $catalogItem,
|
|
?Variant $variant,
|
|
): ?ValidityTime {
|
|
if ($catalogItem->validity_time_id !== null) {
|
|
return $catalogItem->validityTime;
|
|
}
|
|
|
|
if ($variant === null) {
|
|
return null;
|
|
}
|
|
|
|
$variant->loadMissing('definitions.itemAttribute.attribute.options.validityTime');
|
|
|
|
$validityTimes = $variant->definitions
|
|
->map(function ($definition): ?ValidityTime {
|
|
$option = $definition->itemAttribute?->attribute?->options
|
|
->firstWhere('value', $definition->value);
|
|
|
|
return $option?->validityTime;
|
|
})
|
|
->filter()
|
|
->unique(fn (ValidityTime $validityTime): int => $validityTime->getKey())
|
|
->values();
|
|
|
|
if ($validityTimes->count() > 1) {
|
|
throw TicketGenerationException::ambiguousValidityTime($catalogItem);
|
|
}
|
|
|
|
return $validityTimes->first();
|
|
}
|
|
|
|
/**
|
|
* Convert a recurring time window into the fixed date and time purchased by
|
|
* the customer. Equal tickets generated together share the same snapshot.
|
|
*
|
|
* @param array<string, ValidityTime> $fixedValidityTimes
|
|
*/
|
|
private function materializeEventDateValidityTime(
|
|
?Variant $variant,
|
|
?EventDate $eventDate,
|
|
?ValidityTime $validityTime,
|
|
array &$fixedValidityTimes,
|
|
): ?ValidityTime {
|
|
if (
|
|
$variant === null
|
|
|| $eventDate === null
|
|
) {
|
|
return $validityTime;
|
|
}
|
|
|
|
if ($validityTime !== null && $validityTime->type !== ValidityTimeType::TimeWindow) {
|
|
return $validityTime;
|
|
}
|
|
|
|
$cacheKey = $eventDate->getKey().':'.($validityTime?->getKey() ?? 'event');
|
|
|
|
if (isset($fixedValidityTimes[$cacheKey])) {
|
|
return $fixedValidityTimes[$cacheKey];
|
|
}
|
|
|
|
$startsAt = $validityTime?->startsAt($eventDate->date) ?? $eventDate->startsAt();
|
|
$expiresAt = $validityTime?->expiresAt($eventDate->date) ?? $eventDate->endsAt();
|
|
|
|
if (
|
|
$startsAt !== null
|
|
&& $expiresAt !== null
|
|
&& $expiresAt->lessThanOrEqualTo($startsAt)
|
|
) {
|
|
$expiresAt = $expiresAt->addDay();
|
|
}
|
|
|
|
return $fixedValidityTimes[$cacheKey] = ValidityTime::query()->create([
|
|
'type' => ValidityTimeType::FixedWindow,
|
|
'start_time' => null,
|
|
'end_time' => null,
|
|
'fixed_starts_at' => $startsAt,
|
|
'fixed_expires_at' => $expiresAt,
|
|
]);
|
|
}
|
|
}
|