option('revert'))) { return $this->revert((string) $this->option('revert')); } $apply = (bool) $this->option('apply'); $report = ['run' => now()->format('Ymd-His'), 'apply' => $apply, 'entries' => [], 'channel_task_ids' => []]; $firstTask = (int) ChannelTask::query()->withoutGlobalScopes()->max('id'); $succeeded = $runner->each(function (Tenant $tenant) use ($intake, $apply, &$report): void { $channels = Channel::query()->portals()->when($this->option('channel'), fn ($query, $ids) => $query->whereKey($ids))->orderBy('id')->get(); foreach ($channels as $channel) { $entries = $channel->blockedPeriods()->whereIn('source', ['channel', 'email'])->whereNull('linked_booking_id') ->where('is_representation', false)->whereDate('ends_on', '>', today())->orderBy('starts_on')->get(); foreach ($entries as $entry) { $before = collect(self::RESTORED)->mapWithKeys(fn (string $field): array => [$field => $entry->getRawOriginal($field)])->all(); $decision = $apply ? $intake->adopt($entry) : $intake->evaluate($entry); $created = $apply && $decision->outcome === IntakeOutcome::Create && $decision->booking !== null; $report['entries'][] = [ 'tenant' => $tenant->id, 'channel' => $channel->name, 'blocked_period_id' => $entry->id, 'period' => $entry->starts_on->format('d.m.Y').'–'.$entry->ends_on->format('d.m.Y'), 'outcome' => $decision->outcome->value, 'case_type' => $decision->caseType?->value, 'reference' => $decision->reference, 'booking_id' => $decision->booking?->id, 'created' => $created, 'reasons' => $decision->reasons, 'before' => $before, ]; } } }); if ($apply) { $report['channel_task_ids'] = ChannelTask::query()->withoutGlobalScopes()->where('id', '>', $firstTask)->pluck('id')->all(); } $this->table(['Kanal', 'Zeitraum', 'Ergebnis', 'Art', 'Nr.', 'Vorgang', 'Begründung'], array_map(fn (array $entry): array => [ $entry['channel'], $entry['period'], IntakeOutcome::from($entry['outcome'])->getLabel(), strtoupper((string) $entry['case_type']), $entry['reference'] ?? '–', $entry['booking_id'] ? '#'.$entry['booking_id'].($entry['created'] ? ' (neu)' : '') : '–', implode(' ', $entry['reasons']), ], $report['entries'])); $path = self::DIRECTORY.'/'.$report['run'].($apply ? '' : '-vorschau').'.json'; for ($number = 2; Storage::disk('local')->exists($path); $number++) { $path = self::DIRECTORY.'/'.$report['run'].($apply ? '' : '-vorschau').'-'.$number.'.json'; } Storage::disk('local')->put($path, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); $counts = collect($report['entries'])->countBy('outcome'); $this->info(($apply ? 'Übernommen' : 'Vorschau – nichts geändert').': '.$counts->map(fn (int $count, string $outcome): string => IntakeOutcome::from($outcome)->getLabel().' '.$count)->implode(', ')); $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; } /** * Takes an adoption back: cases created by it are removed again as long as nothing has happened with them (no * document, no payment), and every entry gets its former state. Cases with activity are reported, not touched. */ 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); if (! ($report['apply'] ?? false)) { $this->info('Das war eine Vorschau – es gibt nichts zurückzunehmen.'); return self::SUCCESS; } $kept = []; foreach ((array) $report['entries'] as $entry) { $block = BlockedPeriod::query()->withoutGlobalScopes()->withTrashed()->find($entry['blocked_period_id']); $case = ($entry['created'] ?? false) ? Booking::query()->withoutGlobalScopes()->find($entry['booking_id']) : null; if ($case !== null && ($case->contract_path !== null || $case->confirmation_sent_at !== null || $case->payments()->withoutGlobalScopes()->exists())) { $kept[] = $case->reference; continue; } // The entry first gets its former state (it may point to the case), then the case created by the run goes. if ($block !== null && in_array($entry['outcome'], [IntakeOutcome::Create->value, IntakeOutcome::Link->value], true)) { $block->forceFill((array) $entry['before'])->saveQuietly(); } $case?->delete(); } ChannelTask::query()->withoutGlobalScopes()->whereKey((array) ($report['channel_task_ids'] ?? []))->where('status', ChannelTaskStatus::Open) ->update(['status' => ChannelTaskStatus::Dismissed, 'automation_error' => 'Bestandsübernahme zurückgenommen.']); $this->info('Zurückgenommen.'.($kept === [] ? '' : ' Nicht entfernt, weil schon bearbeitet: '.implode(', ', $kept))); return self::SUCCESS; } }