This commit is contained in:
wayanrivan
2026-07-01 14:50:17 +07:00
parent 81a131d7a0
commit eb1d5c4187
3 changed files with 383 additions and 289 deletions

View File

@ -145,28 +145,65 @@ class ReportsController extends Controller
// 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]);
// $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(*) 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 (? 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,
]);
// // RETURN 1 - LIST
// if($req->type != 'report'){
$apiResp = Responses::success("success list vehicles report");
@ -311,9 +348,11 @@ class ReportsController extends Controller
{
$q = "select id, nopol1 from t_vehicles WHERE dlt is null order by nopol1";
$listNopol = DB::select($q);
$listpool = DB::table('t_conf_pool')->where('dlt', null)->get();
$data = [
'listNopol' => $listNopol,
'listpool' => $listpool
];
$log = [
@ -342,6 +381,7 @@ class ReportsController extends Controller
$from_date = $req->input('from_date') - Helper::TIMEFIX;
$to_date = $req->input('to_date') - Helper::TIMEFIX;
$vid = $req->input('vid');
$poolcode = $req->input('poolcode');
// Create a period between the two timestamps
$period = CarbonPeriod::create(
@ -363,25 +403,57 @@ class ReportsController extends Controller
$qString = "(" . implode(' UNION ALL ', $subQueryParts) . ")";
// dd("qString", $qString);
try {
$list = DB::select("SELECT
tv.name,
tv.nopol1,
t.crt_d,
t.speed,
tgta.fulladdress,
tvd.speed_limit,
tvd.pool_code,
tvd.dc_code
FROM
$qString t
INNER JOIN t_vehicles tv ON tv.id = t.vhc_id
INNER JOIN t_vehicles_detail tvd ON tvd.vid = tv.id
LEFT JOIN t_gps_tracks_address tgta ON tgta.master_id = t.id
WHERE
if(? , tv.id = ? , 1=1)
AND t.speed > tvd.speed_limit
ORDER BY t.crt_d;
", [$vid, $vid]);
// $list = DB::select("SELECT
// tv.name,
// tv.nopol1,
// t.crt_d,
// t.speed,
// tgta.fulladdress,
// tvd.speed_limit,
// tvd.pool_code,
// tvd.dc_code
// FROM
// $qString t
// INNER JOIN t_vehicles tv ON tv.id = t.vhc_id
// INNER JOIN t_vehicles_detail tvd ON tvd.vid = tv.id
// LEFT JOIN t_gps_tracks_address tgta ON tgta.master_id = t.id
// WHERE
// if(? , tv.id = ? , 1=1)
// AND t.speed > tvd.speed_limit
// AND if(? , tvd.pool_code = ? , 1=1)
// ORDER BY t.crt_d;
// ", [$vid, $vid, $poolcode, $poolcode]);
$vid = $vid ?: null;
$poolcode = $poolcode ?: null;
$sql = "SELECT
tv.name,
tv.nopol1,
t.crt_d,
t.speed,
tgta.fulladdress,
tvd.speed_limit,
tvd.pool_code,
tvd.dc_code
FROM
$qString t
INNER JOIN t_vehicles tv ON tv.id = t.vhc_id
INNER JOIN t_vehicles_detail tvd ON tvd.vid = tv.id
LEFT JOIN t_gps_tracks_address tgta ON tgta.master_id = t.id
WHERE
(? IS NULL OR tv.id = ?)
AND t.speed > tvd.speed_limit
AND (? IS NULL OR tvd.pool_code = ?)
ORDER BY t.crt_d
";
$list = DB::select($sql, [
$vid,
$vid,
$poolcode,
$poolcode,
]);
// // RETURN 1 - LIST
// if($req->type != 'report'){

View File

@ -23,8 +23,8 @@
@if ($user_role == \App\Models\Users::ROLE_VENDOR || $user_role == \App\Models\Users::ROLE_ADMIN)
@can('vehicle.create')
<!-- <div class="col text-end">
<button id="btnMdlNewVhc" class="btn btn-sm btn-danger">Add New Vehicle</button>
</div> -->
<button id="btnMdlNewVhc" class="btn btn-sm btn-danger">Add New Vehicle</button>
</div> -->
@endcan
{{-- <div class="col-auto text-end ps-0">
@ -37,73 +37,90 @@
</div>
</div>
<div class="card-body">
<!-- filter -->
<div class="row">
<div class="col-2">
<div class="form-group">
<label class="text-muted">From</label>
<!-- default today -->
<!-- <input class="form-control" id="tgl0" value="20-08-2025 00:00"> -->
<input class="form-control" id="tgl0" value="{{ date('d-m-Y 00:00') }}">
</div>
</div>
<div class="col-2">
<div class="form-group">
<label class="text-muted">To</label>
<input class="form-control" id="tgl1" value="{{ date('d-m-Y 23:59') }}">
</div>
</div>
<div class="col-2">
<div class="form-group">
<label class="text-muted">License Plate</label>
<select name="license_plate" class="form-control" id="filterNopol">
<option value="">-- All --</option>
@foreach ($listNopol as $nopol)
<option value="{{ $nopol->id }}">{{ $nopol->nopol1 }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-1 d-flex align-items-end">
<button class="btn btn-primary" id="submitFilter">Submit</button>
</div>
<div class="col-5 d-flex align-items-end">
<button class="btn btn-sm btn-danger ms-auto" id="btnDownloadReport">Download Report</button>
</div>
</div>
</div>
<div class="table-responsive p-3">
<table id="tVehicleTrips" class="table table-hover dataTable w-100">
<thead>
<tr class="">
<!-- <th class="">Vehicle ID</th> -->
<!-- <th class="text-center">Action</th> -->
<!-- <th class="">Vehicle Name</th> -->
<th class="">License Plate Number</th>
<th class="">Dist. Cat.</th>
<th class="">Pool</th>
<th class="">Time</th>
<th class="">Speed (kph)</th>
<th class="">Location</th>
</tr>
</thead>
</table>
<!-- filter -->
<div class="row g-3 align-items-end">
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">From</label>
<input class="form-control" id="tgl0" value="{{ date('d-m-Y 00:00') }}">
</div>
</div>
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">To</label>
<input class="form-control" id="tgl1" value="{{ date('d-m-Y 23:59') }}">
</div>
</div>
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">License Plate</label>
<select name="license_plate" class="form-control" id="filterNopol">
<option value="">-- All --</option>
@foreach ($listNopol as $nopol)
<option value="{{ $nopol->id }}">{{ $nopol->nopol1 }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">Pool</label>
<select name="poolcode" class="form-control" id="poolcode">
<option value="">-- All --</option>
@foreach ($listpool as $listpools)
<option value="{{ $listpools->pool_code }}">{{ $listpools->pool_name }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-12 col-lg-4 d-flex gap-2 justify-content-lg-end">
<button class="btn btn-primary w-100 w-lg-auto" id="submitFilter">
<i class="fa fa-search me-1"></i> Submit
</button>
<button class="btn btn-danger w-100 w-lg-auto" id="btnDownloadReport">
<i class="fa fa-download me-1"></i> Download Report
</button>
</div>
</div>
</div>
<div class="table-responsive p-3">
<table id="tVehicleTrips" class="table table-hover dataTable w-100">
<thead>
<tr class="">
<!-- <th class="">Vehicle ID</th> -->
<!-- <th class="text-center">Action</th> -->
<!-- <th class="">Vehicle Name</th> -->
<th class="">License Plate Number</th>
<th class="">Dist. Cat.</th>
<th class="">Pool</th>
<th class="">Time</th>
<th class="">Speed (kph)</th>
<th class="">Location</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- modal -->
<!-- modal -->
@endsection
@section('customjs')
<script src="{{ asset('assets/js/load-image.all.min.js') }}"></script>
<!-- DataTables Buttons + JSZip (for Excel export) -->
<script src="https://cdn.datatables.net/buttons/2.4.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.html5.min.js"></script>
<script>
<!-- DataTables Buttons + JSZip (for Excel export) -->
<script src="https://cdn.datatables.net/buttons/2.4.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.2/js/buttons.html5.min.js"></script>
<script>
'use strict';
const State = {
file_jimp_worker: "{{ asset('assets/js/worker/jimp.js') }}",
@ -119,130 +136,133 @@
Wrapper.init();
Wrapper.event();
// DTable.activate();
},
init: ()=>{
$('#tgl0, #tgl1').datetimepicker({
format:'d-m-Y H:i',
defaultTime:'00:00',
closeOnDateSelect: true,
// mask:true
});
},
init: () => {
$('#tgl0, #tgl1').datetimepicker({
format: 'd-m-Y H:i',
defaultTime: '00:00',
closeOnDateSelect: true,
// mask:true
});
},
},
event: function() {
$('#submitFilter').on('click', function() {
DTable.reload();
});
// Trigger export on external button click
$('#btnDownloadReport').on('click', function() {
DTable.table.button('.buttons-excel').trigger();
});
DTable.reload();
});
// Trigger export on external button click
$('#btnDownloadReport').on('click', function() {
DTable.table.button('.buttons-excel').trigger();
});
},
};
const DTable = {
table: null,
lastAjax: null, // keep track of the last ajax request
table: null,
lastAjax: null, // keep track of the last ajax request
activate: function() {
DTable.reload();
},
reload: function() {
// Abort the last request if it's still running
if (DTable.lastAjax) {
DTable.lastAjax.abort();
}
if (DTable.table) {
// If table already exists → reload
DTable.table.ajax.reload();
return;
}
// Abort the last request if it's still running
if (DTable.lastAjax) {
DTable.lastAjax.abort();
}
if (DTable.table) {
// If table already exists → reload
DTable.table.ajax.reload();
return;
}
DTable.table = $('#tVehicleTrips').DataTable({
searching: false, // 🔹 remove search box
ordering: false, // 🔹 disable sorting for all columns
processing: true,
serverSide: false,
bLengthChange: true,
deferRender: true,
ajax: function(data, callback, settings) {
// Abort previous
if (DTable.lastAjax) {
DTable.lastAjax.abort();
}
DTable.table = $('#tVehicleTrips').DataTable({
searching: false, // 🔹 remove search box
ordering: false, // 🔹 disable sorting for all columns
processing: true,
serverSide: false,
bLengthChange: true,
deferRender: true,
ajax: function(data, callback, settings) {
// Abort previous
if (DTable.lastAjax) {
DTable.lastAjax.abort();
}
// Fire new request
DTable.lastAjax = $.ajax({
url: `{{ route('api_report_abnormalities_list') }}?
// Fire new request
DTable.lastAjax = $.ajax({
url: `{{ route('api_report_abnormalities_list') }}?
cptid=${AppState.current_company}
&from_date=${moment($('#tgl0').val(), "DD-MM-YYYY HH:mm").unix()}
&to_date=${moment($('#tgl1').val(), "DD-MM-YYYY HH:mm").unix()}
&vid=${$('#filterNopol').val() || ''}`,
type: 'GET',
success: function(json) {
callback(json);
},
error: function(xhr, status, error) {
if (status !== 'abort') {
console.error("AJAX error:", error);
}
},
complete: function() {
DTable.lastAjax = null;
}
});
},
&vid=${$('#filterNopol').val() || ''}
&poolcode=${$('#poolcode').val() || ''}`,
type: 'GET',
success: function(json) {
callback(json);
},
error: function(xhr, status, error) {
if (status !== 'abort') {
console.error("AJAX error:", error);
}
},
complete: function() {
DTable.lastAjax = null;
}
});
},
deferRender: true,
columns: [
// { data: 'id', visible: false }, // vhc_id
// {
// data: 'name',
// className: 'text-start text-nowrap',
// },
{
data: 'nopol1',
className: 'text-start',
},
{
data: 'dc_code',
className: 'text-start',
},
{
data: 'pool_code',
className: 'text-start',
},
{
data: "crt_d",
className: 'text-nowrap',
render: (data, type, row, meta) => {
return moment.unix(data + AppState.TIMEFIX).format('DD MMM YYYY HH:mm:ss');
}
},
{ data: "speed", className: 'text-end'},
{
data: "fulladdress",
render: (data, type, row, meta) => {
return Helper.shortenText(decodeURIComponent(data || ''), 255)
},
},
],
paging: false,
buttons: [
{
extend: 'excelHtml5',
title: () => {
return `
columns: [
// { data: 'id', visible: false }, // vhc_id
// {
// data: 'name',
// className: 'text-start text-nowrap',
// },
{
data: 'nopol1',
className: 'text-start',
},
{
data: 'dc_code',
className: 'text-start',
},
{
data: 'pool_code',
className: 'text-start',
},
{
data: "crt_d",
className: 'text-nowrap',
render: (data, type, row, meta) => {
return moment.unix(data + AppState.TIMEFIX).format(
'DD MMM YYYY HH:mm:ss');
}
},
{
data: "speed",
className: 'text-end'
},
{
data: "fulladdress",
render: (data, type, row, meta) => {
return Helper.shortenText(decodeURIComponent(data || ''), 255)
},
},
],
paging: false,
buttons: [{
extend: 'excelHtml5',
title: () => {
return `
Abnormality Trip Report -
${moment($('#tgl0').val(), "DD-MM-YYYY HH:mm").format('DD MMM YYYY HH:mm')}
to
${moment($('#tgl1').val(), "DD-MM-YYYY HH:mm").format('DD MMM YYYY HH:mm')}`
},
className: 'd-none', // hide default button
exportOptions: {
columns: ':visible:not(:first-child)' // 🔹 exclude first column
}
}
]
});
},
className: 'd-none', // hide default button
exportOptions: {
columns: ':visible:not(:first-child)' // 🔹 exclude first column
}
}]
});
},
};

View File

@ -25,8 +25,8 @@
@if ($user_role == \App\Models\Users::ROLE_VENDOR || $user_role == \App\Models\Users::ROLE_ADMIN)
@can('vehicle.create')
<!-- <div class="col text-end">
<button id="btnMdlNewVhc" class="btn btn-sm btn-danger">Add New Vehicle</button>
</div> -->
<button id="btnMdlNewVhc" class="btn btn-sm btn-danger">Add New Vehicle</button>
</div> -->
@endcan
{{-- <div class="col-auto text-end ps-0">
@ -41,21 +41,22 @@
<div class="card-body">
<!-- filter -->
<div class="row g-3 align-items-end">
<div class="col-md-2">
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">From</label>
<input type="text" class="form-control" id="tgl0" value="{{ date('d-m-Y 00:00') }}">
</div>
</div>
<div class="col-md-2">
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">To</label>
<input type="text" class="form-control" id="tgl1" value="{{ date('d-m-Y 23:59') }}">
</div>
</div>
<div class="col-md-2">
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">License Plate</label>
<select name="license_plate" class="form-control" id="filterNopol">
@ -67,7 +68,7 @@
</div>
</div>
<div class="col-md-2">
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">Vehicle Category</label>
<select name="vehicle_type" class="form-control" id="vehicleType">
@ -79,7 +80,7 @@
</div>
</div>
<div class="col-md-2">
<div class="col-6 col-md-3 col-lg-2">
<div class="form-group">
<label class="form-label text-muted mb-1">Pool</label>
<select name="poolcode" class="form-control" id="poolcode">
@ -91,14 +92,15 @@
</div>
</div>
<div class="col-md-4 d-flex justify-content-end gap-2">
<button class="btn btn-primary" id="submitFilter">
<div class="col-12 col-lg-2 d-flex gap-2 justify-content-lg-end">
<button class="btn btn-primary w-100 w-lg-auto" id="submitFilter">
<i class="fa fa-search me-1"></i> Submit
</button>
<button class="btn btn-danger" id="btnDownloadReport">
<button class="btn btn-danger w-100 w-lg-auto" id="btnDownloadReport">
<i class="fa fa-download me-1"></i> Download Report
</button>
</div>
</div>
</div>
<div class="table-responsive p-3">
@ -124,73 +126,73 @@
</tr>
</thead>
<!-- <tbody>
<tr>
<td>24-679</td>
<td>24-679</td>
<td>26</td>
<td>442.3</td>
</tr>
<tr>
<td>24-682</td>
<td>24-682</td>
<td>28</td>
<td>312.7</td>
</tr>
<tr>
<td>24-877</td>
<td>24-877</td>
<td>15</td>
<td>276.4</td>
</tr>
<tr>
<td>A.16-347</td>
<td>A.16-347</td>
<td>37</td>
<td>543.2</td>
</tr>
<tr>
<td>AL2025-B21</td>
<td>AL2025-B21</td>
<td>18</td>
<td>295.4</td>
</tr>
<tr>
<td>B.16-542</td>
<td>B.16-542</td>
<td>32</td>
<td>478.2</td>
</tr>
<tr>
<td>B.24-264</td>
<td>B.24-264</td>
<td>24</td>
<td>188.4</td>
</tr>
<tr>
<td>B.24-274</td>
<td>B.24-274</td>
<td>33</td>
<td>440.1</td>
</tr>
<tr>
<td>Ford Ranger</td>
<td>26-199</td>
<td>30</td>
<td>631.4</td>
</tr>
<tr>
<td>Nissan AD</td>
<td>B.10-517</td>
<td>27</td>
<td>375.9</td>
</tr>
<tr>
<td>Triton</td>
<td>A.29-127</td>
<td>26</td>
<td>508.2</td>
</tr>
</tbody> -->
<tr>
<td>24-679</td>
<td>24-679</td>
<td>26</td>
<td>442.3</td>
</tr>
<tr>
<td>24-682</td>
<td>24-682</td>
<td>28</td>
<td>312.7</td>
</tr>
<tr>
<td>24-877</td>
<td>24-877</td>
<td>15</td>
<td>276.4</td>
</tr>
<tr>
<td>A.16-347</td>
<td>A.16-347</td>
<td>37</td>
<td>543.2</td>
</tr>
<tr>
<td>AL2025-B21</td>
<td>AL2025-B21</td>
<td>18</td>
<td>295.4</td>
</tr>
<tr>
<td>B.16-542</td>
<td>B.16-542</td>
<td>32</td>
<td>478.2</td>
</tr>
<tr>
<td>B.24-264</td>
<td>B.24-264</td>
<td>24</td>
<td>188.4</td>
</tr>
<tr>
<td>B.24-274</td>
<td>B.24-274</td>
<td>33</td>
<td>440.1</td>
</tr>
<tr>
<td>Ford Ranger</td>
<td>26-199</td>
<td>30</td>
<td>631.4</td>
</tr>
<tr>
<td>Nissan AD</td>
<td>B.10-517</td>
<td>27</td>
<td>375.9</td>
</tr>
<tr>
<td>Triton</td>
<td>A.29-127</td>
<td>26</td>
<td>508.2</td>
</tr>
</tbody> -->
</table>
</div>
</div>
@ -202,17 +204,17 @@
<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="mdlDetailTrip"
aria-labelledby="mdlDetailTripLabel" aria-hidden="true">
<!-- <div class="modal-dialog modal-dialog modal-dialog-centered modal-dialog-scrollable modal-xl">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="mdlDetailTripLabel">-----</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="mdlDetailTripLabel">-----</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
</div>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
</div>
</div>
</div> -->
</div> -->
</div>
@endsection