150 lines
5.3 KiB
PHP
150 lines
5.3 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../includes/functions.php';
|
|
require_once __DIR__ . '/../includes/WablasService.php';
|
|
|
|
session_write_close();
|
|
header("Content-Type: application/json");
|
|
|
|
try {
|
|
$settings = get_company_settings();
|
|
|
|
if (empty($settings['whatsapp_report_enabled']) || empty($settings['whatsapp_report_number']) || empty($settings['whatsapp_report_time'])) {
|
|
echo json_encode(['status' => 'skipped', 'reason' => 'Not enabled or missing settings']);
|
|
exit;
|
|
}
|
|
|
|
$timezone = $settings['timezone'] ?? 'UTC';
|
|
date_default_timezone_set($timezone);
|
|
|
|
$currentDate = date('Y-m-d');
|
|
$currentTime = date('H:i:s');
|
|
$reportTime = $settings['whatsapp_report_time'];
|
|
|
|
// Check if it's past the report time
|
|
if ($currentTime < $reportTime) {
|
|
echo json_encode(['status' => 'skipped', 'reason' => 'Not time yet', 'time' => $currentTime, 'target' => $reportTime]);
|
|
exit;
|
|
}
|
|
|
|
$lastReportFile = __DIR__ . '/../storage/last_daily_report.txt';
|
|
if (!is_dir(dirname($lastReportFile))) {
|
|
mkdir(dirname($lastReportFile), 0755, true);
|
|
}
|
|
|
|
$lastReportDate = file_exists($lastReportFile) ? trim(file_get_contents($lastReportFile)) : '';
|
|
|
|
if ($lastReportDate === $currentDate) {
|
|
echo json_encode(['status' => 'skipped', 'reason' => 'Already sent today']);
|
|
exit;
|
|
}
|
|
|
|
// GENERATE REPORT
|
|
$pdo = db();
|
|
|
|
// Get all completed orders for today
|
|
$stmt = $pdo->prepare(
|
|
"SELECT
|
|
o.id, o.total_amount as total, o.payment_type_id, o.user_id, o.outlet_id,
|
|
pt.name as payment_type_name,
|
|
u.username, u.full_name,
|
|
outl.name as outlet_name
|
|
FROM orders o
|
|
LEFT JOIN payment_types pt ON o.payment_type_id = pt.id
|
|
LEFT JOIN users u ON o.user_id = u.id
|
|
LEFT JOIN outlets outl ON o.outlet_id = outl.id
|
|
WHERE DATE(o.created_at) = ? AND o.status = 'completed'"
|
|
);
|
|
$stmt->execute([$currentDate]);
|
|
$orders = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$totalOrders = count($orders);
|
|
$totalAmount = 0;
|
|
|
|
$outletsData = [];
|
|
|
|
foreach ($orders as $order) {
|
|
$totalAmount += $order['total'];
|
|
|
|
$outletName = $order['outlet_name'] ?? 'Unknown Outlet';
|
|
$staffName = !empty($order['full_name']) ? $order['full_name'] : ($order['username'] ?? 'Unknown Staff');
|
|
$paymentType = $order['payment_type_name'] ?? 'Unknown Payment';
|
|
$amount = (float)$order['total'];
|
|
|
|
if (!isset($outletsData[$outletName])) {
|
|
$outletsData[$outletName] = [
|
|
'total' => 0,
|
|
'staff' => [],
|
|
'payments' => []
|
|
];
|
|
}
|
|
|
|
$outletsData[$outletName]['total'] += $amount;
|
|
|
|
if (!isset($outletsData[$outletName]['staff'][$staffName])) {
|
|
$outletsData[$outletName]['staff'][$staffName] = 0;
|
|
}
|
|
$outletsData[$outletName]['staff'][$staffName] += $amount;
|
|
|
|
if (!isset($outletsData[$outletName]['payments'][$paymentType])) {
|
|
$outletsData[$outletName]['payments'][$paymentType] = 0;
|
|
}
|
|
$outletsData[$outletName]['payments'][$paymentType] += $amount;
|
|
}
|
|
|
|
$companyName = $settings['company_name'] ?? 'Restaurant';
|
|
$currency = $settings['currency_symbol'] ?? '$';
|
|
|
|
$message = "๐ *Daily Summary Report* ๐\n";
|
|
$message .= "๐ข *" . $companyName . "*\n";
|
|
$message .= "๐
Date: " . $currentDate . "\n";
|
|
$message .= "--------------------------------\n";
|
|
$message .= "๐ Total Orders: " . $totalOrders . "\n";
|
|
$message .= "๐ฐ Total Revenue: " . format_currency($totalAmount) . "\n\n";
|
|
|
|
if (empty($outletsData)) {
|
|
$message .= "No completed orders today.\n";
|
|
} else {
|
|
foreach ($outletsData as $outletName => $data) {
|
|
$message .= "๐ช *" . $outletName . "*\n";
|
|
$message .= " Total: " . format_currency($data['total']) . "\n";
|
|
|
|
$message .= " ๐งโ๐ณ *Staff Breakdown:*
|
|
";
|
|
foreach ($data['staff'] as $staff => $amt) {
|
|
$message .= " - " . $staff . ": " . format_currency($amt) . "\n";
|
|
}
|
|
|
|
$message .= " ๐ณ *Payment Breakdown:*
|
|
";
|
|
foreach ($data['payments'] as $pt => $amt) {
|
|
$message .= " - " . $pt . ": " . format_currency($amt) . "\n";
|
|
}
|
|
$message .= "\n";
|
|
}
|
|
}
|
|
|
|
$message .= "--------------------------------\n";
|
|
$message .= "Generated automatically at " . date('H:i:s');
|
|
|
|
// Send via Wablas
|
|
// Write immediately to prevent duplicate triggers
|
|
file_put_contents($lastReportFile, $currentDate);
|
|
|
|
// Send via Wablas
|
|
$wablasService = new WablasService($pdo);
|
|
$result = $wablasService->sendMessage($settings['whatsapp_report_number'], $message);
|
|
|
|
if ($result['success']) {
|
|
echo json_encode(['status' => 'success', 'message' => 'Report sent']);
|
|
} else {
|
|
// Revert on failure
|
|
if (file_exists($lastReportFile)) { unlink($lastReportFile); }
|
|
echo json_encode(['status' => 'error', 'message' => $result['message']]);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
error_log("Daily Report Cron Error: " . $e->getMessage());
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|