query("SELECT * FROM settings")->fetchAll(); $settings = []; foreach ($settings_raw as $s) { $settings[$s['setting_key']] = $s['setting_value']; } $recipient = $settings['report_recipient_email'] ?? getenv('MAIL_TO'); if (empty($recipient)) { die("Error: No recipient email configured in settings or environment.\n"); } // Calculate last month date range $start_date = date('Y-m-01', strtotime('first day of last month')); $end_date = date('Y-m-t', strtotime('last month')); $month_name = date('F Y', strtotime('last month')); // Fetch statistics for the period $stats = []; // 1. Total Donations $stmt = $pdo->prepare("SELECT SUM(amount) as total, COUNT(*) as count FROM donations WHERE status = 'paid' AND created_at BETWEEN ? AND ?"); $stmt->execute([$start_date . ' 00:00:00', $end_date . ' 23:59:59']); $donation_stats = $stmt->fetch(PDO::FETCH_ASSOC); $stats['total_amount'] = $donation_stats['total'] ?? 0; $stats['total_count'] = $donation_stats['count'] ?? 0; // 2. Donations by Category $stmt = $pdo->prepare(" SELECT c.name, SUM(d.amount) as total FROM donations d JOIN cases cs ON d.case_id = cs.id JOIN categories c ON cs.category_id = c.id WHERE d.status = 'paid' AND d.created_at BETWEEN ? AND ? GROUP BY c.id ORDER BY total DESC "); $stmt->execute([$start_date . ' 00:00:00', $end_date . ' 23:59:59']); $stats['by_category'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // 3. Top Cases $stmt = $pdo->prepare(" SELECT cs.title, SUM(d.amount) as total FROM donations d JOIN cases cs ON d.case_id = cs.id WHERE d.status = 'paid' AND d.created_at BETWEEN ? AND ? GROUP BY cs.id ORDER BY total DESC LIMIT 5 "); $stmt->execute([$start_date . ' 00:00:00', $end_date . ' 23:59:59']); $stats['top_cases'] = $stmt->fetchAll(PDO::FETCH_ASSOC); // Build HTML Email $html = "

Monthly Financial Summary

Period: {$month_name}

Total Donations

$" . number_format($stats['total_amount'], 2) . "

Donation Count

{$stats['total_count']}

Donations by Category

"; foreach ($stats['by_category'] as $cat) { $html .= ""; } $html .= "
Category Amount
" . htmlspecialchars($cat['name']) . "$" . number_format($cat['total'], 2) . "

Top 5 Performing Cases

"; foreach ($stats['top_cases'] as $case) { $html .= ""; } $html .= "
Case Title Amount
" . htmlspecialchars($case['title']) . "$" . number_format($case['total'], 2) . "
"; // Send Email $subject = "Financial Summary Report - {$month_name}"; $plain_text = "Monthly Financial Summary for {$month_name}\n\nTotal Amount: $" . number_format($stats['total_amount'], 2) . "\nTotal Count: {$stats['total_count']}\n\nPlease view the HTML version for detailed breakdown."; $res = MailService::sendMail($recipient, $subject, $html, $plain_text); if (!empty($res['success'])) { echo "Report sent successfully to {$recipient} for {$month_name}.\n"; } else { echo "Failed to send report: " . ($res['error'] ?? 'Unknown error') . "\n"; }