require_once __DIR__ . '/FirebaseHelper.php'; include __DIR__ . '/connection.php'; header("Content-Type: text/plain"); // Calculate the date 5 days from now $targetDate = date('d-m-Y', strtotime('+5 days')); echo "Checking for items expiring on: " . $targetDate . "\n"; try { $fcm = new FirebaseHelper(); // 1. Get all users to notify (Since tables don't link to users, we notify all admins/users) $users = []; $sqlUsers = "SELECT mobile, fcm_token FROM login WHERE fcm_token IS NOT NULL AND fcm_token != ''"; $resultUsers = $conn->query($sqlUsers); if ($resultUsers->num_rows > 0) { while ($row = $resultUsers->fetch_assoc()) { $users[] = $row['fcm_token']; } } if (empty($users)) { echo "No users with FCM tokens found.\n"; exit; } // 2. Check Bills $sqlBills = "SELECT * FROM bills WHERE expiry_date = '$targetDate'"; $resultBills = $conn->query($sqlBills); if ($resultBills->num_rows > 0) { while ($bill = $resultBills->fetch_assoc()) { $title = "Bill Expiry Alert"; $body = "Bill '{$bill['bill_name']}' of amount {$bill['amount']} is expiring in 5 days ({$targetDate})."; echo "Found Bill: {$bill['bill_name']}\n"; sendToAll($fcm, $users, $title, $body); } } else { echo "No bills found expiring on $targetDate.\n"; } // 3. Check Dashboard (Domains/Items) $sqlDashboard = "SELECT * FROM dashboard WHERE expiry_date = '$targetDate'"; $resultDashboard = $conn->query($sqlDashboard); if ($resultDashboard->num_rows > 0) { while ($item = $resultDashboard->fetch_assoc()) { $title = "Renewal Alert"; $body = "{$item['type']} '{$item['domain_name']}' is expiring in 5 days ({$targetDate})."; echo "Found Dashboard Item: {$item['domain_name']}\n"; sendToAll($fcm, $users, $title, $body); } } else { echo "No dashboard items found expiring on $targetDate.\n"; } } catch (Exception $e) { echo "Error: " . $e->getMessage() . "\n"; } function sendToAll($fcm, $tokens, $title, $body) { foreach ($tokens as $token) { try { $fcm->sendNotification($token, $title, $body); } catch (Exception $e) { // Check for invalid token errors and maybe remove them from DB if needed // For now, just continue } } } $conn->close(); ?>