'integer', 'event_date_id' => 'integer', 'inventory_id' => 'integer', ]; } /** @return BelongsTo */ public function catalogItem(): BelongsTo { return $this->belongsTo(CatalogItem::class); } /** @return BelongsTo */ public function eventDate(): BelongsTo { return $this->belongsTo(EventDate::class); } /** @return BelongsToMany */ public function eventDates(): BelongsToMany { return $this->belongsToMany( EventDate::class, 'variant_event_dates', 'variant_id', 'event_date_id', )->orderBy('date')->orderBy('time_start'); } /** @return HasMany */ public function sourceTickets(): HasMany { return $this->hasMany(Ticket::class, 'source_variant_id'); } /** @return BelongsTo */ public function inventory(): BelongsTo { return $this->belongsTo(Inventory::class); } /** @return HasMany */ public function definitions(): HasMany { return $this->hasMany(VariantDefinition::class); } /** @return BelongsToMany */ public function attachments(): BelongsToMany { $relation = $this->belongsToMany( Attachment::class, 'catalog_items_attachments', 'variant_id', 'attachment_id' ) ->withPivot('orden') ->orderByPivot('orden'); if ($this->catalog_item_id !== null) { $relation->withPivotValue('catalog_item_id', $this->catalog_item_id); } return $relation; } public function getPrice(): float { return $this->catalogItem->getPrice(); } public function getName(): string { return $this->catalogItem->nombre; } /** @return Collection> */ public function selectionValues(): Collection { $values = $this->definitions ->groupBy('item_attribute_id') ->mapWithKeys(function (Collection $definitions): array { $itemAttribute = $definitions->first()?->itemAttribute; $attributeCode = $itemAttribute?->attribute?->codigo; if ($attributeCode === null) { return []; } $definitionValues = $definitions->pluck('value')->values(); return [ $attributeCode => $itemAttribute->allow_multi_select ? $definitionValues->all() : $definitionValues->first(), ]; }); $eventDateIds = $this->selectedEventDates() ->pluck('id') ->map(fn ($id): string => (string) $id) ->values(); if ($eventDateIds->count() === 1) { $values->put('event_date', $eventDateIds->first()); } elseif ($eventDateIds->isNotEmpty()) { $values->put('event_date', $eventDateIds->all()); } return $values; } /** @return Collection */ public function selectedEventDates(): Collection { $eventDates = $this->eventDates; if ($eventDates->isEmpty() && $this->event_date_id !== null && $this->eventDate !== null) { return collect([$this->eventDate]); } return $eventDates; } }