update tracking
This commit is contained in:
@ -41,256 +41,515 @@ class ReportsController extends Controller
|
|||||||
UserLogs::insert(Auth::user()->id, $log);
|
UserLogs::insert(Auth::user()->id, $log);
|
||||||
return view('menu_v1.reports.vehicle_trips', $data);
|
return view('menu_v1.reports.vehicle_trips', $data);
|
||||||
}
|
}
|
||||||
public function api_report_vehicle_trips_list(Request $req)
|
// public function api_report_vehicle_trips_list(Request $req)
|
||||||
{
|
// {
|
||||||
// $TIMEFIX = Helper::TIMEFIX;
|
// // $TIMEFIX = Helper::TIMEFIX;
|
||||||
// Validate input
|
// // Validate input
|
||||||
// date in unix datetime format
|
// // date in unix datetime format
|
||||||
// dd($req->type);
|
// // dd($req->type);
|
||||||
$rules = [
|
// $rules = [
|
||||||
// 'from_date' => 'required|date',
|
// // 'from_date' => 'required|date',
|
||||||
// 'to_date' => 'required|date|after_or_equal:from_date',
|
// // 'to_date' => 'required|date|after_or_equal:from_date',
|
||||||
'type' => 'nullable|in:report,list', // enum "report", "list". nullable default "list"
|
// 'type' => 'nullable|in:report,list', // enum "report", "list". nullable default "list"
|
||||||
];
|
// ];
|
||||||
|
|
||||||
$isValidInput = Validator::make($req->all(), $rules);
|
// $isValidInput = Validator::make($req->all(), $rules);
|
||||||
if (!$isValidInput->passes()) {
|
// if (!$isValidInput->passes()) {
|
||||||
$apiResp = Responses::bad_input($isValidInput->messages()->first());
|
// $apiResp = Responses::bad_input($isValidInput->messages()->first());
|
||||||
return new Response($apiResp, $apiResp["meta"]["code"]);
|
// return new Response($apiResp, $apiResp["meta"]["code"]);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// $from_date = $req->input('from_date');
|
// // $from_date = $req->input('from_date');
|
||||||
// $to_date = $req->input('to_date');
|
// // $to_date = $req->input('to_date');
|
||||||
$from_date = $req->input('from_date') - Helper::TIMEFIX;
|
// $from_date = $req->input('from_date') - Helper::TIMEFIX;
|
||||||
$to_date = $req->input('to_date') - Helper::TIMEFIX;
|
// $to_date = $req->input('to_date') - Helper::TIMEFIX;
|
||||||
$vid = $req->input('vid');
|
// $vid = $req->input('vid');
|
||||||
$vt = $req->input('vt');
|
// $vt = $req->input('vt');
|
||||||
$poolcode = $req->input('poolcode');
|
// $poolcode = $req->input('poolcode');
|
||||||
// $from_date = 1756054800;
|
// // $from_date = 1756054800;
|
||||||
// $to_date = 1756745940;
|
// // $to_date = 1756745940;
|
||||||
|
|
||||||
// get month year
|
// // get month year
|
||||||
$date = Carbon::createFromTimestamp($from_date);
|
// $date = Carbon::createFromTimestamp($from_date);
|
||||||
$yymm = $date->format('ym');
|
// $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 tracks_2509 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
|
// // FROM trips
|
||||||
// where
|
// // where
|
||||||
// ignition = 4
|
// // ignition = 4
|
||||||
// and isStop = 0
|
// // and isStop = 0
|
||||||
// ),
|
// // ),
|
||||||
// agg AS (
|
// // agg AS (
|
||||||
// SELECT
|
// // SELECT
|
||||||
// COUNT(*) AS row_count,
|
// // COUNT(*) AS row_count,
|
||||||
// v.name,
|
// // v.name,
|
||||||
// v.nopol1,
|
// // v.nopol1,
|
||||||
// vhc_id,
|
// // vhc_id,
|
||||||
// -- trip_id,
|
// // -- trip_id,
|
||||||
// ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
|
// // ROW_NUMBER() OVER (PARTITION BY v.id ORDER BY MIN(a.crt_d)) AS trip_id,
|
||||||
// SUM(pre_milleage) AS mileage,
|
// // SUM(pre_milleage) AS mileage,
|
||||||
// MIN(a.crt_d) AS start,
|
// // MIN(a.crt_d) AS start,
|
||||||
// MAX(a.crt_d) AS finish,
|
// // MAX(a.crt_d) AS finish,
|
||||||
// MIN(a.vhc_milleage) AS startMileage,
|
// // MIN(a.vhc_milleage) AS startMileage,
|
||||||
// MAX(a.vhc_milleage) AS finishMileage,
|
// // 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 = MIN(a.id) LIMIT 1) AS startLoc,
|
||||||
// (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MAX(a.id) LIMIT 1) AS finishLoc
|
// // (SELECT fulladdress FROM t_gps_tracks_address WHERE master_id = MAX(a.id) LIMIT 1) AS finishLoc
|
||||||
// FROM t_vehicles v
|
// // FROM t_vehicles v
|
||||||
// LEFT JOIN numbered a ON a.vhc_id = v.id
|
// // LEFT JOIN numbered a ON a.vhc_id = v.id
|
||||||
// WHERE
|
// // WHERE
|
||||||
// v.dlt is null and trip_id != 0
|
// // v.dlt is null and trip_id != 0
|
||||||
// and if(? , v.id = ? , 1=1)
|
// // and if(? , v.id = ? , 1=1)
|
||||||
// GROUP BY v.id, a.trip_id
|
// // GROUP BY v.id, a.trip_id
|
||||||
// HAVING COUNT(*) > 1
|
// // HAVING COUNT(*) > 1
|
||||||
// )
|
// // )
|
||||||
// SELECT
|
// // SELECT
|
||||||
// *,
|
// // *,
|
||||||
// SUM(mileage) OVER (PARTITION BY agg.id) AS total_mileage,
|
// // SUM(mileage) OVER (PARTITION BY agg.id) AS total_mileage,
|
||||||
// COUNT(trip_id) OVER (PARTITION BY agg.id) AS total_trip,
|
// // COUNT(trip_id) OVER (PARTITION BY agg.id) AS total_trip,
|
||||||
// tvd.pool_code, tvd.dc_code
|
// // tvd.pool_code, tvd.dc_code
|
||||||
// FROM agg agg
|
// // FROM agg agg
|
||||||
// join t_vehicles_detail tvd on tvd.vid = agg.vhc_id
|
// // join t_vehicles_detail tvd on tvd.vid = agg.vhc_id
|
||||||
// ORDER BY agg.id, trip_id
|
// // ORDER BY agg.id, trip_id
|
||||||
// ", [$from_date, $to_date, $vid, $vid]);
|
// // ", [$from_date, $to_date, $vid, $vid]);
|
||||||
|
|
||||||
// $list = DB::select("WITH TotalMileage AS (
|
// // $list = DB::select("WITH TotalMileage AS (
|
||||||
// SELECT id, SUM(mileage) AS total_mileage, count(*) total_trip
|
// // SELECT id, SUM(mileage) AS total_mileage, count(*) total_trip
|
||||||
// FROM trips
|
// // FROM trips
|
||||||
// WHERE start BETWEEN ? AND ?
|
// // WHERE start BETWEEN ? AND ?
|
||||||
// GROUP BY id
|
// // GROUP BY id
|
||||||
// )
|
// // )
|
||||||
// SELECT
|
// // SELECT
|
||||||
// t.*,
|
// // t.*,
|
||||||
// tm.total_mileage, total_trip,
|
// // tm.total_mileage, total_trip,
|
||||||
// vt.name as type_name,
|
// // vt.name as type_name,
|
||||||
// ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY t.start) AS trip_id
|
// // ROW_NUMBER() OVER (PARTITION BY t.id ORDER BY t.start) AS trip_id
|
||||||
// FROM trips t
|
// // FROM trips t
|
||||||
// JOIN TotalMileage tm ON t.id = tm.id
|
// // JOIN TotalMileage tm ON t.id = tm.id
|
||||||
// LEFT JOIN t_vehicles tvt ON tvt.id = t.vhc_id
|
// // LEFT JOIN t_vehicles tvt ON tvt.id = t.vhc_id
|
||||||
// LEFT JOIN t_vehicles_cats vt ON vt.id = tvt.cat_id
|
// // LEFT JOIN t_vehicles_cats vt ON vt.id = tvt.cat_id
|
||||||
// WHERE
|
// // WHERE
|
||||||
// t.start BETWEEN ? AND ?
|
// // t.start BETWEEN ? AND ?
|
||||||
// and if(? , t.id = ? , 1=1)
|
// // and if(? , t.id = ? , 1=1)
|
||||||
// and if(? , vt.id = ? , 1=1)
|
// // and if(? , vt.id = ? , 1=1)
|
||||||
// and if(? , t.pool_code = ? , 1=1)
|
// // and if(? , t.pool_code = ? , 1=1)
|
||||||
// ", [$from_date, $to_date, $from_date, $to_date, $vid, $vid, $vt, $vt, $poolcode, $poolcode]);
|
// // ", [$from_date, $to_date, $from_date, $to_date, $vid, $vid, $vt, $vt, $poolcode, $poolcode]);
|
||||||
$sql =
|
// $sql =
|
||||||
"WITH TotalMileage AS (
|
// "WITH TotalMileage AS (
|
||||||
SELECT
|
// SELECT
|
||||||
id,
|
// id,
|
||||||
SUM(mileage) AS total_mileage,
|
// SUM(mileage) AS total_mileage,
|
||||||
COUNT(*) AS total_trip
|
// COUNT(*) AS total_trip
|
||||||
FROM trips
|
// FROM trips
|
||||||
WHERE start BETWEEN ? AND ?
|
// WHERE start BETWEEN ? AND ?
|
||||||
GROUP BY id
|
// GROUP BY id
|
||||||
)
|
// )
|
||||||
SELECT
|
// SELECT
|
||||||
t.*,
|
// t.*,
|
||||||
tm.total_mileage,
|
// tm.total_mileage,
|
||||||
tm.total_trip,
|
// tm.total_trip,
|
||||||
vt.name AS type_name,
|
// vt.name AS type_name,
|
||||||
ROW_NUMBER() OVER (
|
// ROW_NUMBER() OVER (
|
||||||
PARTITION BY t.id
|
// PARTITION BY t.id
|
||||||
ORDER BY t.start
|
// ORDER BY t.start
|
||||||
) AS trip_id
|
// ) AS trip_id
|
||||||
FROM trips t
|
// FROM trips t
|
||||||
JOIN TotalMileage tm ON t.id = tm.id
|
// JOIN TotalMileage tm ON t.id = tm.id
|
||||||
LEFT JOIN t_vehicles tvt ON tvt.id = t.vhc_id
|
// LEFT JOIN t_vehicles tvt ON tvt.id = t.vhc_id
|
||||||
LEFT JOIN t_vehicles_cats vt ON vt.id = tvt.cat_id
|
// LEFT JOIN t_vehicles_cats vt ON vt.id = tvt.cat_id
|
||||||
WHERE
|
// WHERE
|
||||||
t.start BETWEEN ? AND ?
|
// t.start BETWEEN ? AND ?
|
||||||
AND (? IS NULL OR t.id = ?)
|
// AND (? IS NULL OR t.id = ?)
|
||||||
AND (? IS NULL OR vt.id = ?)
|
// AND (? IS NULL OR vt.id = ?)
|
||||||
AND (? IS NULL OR t.pool_code = ?);
|
// AND (? IS NULL OR t.pool_code = ?);
|
||||||
";
|
// ";
|
||||||
|
|
||||||
$vid = $vid ?: null;
|
// $vid = $vid ?: null;
|
||||||
$vt = $vt ?: null;
|
// $vt = $vt ?: null;
|
||||||
$poolcode = $poolcode ?: null;
|
// $poolcode = $poolcode ?: null;
|
||||||
|
|
||||||
$list = DB::select($sql, [
|
// $list = DB::select($sql, [
|
||||||
$from_date,
|
// $from_date,
|
||||||
$to_date,
|
// $to_date,
|
||||||
$from_date,
|
// $from_date,
|
||||||
$to_date,
|
// $to_date,
|
||||||
$vid,
|
// $vid,
|
||||||
$vid,
|
// $vid,
|
||||||
$vt,
|
// $vt,
|
||||||
$vt,
|
// $vt,
|
||||||
$poolcode,
|
// $poolcode,
|
||||||
$poolcode,
|
// $poolcode,
|
||||||
]);
|
// ]);
|
||||||
// // RETURN 1 - LIST
|
// // // RETURN 1 - LIST
|
||||||
// if($req->type != 'report'){
|
// // if($req->type != 'report'){
|
||||||
$apiResp = Responses::success("success list vehicles report");
|
// $apiResp = Responses::success("success list vehicles report");
|
||||||
$apiResp["data"] = $list;
|
// $apiResp["data"] = $list;
|
||||||
return new Response($apiResp, $apiResp["meta"]["code"]);
|
// return new Response($apiResp, $apiResp["meta"]["code"]);
|
||||||
// }
|
// // }
|
||||||
|
|
||||||
// // RETURN 2 - REPORT
|
// // // RETURN 2 - REPORT
|
||||||
// if($req->type == 'report'){
|
// // if($req->type == 'report'){
|
||||||
// $headings = ['Name', 'License Plate', 'Number of Trip', 'Total Mileage'];
|
// // $headings = ['Name', 'License Plate', 'Number of Trip', 'Total Mileage'];
|
||||||
|
|
||||||
// $export = new class($list, $headings) implements FromArray, WithHeadings {
|
// // $export = new class($list, $headings) implements FromArray, WithHeadings {
|
||||||
// private $list;
|
// // private $list;
|
||||||
// private $headings;
|
// // private $headings;
|
||||||
|
|
||||||
// public function __construct($list, $headings)
|
// // public function __construct($list, $headings)
|
||||||
// {
|
// // {
|
||||||
// $this->list = $list;
|
// // $this->list = $list;
|
||||||
// $this->headings = $headings;
|
// // $this->headings = $headings;
|
||||||
// }
|
// // }
|
||||||
|
|
||||||
// public function array(): array
|
// // public function array(): array
|
||||||
// {
|
// // {
|
||||||
// return array_map(function ($item) {
|
// // return array_map(function ($item) {
|
||||||
// return [
|
// // return [
|
||||||
// $item->name,
|
// // $item->name,
|
||||||
// $item->nopol1,
|
// // $item->nopol1,
|
||||||
// $item->numOfTrip,
|
// // $item->numOfTrip,
|
||||||
// $item->total_milleage,
|
// // $item->total_milleage,
|
||||||
// ];
|
// // ];
|
||||||
// }, $this->list);
|
// // }, $this->list);
|
||||||
// }
|
// // }
|
||||||
|
|
||||||
// public function headings(): array
|
// // public function headings(): array
|
||||||
// {
|
// // {
|
||||||
// return $this->headings;
|
// // return $this->headings;
|
||||||
// }
|
// // }
|
||||||
|
|
||||||
// // Start table from A3
|
// // // Start table from A3
|
||||||
// public function startCell(): string
|
// // public function startCell(): string
|
||||||
// {
|
// // {
|
||||||
// return 'A3';
|
// // return 'A3';
|
||||||
// }
|
// // }
|
||||||
|
|
||||||
// // Add title & border styling
|
// // // Add title & border styling
|
||||||
// public function styles(Worksheet $sheet)
|
// // public function styles(Worksheet $sheet)
|
||||||
// {
|
// // {
|
||||||
// $tgl0 = date('d-m-Y', $GLOBALS['from_date']);
|
// // $tgl0 = date('d-m-Y', $GLOBALS['from_date']);
|
||||||
// $tgl1 = date('d-m-Y', $GLOBALS['to_date']);
|
// // $tgl1 = date('d-m-Y', $GLOBALS['to_date']);
|
||||||
|
|
||||||
// // Title in A1
|
// // // Title in A1
|
||||||
// $sheet->setCellValue('A1', 'Vehicle Trip Report $tgl0 until $tgl1');
|
// // $sheet->setCellValue('A1', 'Vehicle Trip Report $tgl0 until $tgl1');
|
||||||
// $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16);
|
// // $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16);
|
||||||
// $sheet->getStyle('A1')->getAlignment()->setHorizontal('center');
|
// // $sheet->getStyle('A1')->getAlignment()->setHorizontal('center');
|
||||||
// $sheet->mergeCells('A1:D1'); // Merge across 4 columns
|
// // $sheet->mergeCells('A1:D1'); // Merge across 4 columns
|
||||||
|
|
||||||
// // Get last row
|
// // // Get last row
|
||||||
// $lastRow = $this->list->count() + 3; // 3 = heading row
|
// // $lastRow = $this->list->count() + 3; // 3 = heading row
|
||||||
// $range = "A3:D{$lastRow}";
|
// // $range = "A3:D{$lastRow}";
|
||||||
|
|
||||||
// // Add borders
|
// // // Add borders
|
||||||
// $sheet->getStyle($range)->getBorders()->getAllBorders()
|
// // $sheet->getStyle($range)->getBorders()->getAllBorders()
|
||||||
// ->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
|
// // ->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
|
||||||
|
|
||||||
// // Bold headings
|
// // // Bold headings
|
||||||
// $sheet->getStyle('A3:D3')->getFont()->setBold(true);
|
// // $sheet->getStyle('A3:D3')->getFont()->setBold(true);
|
||||||
|
|
||||||
// return [];
|
// // return [];
|
||||||
// }
|
// // }
|
||||||
// };
|
// // };
|
||||||
|
|
||||||
// return Excel::download($export, 'trip_report.xlsx');
|
// // return Excel::download($export, 'trip_report.xlsx');
|
||||||
// }
|
// // }
|
||||||
} catch (\Exception $e) {
|
// } catch (\Exception $e) {
|
||||||
$apiResp = Responses::error($e->getMessage());
|
// $apiResp = Responses::error($e->getMessage());
|
||||||
return new Response($apiResp, $apiResp["meta"]["code"]);
|
// return new Response($apiResp, $apiResp["meta"]["code"]);
|
||||||
// return Responses::json(Responses::SERVER_ERROR, 'An error occurred while generating the report.', (object)[]);
|
// // return Responses::json(Responses::SERVER_ERROR, 'An error occurred while generating the report.', (object)[]);
|
||||||
}
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
public function api_report_vehicle_trips_list(Request $req)
|
||||||
|
{
|
||||||
|
// $TIMEFIX = Helper::TIMEFIX;
|
||||||
|
// Validate input
|
||||||
|
// date in unix datetime format
|
||||||
|
// dd($req->type);
|
||||||
|
$rules = [
|
||||||
|
// 'from_date' => 'required|date',
|
||||||
|
// 'to_date' => 'required|date|after_or_equal:from_date',
|
||||||
|
'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');
|
||||||
|
// $to_date = $req->input('to_date');
|
||||||
|
$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');
|
||||||
|
// $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 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 start BETWEEN ? AND ?
|
||||||
|
// GROUP BY id
|
||||||
|
// )
|
||||||
|
// SELECT
|
||||||
|
// t.*,
|
||||||
|
// tm.total_mileage, 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 if(? , t.id = ? , 1=1)
|
||||||
|
// and if(? , vt.id = ? , 1=1)
|
||||||
|
// and if(? , t.pool_code = ? , 1=1)
|
||||||
|
// ", [$from_date, $to_date, $from_date, $to_date, $vid, $vid, $vt, $vt, $poolcode, $poolcode]);
|
||||||
|
|
||||||
|
$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,
|
||||||
|
]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ==========================================================
|
||||||
|
* GET LAST GPS POSITION FOR EACH VEHICLE
|
||||||
|
* ==========================================================
|
||||||
|
*/
|
||||||
|
$vehicleIds = collect($list)
|
||||||
|
->pluck('vhc_id')
|
||||||
|
->filter()
|
||||||
|
->unique()
|
||||||
|
->values()
|
||||||
|
->toArray();
|
||||||
|
|
||||||
|
if (!empty($vehicleIds)) {
|
||||||
|
|
||||||
|
$placeholders = implode(',', array_fill(0, count($vehicleIds), '?'));
|
||||||
|
|
||||||
|
$gpsSql = "
|
||||||
|
SELECT
|
||||||
|
gt.vhc_id,
|
||||||
|
gt.latitude,
|
||||||
|
gt.longitude,
|
||||||
|
gt.speed,
|
||||||
|
gt.crt_d,
|
||||||
|
tgta.fulladdress
|
||||||
|
FROM t_gps_tracks gt
|
||||||
|
LEFT JOIN t_gps_tracks_address tgta
|
||||||
|
ON tgta.master_id = gt.id
|
||||||
|
INNER JOIN (
|
||||||
|
SELECT
|
||||||
|
vhc_id,
|
||||||
|
MAX(crt_d) AS last_crt_d
|
||||||
|
FROM t_gps_tracks
|
||||||
|
WHERE
|
||||||
|
action = 'location'
|
||||||
|
AND latitude IS NOT NULL
|
||||||
|
AND longitude IS NOT NULL
|
||||||
|
AND crt_d BETWEEN ? AND ?
|
||||||
|
AND vhc_id IN ($placeholders)
|
||||||
|
GROUP BY vhc_id
|
||||||
|
) latest
|
||||||
|
ON latest.vhc_id = gt.vhc_id
|
||||||
|
AND latest.last_crt_d = gt.crt_d
|
||||||
|
";
|
||||||
|
|
||||||
|
$gpsParams = array_merge(
|
||||||
|
[$from_date, $to_date],
|
||||||
|
$vehicleIds
|
||||||
|
);
|
||||||
|
|
||||||
|
$gpsList = DB::select($gpsSql, $gpsParams);
|
||||||
|
|
||||||
|
$gpsByVehicle = collect($gpsList)->keyBy('vhc_id');
|
||||||
|
|
||||||
|
foreach ($list as &$item) {
|
||||||
|
|
||||||
|
$gps = $gpsByVehicle->get($item->vhc_id);
|
||||||
|
|
||||||
|
$item->latitude = $gps->latitude ?? null;
|
||||||
|
$item->longitude = $gps->longitude ?? null;
|
||||||
|
$item->speed = $gps->speed ?? null;
|
||||||
|
$item->fulladdress = $gps->fulladdress ?? null;
|
||||||
|
$item->gps_time = $gps->crt_d ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// // RETURN 1 - LIST
|
||||||
|
// if($req->type != 'report'){
|
||||||
|
$apiResp = Responses::success("success list vehicles report");
|
||||||
|
$apiResp["data"] = $list;
|
||||||
|
return new Response($apiResp, $apiResp["meta"]["code"]);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // RETURN 2 - REPORT
|
||||||
|
// if($req->type == 'report'){
|
||||||
|
// ...
|
||||||
|
// }
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$apiResp = Responses::error($e->getMessage());
|
||||||
|
return new Response($apiResp, $apiResp["meta"]["code"]);
|
||||||
|
// return Responses::json(Responses::SERVER_ERROR, 'An error occurred while generating the report.', (object)[]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function view_report_trip_detail(Request $req)
|
public function view_report_trip_detail(Request $req)
|
||||||
{
|
{
|
||||||
$vid = $req->vid;
|
$vid = $req->vid;
|
||||||
|
|||||||
@ -25,8 +25,8 @@
|
|||||||
@if ($user_role == \App\Models\Users::ROLE_VENDOR || $user_role == \App\Models\Users::ROLE_ADMIN)
|
@if ($user_role == \App\Models\Users::ROLE_VENDOR || $user_role == \App\Models\Users::ROLE_ADMIN)
|
||||||
@can('vehicle.create')
|
@can('vehicle.create')
|
||||||
<!-- <div class="col text-end">
|
<!-- <div class="col text-end">
|
||||||
<button id="btnMdlNewVhc" class="btn btn-sm btn-danger">Add New Vehicle</button>
|
<button id="btnMdlNewVhc" class="btn btn-sm btn-danger">Add New Vehicle</button>
|
||||||
</div> -->
|
</div> -->
|
||||||
@endcan
|
@endcan
|
||||||
|
|
||||||
{{-- <div class="col-auto text-end ps-0">
|
{{-- <div class="col-auto text-end ps-0">
|
||||||
@ -117,8 +117,6 @@
|
|||||||
<th class="hidden">Date</th>
|
<th class="hidden">Date</th>
|
||||||
<th class="hidden">Hour Start</th>
|
<th class="hidden">Hour Start</th>
|
||||||
<th class="hidden">Hour Finish</th>
|
<th class="hidden">Hour Finish</th>
|
||||||
{{-- <th class="hidden">Latitude</th>
|
|
||||||
<th class="hidden">Longitude</th> --}}
|
|
||||||
<th class="">Start</th>
|
<th class="">Start</th>
|
||||||
<th class="">Finish</th>
|
<th class="">Finish</th>
|
||||||
<th class="">Duration</th>
|
<th class="">Duration</th>
|
||||||
@ -131,73 +129,73 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<!-- <tbody>
|
<!-- <tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<td>24-679</td>
|
<td>24-679</td>
|
||||||
<td>24-679</td>
|
<td>24-679</td>
|
||||||
<td>26</td>
|
<td>26</td>
|
||||||
<td>442.3</td>
|
<td>442.3</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>24-682</td>
|
<td>24-682</td>
|
||||||
<td>24-682</td>
|
<td>24-682</td>
|
||||||
<td>28</td>
|
<td>28</td>
|
||||||
<td>312.7</td>
|
<td>312.7</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>24-877</td>
|
<td>24-877</td>
|
||||||
<td>24-877</td>
|
<td>24-877</td>
|
||||||
<td>15</td>
|
<td>15</td>
|
||||||
<td>276.4</td>
|
<td>276.4</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>A.16-347</td>
|
<td>A.16-347</td>
|
||||||
<td>A.16-347</td>
|
<td>A.16-347</td>
|
||||||
<td>37</td>
|
<td>37</td>
|
||||||
<td>543.2</td>
|
<td>543.2</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>AL2025-B21</td>
|
<td>AL2025-B21</td>
|
||||||
<td>AL2025-B21</td>
|
<td>AL2025-B21</td>
|
||||||
<td>18</td>
|
<td>18</td>
|
||||||
<td>295.4</td>
|
<td>295.4</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>B.16-542</td>
|
<td>B.16-542</td>
|
||||||
<td>B.16-542</td>
|
<td>B.16-542</td>
|
||||||
<td>32</td>
|
<td>32</td>
|
||||||
<td>478.2</td>
|
<td>478.2</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>B.24-264</td>
|
<td>B.24-264</td>
|
||||||
<td>B.24-264</td>
|
<td>B.24-264</td>
|
||||||
<td>24</td>
|
<td>24</td>
|
||||||
<td>188.4</td>
|
<td>188.4</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>B.24-274</td>
|
<td>B.24-274</td>
|
||||||
<td>B.24-274</td>
|
<td>B.24-274</td>
|
||||||
<td>33</td>
|
<td>33</td>
|
||||||
<td>440.1</td>
|
<td>440.1</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Ford Ranger</td>
|
<td>Ford Ranger</td>
|
||||||
<td>26-199</td>
|
<td>26-199</td>
|
||||||
<td>30</td>
|
<td>30</td>
|
||||||
<td>631.4</td>
|
<td>631.4</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Nissan AD</td>
|
<td>Nissan AD</td>
|
||||||
<td>B.10-517</td>
|
<td>B.10-517</td>
|
||||||
<td>27</td>
|
<td>27</td>
|
||||||
<td>375.9</td>
|
<td>375.9</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Triton</td>
|
<td>Triton</td>
|
||||||
<td>A.29-127</td>
|
<td>A.29-127</td>
|
||||||
<td>26</td>
|
<td>26</td>
|
||||||
<td>508.2</td>
|
<td>508.2</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody> -->
|
</tbody> -->
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -209,17 +207,17 @@
|
|||||||
<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="mdlDetailTrip"
|
<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="mdlDetailTrip"
|
||||||
aria-labelledby="mdlDetailTripLabel" aria-hidden="true">
|
aria-labelledby="mdlDetailTripLabel" aria-hidden="true">
|
||||||
<!-- <div class="modal-dialog modal-dialog modal-dialog-centered modal-dialog-scrollable modal-xl">
|
<!-- <div class="modal-dialog modal-dialog modal-dialog-centered modal-dialog-scrollable modal-xl">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h5 class="modal-title" id="mdlDetailTripLabel">-----</h5>
|
<h5 class="modal-title" id="mdlDetailTripLabel">-----</h5>
|
||||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div> -->
|
</div> -->
|
||||||
</div>
|
</div>
|
||||||
@endsection
|
@endsection
|
||||||
|
|
||||||
@ -483,41 +481,57 @@
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// {
|
||||||
|
// data: 'finish',
|
||||||
|
// render: (data, type, row, meta) => {
|
||||||
|
// // saat export, hanya tampilkan lokasi tanpa tanggal/jam
|
||||||
|
// if (type === 'export') {
|
||||||
|
// return Helper.shortenText(decodeURIComponent(row.finishLoc ||
|
||||||
|
// 'address'), 255) || "-";
|
||||||
|
// }
|
||||||
|
// return `
|
||||||
|
// ${moment.unix(data + AppState.TIMEFIX).format('DD MMM YYYY HH:mm:ss')}<br>
|
||||||
|
// ${Helper.shortenText(decodeURIComponent(row.finishLoc || 'address'), 255) || "-"}
|
||||||
|
// `;
|
||||||
|
// }
|
||||||
|
// },
|
||||||
{
|
{
|
||||||
data: 'finish',
|
data: 'finish',
|
||||||
render: (data, type, row, meta) => {
|
render: (data, type, row) => {
|
||||||
// saat export, hanya tampilkan lokasi tanpa tanggal/jam
|
|
||||||
|
// Saat export Excel
|
||||||
if (type === 'export') {
|
if (type === 'export') {
|
||||||
return Helper.shortenText(decodeURIComponent(row.finishLoc ||
|
|
||||||
'address'), 255) || "-";
|
const nearestSite = findNearestSite(
|
||||||
|
row.latitude,
|
||||||
|
row.longitude
|
||||||
|
);
|
||||||
|
|
||||||
|
if (nearestSite) {
|
||||||
|
return formatSiteLabel(nearestSite);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Helper.shortenText(
|
||||||
|
decodeURIComponent(row.fulladdress || 'address'),
|
||||||
|
255
|
||||||
|
) || '-';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tampilan di tabel
|
||||||
return `
|
return `
|
||||||
${moment.unix(data + AppState.TIMEFIX).format('DD MMM YYYY HH:mm:ss')}<br>
|
${moment.unix(data + AppState.TIMEFIX)
|
||||||
${Helper.shortenText(decodeURIComponent(row.finishLoc || 'address'), 255) || "-"}
|
.format('DD MMM YYYY HH:mm:ss')}
|
||||||
`;
|
<br>
|
||||||
|
${
|
||||||
|
Helper.shortenText(
|
||||||
|
decodeURIComponent(row.fulladdress || 'address'),
|
||||||
|
255
|
||||||
|
) || '-'
|
||||||
|
}
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// {
|
{
|
||||||
// data: 'finish',
|
|
||||||
// render: (data, type, row, meta) => {
|
|
||||||
// // saat export, cek apakah koordinat finish berdekatan dengan site referensi
|
|
||||||
// if (type === 'export') {
|
|
||||||
// const nearestSite = findNearestSite(row.finishLat, row
|
|
||||||
// .finishLng); // sesuaikan nama field lat/lng
|
|
||||||
// if (nearestSite) {
|
|
||||||
// return formatSiteLabel(nearestSite);
|
|
||||||
// }
|
|
||||||
// // default: tetap pakai alamat dari database
|
|
||||||
// return Helper.shortenText(decodeURIComponent(row.finishLoc ||
|
|
||||||
// 'address'), 255) || "-";
|
|
||||||
// }
|
|
||||||
// return `
|
|
||||||
// ${moment.unix(data + AppState.TIMEFIX).format('DD MMM YYYY HH:mm:ss')}<br>
|
|
||||||
// ${Helper.shortenText(decodeURIComponent(row.finishLoc || 'address'), 255) || "-"}
|
|
||||||
// `;
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
{
|
|
||||||
className: 'text-start',
|
className: 'text-start',
|
||||||
render: function(data, type, row, meta) {
|
render: function(data, type, row, meta) {
|
||||||
const start = moment.unix(row.start);
|
const start = moment.unix(row.start);
|
||||||
|
|||||||
Reference in New Issue
Block a user