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) { +
+ + +
@@ -841,6 +851,7 @@ foreach ($variants_by_product as $pid => $vars) { const custName = document.getElementById("customer-name").value; const custPhone = document.getElementById("customer-phone").value; const carPlate = document.getElementById("car-plate").value; + const prepTime = document.getElementById("prep-time").value; const doRegister = document.getElementById("register-checkbox").checked; if (!custName || !custPhone) { @@ -878,6 +889,7 @@ foreach ($variants_by_product as $pid => $vars) { document.getElementById("customer-name").value = ""; document.getElementById("customer-phone").value = ""; document.getElementById("car-plate").value = ""; + document.getElementById("prep-time").value = "0"; document.getElementById("register-checkbox").checked = false; cart = []; updateCartUI(); diff --git a/storage/cron_debug.log b/storage/cron_debug.log index 47cff9d..09c689d 100644 --- a/storage/cron_debug.log +++ b/storage/cron_debug.log @@ -21677,3 +21677,17 @@ Array [2026-03-27 23:59:02] Report sent successfully to 96897417667 [2026-03-27 23:59:02] Sending detailed report to: 96899359472 [2026-03-27 23:59:03] Report sent successfully to 96899359472 +[2026-04-02 08:33:35] Condition met: sending daily report for 2026-04-02 +[2026-04-02 08:33:35] Querying UTC Range: 2026-04-01 20:00:00 to 2026-04-02 19:59:59 +[2026-04-02 08:33:35] Fetched 4 outlets from database. +[2026-04-02 08:33:35] Sending detailed report to: 96897417667 +[2026-04-02 08:33:36] Report sent successfully to 96897417667 +[2026-04-02 08:33:36] Sending detailed report to: 96899359472 +[2026-04-02 08:33:37] Report sent successfully to 96899359472 +[2026-04-02 11:43:01] Condition met: sending daily report for 2026-04-01 +[2026-04-02 11:43:01] Querying UTC Range: 2026-03-31 20:00:00 to 2026-04-01 19:59:59 +[2026-04-02 11:43:01] Fetched 4 outlets from database. +[2026-04-02 11:43:01] Sending detailed report to: 96897417667 +[2026-04-02 11:43:02] Report sent successfully to 96897417667 +[2026-04-02 11:43:02] Sending detailed report to: 96899359472 +[2026-04-02 11:43:03] Report sent successfully to 96899359472 diff --git a/storage/last_daily_report.txt b/storage/last_daily_report.txt index 64065cd..c754aa1 100644 --- a/storage/last_daily_report.txt +++ b/storage/last_daily_report.txt @@ -1 +1 @@ -2026-03-27 \ No newline at end of file +2026-04-01 \ No newline at end of file