Files
LarsandClaude Opus 5.5 3bcfc09417
Deploy FeWo mit PipelineManager / deploy (push) Successful in 4m44s
Pakete 8, 5 und 6: Beträge und Zahlungen, Ändern und Stornieren, A6 im Vorgang
- Paket 8: Zahlungsarten (Gast an uns, laut Portal, Auszahlung, Erstattung),
  Portalbeträge als Angaben mit Quelle, Übersicht nach Zuständigkeit (R09).
- Paket 5: Ändern mit Folgenvorschau, vereinbart oder dem Gast vorgeschlagen
  (signierter Link), ein Stornoweg mit Grund und Erstattung, Portalstorno erst
  nach Bestätigung.
- Paket 6: eigene Nutzung, Besuch und Sperre als Vorgang mit Kurabgabe und
  Aufenthaltsbestätigung; Bestandsübernahme der Sperren mit identischem Export.
- Wirksame Einstellungen (Expertenansicht), Abnahmekatalog T01–T14, Ergebnis-,
  Abnahme- und Konfigurationsdokument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 22:16:47 +01:00

160 lines
8.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Console\Commands;
use App\Enums\BookingStatus;
use App\Enums\IntakeKind;
use App\Enums\InternalKind;
use App\Models\BlockedPeriod;
use App\Models\Booking;
use App\Models\Tenant;
use App\Process\CaseLog;
use App\Process\ProcessSettings;
use App\Tenancy\TenantRunner;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
#[Signature('cases:adopt-blocks
{--apply : Wirklich übernehmen (ohne: nur Vorschau)}
{--revert= : Eine frühere Übernahme anhand ihres Berichts zurücknehmen}')]
#[Description('Bisherige manuelle Sperren als A6-Vorgänge übernehmen (Vorschau, Bericht, wiederholbar, rücknehmbar); der Kalenderexport für die Portale bleibt gleich')]
class AdoptManualBlocks extends Command
{
private const DIRECTORY = 'block-adoptions';
private const ADOPTED = 'übernommen';
public function handle(TenantRunner $runner, ProcessSettings $settings): int
{
if (filled($this->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;
}
}