53 lines
2.8 KiB
PHP
53 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Enums\BlockedPeriodPurpose;
|
|
use App\Enums\OccupationKind;
|
|
use App\Models\BlockedPeriod;
|
|
use App\Models\Channel;
|
|
use App\Models\Property;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class ClassifyPortalOccupation
|
|
{
|
|
/** @param array{block_purpose?: BlockedPeriodPurpose|string|null, block_note?: string|null, is_private?: bool} $details */
|
|
public function handle(BlockedPeriod $occupation, OccupationKind $kind, ?int $originId = null, array $details = []): void
|
|
{
|
|
Gate::authorize('update', $occupation);
|
|
if (($details['block_purpose'] ?? null) instanceof BlockedPeriodPurpose) {
|
|
$details['block_purpose'] = $details['block_purpose']->value;
|
|
}
|
|
$details = Validator::make($details, [
|
|
'block_purpose' => ['nullable', Rule::enum(BlockedPeriodPurpose::class)],
|
|
'block_note' => ['nullable', 'string', 'max:255'],
|
|
'is_private' => ['sometimes', 'boolean'],
|
|
])->validate();
|
|
if (($details['is_private'] ?? false) && $originId !== null) {
|
|
throw ValidationException::withMessages(['origin_channel_id' => 'Privat und ein externer Buchungskanal können nicht gleichzeitig gewählt werden.']);
|
|
}
|
|
DB::transaction(function () use ($occupation, $kind, $originId, $details): void {
|
|
Property::query()->whereKey($occupation->property_id)->lockForUpdate()->firstOrFail();
|
|
$record = BlockedPeriod::query()->whereKey($occupation->id)->lockForUpdate()->firstOrFail();
|
|
if (! in_array($record->source, ['channel', 'email'], true) || $record->is_representation) {
|
|
throw ValidationException::withMessages(['occupation_kind' => 'Nur eigenständige Portalereignisse können hier eingeordnet werden.']);
|
|
}
|
|
if ($originId !== null && ! Channel::query()->where('property_id', $record->property_id)->whereKey($originId)->exists()) {
|
|
throw ValidationException::withMessages(['origin_channel_id' => 'Der Ursprungskanal muss zur selben Wohnung gehören.']);
|
|
}
|
|
$record->forceFill([
|
|
'occupation_kind' => $kind,
|
|
'origin_channel_id' => $originId,
|
|
'is_private' => $details['is_private'] ?? false,
|
|
'block_purpose' => $kind === OccupationKind::Block ? (array_key_exists('block_purpose', $details) ? $details['block_purpose'] : $record->block_purpose) : null,
|
|
'block_note' => $kind === OccupationKind::Block ? (array_key_exists('block_note', $details) ? $details['block_note'] : $record->block_note) : null,
|
|
])->save();
|
|
app(ReconcileChannelRepresentations::class)->handle($record->property);
|
|
});
|
|
}
|
|
}
|