114 lines
2.7 KiB
PHP
114 lines
2.7 KiB
PHP
<?php
|
|
include "../connect.php";
|
|
$current_month = date('m');
|
|
$current_year = date('Y');
|
|
// Get the first and last days of the current month.
|
|
$first_day_of_month = date('Y-m-d', strtotime($current_year . '-' . $current_month . '-01'));
|
|
$last_day_of_month = date('Y-m-t', strtotime($first_day_of_month));
|
|
|
|
// Create a SQL query to select the total duration for the driver for each day in the current month.
|
|
$sql = "WITH
|
|
RECURSIVE date_series AS(
|
|
SELECT
|
|
'$first_day_of_month' AS DATE
|
|
UNION ALL
|
|
SELECT
|
|
DATE_ADD(DATE, INTERVAL 1 DAY)
|
|
FROM
|
|
date_series
|
|
WHERE
|
|
DATE < LEAST(
|
|
CURDATE(), DATE_SUB('$last_day_of_month', INTERVAL 1 DAY))
|
|
)
|
|
SELECT
|
|
date_series.date AS day,
|
|
(
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
`driver`
|
|
) AS totalDrivers,
|
|
(
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
driver
|
|
WHERE
|
|
DATE(driver.created_at) = date_series.date
|
|
) AS dailyTotalDrivers,
|
|
(
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
notesForDriverService
|
|
WHERE
|
|
DATE(
|
|
notesForDriverService.createdAt
|
|
) = date_series.date
|
|
) AS dailyTotalCallingDrivers,
|
|
(
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
notesForDriverService n
|
|
JOIN driver d ON
|
|
n.phone = d.phone
|
|
WHERE
|
|
DATE(n.createdAt) = date_series.date
|
|
) AS dailyMatchingNotes,
|
|
(
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
driver
|
|
WHERE
|
|
driver.created_at >= '$first_day_of_month' AND driver.created_at < '$last_day_of_month'
|
|
) AS totalMonthlyDrivers,
|
|
(
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
notesForDriverService
|
|
WHERE
|
|
notesForDriverService.createdAt >= '$first_day_of_month' AND notesForDriverService.createdAt < '$last_day_of_month'
|
|
) AS totalMonthlyCallingDrivers,
|
|
(
|
|
SELECT
|
|
COUNT(*)
|
|
FROM
|
|
notesForDriverService n
|
|
JOIN driver d ON
|
|
n.phone = d.phone
|
|
WHERE
|
|
n.createdAt >= '$first_day_of_month' AND n.createdAt < '$last_day_of_month'
|
|
) AS totalMonthlyMatchingNotes
|
|
FROM
|
|
date_series
|
|
LEFT JOIN driver ON DATE(driver.created_at) = date_series.date
|
|
LEFT JOIN notesForDriverService ON DATE(
|
|
notesForDriverService.createdAt
|
|
) = date_series.date AND notesForDriverService.phone = driver.phone
|
|
WHERE
|
|
date_series.date >= '$first_day_of_month' AND date_series.date <= LEAST(
|
|
CURDATE(), DATE_SUB('$last_day_of_month', INTERVAL 1 DAY))
|
|
GROUP BY
|
|
date_series.date
|
|
ORDER BY
|
|
date_series.date
|
|
DESC
|
|
;
|
|
|
|
";
|
|
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute();
|
|
$car_locations = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($car_locations) {
|
|
// Print the car location data as JSON
|
|
printSuccess($data = $car_locations);
|
|
} else {
|
|
// Print a failure message
|
|
printFailure($message = "No car locations found");
|
|
}
|
|
?>
|