diff --git a/api/daily_report_cron.php b/api/daily_report_cron.php index ab826b5..d422dcf 100644 --- a/api/daily_report_cron.php +++ b/api/daily_report_cron.php @@ -36,16 +36,35 @@ try { $pdo = db(); - // Use UTC strategy for consistent querying - // 1. Calculate Local Start/End of Today $nowDt = new DateTime('now', new DateTimeZone($timezone)); + $reportTime = $settings['whatsapp_report_time']; + $lastReportFile = __DIR__ . '/../storage/last_daily_report.txt'; + $lastReportDate = file_exists($lastReportFile) ? trim(file_get_contents($lastReportFile)) : ''; + + // Determine what day we should be reporting for based on current time vs scheduled time + $targetTodayDt = clone $nowDt; + $timeParts = explode(':', $reportTime); + $targetTodayDt->setTime((int)$timeParts[0], (int)($timeParts[1] ?? 0), 0); - $startLocal = clone $nowDt; - $startLocal->setTime(0, 0, 0); - - $endLocal = clone $nowDt; - $endLocal->setTime(23, 59, 59); - + if ($nowDt->getTimestamp() >= $targetTodayDt->getTimestamp()) { + // It's after the report time today. We report on TODAY. + $reportDateStr = $nowDt->format('Y-m-d'); + $startLocal = clone $nowDt; + $startLocal->setTime(0, 0, 0); + $endLocal = clone $nowDt; + $endLocal->setTime(23, 59, 59); + } else { + // It's before the report time today. The last scheduled time was YESTERDAY. + $startLocal = clone $nowDt; + $startLocal->modify('-1 day'); + $startLocal->setTime(0, 0, 0); + + $endLocal = clone $startLocal; + $endLocal->setTime(23, 59, 59); + + $reportDateStr = $startLocal->format('Y-m-d'); + } + // 2. Convert to UTC strings for DB query $startUtc = clone $startLocal; $startUtc->setTimezone(new DateTimeZone('UTC')); @@ -62,23 +81,12 @@ try { cron_log("Warning: Could not set MySQL timezone to UTC: " . $e->getMessage()); } - $reportTime = $settings['whatsapp_report_time']; - $lastReportFile = __DIR__ . '/../storage/last_daily_report.txt'; - $lastReportDate = file_exists($lastReportFile) ? trim(file_get_contents($lastReportFile)) : ''; - - // Check schedule relative to Local Time - $targetTodayDt = clone $nowDt; - $timeParts = explode(':', $reportTime); - $targetTodayDt->setTime((int)$timeParts[0], (int)($timeParts[1] ?? 0), 0); - - $diffToday = $nowDt->getTimestamp() - $targetTodayDt->getTimestamp(); - - // Run if within 15 mins of schedule AND not already run for this local date - if ($diffToday >= 0 && $diffToday <= 900 && $lastReportDate !== $nowDt->format('Y-m-d')) { - cron_log("Condition met: sending daily report for " . $nowDt->format('Y-m-d')); + // Run if we haven't already sent the report for this target date + if ($lastReportDate !== $reportDateStr) { + cron_log("Condition met: sending daily report for " . $reportDateStr); cron_log("Querying UTC Range: $strStartUtc to $strEndUtc"); - $todayDisplay = $nowDt->format('Y-m-d'); + $todayDisplay = $reportDateStr; $companyName = $settings['company_name'] ?? 'Company'; // --- 1. Global Stats --- diff --git a/online_order.php b/online_order.php index fcd8422..704f3fb 100644 --- a/online_order.php +++ b/online_order.php @@ -637,6 +637,16 @@ foreach ($variants_by_product as $pid => $vars) { +