option('revert'))) { return $this->revert((string) $this->option('revert')); } $apply = (bool) $this->option('apply'); $report = ['run' => now()->format('Ymd-His'), 'apply' => $apply, 'entries' => []]; $succeeded = $runner->each(function (Tenant $tenant) use ($apply, $settings, &$report): void { $blocks = BlockedPeriod::query()->with(['property', 'guest'])->where('source', 'manual')->whereNull('linked_booking_id')->orderBy('starts_on')->get(); foreach ($blocks as $block) { $kind = $block->guest_id !== null ? InternalKind::PrivateStay : InternalKind::fromPurpose($block->block_purpose); $entry = [ 'tenant' => $tenant->id, 'blocked_period_id' => $block->id, 'property' => $block->property?->name, 'period' => $block->starts_on->format('d.m.Y').'–'.$block->ends_on->format('d.m.Y'), 'kind' => $kind->value, 'occasion' => $block->block_note ?: $block->reason, 'booking_id' => null, 'result' => 'neu', ]; if (Booking::query()->where('export_uid', 'block-'.$block->id)->exists()) { $entry['result'] = 'schon übernommen'; } elseif (Booking::query()->occupying()->where('property_id', $block->property_id)->overlapping($block->starts_on, $block->ends_on)->exists()) { $entry['result'] = 'unklar: überschneidet sich mit einem Vorgang – nicht übernommen'; } elseif ($apply) { $entry['booking_id'] = $this->adopt($block, $kind, $settings)->id; $entry['result'] = self::ADOPTED; } $report['entries'][] = $entry; } }); $this->table(['Wohnung', 'Zeitraum', 'Art', 'Anlass', 'Ergebnis', 'Vorgang'], array_map(fn (array $entry): array => [ $entry['property'], $entry['period'], InternalKind::from($entry['kind'])->getLabel(), $entry['occasion'], $entry['result'], $entry['booking_id'] ? '#'.$entry['booking_id'] : '–', ], $report['entries'])); $suffix = $apply ? '' : '-vorschau'; $path = self::DIRECTORY.'/'.$report['run'].$suffix.'.json'; for ($number = 2; Storage::disk('local')->exists($path); $number++) { $path = self::DIRECTORY.'/'.$report['run'].$suffix.'-'.$number.'.json'; } Storage::disk('local')->put($path, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); $counts = collect($report['entries'])->countBy('result')->map(fn (int $count, string $result): string => $result.' '.$count)->implode(', '); $this->info(($apply ? 'Übernommen' : 'Vorschau – nichts geändert').': '.($counts ?: 'keine manuellen Sperren')); $this->line('Bericht: storage/app/private/'.$path.($apply ? ' (Rücknahme: --revert='.$path.')' : '')); if ($succeeded < $runner->tenants()->count()) { $this->error('Bei mindestens einem Vermieter ist ein Fehler aufgetreten – Details im Protokoll (storage/logs).'); return self::FAILURE; } return self::SUCCESS; } /** * The block becomes a case without any side effect: no portal tasks and no mails (the portals have the dates * already), the same export identity and time stamp; the block is kept (deleted softly) for a revert. */ private function adopt(BlockedPeriod $block, InternalKind $kind, ProcessSettings $settings): Booking { return DB::transaction(function () use ($block, $kind, $settings): Booking { $profile = $settings->resolve($block->property, null, IntakeKind::Internal)->withOrigin('migration'); $booking = Model::withoutEvents(function () use ($block, $kind, $profile): Booking { $booking = new Booking([ 'status' => BookingStatus::Confirmed, 'source' => 'internal', 'intake' => IntakeKind::Internal, 'internal_kind' => $kind, 'occasion' => $block->block_note ?: $block->reason, 'arrival' => $block->starts_on->toDateString(), 'departure' => $block->ends_on->toDateString(), 'export_uid' => 'block-'.$block->id, 'confirmed_at' => $block->created_at, ]); $booking->forceFill([ 'tenant_id' => $block->tenant_id, 'property_id' => $block->property_id, 'guest_id' => $block->guest_id, 'case_type' => $profile->caseType, 'profile' => $profile->toArray(), 'created_at' => $block->created_at, 'updated_at' => $block->updated_at, ]); $booking->timestamps = false; $booking->save(); // Dates are only cast while timestamps are on; the reference counts by the year of creation. $booking->timestamps = true; $reference = $booking->refresh()->nextReference(); $booking->timestamps = false; $booking->forceFill(['reference' => $reference])->save(); $block->forceFill(['linked_booking_id' => $booking->id, 'review_note' => 'In Vorgang übernommen (A6).'])->saveQuietly(); $block->deleteQuietly(); return $booking; }); CaseLog::record($booking, 'intake', 'Aus der bisherigen Sperrzeitenliste übernommen'.($booking->occasion ? ': '.$booking->occasion : ''), actor: CaseLog::SYSTEM, data: ['blocked_period_id' => $block->id]); return $booking; }); } /** * Takes an adoption back: the block comes back as it was and the case created by the run goes – unless somebody has * worked on the case since (then it is reported and kept). */ private function revert(string $path): int { if (! Storage::disk('local')->exists($path)) { $this->error('Bericht nicht gefunden: '.$path); return self::FAILURE; } $report = json_decode((string) Storage::disk('local')->get($path), true); $kept = []; foreach ((array) ($report['entries'] ?? []) as $entry) { if (($entry['result'] ?? null) !== self::ADOPTED) { continue; } $booking = Booking::query()->withoutGlobalScopes()->find($entry['booking_id']); if ($booking !== null && $booking->events()->where('type', '!=', 'intake')->exists()) { $kept[] = $booking->reference; continue; } Model::withoutEvents(function () use ($entry, $booking): void { $block = BlockedPeriod::query()->withoutGlobalScopes()->withTrashed()->find($entry['blocked_period_id']); $block?->forceFill(['linked_booking_id' => null, 'review_note' => null, 'deleted_at' => null])->saveQuietly(); $booking?->delete(); }); } $this->info('Zurückgenommen.'.($kept === [] ? '' : ' Nicht entfernt, weil seitdem bearbeitet: '.implode(', ', $kept))); return self::SUCCESS; } }