whereIn('status', [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid]) ->whereDate('departure', '<', CarbonImmutable::today()) ->with(['guest', 'property', 'payments', 'importChannel', 'originChannel']) ->get(); foreach ($stays as $booking) { // After the departure the arrival details are of no use any more. foreach (BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ArrivalDetails)->where('status', BookingTaskStatus::Open)->get() as $task) { $task->update(['status' => BookingTaskStatus::Dismissed, 'note' => 'Nach der Abreise nicht mehr nötig.', 'completed_by' => CaseLog::SYSTEM, 'completed_at' => now()]); } if (CaseClosure::stayBlockers($booking)->isNotEmpty()) { continue; } CaseLog::context(fn () => $this->close($booking), actor: CaseLog::SYSTEM.' (automatisch)'); $closed++; } // Cases closed before package 9 (or whose money became clear without a payment) get their commercial closing too. $unsettled = Booking::query()->whereIn('status', [BookingStatus::Completed, BookingStatus::Cancelled])->whereNull('settled_at') ->whereDate('departure', '>', CarbonImmutable::today()->subYear())->with(['payments', 'importChannel', 'originChannel'])->get(); foreach ($unsettled as $booking) { CaseLog::context(fn () => CaseClosure::refresh($booking), actor: CaseLog::SYSTEM.' (automatisch)'); } return $closed; } /** * The landlord closes the stay; open tasks of the stay are marked as done or handed over with the note. * * @throws ReservationException */ public function byHand(Booking $booking, string $by, ?string $note = null): void { if (! in_array($booking->status, [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid], true)) { throw ReservationException::invalidTransition($booking->status->getLabel(), 'abgeschlossen'); } if ($booking->departure->isAfter(CarbonImmutable::today())) { throw new ReservationException('Der Aufenthalt endet erst am '.$booking->departure->format('d.m.Y').'.'); } CaseLog::context(function () use ($booking, $by, $note): void { foreach (CaseClosure::stayBlockers($booking) as $task) { $task->complete($by, filled($note) ? $note : 'Mit dem Abschluss des Aufenthalts erledigt bzw. übergeben.'); } BookingTask::query()->where('booking_id', $booking->id)->where('kind', BookingTaskKind::ArrivalDetails)->where('status', BookingTaskStatus::Open) ->update(['status' => BookingTaskStatus::Dismissed, 'completed_by' => $by, 'completed_at' => now()]); $this->close($booking); }, actor: $by, reason: $note); } private function close(Booking $booking): void { DB::transaction(function () use ($booking): void { $booking->lockForTransition(); if (! in_array($booking->status, [BookingStatus::Confirmed, BookingStatus::DepositPaid, BookingStatus::Paid], true)) { return; } $booking->update(['status' => BookingStatus::Completed, 'completed_at' => now()]); $this->followUps($booking); }); CaseClosure::refresh($booking->fresh()); } /** * Open money becomes a follow-up of the closed stay, so it stays visible and assigned. */ private function followUps(Booking $booking): void { $booking->unsetRelation('payments'); $money = CaseMoney::of($booking); if ($money->collector() === Responsibility::Landlord && $money->openForUs() > 0) { $chasing = BookingTask::query()->where('booking_id', $booking->id)->where('status', BookingTaskStatus::Open)->get()->contains(fn (BookingTask $task): bool => $task->kind->isPayment()); if (! $chasing) { BookingTask::openOnce($booking, BookingTaskKind::OpenClaim, '', ['due_on' => today(), 'note' => 'Noch offen vom Gast: '.Money::format($money->openForUs()).'.']); } } if ($money->collector() === Responsibility::Portal) { $payout = $money->payout(); if ($payout['received'] === 0 || ($payout['outstanding'] ?? 0) > 0) { $portal = $booking->processChannel()?->name ?? 'des Portals'; BookingTask::openOnce($booking, BookingTaskKind::PayoutCheck, '', [ 'due_on' => $booking->departure->addDays(14), 'responsible' => $booking->processChannel()?->name, 'note' => match (true) { $payout['values'] !== [] => 'Angekündigt: '.implode(' bzw. ', array_map(fn (int $value): string => Money::format($value), $payout['values'])).'; erhalten: '.Money::format($payout['received']).'.', default => 'Auszahlung von '.$portal.' noch nicht erfasst.', }, ]); } } } }