53 lines
2.3 KiB
PHP
53 lines
2.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../includes/functions.php';
|
|
require_once __DIR__ . '/../includes/WablasService.php';
|
|
|
|
// Enable error logging for cron
|
|
ini_set('log_errors', 1);
|
|
$logFile = __DIR__ . '/../storage/cron_debug.log';
|
|
ini_set('error_log', $logFile);
|
|
|
|
file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] Starting execution check\n", FILE_APPEND);
|
|
|
|
try {
|
|
$settings = get_company_settings();
|
|
|
|
$debugMsg = "Settings retrieved: \n" . print_r($settings, true) . "\n";
|
|
file_put_contents($logFile, $debugMsg, FILE_APPEND);
|
|
|
|
if (empty($settings['whatsapp_report_enabled']) || empty($settings['whatsapp_report_number']) || empty($settings['whatsapp_report_time'])) {
|
|
file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] Skipping: settings missing or disabled\n", FILE_APPEND);
|
|
exit;
|
|
}
|
|
|
|
$timezone = !empty($settings['timezone']) ? $settings['timezone'] : 'UTC';
|
|
date_default_timezone_set($timezone);
|
|
|
|
$reportTime = $settings['whatsapp_report_time'];
|
|
|
|
$lastReportFile = __DIR__ . '/../storage/last_daily_report.txt';
|
|
|
|
$lastReportDate = file_exists($lastReportFile) ? trim(file_get_contents($lastReportFile)) : '';
|
|
|
|
$nowDt = new DateTime('now', new DateTimeZone($timezone));
|
|
|
|
$targetTodayDt = clone $nowDt;
|
|
$timeParts = explode(':', $reportTime);
|
|
$targetTodayDt->setTime((int)$timeParts[0], (int)($timeParts[1] ?? 0), 0);
|
|
|
|
$diffToday = $nowDt->getTimestamp() - $targetTodayDt->getTimestamp();
|
|
|
|
$logEntry = "Now: " . $nowDt->format('Y-m-d H:i:s') . " | Target: " . $targetTodayDt->format('Y-m-d H:i:s') . " | Diff: $diffToday | LastSent: $lastReportDate\n";
|
|
file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] " . $logEntry, FILE_APPEND);
|
|
|
|
if ($diffToday >= 0 && $diffToday <= 900 && $lastReportDate !== $nowDt->format('Y-m-d')) {
|
|
file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] Condition met: sending report\n", FILE_APPEND);
|
|
// ... (rest of logic)
|
|
} else {
|
|
file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] Condition not met: skipping\n", FILE_APPEND);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] CRITICAL ERROR: " . $e->getMessage() . "\n", FILE_APPEND);
|
|
} |