parseDay((string) $this->option('to')) ?? Carbon::now()->endOfDay(); $from = $this->resolveFrom($reports, $to); if ($from->gt($to)) { [$from, $to] = [$to->copy()->startOfDay(), $from->copy()->endOfDay()]; } $force = (bool) $this->option('force') || (bool) $this->option('all'); $result = $reports->rebuildAll($from, $to, $force); $this->info(sprintf( 'daily-stats rebuilt=%d agents=%d from=%s to=%s', $result['days'], $result['agents'], $from->toDateString(), $to->toDateString(), )); return self::SUCCESS; } private function resolveFrom(DailyReportService $reports, Carbon $to): Carbon { if ((bool) $this->option('all')) { return $reports->earliestDataDate() ?? $to->copy()->startOfDay(); } $from = $this->parseDay((string) $this->option('from')); if ($from !== null) { return $from; } $days = max(1, (int) $this->option('days')); return $to->copy()->subDays($days - 1)->startOfDay(); } private function parseDay(string $raw): ?Carbon { $raw = trim($raw); if ($raw === '') { return null; } try { return Carbon::parse($raw)->startOfDay(); } catch (\Throwable) { $this->warn('ignored invalid date: '.$raw); return null; } } }