update trip report

This commit is contained in:
Pringgosutono
2025-10-08 16:01:55 +07:00
parent 136db1a158
commit 18897186e0

View File

@ -23,7 +23,7 @@ class ReportsController extends Controller
{
public function view_report_vehicle_trips(Request $req)
{
$q = "select id, nopol1 from t_vehicles WHERE dlt is null order by nopol1";
$q = "SELECT id, nopol1 from t_vehicles WHERE dlt is null order by nopol1";
$listNopol = DB::select($q);
$data = [
@ -58,78 +58,98 @@ class ReportsController extends Controller
// $from_date = 1756054800;
// $to_date = 1756745940;
// get month year
$date = Carbon::createFromTimestamp($from_date);
$yymm = $date->format('ym');
try {
$list = DB::select("WITH
gaps AS (
SELECT
-- previous gap since previous row > 1 hour (3600s)
CASE
WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600
THEN 1 ELSE 0
END AS isStop,
t.*
FROM t_gps_tracks t
WHERE
t.latitude IS NOT NULL
AND t.longitude IS NOT NULL
AND t.action = 'location'
AND t.crt_d BETWEEN ? AND ?
)
, trips AS (
SELECT
-- mark the start of a trip when ignition=4 and previous ignition <> 4
CASE
WHEN ignition = 4
AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4
or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1
THEN 1 ELSE 0
END AS trip_start,
g.*
FROM gaps g
)
, numbered AS (
SELECT
*,
-- assign a trip_id by cumulative sum of trip_start
SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id
// $list = DB::select("WITH
// gaps AS (
// SELECT
// -- previous gap since previous row > 1 hour (3600s)
// CASE
// WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600
// THEN 1 ELSE 0
// END AS isStop,
// t.*
// FROM tracks_2509 t
// WHERE
// t.latitude IS NOT NULL
// AND t.longitude IS NOT NULL
// AND t.action = 'location'
// AND t.crt_d BETWEEN ? AND ?
// )
// , trips AS (
// SELECT
// -- mark the start of a trip when ignition=4 and previous ignition <> 4
// CASE
// WHEN ignition = 4
// AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 4
// or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1
// THEN 1 ELSE 0
// END AS trip_start,
// g.*
// FROM gaps g
// )
// , numbered AS (
// SELECT
// *,
// -- assign a trip_id by cumulative sum of trip_start
// SUM(trip_start) OVER (PARTITION BY vhc_id ORDER BY crt_d) AS trip_id
// FROM trips
// where
// ignition = 4
// and isStop = 0
// ),
// agg AS (
// SELECT
// COUNT(*) AS row_count,
// v.name,
// v.nopol1,
// vhc_id,
// -- trip_id,
// ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
// SUM(pre_milleage) AS mileage,
// MIN(a.crt_d) AS start,
// MAX(a.crt_d) AS finish,
// MIN(a.vhc_milleage) AS startMileage,
// MAX(a.vhc_milleage) AS finishMileage,
// (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MIN(a.id) LIMIT 1) AS startLoc,
// (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MAX(a.id) LIMIT 1) AS finishLoc
// FROM t_vehicles v
// LEFT JOIN numbered a ON a.vhc_id = v.id
// WHERE
// v.dlt is null and trip_id != 0
// and if(? , v.id = ? , 1=1)
// GROUP BY v.id, a.trip_id
// HAVING COUNT(*) > 1
// )
// SELECT
// *,
// SUM(mileage) OVER (PARTITION BY agg.id) AS total_mileage,
// COUNT(trip_id) OVER (PARTITION BY agg.id) AS total_trip,
// tvd.pool_code, tvd.dc_code
// FROM agg agg
// join t_vehicles_detail tvd on tvd.vid = agg.vhc_id
// ORDER BY agg.id, trip_id
// ", [$from_date, $to_date, $vid, $vid]);
$list = DB::select("WITH TotalMileage AS (
SELECT id, SUM(mileage) AS total_mileage, count(*) total_trip
FROM trips
where
ignition = 4
and isStop = 0
),
agg AS (
SELECT
COUNT(*) AS row_count,
v.id,
v.name,
v.nopol1,
vhc_id,
-- trip_id,
ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
SUM(pre_milleage) AS mileage,
MIN(a.crt_d) AS start,
MAX(a.crt_d) AS finish,
MIN(a.vhc_milleage) AS startMileage,
MAX(a.vhc_milleage) AS finishMileage,
(SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MIN(a.id) LIMIT 1) AS startLoc,
(SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MAX(a.id) LIMIT 1) AS finishLoc
FROM t_vehicles v
LEFT JOIN numbered a ON a.vhc_id = v.id
WHERE
v.dlt is null and trip_id != 0
and if(? , v.id = ? , 1=1)
GROUP BY v.id, a.trip_id
HAVING COUNT(*) > 1
WHERE start BETWEEN ? AND ?
GROUP BY id
)
SELECT
*,
SUM(mileage) OVER (PARTITION BY agg.id) AS total_mileage,
COUNT(trip_id) OVER (PARTITION BY agg.id) AS total_trip,
tvd.pool_code, tvd.dc_code
FROM agg agg
join t_vehicles_detail tvd on tvd.vid = agg.id
ORDER BY agg.id, trip_id
", [$from_date, $to_date, $vid, $vid]);
t.*,
tm.total_mileage, total_trip,
ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY t.start) AS trip_id
FROM trips t
JOIN TotalMileage tm ON t.id = tm.id
WHERE
t.start BETWEEN ? AND ?
and if(? , t.id = ? , 1=1)
", [$from_date, $to_date, $from_date, $to_date, $vid, $vid]);
// // RETURN 1 - LIST
// if($req->type != 'report'){