From 0359421f3e5e71102060b31142195992f1bc297a Mon Sep 17 00:00:00 2001 From: wayanrivan Date: Fri, 10 Jul 2026 14:48:03 +0700 Subject: [PATCH] update --- app/Http/Controllers/ReportsController.php | 140 +++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 6cbc105..388e0fc 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -291,6 +291,146 @@ class ReportsController extends Controller // return Responses::json(Responses::SERVER_ERROR, 'An error occurred while generating the report.', (object)[]); } } + // public function api_report_vehicle_trips_list(Request $req) + // { + // $rules = [ + // 'type' => 'nullable|in:report,list', // enum "report", "list". nullable default "list" + // ]; + + // $isValidInput = Validator::make($req->all(), $rules); + // if (!$isValidInput->passes()) { + // $apiResp = Responses::bad_input($isValidInput->messages()->first()); + // return new Response($apiResp, $apiResp["meta"]["code"]); + // } + + // $from_date = $req->input('from_date') - Helper::TIMEFIX; + // $to_date = $req->input('to_date') - Helper::TIMEFIX; + // $vid = $req->input('vid'); + // $vt = $req->input('vt'); + // $poolcode = $req->input('poolcode'); + + // $date = Carbon::createFromTimestamp($from_date); + // $yymm = $date->format('ym'); + + // try { + // // ========================================================= + // // STEP 1 - Ambil data trips (TANPA gps lat/long dulu) + // // ========================================================= + // $sql = + // "WITH TotalMileage AS ( + // SELECT + // id, + // SUM(mileage) AS total_mileage, + // COUNT(*) AS total_trip + // FROM trips + // WHERE start BETWEEN ? AND ? + // GROUP BY id + // ) + // SELECT + // t.*, + // tm.total_mileage, + // tm.total_trip, + // vt.name AS type_name, + // 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 + // LEFT JOIN t_vehicles tvt ON tvt.id = t.vhc_id + // LEFT JOIN t_vehicles_cats vt ON vt.id = tvt.cat_id + // WHERE + // t.start BETWEEN ? AND ? + // AND (? IS NULL OR t.id = ?) + // AND (? IS NULL OR vt.id = ?) + // AND (? IS NULL OR t.pool_code = ?); + // "; + + // $vid = $vid ?: null; + // $vt = $vt ?: null; + // $poolcode = $poolcode ?: null; + + // $list = DB::select($sql, [ + // $from_date, + // $to_date, + // $from_date, + // $to_date, + // $vid, + // $vid, + // $vt, + // $vt, + // $poolcode, + // $poolcode, + // ]); + + // // ========================================================= + // // STEP 2 - Ambil SEMUA titik GPS relevan dalam 1x query saja + // // (bukan per trip, tapi sekali untuk seluruh vhc_id yang muncul + // // di hasil trips, dalam rentang tanggal laporan) + // // ========================================================= + // if (count($list) > 0) { + // $vhcIds = array_values(array_unique(array_map(function ($t) { + // return $t->vhc_id; + // }, $list))); + + // $placeholders = implode(',', array_fill(0, count($vhcIds), '?')); + + // $gpsPoints = DB::select(" + // SELECT vhc_id, crt_d, latitude, longitude + // FROM t_gps_tracks + // WHERE vhc_id IN ({$placeholders}) + // AND crt_d BETWEEN ? AND ? + // AND latitude IS NOT NULL + // AND longitude IS NOT NULL + // ORDER BY vhc_id ASC, crt_d ASC + // ", array_merge($vhcIds, [$from_date, $to_date])); + + // // ========================================================= + // // STEP 3 - Kelompokkan titik GPS per vhc_id (sudah urut crt_d ASC) + // // ========================================================= + // $gpsByVehicle = []; + // foreach ($gpsPoints as $g) { + // $gpsByVehicle[$g->vhc_id][] = $g; + // } + + // // ========================================================= + // // STEP 4 - Cari titik pertama & terakhir per trip di memori PHP + // // (bukan lewat query DB lagi, jadi murah) + // // ========================================================= + // foreach ($list as $t) { + // $points = $gpsByVehicle[$t->vhc_id] ?? []; + + // $startPoint = null; + // $finishPoint = null; + + // foreach ($points as $p) { + // if ($p->crt_d < $t->start || $p->crt_d > $t->finish) { + // continue; + // } + // if ($startPoint === null) { + // $startPoint = $p; // titik pertama dalam rentang trip + // } + // $finishPoint = $p; // terus diupdate sampai titik terakhir dalam rentang + // } + + // $t->start_latitude = $startPoint->latitude ?? null; + // $t->start_longitude = $startPoint->longitude ?? null; + // $t->finish_latitude = $finishPoint->latitude ?? null; + // $t->finish_longitude = $finishPoint->longitude ?? null; + // } + // } + + // // ========================================================= + // // RETURN + // // ========================================================= + // $apiResp = Responses::success("success list vehicles report"); + // $apiResp["data"] = $list; + // return new Response($apiResp, $apiResp["meta"]["code"]); + // } catch (\Exception $e) { + // $apiResp = Responses::error($e->getMessage()); + // return new Response($apiResp, $apiResp["meta"]["code"]); + // } + // } // public function api_report_vehicle_trips_list(Request $req) // {