*/ use BelongsToTenant, HasChannelProvenance, HasFactory, SoftDeletes; protected $attributes = ['is_representation' => false, 'export_approved' => true]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'occupation_kind' => OccupationKind::class, 'starts_on' => 'immutable_date', 'ends_on' => 'immutable_date', 'needs_review' => 'boolean', 'missing_imports' => 'integer', 'is_representation' => 'boolean', 'export_approved' => 'boolean', 'reviewed_at' => 'immutable_datetime', 'ical_properties' => 'encrypted:array', 'ical_raw' => 'encrypted', 'ical_interpretation' => 'encrypted:array', 'leading_priority' => 'integer', ]; } /** * @return BelongsTo */ public function property(): BelongsTo { return $this->belongsTo(Property::class); } /** * @return BelongsTo */ public function channel(): BelongsTo { return $this->belongsTo(Channel::class); } /** @return BelongsTo */ public function originChannel(): BelongsTo { return $this->belongsTo(Channel::class); } /** @return BelongsTo */ public function canonicalBlock(): BelongsTo { return $this->belongsTo(self::class, 'canonical_block_id')->withTrashed(); } /** @return BelongsTo */ public function linkedBooking(): BelongsTo { return $this->belongsTo(Booking::class, 'linked_booking_id'); } /** @return BelongsTo */ public function guest(): BelongsTo { return $this->belongsTo(Guest::class); } public function occupationKind(): OccupationKind { if ($this->source === 'manual') { return OccupationKind::Block; } return $this->occupation_kind ?? ($this->ical_event_type === 'portal_reservation' ? OccupationKind::Reservation : OccupationKind::Unknown); } public function guestName(): ?string { if ($this->is_representation) { return $this->linkedBooking?->guest?->full_name ?? $this->canonicalBlock?->guestName(); } return $this->guest?->full_name ?? ($this->ical_interpretation['fields']['guest_full_name'] ?? $this->ical_interpretation['fields']['guest_first_name'] ?? null); } public function assignedGuest(): ?Guest { return $this->is_representation ? ($this->linkedBooking?->guest ?? $this->canonicalBlock?->assignedGuest()) : $this->guest; } /** @return HasMany */ public function channelRepresentations(): HasMany { return $this->hasMany(self::class, 'canonical_block_id'); } /** @param Builder $query * @return Builder */ #[Scope] protected function reservations(Builder $query): Builder { return $query->whereIn('source', ['channel', 'email'])->where(fn (Builder $query) => $query ->where('occupation_kind', OccupationKind::Reservation) ->orWhere(fn (Builder $query) => $query->whereNull('occupation_kind')->where('ical_event_type', 'portal_reservation'))); } /** @param Builder $query * @return Builder */ #[Scope] protected function explicitBlocks(Builder $query): Builder { return $query->where(fn (Builder $query) => $query->where('source', 'manual')->orWhere('occupation_kind', OccupationKind::Block)); } /** @param Builder $query * @return Builder */ #[Scope] protected function unclear(Builder $query): Builder { return $query->whereIn('source', ['channel', 'email'])->where(fn (Builder $query) => $query ->where('needs_review', true) ->orWhere('occupation_kind', OccupationKind::Unknown) ->orWhere(fn (Builder $query) => $query->whereNull('occupation_kind')->where(fn (Builder $query) => $query->whereNull('ical_event_type')->orWhere('ical_event_type', '!=', 'portal_reservation')))); } /** @param Builder $query * @return Builder */ #[Scope] protected function occupying(Builder $query): Builder { return $query->where('is_representation', false); } /** * @param Builder $query * @return Builder */ #[Scope] protected function overlapping(Builder $query, CarbonInterface $from, CarbonInterface $until): Builder { return $query ->whereDate('starts_on', '<', $until) ->whereDate('ends_on', '>', $from); } }