Files
Siro/backend/serviceapp/getRidesStatic.php
2026-06-16 01:17:29 +03:00

69 lines
2.1 KiB
PHP

<?php
require_once __DIR__ . '/../connect.php';
// 1. استقبال تواريخ البداية والنهاية بدلاً من الشهر والسنة فقط
if (isset($_POST['start_date']) && isset($_POST['end_date'])) {
// إذا أرسل التطبيق تواريخ محددة (الوضع الجديد)
$start_date = $_POST['start_date']; // Format: YYYY-MM-DD
$end_date = $_POST['end_date']; // Format: YYYY-MM-DD
} else {
// (Fallback) إذا لم يرسل تواريخ، نستخدم منطق الشهر والسنة القديم
$current_month = isset($_POST['month']) ? $_POST['month'] : date('m');
$current_year = isset($_POST['year']) ? $_POST['year'] : date('Y');
// التأكد من أن صيغة الشهر خانتين
$current_month = str_pad($current_month, 2, "0", STR_PAD_LEFT);
$start_date = date('Y-m-d', strtotime($current_year . '-' . $current_month . '-01'));
$end_date = date('Y-m-t', strtotime($start_date));
}
$end_date_full = $end_date . ' 23:59:59';
$sql = "
WITH RECURSIVE date_series AS (
SELECT :start_date AS date
UNION ALL
SELECT DATE_ADD(date, INTERVAL 1 DAY)
FROM date_series
WHERE date < :end_date
)
SELECT
date_series.date AS day,
COALESCE(SUM(ride.status = 'Finished'), 0) AS totalRides,
(SELECT COUNT(*) FROM ride
WHERE ride.created_at >= :start_date_total
AND ride.created_at <= :end_date_total
AND ride.status = 'Finished') AS totalMonthly
FROM
date_series
LEFT JOIN
ride ON DATE(ride.created_at) = date_series.date
AND ride.status = 'Finished'
WHERE
date_series.date >= :start_date_where
AND date_series.date <= :end_date_where
GROUP BY
date_series.date
ORDER BY
date_series.date ASC";
$stmt = $con->prepare($sql);
$stmt->execute([
':start_date' => $start_date,
':end_date' => $end_date,
':start_date_total' => $start_date,
':end_date_total' => $end_date_full,
':start_date_where' => $start_date,
':end_date_where' => $end_date
]);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($data) {
jsonSuccess($data);
} else {
jsonError("No data found");
}
?>