- Renamed `desfile_entry_reservations` to `entry_reservations` and `desfile_reservation_batches` to `reservation_batches`. - Updated models, services, and requests to use the new `EntryReservation` model from the `Ticket` domain instead of the deleted `Desfile` model. - Introduced a new `EntryReservationPaymentType` enum to manage payment types. - Modified database migrations to ensure data integrity during the transition. - Updated tests to reflect changes in the database schema and model relationships. - Ensured that existing functionality remains intact while improving code organization and clarity.
88 lines
2.5 KiB
PHP
88 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Ticketing\Ticket\Models;
|
|
|
|
use App\Domains\Commerce\Catalog\Models\CatalogItem;
|
|
use App\Domains\Commerce\Catalog\Models\Inventory;
|
|
use App\Domains\Commerce\Catalog\Models\Variant;
|
|
use App\Domains\Ticketing\Ticket\Enums\EntryReservationPaymentType;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
#[Fillable([
|
|
'catalog_item_id',
|
|
'variant_id',
|
|
'ticket_id',
|
|
'inventory_id',
|
|
'batch_id',
|
|
'fecha_reserva',
|
|
'importe',
|
|
'tipo_pago',
|
|
])]
|
|
class EntryReservation extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'entry_reservations';
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'catalog_item_id' => 'integer',
|
|
'variant_id' => 'integer',
|
|
'fecha_reserva' => 'datetime',
|
|
'importe' => 'decimal:2',
|
|
'tipo_pago' => EntryReservationPaymentType::class,
|
|
];
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function (self $reservation): void {
|
|
if ($reservation->variant_id === null) {
|
|
return;
|
|
}
|
|
|
|
$variant = Variant::withTrashed()->findOrFail($reservation->variant_id);
|
|
// Existing variant-based callers can keep creating reservations as before.
|
|
$reservation->catalog_item_id ??= $variant->catalog_item_id;
|
|
if ($reservation->catalog_item_id !== $variant->catalog_item_id) {
|
|
throw ValidationException::withMessages([
|
|
'variant_id' => 'La variante no pertenece al ítem del catálogo seleccionado.',
|
|
]);
|
|
}
|
|
});
|
|
}
|
|
|
|
/** @return BelongsTo<CatalogItem, $this> */
|
|
public function catalogItem(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CatalogItem::class)->withTrashed();
|
|
}
|
|
|
|
/** @return BelongsTo<ReservationBatch, $this> */
|
|
public function batch(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ReservationBatch::class, 'batch_id');
|
|
}
|
|
|
|
/** @return BelongsTo<Variant, $this> */
|
|
public function variant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Variant::class)->withTrashed();
|
|
}
|
|
|
|
public function ticket(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Ticket::class);
|
|
}
|
|
|
|
public function inventory(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Inventory::class);
|
|
}
|
|
}
|