update sending report

This commit is contained in:
Flatlogic Bot 2026-04-02 07:46:25 +00:00
parent 2ae728c351
commit b0a261b00c
4 changed files with 58 additions and 24 deletions

View File

@ -36,15 +36,34 @@ try {
$pdo = db(); $pdo = db();
// Use UTC strategy for consistent querying
// 1. Calculate Local Start/End of Today
$nowDt = new DateTime('now', new DateTimeZone($timezone)); $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);
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 = clone $nowDt;
$startLocal->setTime(0, 0, 0); $startLocal->setTime(0, 0, 0);
$endLocal = clone $nowDt; $endLocal = clone $nowDt;
$endLocal->setTime(23, 59, 59); $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 // 2. Convert to UTC strings for DB query
$startUtc = clone $startLocal; $startUtc = clone $startLocal;
@ -62,23 +81,12 @@ try {
cron_log("Warning: Could not set MySQL timezone to UTC: " . $e->getMessage()); cron_log("Warning: Could not set MySQL timezone to UTC: " . $e->getMessage());
} }
$reportTime = $settings['whatsapp_report_time']; // Run if we haven't already sent the report for this target date
$lastReportFile = __DIR__ . '/../storage/last_daily_report.txt'; if ($lastReportDate !== $reportDateStr) {
$lastReportDate = file_exists($lastReportFile) ? trim(file_get_contents($lastReportFile)) : ''; cron_log("Condition met: sending daily report for " . $reportDateStr);
// 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'));
cron_log("Querying UTC Range: $strStartUtc to $strEndUtc"); cron_log("Querying UTC Range: $strStartUtc to $strEndUtc");
$todayDisplay = $nowDt->format('Y-m-d'); $todayDisplay = $reportDateStr;
$companyName = $settings['company_name'] ?? 'Company'; $companyName = $settings['company_name'] ?? 'Company';
// --- 1. Global Stats --- // --- 1. Global Stats ---

View File

@ -637,6 +637,16 @@ foreach ($variants_by_product as $pid => $vars) {
<label class="form-label"><?= tt('car_plate') ?></label> <label class="form-label"><?= tt('car_plate') ?></label>
<input type="text" class="form-control" id="car-plate" placeholder="<?= tt('car_plate_placeholder') ?>"> <input type="text" class="form-control" id="car-plate" placeholder="<?= tt('car_plate_placeholder') ?>">
</div> </div>
<div class="mb-3">
<label class="form-label"><?= tt('ready_in') ?></label>
<select class="form-select" id="prep-time">
<option value="0"><?= tt('ready_asap') ?></option>
<option value="10"><?= tt('ready_10m') ?></option>
<option value="15"><?= tt('ready_15m') ?></option>
<option value="20"><?= tt('ready_20m') ?></option>
<option value="30"><?= tt('ready_30m') ?></option>
</select>
</div>
<div class="mb-4 d-flex align-items-center"> <div class="mb-4 d-flex align-items-center">
<input type="checkbox" id="register-checkbox" class="form-check-input <?= $is_ar ? 'ms-2' : 'me-2' ?>"> <input type="checkbox" id="register-checkbox" class="form-check-input <?= $is_ar ? 'ms-2' : 'me-2' ?>">
<label class="form-check-label" for="register-checkbox"><?= tt('register_to_earn') ?></label> <label class="form-check-label" for="register-checkbox"><?= tt('register_to_earn') ?></label>
@ -841,6 +851,7 @@ foreach ($variants_by_product as $pid => $vars) {
const custName = document.getElementById("customer-name").value; const custName = document.getElementById("customer-name").value;
const custPhone = document.getElementById("customer-phone").value; const custPhone = document.getElementById("customer-phone").value;
const carPlate = document.getElementById("car-plate").value; const carPlate = document.getElementById("car-plate").value;
const prepTime = document.getElementById("prep-time").value;
const doRegister = document.getElementById("register-checkbox").checked; const doRegister = document.getElementById("register-checkbox").checked;
if (!custName || !custPhone) { if (!custName || !custPhone) {
@ -878,6 +889,7 @@ foreach ($variants_by_product as $pid => $vars) {
document.getElementById("customer-name").value = ""; document.getElementById("customer-name").value = "";
document.getElementById("customer-phone").value = ""; document.getElementById("customer-phone").value = "";
document.getElementById("car-plate").value = ""; document.getElementById("car-plate").value = "";
document.getElementById("prep-time").value = "0";
document.getElementById("register-checkbox").checked = false; document.getElementById("register-checkbox").checked = false;
cart = []; cart = [];
updateCartUI(); updateCartUI();

View File

@ -21677,3 +21677,17 @@ Array
[2026-03-27 23:59:02] Report sent successfully to 96897417667 [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:02] Sending detailed report to: 96899359472
[2026-03-27 23:59:03] Report sent successfully 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

View File

@ -1 +1 @@
2026-03-27 2026-04-01