update trip report
This commit is contained in:
@ -23,7 +23,7 @@ class ReportsController extends Controller
|
|||||||
{
|
{
|
||||||
public function view_report_vehicle_trips(Request $req)
|
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);
|
$listNopol = DB::select($q);
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
@ -58,78 +58,98 @@ class ReportsController extends Controller
|
|||||||
// $from_date = 1756054800;
|
// $from_date = 1756054800;
|
||||||
// $to_date = 1756745940;
|
// $to_date = 1756745940;
|
||||||
|
|
||||||
|
// get month year
|
||||||
|
$date = Carbon::createFromTimestamp($from_date);
|
||||||
|
$yymm = $date->format('ym');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$list = DB::select("WITH
|
// $list = DB::select("WITH
|
||||||
gaps AS (
|
// gaps AS (
|
||||||
SELECT
|
// SELECT
|
||||||
-- previous gap since previous row > 1 hour (3600s)
|
// -- previous gap since previous row > 1 hour (3600s)
|
||||||
CASE
|
// CASE
|
||||||
WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600
|
// WHEN (crt_d - LAG(crt_d, 1, NULL) OVER (PARTITION BY vhc_id ORDER BY crt_d)) > 3600
|
||||||
THEN 1 ELSE 0
|
// THEN 1 ELSE 0
|
||||||
END AS isStop,
|
// END AS isStop,
|
||||||
t.*
|
// t.*
|
||||||
FROM t_gps_tracks t
|
// FROM tracks_2509 t
|
||||||
WHERE
|
// WHERE
|
||||||
t.latitude IS NOT NULL
|
// t.latitude IS NOT NULL
|
||||||
AND t.longitude IS NOT NULL
|
// AND t.longitude IS NOT NULL
|
||||||
AND t.action = 'location'
|
// AND t.action = 'location'
|
||||||
AND t.crt_d BETWEEN ? AND ?
|
// AND t.crt_d BETWEEN ? AND ?
|
||||||
)
|
// )
|
||||||
, trips AS (
|
// , trips AS (
|
||||||
SELECT
|
// SELECT
|
||||||
-- mark the start of a trip when ignition=4 and previous ignition <> 4
|
// -- mark the start of a trip when ignition=4 and previous ignition <> 4
|
||||||
CASE
|
// CASE
|
||||||
WHEN ignition = 4
|
// WHEN ignition = 4
|
||||||
AND LAG(ignition, 1, 0) OVER (PARTITION BY vhc_id ORDER BY crt_d) <> 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
|
// or LAG(isStop, 1, 0) over (PARTITION BY vhc_id ORDER BY crt_d) = 1
|
||||||
THEN 1 ELSE 0
|
// THEN 1 ELSE 0
|
||||||
END AS trip_start,
|
// END AS trip_start,
|
||||||
g.*
|
// g.*
|
||||||
FROM gaps g
|
// FROM gaps g
|
||||||
)
|
// )
|
||||||
, numbered AS (
|
// , numbered AS (
|
||||||
SELECT
|
// SELECT
|
||||||
*,
|
// *,
|
||||||
-- assign a trip_id by cumulative sum of trip_start
|
// -- 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
|
// 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
|
FROM trips
|
||||||
where
|
WHERE start BETWEEN ? AND ?
|
||||||
ignition = 4
|
GROUP BY id
|
||||||
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
|
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT
|
||||||
*,
|
t.*,
|
||||||
SUM(mileage) OVER (PARTITION BY agg.id) AS total_mileage,
|
tm.total_mileage, total_trip,
|
||||||
COUNT(trip_id) OVER (PARTITION BY agg.id) AS total_trip,
|
ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY t.start) AS trip_id
|
||||||
tvd.pool_code, tvd.dc_code
|
FROM trips t
|
||||||
FROM agg agg
|
JOIN TotalMileage tm ON t.id = tm.id
|
||||||
join t_vehicles_detail tvd on tvd.vid = agg.id
|
WHERE
|
||||||
ORDER BY agg.id, trip_id
|
t.start BETWEEN ? AND ?
|
||||||
", [$from_date, $to_date, $vid, $vid]);
|
and if(? , t.id = ? , 1=1)
|
||||||
|
", [$from_date, $to_date, $from_date, $to_date, $vid, $vid]);
|
||||||
|
|
||||||
// // RETURN 1 - LIST
|
// // RETURN 1 - LIST
|
||||||
// if($req->type != 'report'){
|
// if($req->type != 'report'){
|
||||||
|
|||||||
Reference in New Issue
Block a user