Files
fewo-stommer/app/Models/Tenant.php
T

178 lines
5.8 KiB
PHP

<?php
namespace App\Models;
use App\Support\Settings;
use App\Tenancy\TenantDefaults;
use Database\Factories\TenantFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* A landlord. Owns websites, apartments, guests, bookings and settings. One account can belong
* to several landlords; the operator is not a landlord.
*/
#[Fillable([
'name', 'slug', 'is_active',
'owner_name', 'owner_street', 'owner_city', 'owner_phone', 'contact_email', 'notification_emails',
'sender_name', 'sender_email',
'bank_account_holder', 'bank_iban', 'bank_bic', 'bank_name', 'tax_note',
'operating_mode', 'test_recipient', 'suspended_message',
'mailbox_host', 'mailbox_port', 'mailbox_encryption', 'mailbox_username', 'mailbox_password', 'mailbox_folder',
'deposit_due_days', 'review_request_after_days', 'guest_retention_years', 'retention_inquiry_months',
'retention_portal_email_excerpt_days', 'retention_attribution_months',
'spam_min_seconds', 'spam_per_ip_per_hour', 'spam_per_email_per_day', 'spam_open_requests_per_email', 'spam_max_links',
'channel_stale_after_minutes', 'channel_task_reminder_after_minutes',
'reminder_interval_days', 'digest_daily', 'digest_weekly', 'digest_weekday', 'digest_hour', 'digest_sections',
'digest_when_empty', 'digest_daily_sent_on', 'digest_weekly_sent_on',
'process_defaults', 'document_releases', 'communication_rules',
])]
class Tenant extends Model
{
/** @use HasFactory<TenantFactory> */
use HasFactory;
/** Reminder and summary defaults: repeat nothing, a daily summary from 7 a.m., the weekly one on Mondays. */
protected $attributes = [
'reminder_interval_days' => 0,
'digest_daily' => true,
'digest_weekly' => false,
'digest_weekday' => 1,
'digest_hour' => 7,
'digest_when_empty' => false,
];
/**
* The landlord the existing single-website data belongs to.
*/
public static function stommer(): self
{
return static::query()->firstOrCreate(
['slug' => 'stommer'],
['name' => 'Stommer'] + TenantDefaults::forLegacyLandlord(encryptBank: false),
);
}
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_active' => 'boolean',
'bank_account_holder' => 'encrypted',
'bank_iban' => 'encrypted',
'bank_bic' => 'encrypted',
'bank_name' => 'encrypted',
'tax_note' => 'encrypted',
'mailbox_password' => 'encrypted',
'reminder_interval_days' => 'integer',
'digest_daily' => 'boolean',
'digest_weekly' => 'boolean',
'digest_weekday' => 'integer',
'digest_hour' => 'integer',
'digest_sections' => 'array',
'process_defaults' => 'array',
'document_releases' => 'array',
'communication_rules' => 'array',
'digest_when_empty' => 'boolean',
'digest_daily_sent_on' => 'immutable_date',
'digest_weekly_sent_on' => 'immutable_date',
];
}
/**
* @return HasMany<Site, $this>
*/
public function sites(): HasMany
{
return $this->hasMany(Site::class);
}
/**
* @return HasMany<Property, $this>
*/
public function properties(): HasMany
{
return $this->hasMany(Property::class);
}
/**
* @return HasMany<TenantService, $this>
*/
public function services(): HasMany
{
return $this->hasMany(TenantService::class);
}
/**
* @return HasMany<ServiceUsage, $this>
*/
public function serviceUsages(): HasMany
{
return $this->hasMany(ServiceUsage::class);
}
/**
* The mailbox this landlord reads portal notifications from, or null when they have none. An empty
* host means the fast path is simply off for them.
*
* @return array{host: string, port: int, encryption: string, username: string, password: ?string, folder: string}|null
*/
public function mailbox(): ?array
{
// v0.11: the active mailbox record leads; the tenant columns remain the fallback until every system migrated.
$mailbox = $this->activeMailbox();
if ($mailbox !== null) {
return $mailbox->connection();
}
if (blank($this->mailbox_host) || blank($this->mailbox_username)) {
return null;
}
return [
'host' => (string) $this->mailbox_host,
'port' => (int) ($this->mailbox_port ?: 993),
'encryption' => (string) ($this->mailbox_encryption ?: 'ssl'),
'username' => (string) $this->mailbox_username,
'password' => $this->mailbox_password,
'folder' => (string) ($this->mailbox_folder ?: 'INBOX'),
];
}
/**
* The landlord's active mailbox (at most one, CH-05).
*/
public function activeMailbox(): ?Mailbox
{
return Mailbox::query()->withoutGlobalScopes()->where('tenant_id', $this->id)->where('is_active', true)->orderBy('id')->first();
}
public function isLive(): bool
{
return $this->operating_mode === 'live';
}
/**
* Notification addresses of this landlord.
*
* @return list<string>
*/
public function notificationRecipients(): array
{
return Settings::addressList($this->notification_emails);
}
/**
* @return BelongsToMany<User, $this>
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class)->withPivot('role')->withTimestamps();
}
}