update feature

This commit is contained in:
Pringgosutono
2025-08-29 20:43:56 +07:00
parent f906c80835
commit 64d7ce4f65
8 changed files with 1101 additions and 214 deletions

View File

@ -0,0 +1,34 @@
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Validator;
use Auth;
use App\Responses;
use App\Helper;
use App\Models\Vehicles;
use App\Models\Devices;
use App\Models\VehiclesDetail;
use App\Models\Users;
class ReportsController extends Controller
{
public function view_report_vehicle_trips(Request $req)
{
$data = [
// 'lanes' => ConfRates::getLanesActive(),
// 'provs' => Region::listProv(),
// 'vendors' => Users::getUsersActiveByRole(Users::ROLE_VENDOR),
// 'truck_types' => ConfTruckTypes::listTruckTypes(ConfTruckTypes::IS_ACTIVE),
];
return view('menu_v1.reports.vehicle_trips', $data);
}
}

View File

@ -143,8 +143,8 @@ class TrackController extends Controller
$filter["start_date"] = $req->start_date;
$filter["end_date"] = $req->end_date;
$filter["start_date"] = strtotime(gmdate("Y-m-d H:i:s", $req->start_date));
$filter["end_date"] = strtotime(gmdate("Y-m-d H:i:s", $req->end_date));
// $filter["start_date"] = strtotime(gmdate("Y-m-d H:i:s", $req->start_date));
// $filter["end_date"] = strtotime(gmdate("Y-m-d H:i:s", $req->end_date));
$filter["limit"] = 100000;
} else {

View File

@ -436,85 +436,88 @@ class Tracks extends Model
$query .= " GROUP BY v.id ORDER BY tr.crt_d DESC LIMIT 500";
$list = DB::select($query, $params);
// dd($list);
// Post-processing per row
foreach ($list as $_list) {
$_list->vhc_sum_milleage =
optional(
DB::select(
"SELECT SUM(pre_milleage) as vhc_sum_milleage FROM " .
self::T_TRACKS .
" WHERE vhc_id = ?",
[$_list->vid]
)
)[0]->vhc_sum_milleage ?? "";
// foreach ($list as $_list) {
// $_list->vhc_sum_milleage =
// optional(
// DB::select(
// "SELECT SUM(pre_milleage) as vhc_sum_milleage FROM " .
// self::T_TRACKS .
// " WHERE vhc_id = ?",
// [$_list->vid]
// )
// )[0]->vhc_sum_milleage ?? "";
$_list->lst_idle_at =
optional(
DB::select(
"SELECT crt_s FROM " .
self::T_TRACKS .
" WHERE stts_engine = ? AND vhc_id = ? AND crt_s >= (
SELECT crt_s FROM " .
self::T_TRACKS .
" WHERE stts_engine IN (?, ?) AND vhc_id = ? AND crt_s <= ? ORDER BY id DESC LIMIT 1
) ORDER BY id ASC LIMIT 1",
[
self::STTS_EN_IDLING,
$_list->vid,
self::STTS_EN_STOPING,
self::STTS_EN_MOVING,
$_list->vid,
$_list->lst_loc_crt_s,
]
)
)[0]->crt_s ?? "";
// $_list->lst_idle_at =
// optional(
// DB::select(
// "SELECT crt_s FROM " .
// self::T_TRACKS .
// " WHERE stts_engine = ? AND vhc_id = ? AND crt_s >= (
// SELECT crt_s FROM " .
// self::T_TRACKS .
// " WHERE stts_engine IN (?, ?) AND vhc_id = ? AND crt_s <= ? ORDER BY id DESC LIMIT 1
// ) ORDER BY id ASC LIMIT 1",
// [
// self::STTS_EN_IDLING,
// $_list->vid,
// self::STTS_EN_STOPING,
// self::STTS_EN_MOVING,
// $_list->vid,
// $_list->lst_loc_crt_s,
// ]
// )
// )[0]->crt_s ?? "";
$_list->lst_stop_at =
optional(
DB::select(
"SELECT crt_s FROM " .
self::T_TRACKS .
" WHERE stts_engine = ? AND vhc_id = ? AND crt_s >= (
SELECT crt_s FROM " .
self::T_TRACKS .
" WHERE stts_engine IN (?, ?) AND vhc_id = ? AND crt_s <= ? ORDER BY id DESC LIMIT 1
) ORDER BY id ASC LIMIT 1",
[
self::STTS_EN_STOPING,
$_list->vid,
self::STTS_EN_IDLING,
self::STTS_EN_MOVING,
$_list->vid,
$_list->lst_loc_crt_s,
]
)
)[0]->crt_s ?? "";
// $_list->lst_stop_at =
// optional(
// DB::select(
// "SELECT crt_s FROM " .
// self::T_TRACKS .
// " WHERE stts_engine = ? AND vhc_id = ? AND crt_s >= (
// SELECT crt_s FROM " .
// self::T_TRACKS .
// " WHERE stts_engine IN (?, ?) AND vhc_id = ? AND crt_s <= ? ORDER BY id DESC LIMIT 1
// ) ORDER BY id ASC LIMIT 1",
// [
// self::STTS_EN_STOPING,
// $_list->vid,
// self::STTS_EN_IDLING,
// self::STTS_EN_MOVING,
// $_list->vid,
// $_list->lst_loc_crt_s,
// ]
// )
// )[0]->crt_s ?? "";
$_list->lst_heartbeat =
optional(
DB::select(
"SELECT COUNT(id) as lst_heartbeat FROM " .
self::T_TRACKS .
" WHERE vhc_id = ? AND action = 'heartbeat' AND crt BETWEEN ? AND ? LIMIT 1",
[$_list->vid, $now - 600, $now]
)
)[0]->lst_heartbeat ?? "";
}
// $_list->lst_heartbeat =
// optional(
// DB::select(
// "SELECT COUNT(id) as lst_heartbeat FROM " .
// self::T_TRACKS .
// " WHERE vhc_id = ? AND action = 'heartbeat' AND crt BETWEEN ? AND ? LIMIT 1",
// [$_list->vid, $now - 600, $now]
// )
// )[0]->lst_heartbeat ?? "";
// }
return $list;
}
public static function lastMoveTracks($vid, $filter = [])
{
// dd($vid, $filter);
$now = time();
$params = [];
$query = "SELECT";
$query .= " v.id as vid,v.device_id,v.nopol1,v.nopol2,v.nopol3";
$query .= " ,tr.id as master_id,tr.latitude,tr.longitude,tr.speed,tr.orientation";
// ,(tr.crt + ( 7 * 3600 )) AS lst_loc_crt
// ,(tr.crt_d + ( 7 * 3600 )) AS lst_loc_crt_d
$query .= "
,(tr.crt + ( 7 * 3600 )) AS lst_loc_crt
,(tr.crt_d + ( 7 * 3600 )) AS lst_loc_crt_d
,tr.crt AS lst_loc_crt
,tr.crt_d AS lst_loc_crt_d
,tr.crt_s AS lst_loc_crt_s";
$query .= " ,tr.ignition,tr.stts_engine";
$query .= " ,tr.pre_milleage,tr.sum_milleage,tr.vhc_milleage,v.sum_milleage AS vhc_sum_milleage_1";
@ -563,61 +566,88 @@ class Tracks extends Model
$query .= " ;";
$rawTracks = DB::select($query, $params);
$rawTracks = collect($rawTracks);
// dd($rawTracks->toJson(JSON_PRETTY_PRINT));
// dd($rawTracks);
$filtered = collect();
$zeroBlock = collect();
$tempBlock = collect();
// // filter
// $filtered = collect();
// $zeroBlock = collect();
// $tempBlock = collect();
$lastIndex = $rawTracks->count() - 1;
// $lastIndex = $rawTracks->count() - 1;
foreach ($rawTracks as $i => $track) {
$isZero = $track->speed == 0;
// foreach ($rawTracks as $i => $track) {
// $isZero = $track->speed == 0;
if ($i === $lastIndex) {
if ($isZero) {
$zeroBlock->push($track);
} else {
if ($zeroBlock->isNotEmpty()) {
$tempBlock = $zeroBlock;
$tempBlock->push($track);
// if ($i === $lastIndex) {
// if ($isZero) {
// $zeroBlock->push($track);
// } else {
// if ($zeroBlock->isNotEmpty()) {
// $tempBlock = $zeroBlock;
// $tempBlock->push($track);
$zerosOnly = $tempBlock->filter(fn($t) => $t->speed == 0);
// $zerosOnly = $tempBlock->filter(fn($t) => $t->speed == 0);
if ($zerosOnly->count() === 1) {
$filtered->push($zerosOnly->first());
} elseif ($zerosOnly->count() > 1) {
$filtered->push($zerosOnly->first());
$filtered->push($zerosOnly->last());
}
} else {
$filtered->push($track);
}
}
continue;
}
// if ($zerosOnly->count() === 1) {
// $filtered->push($zerosOnly->first());
// } elseif ($zerosOnly->count() > 1) {
// $filtered->push($zerosOnly->first());
// $filtered->push($zerosOnly->last());
// }
// } else {
// $filtered->push($track);
// }
// }
// continue;
// }
// //
// if ($isZero) {
// $zeroBlock->push($track);
// } else {
// if ($zeroBlock->isNotEmpty()) {
// $filtered->push($zeroBlock->first());
// $zeroBlock = collect();
// }
// $filtered->push($track);
// }
// }
if ($isZero) {
$zeroBlock->push($track);
} else {
if ($zeroBlock->isNotEmpty()) {
$filtered->push($zeroBlock->first());
$zeroBlock = collect();
}
$filtered->push($track);
}
}
// if ($zeroBlock->isNotEmpty() && $rawTracks->last()->speed == 0) {
// if ($zeroBlock->count() === 1) {
// $filtered->push($zeroBlock->first());
// } else {
// $filtered->push($zeroBlock->first());
// $filtered->push($zeroBlock->last());
// }
// }
// grouping by trip
$groupTrip = [];
$temp = [];
// loop from top to bottom
foreach ($rawTracks as $item) {
if ($item->ignition == 4) {
// collect data when ignition is 4
$temp[] = $item;
} else {
// if ignition != 4, save group if exists then reset
if (!empty($temp)) {
$groupTrip[] = $temp;
$temp = [];
}
}
}
// add last collected group if any
if (!empty($temp)) {
$groupTrip[] = $temp;
}
// dd($groupTrip);
if ($zeroBlock->isNotEmpty() && $rawTracks->last()->speed == 0) {
if ($zeroBlock->count() === 1) {
$filtered->push($zeroBlock->first());
} else {
$filtered->push($zeroBlock->first());
$filtered->push($zeroBlock->last());
}
}
// return $filtered->take(500);
return $filtered;
return $groupTrip;
}
public static function nearestInCircle($lat, $lng, $rad, $filter = [])