website text key it replaces in an offer. */ /** Attributes whose change makes the release and the portal confirmation lapse. */ /** Whether the statements were given for the pending save (then a text change does not withdraw them). */ private bool $statementsApplied = false; private const CONTENT = ['texts', 'body', 'annex_title', 'annex_body', 'cancellation_policy_id', 'annex_ids']; public const TEXTS = [ 'intro' => 'contract.intro', 'terms' => 'contract.terms', 'closing' => 'contract.closing', 'cancellation' => 'stay.cancellation', 'house_rules' => 'stay.house_rules', 'contract_note' => 'offer.contract_note', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return ['texts' => 'array', 'version' => 'integer', 'annex_ids' => 'array', 'released_at' => 'datetime', 'portal_terms_confirmed_at' => 'datetime']; } protected static function booted(): void { static::saved(function (ContractTemplate $template): void { $template->statementsApplied = false; }); static::saving(function (ContractTemplate $template): void { if ($template->exists && $template->isDirty(self::CONTENT) && ! $template->statementsApplied) { if (! $template->isDirty('released_at')) { $template->released_at = null; $template->released_by = null; } if (! $template->isDirty('portal_terms_confirmed_at')) { $template->portal_terms_confirmed_at = null; $template->portal_terms_confirmed_by = null; } } }); } /** * Records the landlord's statements on the (possibly just changed) text: a statement is renewed when it is given for * a changed text, kept when nothing changed and withdrawn when it is not given. */ public function applyStatements(bool $released, bool $portalTerms, string $actor): self { $changed = $this->isDirty(self::CONTENT); $this->statementsApplied = true; if (! $released) { $this->forceFill(['released_at' => null, 'released_by' => null]); } elseif ($changed || ! $this->isReleased()) { $this->forceFill(['released_at' => now(), 'released_by' => $actor]); } if (! $portalTerms) { $this->forceFill(['portal_terms_confirmed_at' => null, 'portal_terms_confirmed_by' => null]); } elseif ($changed || ! $this->confirmsPortalTerms()) { $this->forceFill(['portal_terms_confirmed_at' => now(), 'portal_terms_confirmed_by' => $actor]); } return $this; } public function isReleased(): bool { return $this->released_at !== null; } /** * Whether the landlord confirmed that this version only reproduces the terms guests accept when booking on a portal. */ public function confirmsPortalTerms(): bool { return $this->portal_terms_confirmed_at !== null; } /** * A complete contract document (text with placeholders) instead of text blocks. */ public function hasDocument(): bool { return filled($this->body) && trim(strip_tags((string) $this->body)) !== ''; } /** * @return BelongsTo */ public function cancellationPolicy(): BelongsTo { return $this->belongsTo(CancellationPolicy::class); } /** * The annexes of this template whose condition fits the booking. * * @return Collection */ public function annexesFor(Booking $booking): Collection { return ContractAnnex::query()->whereIn('id', (array) ($this->annex_ids ?? []))->orderBy('name')->get()->filter(fn (ContractAnnex $annex): bool => $annex->appliesTo($booking))->values(); } public function label(): string { return $this->name.' (Version '.$this->version.')'; } /** * Texts by website text key; blank ones fall back to the website texts. * * @return array */ public function overrides(): array { $overrides = []; foreach (self::TEXTS as $field => $key) { $value = $this->texts[$field] ?? null; if (filled($value) && trim(strip_tags((string) $value)) !== '') { $overrides[$key] = (string) $value; } } return $overrides; } public function isUsed(): bool { return Booking::query()->where('offer_terms->template->id', $this->id)->exists(); } public function newVersion(): self { $latest = static::query()->where('name', $this->name)->max('version'); return static::query()->create([ 'name' => $this->name, 'version' => $latest + 1, 'texts' => $this->texts, 'body' => $this->body, 'annex_title' => $this->annex_title, 'annex_body' => $this->annex_body, 'cancellation_policy_id' => $this->cancellation_policy_id, 'annex_ids' => $this->annex_ids, 'notes' => $this->notes, ]); } }