- Added `ticket_generation_policy` column to `catalog_items` table to manage ticket generation strategies. - Created `ticket_validity_times` table to allow multiple validity times per ticket. - Introduced `validity_time_id` column in `event_dates` table to associate event dates with validity times. - Established `ticket_validity_groups` and `ticket_validity_group_times` tables to group validity times for tickets. - Updated seeder to set default ticket generation policy for specific tenant. - Enhanced tests to cover new functionality, including ticket generation based on event dates and validity times. - Refactored ticket validity checks to accommodate multiple validity times in a group.
201 lines
5.5 KiB
PHP
201 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Ticket\Models;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use App\Domains\Catalog\Models\Variant;
|
|
use App\Domains\Purchase\Models\Purchase;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use Carbon\CarbonInterface;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Collection;
|
|
|
|
#[Fillable([
|
|
'tenant_code',
|
|
'ticket',
|
|
'name',
|
|
'description',
|
|
'source_purchase_id',
|
|
'source_catalog_item_id',
|
|
'source_variant_id',
|
|
'used_at',
|
|
'scanner_user_id',
|
|
'user_id',
|
|
])]
|
|
class Ticket extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public const STATUS_ACTIVE = 'active';
|
|
|
|
public const STATUS_EXPIRED = 'expired';
|
|
|
|
public const STATUS_USED = 'used';
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $appends = [
|
|
'is_valid',
|
|
'is_expired',
|
|
'is_used',
|
|
'status',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'source_catalog_item_id' => 'integer',
|
|
'source_variant_id' => 'integer',
|
|
'source_purchase_id' => 'integer',
|
|
'used_at' => 'datetime',
|
|
'scanner_user_id' => 'integer',
|
|
'user_id' => 'integer',
|
|
];
|
|
}
|
|
|
|
/** @return BelongsTo<Tenant, $this> */
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
|
|
}
|
|
|
|
/** @return BelongsTo<User, $this> */
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
/** @return BelongsTo<User, $this> */
|
|
public function scannerUser(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'scanner_user_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Purchase, $this> */
|
|
public function sourcePurchase(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Purchase::class, 'source_purchase_id');
|
|
}
|
|
|
|
/** @return BelongsTo<CatalogItem, $this> */
|
|
public function sourceCatalogItem(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CatalogItem::class, 'source_catalog_item_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Variant, $this> */
|
|
public function sourceVariant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Variant::class, 'source_variant_id');
|
|
}
|
|
|
|
/** @return HasMany<TicketValidityGroup, $this> */
|
|
public function validityGroups(): HasMany
|
|
{
|
|
return $this->hasMany(TicketValidityGroup::class);
|
|
}
|
|
|
|
public function isValid(): bool
|
|
{
|
|
if ($this->used_at !== null) {
|
|
return false;
|
|
}
|
|
|
|
$validityGroups = $this->resolvedValidityGroups();
|
|
|
|
return $validityGroups->isEmpty()
|
|
|| $validityGroups->contains(
|
|
fn (TicketValidityGroup $group): bool => $group->isValid()
|
|
);
|
|
}
|
|
|
|
public function getIsValidAttribute(): bool
|
|
{
|
|
return $this->isValid();
|
|
}
|
|
|
|
public function getIsExpiredAttribute(): bool
|
|
{
|
|
$validityGroups = $this->resolvedValidityGroups();
|
|
|
|
return $this->used_at === null
|
|
&& $validityGroups->isNotEmpty()
|
|
&& $validityGroups->every(
|
|
fn (TicketValidityGroup $group): bool => $group->isExpired()
|
|
);
|
|
}
|
|
|
|
public function getIsUsedAttribute(): bool
|
|
{
|
|
return $this->used_at !== null;
|
|
}
|
|
|
|
public function getStatusAttribute(): string
|
|
{
|
|
if ($this->is_used) {
|
|
return self::STATUS_USED;
|
|
}
|
|
|
|
if ($this->is_expired) {
|
|
return self::STATUS_EXPIRED;
|
|
}
|
|
|
|
return self::STATUS_ACTIVE;
|
|
}
|
|
|
|
public function getEffectiveStartsAt(): ?CarbonInterface
|
|
{
|
|
return $this->resolvedValidityGroups()
|
|
->map(fn (TicketValidityGroup $group): ?CarbonInterface => $group->effectiveStartsAt())
|
|
->filter()
|
|
->sortBy(fn (CarbonInterface $startsAt): int => $startsAt->getTimestamp())
|
|
->first();
|
|
}
|
|
|
|
public function getEffectiveExpiresAt(): ?CarbonInterface
|
|
{
|
|
return $this->resolvedValidityGroups()
|
|
->map(fn (TicketValidityGroup $group): ?CarbonInterface => $group->effectiveExpiresAt())
|
|
->filter()
|
|
->sortByDesc(fn (CarbonInterface $expiresAt): int => $expiresAt->getTimestamp())
|
|
->first();
|
|
}
|
|
|
|
/** @return Collection<int, ValidityTime> */
|
|
public function allValidityTimes(): Collection
|
|
{
|
|
return $this->resolvedValidityGroups()
|
|
->flatMap(fn (TicketValidityGroup $group): EloquentCollection => $group->resolvedValidityTimes())
|
|
->unique(
|
|
fn (ValidityTime $validityTime): int => $validityTime->getKey()
|
|
?? spl_object_id($validityTime)
|
|
)
|
|
->values();
|
|
}
|
|
|
|
/** @return EloquentCollection<int, TicketValidityGroup> */
|
|
public function resolvedValidityGroups(): EloquentCollection
|
|
{
|
|
if ($this->relationLoaded('validityGroups')) {
|
|
return $this->getRelation('validityGroups');
|
|
}
|
|
|
|
if (! $this->exists) {
|
|
return new EloquentCollection;
|
|
}
|
|
|
|
$validityGroups = $this->validityGroups()
|
|
->with('validityTimes')
|
|
->get();
|
|
$this->setRelation('validityGroups', $validityGroups);
|
|
|
|
return $validityGroups;
|
|
}
|
|
}
|