process_overrides ?? []); if ($property->contract_template_id !== null) { $propertyLevel[ProcessSetting::ContractTemplate->value] = $property->contract_template_id; } $levels = [ ProcessProfile::TENANT => $this->tenantDefaults($property->tenant_id), ProcessProfile::PROPERTY => $propertyLevel, ($channel?->isWebsite() ? ProcessProfile::WEBSITE : ProcessProfile::CHANNEL) => $intake === IntakeKind::Internal || $channel === null ? [] : (array) (($channel->process_overrides ?? [])[$intake->value] ?? []), ]; $model = $this->chosenModel($channel, $intake); $templates = $this->chosenTemplates($channel, $intake, $model); $suggested = []; foreach ($templates as $class) { $suggested = [...$suggested, ...$class::defaults()]; } $values = []; $sources = []; foreach (ProcessSetting::cases() as $setting) { $values[$setting->value] = $setting->default(); $sources[$setting->value] = ProcessProfile::SYSTEM; // A function template only suggests: below every level the landlord can set. if (($suggestion = $setting->normalize($suggested[$setting->value] ?? null)) !== null) { $values[$setting->value] = $suggestion; $sources[$setting->value] = ProcessProfile::TEMPLATE_DEFAULT; } foreach ($levels as $level => $settings) { if (in_array($level, [ProcessProfile::CHANNEL, ProcessProfile::WEBSITE], true) && ! in_array($intake, $setting->channelIntakes(), true)) { continue; } $value = $setting->normalize($settings[$setting->value] ?? null); if ($value !== null) { $values[$setting->value] = $value; $sources[$setting->value] = $level; } } } // Architektur V2: the process model chosen for the channel and intake kind fixes some settings (e.g. the // pre-reservation of the offer model, the responsibilities of A3–A5); these win over every level. foreach ($model === null ? [] : $model::fixes() as $key => $fixed) { $setting = ProcessSetting::tryFrom($key); if ($setting !== null) { $values[$key] = $setting->normalize($fixed) ?? $fixed; $sources[$key] = ProcessProfile::MODEL; } } // Function templates chosen for the model's slots fix the responsibility of their area. foreach ($templates as $class) { foreach ($class::fixes() as $key => $fixed) { $values[$key] = ProcessSetting::tryFrom($key)?->normalize($fixed) ?? $fixed; $sources[$key] = ProcessProfile::TEMPLATE; } } if ($intake === IntakeKind::Internal) { // Own use, private stays and blocks (A6): no rental documents, no payment reminders, no guest mails – fixed by // the case type, not by a setting. foreach ([ProcessSetting::Documents, ProcessSetting::Payments, ProcessSetting::GuestMessages] as $setting) { $values[$setting->value] = Responsibility::Nobody->value; $sources[$setting->value] = ProcessProfile::CASE_TYPE; } } elseif ($intake !== IntakeKind::Booking) { // A request is answered with our own offer and an internal case is ours anyway: the documents are ours. $values[ProcessSetting::Documents->value] = Responsibility::Landlord->value; $sources[ProcessSetting::Documents->value] = ProcessProfile::SYSTEM; } // What was set nowhere follows the related setting, as it always did. foreach ([ProcessSetting::ArrivalInfoSender, ProcessSetting::ReviewRequestSender] as $sender) { if ($values[$sender->value] === null && $intake !== IntakeKind::Internal) { $values[$sender->value] = $values[ProcessSetting::GuestMessages->value]; $sources[$sender->value] = ProcessProfile::DERIVED; } } if ($values[ProcessSetting::InquiryAnswer->value] === null && $intake === IntakeKind::Inquiry) { $values[ProcessSetting::InquiryAnswer->value] = $values[ProcessSetting::PreReservation->value] ? InquiryAnswer::Offer->value : InquiryAnswer::Information->value; $sources[ProcessSetting::InquiryAnswer->value] = ProcessProfile::DERIVED; } $documents = Responsibility::from((string) $values[ProcessSetting::Documents->value]); $payments = Responsibility::from((string) $values[ProcessSetting::Payments->value]); return new ProcessProfile($intake, ProcessProfile::caseTypeFor($intake, $channel?->isPortal() ?? false, $documents, $payments), $values, $sources); } /** * The function templates in force for the chosen model's slots (chosen ones, otherwise the model's defaults). * * @return array> */ private function chosenTemplates(?Channel $channel, IntakeKind $intake, ?ProcessModel $model): array { if ($model === null || $model->slots() === []) { return []; } $composer = app(TemplateComposer::class); return $composer->classes($composer->resolve($model, (array) (ProcessPlan::configurationOf($channel, $intake)['templates'] ?? []))); } /** * The model chosen for the channel and intake kind (Kanal › Ablauf), when it is registered and fits the intake. */ private function chosenModel(?Channel $channel, IntakeKind $intake): ?ProcessModel { $key = (string) (ProcessPlan::configurationOf($channel, $intake)['model'] ?? ''); $catalogue = app(ProcessCatalogue::class); if ($key === '' || ! $catalogue->hasModel($key)) { return null; } $model = $catalogue->model($key); return in_array($intake, $model::intakes(), true) ? $model : null; } /** * Settings of a request from the own website: the website's entry of the channel manager is its channel level * (the case itself stays direct, without a channel). */ public function forWebsite(Property $property, IntakeKind $intake = IntakeKind::Inquiry): ProcessProfile { return $this->resolve($property, Channel::existingWebsiteOf($property), $intake); } /** * The settings that would apply to a case right now (its frozen profile is what counts for the case itself). */ public function forBooking(Booking $booking): ProcessProfile { // The case's own landlord counts, whichever landlord is current (e.g. a case written by the system). $property = $booking->property ?? Property::withoutGlobalScope('tenant')->findOrFail($booking->property_id); $channel = $booking->processChannel() ?? ($booking->source === 'website' ? Channel::existingWebsiteOf($property) : null); return $this->resolve($property, $channel, $booking->intake ?? self::intakeFor($booking)); } /** * Landlord defaults as stored on the tenant (not set: empty). * * @return array */ public function tenantDefaults(?int $tenantId): array { if ($tenantId === null) { return []; } return (array) (Tenant::query()->find($tenantId)?->process_defaults ?? []); } /** * Intake kind of a case that does not name one: a case from a channel that only passes on requests is a request, * from other channels a booking that is already binding; everything else (website, phone, e-mail) a request. */ public static function intakeFor(Booking $booking): IntakeKind { $channel = $booking->processChannel(); if ($channel === null || $channel->isWebsite()) { return IntakeKind::Inquiry; } return $channel->booking_model === ChannelBookingModel::Inquiry ? IntakeKind::Inquiry : IntakeKind::Booking; } }