update trip report

This commit is contained in:
Pringgosutono
2025-09-02 09:57:39 +07:00
parent cf6594a5da
commit 3ad22e0cf5
2 changed files with 96 additions and 74 deletions

View File

@ -97,79 +97,79 @@ class ReportsController extends Controller
$list = DB::select($q, $d);
// RETURN 1 - LIST
if($req->type != 'report'){
// // 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'){
$headings = ['Name', 'License Plate', 'Number of Trip', 'Total Mileage'];
// // RETURN 2 - REPORT
// if($req->type == 'report'){
// $headings = ['Name', 'License Plate', 'Number of Trip', 'Total Mileage'];
$export = new class($list, $headings) implements FromArray, WithHeadings {
private $list;
private $headings;
// $export = new class($list, $headings) implements FromArray, WithHeadings {
// private $list;
// private $headings;
public function __construct($list, $headings)
{
$this->list = $list;
$this->headings = $headings;
}
// public function __construct($list, $headings)
// {
// $this->list = $list;
// $this->headings = $headings;
// }
public function array(): array
{
return array_map(function ($item) {
return [
$item->name,
$item->nopol1,
$item->numOfTrip,
$item->total_milleage,
];
}, $this->list);
}
// public function array(): array
// {
// return array_map(function ($item) {
// return [
// $item->name,
// $item->nopol1,
// $item->numOfTrip,
// $item->total_milleage,
// ];
// }, $this->list);
// }
public function headings(): array
{
return $this->headings;
}
// public function headings(): array
// {
// return $this->headings;
// }
// Start table from A3
public function startCell(): string
{
return 'A3';
}
// // Start table from A3
// public function startCell(): string
// {
// return 'A3';
// }
// Add title & border styling
public function styles(Worksheet $sheet)
{
$tgl0 = date('d-m-Y', $GLOBALS['from_date']);
$tgl1 = date('d-m-Y', $GLOBALS['to_date']);
// // Add title & border styling
// public function styles(Worksheet $sheet)
// {
// $tgl0 = date('d-m-Y', $GLOBALS['from_date']);
// $tgl1 = date('d-m-Y', $GLOBALS['to_date']);
// Title in A1
$sheet->setCellValue('A1', 'Vehicle Trip Report $tgl0 until $tgl1');
$sheet->mergeCells('A1:D1'); // Merge across 4 columns
$sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16);
$sheet->getStyle('A1')->getAlignment()->setHorizontal('center');
// // Title in A1
// $sheet->setCellValue('A1', 'Vehicle Trip Report $tgl0 until $tgl1');
// $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(16);
// $sheet->getStyle('A1')->getAlignment()->setHorizontal('center');
// $sheet->mergeCells('A1:D1'); // Merge across 4 columns
// Get last row
$lastRow = $this->list->count() + 3; // 3 = heading row
$range = "A3:D{$lastRow}";
// // Get last row
// $lastRow = $this->list->count() + 3; // 3 = heading row
// $range = "A3:D{$lastRow}";
// Add borders
$sheet->getStyle($range)->getBorders()->getAllBorders()
->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
// // Add borders
// $sheet->getStyle($range)->getBorders()->getAllBorders()
// ->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
// Bold headings
$sheet->getStyle('A3:D3')->getFont()->setBold(true);
// // Bold headings
// $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) {
$apiResp = Responses::error($e->getMessage());
return new Response($apiResp, $apiResp["meta"]["code"]);

View File

@ -183,26 +183,48 @@
const DTable = {
table: null,
lastAjax: null, // keep track of the last ajax request
activate: function() {
DTable.reload();
},
reload: function() {
DTable.table = $('#tVehicleTrips').DataTable({
processing: true,
serverSide: false,
bLengthChange: true,
deferRender: true,
destroy: true,
ajax: {
url: `{{ route('api_report_vehicle_trips_list') }}?
cptid=${AppState.current_company}
&from_date=${moment(safeVal('#tgl0')).startOf('day').unix()}
&to_date=${moment(safeVal('#tgl1')).endOf('day').unix()}`,
type: 'GET',
complete: function() {
// removeTableSkeletonLoading()
},
},
// Abort the last request if it's still running
if (DTable.lastAjax) {
DTable.lastAjax.abort();
}
DTable.table = $('#tVehicleTrips').DataTable({
processing: true,
serverSide: false,
bLengthChange: true,
deferRender: true,
destroy: true,
ajax: function(data, callback, settings) {
// Abort previous
if (DTable.lastAjax) {
DTable.lastAjax.abort();
}
// Fire new request
DTable.lastAjax = $.ajax({
url: `{{ route('api_report_vehicle_trips_list') }}?
cptid=${AppState.current_company}
&from_date=${moment(safeVal('#tgl0')).startOf('day').unix()}
&to_date=${moment(safeVal('#tgl1')).endOf('day').unix()}`,
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: [
{
@ -240,7 +262,7 @@
buttons: [
{
extend: 'excelHtml5',
title: 'Trip Report',
title: `Vehicle Trip Report - ${moment(safeVal('#tgl0')).format('DD MMM YYYY')} to ${moment(safeVal('#tgl1')).format('DD MMM YYYY')}`,
className: 'd-none' // hide default button
}
]