feat(variants): add minimum and maximum use dates to variants and update related logic
This commit is contained in:
@@ -29,6 +29,15 @@ class TicketGenerationException extends RuntimeException
|
||||
return new self("El producto {$catalogItem->id} alcanzó su fecha máxima de uso.");
|
||||
}
|
||||
|
||||
public static function variantNotFound(
|
||||
CatalogItem $catalogItem,
|
||||
int $variantId,
|
||||
): self {
|
||||
return new self(
|
||||
"La variante {$variantId} no pertenece al producto {$catalogItem->id}.",
|
||||
);
|
||||
}
|
||||
|
||||
public static function purchaseWithoutUser(Purchase $purchase): self
|
||||
{
|
||||
return new self("La compra {$purchase->id} no tiene un usuario asociado.");
|
||||
|
||||
@@ -4,6 +4,7 @@ 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\Ticket\Exceptions\TicketGenerationException;
|
||||
use App\Domains\Ticket\Models\Ticket;
|
||||
use Carbon\CarbonInterface;
|
||||
@@ -29,38 +30,60 @@ class TicketGeneratorService
|
||||
$now = now();
|
||||
|
||||
return DB::transaction(function () use ($catalogItem, $user, $quantity, $sourceVariantId, $now): Collection {
|
||||
$catalogItems = $this->resolveCatalogItems($catalogItem, $quantity, $now);
|
||||
$targets = $this->resolveTargets(
|
||||
$catalogItem,
|
||||
$quantity,
|
||||
$sourceVariantId,
|
||||
$now,
|
||||
);
|
||||
|
||||
return $catalogItems->map(fn (CatalogItem $item): Ticket => Ticket::query()->create([
|
||||
'tenant_code' => $item->tenant_code,
|
||||
'ticket' => (string) Str::uuid(),
|
||||
'name' => $item->nombre,
|
||||
'description' => (string) ($item->descripcion ?? ''),
|
||||
'source_catalog_item_id' => $catalogItem->getKey(),
|
||||
'source_variant_id' => $sourceVariantId,
|
||||
'starts_at' => $item->minimum_use_date,
|
||||
'expires_at' => $item->maximum_use_date,
|
||||
'used_at' => null,
|
||||
'user_id' => $user->getKey(),
|
||||
]));
|
||||
return $targets->map(function (array $target) use (
|
||||
$catalogItem,
|
||||
$sourceVariantId,
|
||||
$user,
|
||||
): Ticket {
|
||||
$item = $target['catalog_item'];
|
||||
$selectedItem = $target['variant'] ?? $item;
|
||||
|
||||
return Ticket::query()->create([
|
||||
'tenant_code' => $item->tenant_code,
|
||||
'ticket' => (string) Str::uuid(),
|
||||
'name' => $item->nombre,
|
||||
'description' => (string) ($item->descripcion ?? ''),
|
||||
'source_catalog_item_id' => $catalogItem->getKey(),
|
||||
'source_variant_id' => $sourceVariantId,
|
||||
'starts_at' => $selectedItem->getMinimumUseDate(),
|
||||
'expires_at' => $selectedItem->getMaximumUseDate(),
|
||||
'used_at' => null,
|
||||
'user_id' => $user->getKey(),
|
||||
]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, CatalogItem>
|
||||
* @return Collection<int, array{catalog_item: CatalogItem, variant: Variant|null}>
|
||||
*/
|
||||
private function resolveCatalogItems(
|
||||
private function resolveTargets(
|
||||
CatalogItem $catalogItem,
|
||||
int $quantity,
|
||||
?int $sourceVariantId,
|
||||
CarbonInterface $now,
|
||||
): Collection {
|
||||
if (! $catalogItem->isBundle()) {
|
||||
$this->validateCatalogItem($catalogItem, $now);
|
||||
$variant = $this->resolveVariant($catalogItem, $sourceVariantId);
|
||||
$this->validateTarget($catalogItem, $variant, $now);
|
||||
|
||||
return Collection::times($quantity, fn (): CatalogItem => $catalogItem);
|
||||
return Collection::times($quantity, fn (): array => [
|
||||
'catalog_item' => $catalogItem,
|
||||
'variant' => $variant,
|
||||
]);
|
||||
}
|
||||
|
||||
$catalogItem->loadMissing('bundleComponents.catalogItem');
|
||||
$catalogItem->loadMissing([
|
||||
'bundleComponents.catalogItem',
|
||||
'bundleComponents.variant.catalogItem',
|
||||
]);
|
||||
|
||||
if ($catalogItem->bundleComponents->isEmpty()) {
|
||||
throw TicketGenerationException::emptyBundle($catalogItem);
|
||||
@@ -69,23 +92,56 @@ class TicketGeneratorService
|
||||
return $catalogItem->bundleComponents
|
||||
->flatMap(function ($component) use ($quantity, $now): Collection {
|
||||
$componentItem = $component->catalogItem;
|
||||
$this->validateCatalogItem($componentItem, $now);
|
||||
$variant = $component->variant;
|
||||
$this->validateTarget($componentItem, $variant, $now);
|
||||
|
||||
return Collection::times(
|
||||
$quantity * $component->quantity,
|
||||
fn (): CatalogItem => $componentItem,
|
||||
fn (): array => [
|
||||
'catalog_item' => $componentItem,
|
||||
'variant' => $variant,
|
||||
],
|
||||
);
|
||||
})
|
||||
->values();
|
||||
}
|
||||
|
||||
private function validateCatalogItem(CatalogItem $catalogItem, CarbonInterface $now): void
|
||||
{
|
||||
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,
|
||||
CarbonInterface $now,
|
||||
): void {
|
||||
if (! $catalogItem->has_tickets) {
|
||||
throw TicketGenerationException::ticketsDisabled($catalogItem);
|
||||
}
|
||||
|
||||
if ($catalogItem->maximum_use_date?->lessThanOrEqualTo($now)) {
|
||||
$selectedItem = $variant ?? $catalogItem;
|
||||
|
||||
if ($selectedItem->getMaximumUseDate()?->lessThanOrEqualTo($now)) {
|
||||
throw TicketGenerationException::maximumUseDateReached($catalogItem);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user