- Added ValidityTime model and migration to manage ticket validity periods. - Updated TicketGeneratorService to resolve and assign validity times to tickets. - Refactored ticket generation logic to remove legacy date fields and use validity time. - Introduced timezone support for tenants to handle service dates correctly. - Updated migrations to remove deprecated columns and add foreign keys for validity times. - Modified seeders and tests to accommodate new validity time structure. - Enhanced tests to validate ticket generation and validity time behavior.
144 lines
3.7 KiB
PHP
144 lines
3.7 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\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
#[Fillable([
|
|
'tenant_code',
|
|
'ticket',
|
|
'name',
|
|
'description',
|
|
'source_purchase_id',
|
|
'source_catalog_item_id',
|
|
'source_variant_id',
|
|
'validity_time_id',
|
|
'service_date',
|
|
'used_at',
|
|
'scanner_user_id',
|
|
'user_id',
|
|
])]
|
|
class Ticket extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $appends = [
|
|
'is_valid',
|
|
'is_expired',
|
|
'is_used',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'source_catalog_item_id' => 'integer',
|
|
'source_variant_id' => 'integer',
|
|
'source_purchase_id' => 'integer',
|
|
'validity_time_id' => 'integer',
|
|
'service_date' => 'date',
|
|
'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 BelongsTo<ValidityTime, $this> */
|
|
public function validityTime(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ValidityTime::class);
|
|
}
|
|
|
|
public function isValid(): bool
|
|
{
|
|
$now = now();
|
|
$startsAt = $this->getEffectiveStartsAt();
|
|
$expiresAt = $this->getEffectiveExpiresAt();
|
|
|
|
return $this->used_at === null
|
|
&& ($startsAt === null || $startsAt->lessThanOrEqualTo($now))
|
|
&& ($expiresAt === null || $expiresAt->greaterThan($now));
|
|
}
|
|
|
|
public function getIsValidAttribute(): bool
|
|
{
|
|
return $this->isValid();
|
|
}
|
|
|
|
public function getIsExpiredAttribute(): bool
|
|
{
|
|
$expiresAt = $this->getEffectiveExpiresAt();
|
|
|
|
return $this->used_at === null
|
|
&& $expiresAt !== null
|
|
&& $expiresAt->lessThanOrEqualTo(now());
|
|
}
|
|
|
|
public function getIsUsedAttribute(): bool
|
|
{
|
|
return $this->used_at !== null;
|
|
}
|
|
|
|
public function getEffectiveStartsAt(): ?CarbonInterface
|
|
{
|
|
return $this->validityTime?->startsAt(
|
|
$this->service_date,
|
|
$this->tenant?->timezone ?? config('app.timezone'),
|
|
);
|
|
}
|
|
|
|
public function getEffectiveExpiresAt(): ?CarbonInterface
|
|
{
|
|
return $this->validityTime?->expiresAt(
|
|
$this->service_date,
|
|
$this->tenant?->timezone ?? config('app.timezone'),
|
|
);
|
|
}
|
|
}
|