connector()->fetchOccupations($channel); } catch (ChannelSyncException $exception) { $channel->forceFill(['last_synced_at' => now(), 'last_error' => $exception->getMessage()])->save(); return $channel->syncLogs()->create([ 'successful' => false, 'message' => $exception->getMessage(), 'duration_ms' => $this->elapsed($startedAt), ]); } $today = CarbonImmutable::today(); $suffix = config('channels.uid_suffix'); $periods = array_values(array_filter( $periods, fn (ExternalPeriod $period): bool => $period->endsOn->greaterThan($today) && ! str_ends_with($period->uid, $suffix), )); $counts = ['added' => 0, 'changed' => 0, 'removed' => 0]; $conflicts = []; DB::transaction(function () use ($channel, $periods, $today, &$counts, &$conflicts): void { $existing = $channel->blockedPeriods()->get()->keyBy('external_uid'); $seen = []; foreach ($periods as $period) { $seen[$period->uid] = true; $block = $existing->get($period->uid); if ($block && $block->starts_on->equalTo($period->startsOn) && $block->ends_on->equalTo($period->endsOn)) { continue; } $attributes = [ 'property_id' => $channel->property_id, 'starts_on' => $period->startsOn->toDateString(), 'ends_on' => $period->endsOn->toDateString(), 'reason' => "{$channel->name}".($period->summary ? ": {$period->summary}" : ''), 'source' => 'channel', ]; if ($block) { $block->update($attributes); $counts['changed']++; } else { $block = $channel->blockedPeriods()->create([...$attributes, 'external_uid' => $period->uid]); $counts['added']++; } array_push($conflicts, ...$this->conflictsFor($channel, $block)); } $stale = $existing->reject(fn (BlockedPeriod $block, string $uid): bool => isset($seen[$uid]) || $block->ends_on->lessThanOrEqualTo($today)); $stale->each->delete(); $counts['removed'] = $stale->count(); }); $channel->forceFill([ 'last_synced_at' => now(), 'last_success_at' => now(), 'last_error' => null, 'alerted_at' => null, ])->save(); if ($conflicts !== []) { rescue(fn () => Mail::to(config('fewo.notification_email'))->send(new ChannelConflict($channel, $conflicts))); } return $channel->syncLogs()->create([ 'successful' => true, 'events' => count($periods), ...$counts, 'conflicts' => count($conflicts), 'duration_ms' => $this->elapsed($startedAt), ]); } /** * Occupations from other sources that overlap the imported block. * * @return list */ private function conflictsFor(Channel $channel, BlockedPeriod $block): array { $property = $channel->property; $imported = $block->starts_on->format('d.m.Y').' – '.$block->ends_on->format('d.m.Y'); $bookings = $property->bookings()->occupying()->overlapping($block->starts_on, $block->ends_on)->with('guest')->get() ->map(fn (Booking $booking): string => "{$channel->name} meldet {$imported}, überschneidet sich mit Buchung {$booking->reference} ({$booking->guest->full_name}, {$booking->arrival->format('d.m.Y')} – {$booking->departure->format('d.m.Y')})"); $blocks = $property->blockedPeriods() ->overlapping($block->starts_on, $block->ends_on) ->whereKeyNot($block->getKey()) ->where(fn ($query) => $query->whereNull('channel_id')->orWhere('channel_id', '!=', $channel->id)) ->with('channel') ->get() ->map(fn (BlockedPeriod $other): string => "{$channel->name} meldet {$imported}, überschneidet sich mit Sperre „".($other->channel->name ?? $other->reason ?? 'manuell')."“ ({$other->starts_on->format('d.m.Y')} – {$other->ends_on->format('d.m.Y')})"); return [...$bookings->all(), ...$blocks->all()]; } private function elapsed(float $startedAt): int { return (int) round((microtime(true) - $startedAt) * 1000); } }