penambahan fitur ekspor di hse, qma, dan pg
This commit is contained in:
@@ -5,21 +5,25 @@ namespace App\Http\Controllers\Qma;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Gate;
|
||||
use App\Models\VehicleActivity;
|
||||
use App\Services\QmaTablePdfExporter;
|
||||
use App\Services\SimpleXlsxExporter;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ActivityLogController extends Controller
|
||||
{
|
||||
/**
|
||||
* Tampilkan Log Aktivitas QMA 10 Kolom lengkap dengan filter tanggal dan gate.
|
||||
* Helper query builder untuk log aktivitas kendaraan dengan seluruh filter aktif.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
private function getFilteredActivityQuery(Request $request): Builder
|
||||
{
|
||||
$query = VehicleActivity::with(['vehicle', 'driver', 'gate', 'securityGuard'])
|
||||
->latest('recorded_at');
|
||||
|
||||
// Filter Rentang Tanggal (Default Hari Ini)
|
||||
// Filter Rentang Tanggal (Default Hari Ini jika tidak diisi)
|
||||
$today = Carbon::today()->toDateString();
|
||||
$startDate = $request->input('start_date', $today);
|
||||
$endDate = $request->input('end_date', $today);
|
||||
@@ -64,12 +68,225 @@ class ActivityLogController extends Controller
|
||||
$query->where('validation_status', $request->input('status'));
|
||||
}
|
||||
|
||||
$activities = $query->paginate(20)->withQueryString();
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan Log Aktivitas QMA 10 Kolom lengkap dengan filter tanggal dan gate.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$today = Carbon::today()->toDateString();
|
||||
$startDate = $request->input('start_date', $today);
|
||||
$endDate = $request->input('end_date', $today);
|
||||
|
||||
$activities = $this->getFilteredActivityQuery($request)->paginate(20)->withQueryString();
|
||||
$gates = Gate::where('is_active', true)->orderBy('name')->get();
|
||||
|
||||
return view('qma.activity_log', compact('activities', 'gates', 'startDate', 'endDate'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Log Aktivitas Kendaraan (Timesheet LV) ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportExcel(Request $request): Response
|
||||
{
|
||||
$activities = $this->getFilteredActivityQuery($request)->get();
|
||||
|
||||
$today = Carbon::today()->toDateString();
|
||||
$startDate = $request->input('start_date', $today);
|
||||
$endDate = $request->input('end_date', $today);
|
||||
|
||||
$gateText = 'Semua Gerbang';
|
||||
if ($request->filled('gate_id')) {
|
||||
$gateObj = Gate::find($request->input('gate_id'));
|
||||
if ($gateObj) $gateText = $gateObj->name;
|
||||
}
|
||||
|
||||
$dirText = $request->filled('direction') ? ($request->input('direction') === 'in' ? 'Masuk (In)' : 'Keluar (Out)') : 'Semua Arah';
|
||||
$statusText = $request->filled('status') ? ($request->input('status') === 'approved' ? 'Disetujui PG' : 'Menunggu PG') : 'Semua Status';
|
||||
$printedAt = now()->translatedFormat('d F Y, H:i');
|
||||
|
||||
$metaLines = [
|
||||
'Periode: ' . Carbon::parse($startDate)->translatedFormat('d M Y') . ' s/d ' . Carbon::parse($endDate)->translatedFormat('d M Y') .
|
||||
' | Gerbang: ' . $gateText . ' | Arah: ' . $dirText . ' | Status: ' . $statusText,
|
||||
'Total Data: ' . $activities->count() . ' Catatan Pergerakan | Tanggal Dicetak: ' . $printedAt . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Tanggal & Waktu' => 20,
|
||||
'Kode LV' => 14,
|
||||
'Unit Kendaraan' => 28,
|
||||
'No. Polisi' => 16,
|
||||
'Penanggung Jawab' => 22,
|
||||
'Pengemudi (Driver)' => 26,
|
||||
'Pos Gerbang' => 20,
|
||||
'Arah' => 12,
|
||||
'KM Lalu' => 14,
|
||||
'KM Kini' => 14,
|
||||
'Selisih KM' => 14,
|
||||
'Aktivitas / Keperluan' => 34,
|
||||
'Status PG' => 18,
|
||||
'Petugas Security' => 22,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($activities as $idx => $act) {
|
||||
$formattedDate = $act->recorded_at ? $act->recorded_at->translatedFormat('d/m/Y H:i') . ' WIB' : '-';
|
||||
$vehicleCode = $act->vehicle?->code ?: '-';
|
||||
$vehicleUnit = $act->vehicle?->brand_type ?: '-';
|
||||
$plat = $act->vehicle?->license_plate ?: '-';
|
||||
$dept = $act->vehicle?->department ?: ($act->vehicle?->division ?: '-');
|
||||
$driverName = $act->driver ? $act->driver->name . ' (' . $act->driver->nik . ')' : '-';
|
||||
$gateName = $act->gate?->name ?: '-';
|
||||
$direction = strtoupper($act->direction ?: '-');
|
||||
$prevKm = $act->previous_km !== null ? number_format($act->previous_km, 0, ',', '.') : '-';
|
||||
$currKm = $act->current_km !== null ? number_format($act->current_km, 0, ',', '.') : '-';
|
||||
$diffKm = $act->distance !== null ? number_format($act->distance, 0, ',', '.') : '-';
|
||||
$purpose = $act->purpose ?: '-';
|
||||
$statusPg = $act->validation_status === 'approved' ? 'Disetujui' : ($act->validation_status === 'rejected' ? 'Ditolak' : 'Menunggu PG');
|
||||
$guard = $act->securityGuard?->name ?: '-';
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$formattedDate,
|
||||
$vehicleCode,
|
||||
$vehicleUnit,
|
||||
$plat,
|
||||
$dept,
|
||||
$driverName,
|
||||
$gateName,
|
||||
$direction,
|
||||
$prevKm,
|
||||
$currKm,
|
||||
$diffKm,
|
||||
$purpose,
|
||||
$statusPg,
|
||||
$guard,
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'center', 'left', 'center', 'left', 'left', 'center', 'center', 'right', 'right', 'right', 'left', 'center', 'left'];
|
||||
$title = 'LAPORAN TIMESHEET & LOG AKTIVITAS LIGHT VEHICLE - PT KERINCI MERANGIN HIDRO';
|
||||
$fileName = 'Laporan_Timesheet_LV_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
|
||||
$binary = SimpleXlsxExporter::create($title, $metaLines, $headers, $rows, $alignments);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Log Aktivitas Kendaraan (Timesheet LV) ke format PDF (Pratinjau Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportPdf(Request $request): Response
|
||||
{
|
||||
$activities = $this->getFilteredActivityQuery($request)->get();
|
||||
|
||||
$today = Carbon::today()->toDateString();
|
||||
$startDate = $request->input('start_date', $today);
|
||||
$endDate = $request->input('end_date', $today);
|
||||
|
||||
$gateText = 'Semua Gerbang';
|
||||
if ($request->filled('gate_id')) {
|
||||
$gateObj = Gate::find($request->input('gate_id'));
|
||||
if ($gateObj) $gateText = $gateObj->name;
|
||||
}
|
||||
|
||||
$dirText = $request->filled('direction') ? ($request->input('direction') === 'in' ? 'Masuk (In)' : 'Keluar (Out)') : 'Semua Arah';
|
||||
$statusText = $request->filled('status') ? ($request->input('status') === 'approved' ? 'Disetujui PG' : 'Menunggu PG') : 'Semua Status';
|
||||
$dateRangeStr = Carbon::parse($startDate)->translatedFormat('d M Y') . ' s/d ' . Carbon::parse($endDate)->translatedFormat('d M Y');
|
||||
|
||||
$totalKm = $activities->sum('distance');
|
||||
$inCount = $activities->where('direction', 'in')->count();
|
||||
$outCount = $activities->where('direction', 'out')->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Periode Tanggal' => $dateRangeStr,
|
||||
'Pos Gerbang' => $gateText,
|
||||
'Arah / Status' => "Arah: {$dirText} | Status: {$statusText}",
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Total Catatan' => $activities->count() . ' Pergerakan Armada',
|
||||
'Total Jarak Tempuh' => number_format($totalKm, 1, ',', '.') . ' KM',
|
||||
'Rincian Arah' => "{$inCount} Masuk, {$outCount} Keluar",
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 8, 'align' => 'C'],
|
||||
['title' => 'TANGGAL & JAM', 'width' => 25, 'align' => 'C'],
|
||||
['title' => 'KODE LV', 'width' => 16, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'UNIT KENDARAAN', 'width' => 34, 'align' => 'L', 'max_char' => 22],
|
||||
['title' => 'DRIVER (PENGEMUDI)', 'width' => 30, 'align' => 'L', 'max_char' => 18],
|
||||
['title' => 'GERBANG', 'width' => 22, 'align' => 'C', 'max_char' => 14],
|
||||
['title' => 'AKSES', 'width' => 16, 'align' => 'C', 'is_status' => true],
|
||||
['title' => 'KM LALU', 'width' => 18, 'align' => 'R'],
|
||||
['title' => 'KM KINI', 'width' => 18, 'align' => 'R'],
|
||||
['title' => 'JARAK', 'width' => 16, 'align' => 'R'],
|
||||
['title' => 'AKTIVITAS / KEPERLUAN', 'width' => 42, 'align' => 'L', 'max_char' => 30],
|
||||
['title' => 'STATUS PG', 'width' => 22, 'align' => 'C', 'is_status' => true],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($activities as $idx => $act) {
|
||||
$formattedDate = $act->recorded_at ? $act->recorded_at->translatedFormat('d/m/y H:i') : '-';
|
||||
$vehicleCode = $act->vehicle?->code ?: '-';
|
||||
$vehicleUnit = $act->vehicle?->brand_type ?: '-';
|
||||
$driverName = $act->driver?->name ?: '-';
|
||||
$gateName = $act->gate?->name ?: '-';
|
||||
$direction = strtoupper($act->direction ?: '-');
|
||||
$prevKm = $act->previous_km !== null ? number_format($act->previous_km, 0, ',', '.') : '-';
|
||||
$currKm = $act->current_km !== null ? number_format($act->current_km, 0, ',', '.') : '-';
|
||||
$diffKm = $act->distance !== null ? number_format($act->distance, 0, ',', '.') . ' KM' : '-';
|
||||
$purpose = $act->purpose ?: '-';
|
||||
$statusPg = $act->validation_status === 'approved' ? 'Disetujui' : ($act->validation_status === 'rejected' ? 'Ditolak' : 'Menunggu PG');
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$formattedDate,
|
||||
$vehicleCode,
|
||||
$vehicleUnit,
|
||||
$driverName,
|
||||
$gateName,
|
||||
$direction,
|
||||
$prevKm,
|
||||
$currKm,
|
||||
$diffKm,
|
||||
$purpose,
|
||||
$statusPg,
|
||||
];
|
||||
}
|
||||
|
||||
$title = 'Laporan Timesheet & Aktivitas Light Vehicle (LV)';
|
||||
$subtitle = 'Log Pergerakan Armada Gerbang Proyek KMH - Periode: ' . $dateRangeStr;
|
||||
$pdfBinary = QmaTablePdfExporter::generate(
|
||||
$title,
|
||||
$subtitle,
|
||||
'LOG AKTIVITAS RKM-KMH',
|
||||
$metaLeft,
|
||||
$metaRight,
|
||||
$columns,
|
||||
$rows
|
||||
);
|
||||
|
||||
$fileName = 'Laporan_Timesheet_LV_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan Summary Aktivitas Kendaraan (Rekapitulasi Armada, Driver, Pos Gerbang & Statistik).
|
||||
*/
|
||||
@@ -173,4 +390,265 @@ class ActivityLogController extends Controller
|
||||
'gates'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Rekapitulasi Summary Aktivitas Kendaraan ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportSummaryExcel(Request $request): Response
|
||||
{
|
||||
$today = Carbon::today()->toDateString();
|
||||
$startDate = $request->input('start_date', $today);
|
||||
$endDate = $request->input('end_date', $today);
|
||||
$type = $request->input('type');
|
||||
$gateId = $request->input('gate_id');
|
||||
|
||||
$query = VehicleActivity::query();
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$start = Carbon::parse($startDate)->startOfDay();
|
||||
$end = Carbon::parse($endDate)->endOfDay();
|
||||
$query->whereBetween('recorded_at', [$start, $end]);
|
||||
} elseif ($startDate) {
|
||||
$query->where('recorded_at', '>=', Carbon::parse($startDate)->startOfDay());
|
||||
} elseif ($endDate) {
|
||||
$query->where('recorded_at', '<=', Carbon::parse($endDate)->endOfDay());
|
||||
}
|
||||
|
||||
if ($request->filled('gate_id')) {
|
||||
$query->where('gate_id', $gateId);
|
||||
}
|
||||
|
||||
if ($request->filled('type')) {
|
||||
$type = $request->input('type');
|
||||
$query->whereHas('vehicle', function ($q) use ($type) {
|
||||
if ($type === 'lv' || $type === 'light_vehicle') {
|
||||
$q->where('category', 'light_vehicle');
|
||||
} elseif ($type === 'support' || $type === 'unit_support') {
|
||||
$q->where('category', 'unit_support');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$vehicleSummaries = (clone $query)
|
||||
->selectRaw("vehicle_id, COUNT(*) as total_trips, SUM(CASE WHEN direction = 'in' THEN 1 ELSE 0 END) as in_trips, SUM(CASE WHEN direction = 'out' THEN 1 ELSE 0 END) as out_trips, SUM(distance) as total_km")
|
||||
->groupBy('vehicle_id')
|
||||
->with('vehicle')
|
||||
->orderByDesc('total_km')
|
||||
->get();
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = trim($request->input('search'));
|
||||
$vehicleSummaries = $vehicleSummaries->filter(function ($item) use ($search) {
|
||||
$veh = $item->vehicle;
|
||||
if (!$veh) return false;
|
||||
$haystack = strtolower(($veh->code ?? '') . ' ' . ($veh->brand_type ?? '') . ' ' . ($veh->license_plate ?? '') . ' ' . ($veh->ownership ?? '') . ' ' . ($veh->division ?? ''));
|
||||
return str_contains($haystack, strtolower($search));
|
||||
})->values();
|
||||
}
|
||||
|
||||
$gateText = 'Semua Gerbang';
|
||||
if ($request->filled('gate_id')) {
|
||||
$g = Gate::find($gateId);
|
||||
if ($g) $gateText = $g->name;
|
||||
}
|
||||
|
||||
$typeText = 'Semua Armada';
|
||||
if ($type === 'lv' || $type === 'light_vehicle') $typeText = 'Light Vehicle';
|
||||
elseif ($type === 'support' || $type === 'unit_support') $typeText = 'Unit Support';
|
||||
|
||||
$totalKmAll = $vehicleSummaries->sum('total_km');
|
||||
$totalTripsAll = $vehicleSummaries->sum('total_trips');
|
||||
$dateRangeStr = Carbon::parse($startDate)->translatedFormat('d M Y') . ' s/d ' . Carbon::parse($endDate)->translatedFormat('d M Y');
|
||||
$printedAt = now()->translatedFormat('d F Y, H:i');
|
||||
|
||||
$metaLines = [
|
||||
'Periode: ' . $dateRangeStr . ' | Kategori Armada: ' . $typeText . ' | Gerbang: ' . $gateText .
|
||||
($request->filled('search') ? ' | Pencarian: "' . $request->input('search') . '"' : ''),
|
||||
'Total: ' . $vehicleSummaries->count() . ' Unit Aktif | Akumulasi Trip: ' . number_format($totalTripsAll, 0, ',', '.') . ' Trip | Total Jarak: ' . number_format($totalKmAll, 1, ',', '.') . ' KM | Dicetak: ' . $printedAt . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Kode Unit' => 16,
|
||||
'Unit / Merek' => 30,
|
||||
'No. Polisi' => 16,
|
||||
'Kategori' => 18,
|
||||
'Penanggung Jawab' => 24,
|
||||
'Divisi' => 20,
|
||||
'Trip Masuk' => 14,
|
||||
'Trip Keluar' => 14,
|
||||
'Total Trip' => 14,
|
||||
'Total Jarak (KM)' => 18,
|
||||
'Odometer Terkini' => 18,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($vehicleSummaries as $idx => $vs) {
|
||||
$veh = $vs->vehicle;
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$veh?->code ?: '-',
|
||||
$veh?->brand_type ?: '-',
|
||||
$veh?->license_plate ?: '-',
|
||||
$veh?->isLightVehicle() ? 'Light Vehicle' : 'Unit Support',
|
||||
$veh?->ownership ?: '-',
|
||||
$veh?->division ?: ($veh?->department ?: '-'),
|
||||
(int) $vs->in_trips,
|
||||
(int) $vs->out_trips,
|
||||
(int) $vs->total_trips,
|
||||
number_format($vs->total_km, 1, ',', '.'),
|
||||
$veh?->current_km !== null ? number_format($veh->current_km, 0, ',', '.') . ' KM' : '-',
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'left', 'center', 'center', 'left', 'left', 'center', 'center', 'center', 'right', 'right'];
|
||||
$title = 'LAPORAN REKAPITULASI SUMMARY AKTIVITAS PER KENDARAAN - PT KERINCI MERANGIN HIDRO';
|
||||
$fileName = 'Laporan_Summary_Aktivitas_Kendaraan_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
|
||||
$binary = SimpleXlsxExporter::create($title, $metaLines, $headers, $rows, $alignments);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Rekapitulasi Summary Aktivitas Kendaraan ke format PDF (Pratinjau Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportSummaryPdf(Request $request): Response
|
||||
{
|
||||
$today = Carbon::today()->toDateString();
|
||||
$startDate = $request->input('start_date', $today);
|
||||
$endDate = $request->input('end_date', $today);
|
||||
$type = $request->input('type');
|
||||
$gateId = $request->input('gate_id');
|
||||
|
||||
$query = VehicleActivity::query();
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$start = Carbon::parse($startDate)->startOfDay();
|
||||
$end = Carbon::parse($endDate)->endOfDay();
|
||||
$query->whereBetween('recorded_at', [$start, $end]);
|
||||
} elseif ($startDate) {
|
||||
$query->where('recorded_at', '>=', Carbon::parse($startDate)->startOfDay());
|
||||
} elseif ($endDate) {
|
||||
$query->where('recorded_at', '<=', Carbon::parse($endDate)->endOfDay());
|
||||
}
|
||||
|
||||
if ($request->filled('gate_id')) {
|
||||
$query->where('gate_id', $gateId);
|
||||
}
|
||||
|
||||
if ($request->filled('type')) {
|
||||
$type = $request->input('type');
|
||||
$query->whereHas('vehicle', function ($q) use ($type) {
|
||||
if ($type === 'lv' || $type === 'light_vehicle') {
|
||||
$q->where('category', 'light_vehicle');
|
||||
} elseif ($type === 'support' || $type === 'unit_support') {
|
||||
$q->where('category', 'unit_support');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$vehicleSummaries = (clone $query)
|
||||
->selectRaw("vehicle_id, COUNT(*) as total_trips, SUM(CASE WHEN direction = 'in' THEN 1 ELSE 0 END) as in_trips, SUM(CASE WHEN direction = 'out' THEN 1 ELSE 0 END) as out_trips, SUM(distance) as total_km")
|
||||
->groupBy('vehicle_id')
|
||||
->with('vehicle')
|
||||
->orderByDesc('total_km')
|
||||
->get();
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = trim($request->input('search'));
|
||||
$vehicleSummaries = $vehicleSummaries->filter(function ($item) use ($search) {
|
||||
$veh = $item->vehicle;
|
||||
if (!$veh) return false;
|
||||
$haystack = strtolower(($veh->code ?? '') . ' ' . ($veh->brand_type ?? '') . ' ' . ($veh->license_plate ?? '') . ' ' . ($veh->ownership ?? '') . ' ' . ($veh->division ?? ''));
|
||||
return str_contains($haystack, strtolower($search));
|
||||
})->values();
|
||||
}
|
||||
|
||||
$gateText = 'Semua Gerbang';
|
||||
if ($request->filled('gate_id')) {
|
||||
$g = Gate::find($gateId);
|
||||
if ($g) $gateText = $g->name;
|
||||
}
|
||||
|
||||
$typeText = 'Semua Armada';
|
||||
if ($type === 'lv' || $type === 'light_vehicle') $typeText = 'Light Vehicle';
|
||||
elseif ($type === 'support' || $type === 'unit_support') $typeText = 'Unit Support';
|
||||
|
||||
$totalKmAll = $vehicleSummaries->sum('total_km');
|
||||
$totalTripsAll = $vehicleSummaries->sum('total_trips');
|
||||
$dateRangeStr = Carbon::parse($startDate)->translatedFormat('d M Y') . ' s/d ' . Carbon::parse($endDate)->translatedFormat('d M Y');
|
||||
|
||||
$metaLeft = [
|
||||
'Periode Tanggal' => $dateRangeStr,
|
||||
'Kategori Armada' => $typeText,
|
||||
'Pos Gerbang' => $gateText,
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Armada Beroperasi' => $vehicleSummaries->count() . ' Unit Aktif',
|
||||
'Akumulasi Trip' => number_format($totalTripsAll, 0, ',', '.') . ' Pergerakan',
|
||||
'Total Jarak Tempuh' => number_format($totalKmAll, 1, ',', '.') . ' KM',
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 10, 'align' => 'C'],
|
||||
['title' => 'KODE UNIT', 'width' => 22, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'UNIT / MEREK', 'width' => 48, 'align' => 'L', 'max_char' => 30],
|
||||
['title' => 'NO. POLISI', 'width' => 22, 'align' => 'C'],
|
||||
['title' => 'KATEGORI', 'width' => 26, 'align' => 'C'],
|
||||
['title' => 'PENANGGUNG JAWAB', 'width' => 40, 'align' => 'L', 'max_char' => 26],
|
||||
['title' => 'MASUK', 'width' => 18, 'align' => 'C'],
|
||||
['title' => 'KELUAR', 'width' => 18, 'align' => 'C'],
|
||||
['title' => 'TOTAL TRIP', 'width' => 20, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'TOTAL JARAK', 'width' => 25, 'align' => 'R', 'bold' => true],
|
||||
['title' => 'ODOMETER', 'width' => 18, 'align' => 'R'],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($vehicleSummaries as $idx => $vs) {
|
||||
$veh = $vs->vehicle;
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$veh?->code ?: '-',
|
||||
$veh?->brand_type ?: '-',
|
||||
$veh?->license_plate ?: '-',
|
||||
$veh?->isLightVehicle() ? 'Light Vehicle' : 'Unit Support',
|
||||
$veh?->ownership ?: '-',
|
||||
(int) $vs->in_trips,
|
||||
(int) $vs->out_trips,
|
||||
(int) $vs->total_trips,
|
||||
number_format($vs->total_km, 1, ',', '.') . ' KM',
|
||||
$veh?->current_km !== null ? number_format($veh->current_km, 0, ',', '.') : '-',
|
||||
];
|
||||
}
|
||||
|
||||
$title = 'Laporan Summary Aktivitas per Armada Kendaraan';
|
||||
$subtitle = 'Rekapitulasi Pergerakan, Frekuensi Trip, dan Jarak Tempuh - Periode: ' . $dateRangeStr;
|
||||
$pdfBinary = QmaTablePdfExporter::generate(
|
||||
$title,
|
||||
$subtitle,
|
||||
'SUMMARY AKTIVITAS RKM-KMH',
|
||||
$metaLeft,
|
||||
$metaRight,
|
||||
$columns,
|
||||
$rows
|
||||
);
|
||||
|
||||
$fileName = 'Laporan_Summary_Aktivitas_Kendaraan_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,18 +5,22 @@ namespace App\Http\Controllers\Qma;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\P2hRecord;
|
||||
use App\Models\Vehicle;
|
||||
use App\Services\QmaTablePdfExporter;
|
||||
use App\Services\SimpleXlsxExporter;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class P2hInspectionController extends Controller
|
||||
{
|
||||
/**
|
||||
* Tampilkan riwayat dan log pemeriksaan P2H Unit Support untuk Admin QMA.
|
||||
* Helper query builder untuk riwayat pemeriksaan P2H Unit Support dengan filter aktif.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
private function getFilteredP2hQuery(Request $request): Builder
|
||||
{
|
||||
$query = P2hRecord::with(['vehicle', 'employee', 'template', 'validator']);
|
||||
|
||||
@@ -45,7 +49,15 @@ class P2hInspectionController extends Controller
|
||||
});
|
||||
}
|
||||
|
||||
$records = $query->orderByDesc('inspected_at')->paginate(15)->withQueryString();
|
||||
return $query->orderByDesc('inspected_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan riwayat dan log pemeriksaan P2H Unit Support untuk Admin QMA.
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$records = $this->getFilteredP2hQuery($request)->paginate(15)->withQueryString();
|
||||
|
||||
// Statistik Ringkasan P2H
|
||||
$totalP2h = P2hRecord::count();
|
||||
@@ -64,6 +76,191 @@ class P2hInspectionController extends Controller
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Riwayat Pemeriksaan P2H Unit Support ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportExcel(Request $request): Response
|
||||
{
|
||||
$records = $this->getFilteredP2hQuery($request)->get();
|
||||
|
||||
$dateFilter = $request->filled('date') ? Carbon::parse($request->input('date'))->translatedFormat('d F Y') : 'Semua Tanggal';
|
||||
$statusFilter = $request->filled('validation_status') ?
|
||||
($request->input('validation_status') === 'approved' ? 'Disetujui QMA' :
|
||||
($request->input('validation_status') === 'rejected' ? 'Ditolak' : 'Menunggu Review')) : 'Semua Status';
|
||||
$condFilter = $request->filled('summary_condition') ?
|
||||
($request->input('summary_condition') === 'has_defect' ? 'Ada Kerusakan' : 'Normal') : 'Semua Kondisi';
|
||||
$printedAt = now()->translatedFormat('d F Y, H:i');
|
||||
|
||||
$metaLines = [
|
||||
'Filter: Tanggal: ' . $dateFilter . ' | Status QMA: ' . $statusFilter . ' | Kondisi Fisik: ' . $condFilter .
|
||||
($request->filled('search') ? ' | Pencarian: "' . $request->input('search') . '"' : ''),
|
||||
'Total Data: ' . $records->count() . ' Form Pemeriksaan P2H | Tanggal Dicetak: ' . $printedAt . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Waktu Inspeksi' => 20,
|
||||
'No. Transaksi' => 22,
|
||||
'Kode Unit' => 16,
|
||||
'Kategori & Model Alat' => 30,
|
||||
'Operator (SIMPER)' => 28,
|
||||
'NIK' => 16,
|
||||
'Shift' => 14,
|
||||
'HM Awal Operasi' => 18,
|
||||
'Kondisi Fisik' => 20,
|
||||
'Jumlah Temuan' => 16,
|
||||
'Status QMA' => 20,
|
||||
'Catatan Inspeksi / Temuan' => 36,
|
||||
'Validator QMA' => 24,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($records as $idx => $rec) {
|
||||
$waktu = $rec->inspected_at ? $rec->inspected_at->translatedFormat('d/m/Y H:i') . ' WIB' : '-';
|
||||
$unitCode = $rec->vehicle?->code ?: '-';
|
||||
$unitModel = $rec->vehicle?->brand_type ?: ($rec->vehicle?->unit_category ?: '-');
|
||||
$opName = $rec->employee?->name ?: '-';
|
||||
$opNik = $rec->employee?->nik ?: '-';
|
||||
$shift = $rec->shift ?: '-';
|
||||
$hm = $rec->start_hm !== null ? number_format($rec->start_hm, 1, ',', '.') . ' HM' : '-';
|
||||
$kondisi = $rec->hasDefects() ? 'Ada Kerusakan (' . $rec->defect_count . ' Item)' : 'Normal (Baik)';
|
||||
$defectCount = $rec->defect_count ?: '0';
|
||||
$statusQma = $rec->isApproved() ? 'Disetujui QMA' : ($rec->isRejected() ? 'Ditolak' : 'Menunggu Review');
|
||||
$notes = $rec->general_notes ?: ($rec->validation_notes ?: '-');
|
||||
$validator = $rec->validator?->name ?: '-';
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$waktu,
|
||||
$rec->transaction_no ?: '-',
|
||||
$unitCode,
|
||||
$unitModel,
|
||||
$opName,
|
||||
$opNik,
|
||||
$shift,
|
||||
$hm,
|
||||
$kondisi,
|
||||
$defectCount,
|
||||
$statusQma,
|
||||
$notes,
|
||||
$validator,
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'center', 'center', 'left', 'left', 'center', 'center', 'right', 'center', 'center', 'center', 'left', 'left'];
|
||||
$title = 'LAPORAN PEMERIKSAAN HARIAN (P2H) ALAT BERAT & UNIT SUPPORT - PT KERINCI MERANGIN HIDRO';
|
||||
$fileName = 'Laporan_P2H_Unit_Support_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
|
||||
$binary = SimpleXlsxExporter::create($title, $metaLines, $headers, $rows, $alignments);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Riwayat Pemeriksaan P2H Unit Support ke format PDF (Pratinjau Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportPdf(Request $request): Response
|
||||
{
|
||||
$records = $this->getFilteredP2hQuery($request)->get();
|
||||
|
||||
$dateFilter = $request->filled('date') ? Carbon::parse($request->input('date'))->translatedFormat('d F Y') : 'Semua Tanggal';
|
||||
$statusFilter = $request->filled('validation_status') ?
|
||||
($request->input('validation_status') === 'approved' ? 'Disetujui QMA' :
|
||||
($request->input('validation_status') === 'rejected' ? 'Ditolak' : 'Menunggu Review')) : 'Semua Status';
|
||||
$condFilter = $request->filled('summary_condition') ?
|
||||
($request->input('summary_condition') === 'has_defect' ? 'Ada Kerusakan' : 'Normal') : 'Semua Kondisi';
|
||||
|
||||
$approvedCount = $records->where('validation_status', 'approved')->count();
|
||||
$pendingCount = $records->where('validation_status', 'pending')->count();
|
||||
$rejectedCount = $records->where('validation_status', 'rejected')->count();
|
||||
$defectCount = $records->where('summary_condition', 'has_defect')->count();
|
||||
$normalCount = $records->where('summary_condition', '!=', 'has_defect')->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Filter Tanggal' => $dateFilter,
|
||||
'Status Approval' => $statusFilter,
|
||||
'Kondisi Fisik' => $condFilter,
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Total Pemeriksaan' => $records->count() . ' Form P2H Terverifikasi',
|
||||
'Rincian Kondisi' => "{$normalCount} Normal, {$defectCount} Ada Kerusakan",
|
||||
'Rincian Status' => "{$approvedCount} Disetujui, {$pendingCount} Menunggu, {$rejectedCount} Ditolak",
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 8, 'align' => 'C'],
|
||||
['title' => 'WAKTU', 'width' => 24, 'align' => 'C'],
|
||||
['title' => 'NO. TRANSAKSI', 'width' => 26, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'KODE', 'width' => 16, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'MODEL ALAT', 'width' => 34, 'align' => 'L', 'max_char' => 22],
|
||||
['title' => 'OPERATOR', 'width' => 32, 'align' => 'L', 'max_char' => 20],
|
||||
['title' => 'SHIFT', 'width' => 14, 'align' => 'C'],
|
||||
['title' => 'HM', 'width' => 18, 'align' => 'R'],
|
||||
['title' => 'KONDISI', 'width' => 28, 'align' => 'C', 'is_status' => true],
|
||||
['title' => 'STATUS QMA', 'width' => 25, 'align' => 'C', 'is_status' => true],
|
||||
['title' => 'VALIDATOR', 'width' => 22, 'align' => 'L', 'max_char' => 15],
|
||||
['title' => 'CATATAN', 'width' => 20, 'align' => 'L', 'max_char' => 14],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($records as $idx => $rec) {
|
||||
$waktu = $rec->inspected_at ? $rec->inspected_at->translatedFormat('d/m/y H:i') : '-';
|
||||
$unitCode = $rec->vehicle?->code ?: '-';
|
||||
$unitModel = $rec->vehicle?->brand_type ?: '-';
|
||||
$opName = $rec->employee?->name ?: '-';
|
||||
$shift = $rec->shift ?: '-';
|
||||
$hm = $rec->start_hm !== null ? number_format($rec->start_hm, 1, ',', '.') : '-';
|
||||
$kondisi = $rec->hasDefects() ? 'Ada Kerusakan (' . $rec->defect_count . ')' : 'Normal (Baik)';
|
||||
$statusQma = $rec->isApproved() ? 'Disetujui QMA' : ($rec->isRejected() ? 'Ditolak' : 'Menunggu Review');
|
||||
$validator = $rec->validator?->name ?: '-';
|
||||
$notes = $rec->general_notes ?: ($rec->validation_notes ?: '-');
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$waktu,
|
||||
$rec->transaction_no ?: '-',
|
||||
$unitCode,
|
||||
$unitModel,
|
||||
$opName,
|
||||
$shift,
|
||||
$hm,
|
||||
$kondisi,
|
||||
$statusQma,
|
||||
$validator,
|
||||
$notes,
|
||||
];
|
||||
}
|
||||
|
||||
$title = 'Laporan Pemeriksaan Harian (P2H) Unit Support';
|
||||
$subtitle = 'Rekapitulasi Kelaikan Alat Berat & Unit Pendukung Lapangan - PT KMH';
|
||||
$pdfBinary = QmaTablePdfExporter::generate(
|
||||
$title,
|
||||
$subtitle,
|
||||
'P2H UNIT SUPPORT RKM-KMH',
|
||||
$metaLeft,
|
||||
$metaRight,
|
||||
$columns,
|
||||
$rows
|
||||
);
|
||||
|
||||
$fileName = 'Laporan_P2H_Unit_Support_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan Summary Pemeriksaan P2H Unit Support (Analitik, Rekapitulasi Alat Berat & Status).
|
||||
*/
|
||||
@@ -155,6 +352,249 @@ class P2hInspectionController extends Controller
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper untuk memproses data rekapitulasi summary P2H per unit support.
|
||||
*/
|
||||
private function getP2hSummaryData(Request $request): array
|
||||
{
|
||||
$today = Carbon::today()->toDateString();
|
||||
$startDate = $request->input('start_date', Carbon::now()->startOfMonth()->toDateString());
|
||||
$endDate = $request->input('end_date', $today);
|
||||
$vehicleId = $request->input('vehicle_id');
|
||||
$validationStatus = $request->input('validation_status');
|
||||
$shift = $request->input('shift');
|
||||
$search = trim($request->input('search', ''));
|
||||
|
||||
$query = P2hRecord::with(['vehicle', 'employee', 'template']);
|
||||
|
||||
if ($startDate && $endDate) {
|
||||
$start = Carbon::parse($startDate)->startOfDay();
|
||||
$end = Carbon::parse($endDate)->endOfDay();
|
||||
$query->whereBetween('inspected_at', [$start, $end]);
|
||||
} elseif ($startDate) {
|
||||
$query->where('inspected_at', '>=', Carbon::parse($startDate)->startOfDay());
|
||||
} elseif ($endDate) {
|
||||
$query->where('inspected_at', '<=', Carbon::parse($endDate)->endOfDay());
|
||||
}
|
||||
|
||||
if ($request->filled('vehicle_id')) {
|
||||
$query->where('vehicle_id', $vehicleId);
|
||||
}
|
||||
|
||||
if ($request->filled('validation_status')) {
|
||||
$query->where('validation_status', $validationStatus);
|
||||
}
|
||||
|
||||
if ($request->filled('shift')) {
|
||||
$query->where('shift', $shift);
|
||||
}
|
||||
|
||||
if ($search !== '') {
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('transaction_no', 'like', "%{$search}%")
|
||||
->orWhereHas('vehicle', fn($v) => $v->where('code', 'like', "%{$search}%")->orWhere('brand_type', 'like', "%{$search}%"))
|
||||
->orWhereHas('employee', fn($e) => $e->where('name', 'like', "%{$search}%")->orWhere('nik', 'like', "%{$search}%"));
|
||||
});
|
||||
}
|
||||
|
||||
$records = $query->orderByDesc('inspected_at')->get();
|
||||
|
||||
$unitSummary = $records->groupBy('vehicle_id')->map(function ($group) {
|
||||
$vehicle = $group->first()->vehicle;
|
||||
return [
|
||||
'vehicle' => $vehicle,
|
||||
'total' => $group->count(),
|
||||
'approved' => $group->where('validation_status', 'approved')->count(),
|
||||
'pending' => $group->where('validation_status', 'pending')->count(),
|
||||
'rejected' => $group->where('validation_status', 'rejected')->count(),
|
||||
'has_defect' => $group->where('summary_condition', 'has_defect')->count(),
|
||||
'good' => $group->where('summary_condition', 'good')->count(),
|
||||
'latest_hm' => $group->max('start_hm'),
|
||||
'latest_inspection' => $group->max('inspected_at'),
|
||||
];
|
||||
})->sortByDesc('total');
|
||||
|
||||
return [
|
||||
'records' => $records,
|
||||
'unitSummary' => $unitSummary,
|
||||
'startDate' => $startDate,
|
||||
'endDate' => $endDate,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Rekapitulasi Summary P2H Unit Support ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportSummaryExcel(Request $request): Response
|
||||
{
|
||||
$data = $this->getP2hSummaryData($request);
|
||||
$records = $data['records'];
|
||||
$unitSummary = $data['unitSummary'];
|
||||
$startDate = $data['startDate'];
|
||||
$endDate = $data['endDate'];
|
||||
|
||||
$dateRangeStr = Carbon::parse($startDate)->translatedFormat('d M Y') . ' s/d ' . Carbon::parse($endDate)->translatedFormat('d M Y');
|
||||
$printedAt = now()->translatedFormat('d F Y, H:i');
|
||||
|
||||
$statusFilter = $request->filled('validation_status') ?
|
||||
($request->input('validation_status') === 'approved' ? 'Disetujui QMA' :
|
||||
($request->input('validation_status') === 'rejected' ? 'Ditolak' : 'Menunggu Approval')) : 'Semua Status';
|
||||
$shiftFilter = $request->filled('shift') ? $request->input('shift') : 'Semua Shift';
|
||||
|
||||
$totalP2hAll = $records->count();
|
||||
$totalApprovedAll = $records->where('validation_status', 'approved')->count();
|
||||
$totalDefectsAll = $records->where('summary_condition', 'has_defect')->count();
|
||||
|
||||
$metaLines = [
|
||||
'Periode: ' . $dateRangeStr . ' | Status: ' . $statusFilter . ' | Shift: ' . $shiftFilter .
|
||||
($request->filled('search') ? ' | Pencarian: "' . $request->input('search') . '"' : ''),
|
||||
'Total: ' . $unitSummary->count() . ' Unit Alat Berat | Total Form: ' . $totalP2hAll . ' P2H (' . $totalApprovedAll . ' Disetujui, ' . $totalDefectsAll . ' Ada Kerusakan) | Dicetak: ' . $printedAt . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Kode Unit' => 16,
|
||||
'Model & Kategori Alat' => 32,
|
||||
'Kategori' => 20,
|
||||
'Total P2H' => 14,
|
||||
'Disetujui' => 14,
|
||||
'Menunggu' => 14,
|
||||
'Ditolak' => 14,
|
||||
'Temuan Rusak' => 16,
|
||||
'HM Terakhir' => 18,
|
||||
'Terakhir Diperiksa' => 22,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
$idx = 1;
|
||||
foreach ($unitSummary as $us) {
|
||||
$veh = $us['vehicle'];
|
||||
$latestInspection = $us['latest_inspection'] ? Carbon::parse($us['latest_inspection'])->translatedFormat('d/m/Y H:i') . ' WIB' : '-';
|
||||
$hm = $us['latest_hm'] !== null ? number_format($us['latest_hm'], 1, ',', '.') . ' HM' : '-';
|
||||
|
||||
$rows[] = [
|
||||
$idx++,
|
||||
$veh?->code ?: '-',
|
||||
$veh?->brand_type ?: '-',
|
||||
$veh?->unit_category ?: 'Alat Berat',
|
||||
(int) $us['total'],
|
||||
(int) $us['approved'],
|
||||
(int) $us['pending'],
|
||||
(int) $us['rejected'],
|
||||
(int) $us['has_defect'],
|
||||
$hm,
|
||||
$latestInspection,
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'left', 'left', 'center', 'center', 'center', 'center', 'center', 'right', 'center'];
|
||||
$title = 'LAPORAN REKAPITULASI SUMMARY P2H UNIT SUPPORT - PT KERINCI MERANGIN HIDRO';
|
||||
$fileName = 'Laporan_Summary_P2H_Unit_Support_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
|
||||
$binary = SimpleXlsxExporter::create($title, $metaLines, $headers, $rows, $alignments);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor Rekapitulasi Summary P2H Unit Support ke format PDF (Pratinjau Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportSummaryPdf(Request $request): Response
|
||||
{
|
||||
$data = $this->getP2hSummaryData($request);
|
||||
$records = $data['records'];
|
||||
$unitSummary = $data['unitSummary'];
|
||||
$startDate = $data['startDate'];
|
||||
$endDate = $data['endDate'];
|
||||
|
||||
$dateRangeStr = Carbon::parse($startDate)->translatedFormat('d M Y') . ' s/d ' . Carbon::parse($endDate)->translatedFormat('d M Y');
|
||||
|
||||
$statusFilter = $request->filled('validation_status') ?
|
||||
($request->input('validation_status') === 'approved' ? 'Disetujui QMA' :
|
||||
($request->input('validation_status') === 'rejected' ? 'Ditolak' : 'Menunggu Approval')) : 'Semua Status';
|
||||
$shiftFilter = $request->filled('shift') ? $request->input('shift') : 'Semua Shift';
|
||||
|
||||
$totalApproved = $records->where('validation_status', 'approved')->count();
|
||||
$totalPending = $records->where('validation_status', 'pending')->count();
|
||||
$totalRejected = $records->where('validation_status', 'rejected')->count();
|
||||
$totalDefects = $records->where('summary_condition', 'has_defect')->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Periode Tanggal' => $dateRangeStr,
|
||||
'Status Approval' => $statusFilter,
|
||||
'Filter Shift' => $shiftFilter,
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Unit Support Aktif' => $unitSummary->count() . ' Unit Alat Berat',
|
||||
'Total Pemeriksaan' => $records->count() . ' Form P2H Terkumpul',
|
||||
'Rincian Status' => "{$totalApproved} Disetujui, {$totalPending} Menunggu, {$totalRejected} Ditolak",
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 10, 'align' => 'C'],
|
||||
['title' => 'KODE UNIT', 'width' => 24, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'MODEL & KATEGORI ALAT', 'width' => 65, 'align' => 'L', 'max_char' => 42],
|
||||
['title' => 'TOTAL P2H', 'width' => 22, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'DISETUJUI', 'width' => 22, 'align' => 'C', 'is_status' => true],
|
||||
['title' => 'MENUNGGU', 'width' => 22, 'align' => 'C', 'is_status' => true],
|
||||
['title' => 'DITOLAK', 'width' => 20, 'align' => 'C', 'is_status' => true],
|
||||
['title' => 'TEMUAN', 'width' => 24, 'align' => 'C', 'is_status' => true],
|
||||
['title' => 'HM TERAKHIR', 'width' => 26, 'align' => 'R'],
|
||||
['title' => 'TERAKHIR DIPERIKSA', 'width' => 32, 'align' => 'C'],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
$idx = 1;
|
||||
foreach ($unitSummary as $us) {
|
||||
$veh = $us['vehicle'];
|
||||
$latestInspection = $us['latest_inspection'] ? Carbon::parse($us['latest_inspection'])->translatedFormat('d/m/y H:i') : '-';
|
||||
$hm = $us['latest_hm'] !== null ? number_format($us['latest_hm'], 1, ',', '.') : '-';
|
||||
$modelCat = ($veh?->brand_type ?: '-') . ($veh?->unit_category ? ' (' . $veh->unit_category . ')' : '');
|
||||
|
||||
$rows[] = [
|
||||
$idx++,
|
||||
$veh?->code ?: '-',
|
||||
$modelCat,
|
||||
(int) $us['total'],
|
||||
(int) $us['approved'],
|
||||
(int) $us['pending'],
|
||||
(int) $us['rejected'],
|
||||
(int) $us['has_defect'],
|
||||
$hm,
|
||||
$latestInspection,
|
||||
];
|
||||
}
|
||||
|
||||
$title = 'Laporan Summary Rekapitulasi P2H Unit Support';
|
||||
$subtitle = 'Kepatuhan & Kelayakan Operasional Alat Berat Lapangan - Periode: ' . $dateRangeStr;
|
||||
$pdfBinary = QmaTablePdfExporter::generate(
|
||||
$title,
|
||||
$subtitle,
|
||||
'SUMMARY P2H RKM-KMH',
|
||||
$metaLeft,
|
||||
$metaRight,
|
||||
$columns,
|
||||
$rows
|
||||
);
|
||||
|
||||
$fileName = 'Laporan_Summary_P2H_Unit_Support_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tampilkan detail lembar pemeriksaan P2H (seluruh section & butir jawaban).
|
||||
*/
|
||||
|
||||
@@ -9,9 +9,13 @@ use App\Models\P2hRecord;
|
||||
use App\Models\P2hTemplate;
|
||||
use App\Models\Vehicle;
|
||||
use App\Models\VehicleActivity;
|
||||
use App\Services\SimpleXlsxExporter;
|
||||
use App\Services\VehiclePdfExporter;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\View\View;
|
||||
@@ -78,9 +82,9 @@ class VehicleController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Halaman khusus pengelolaan armada Kendaraan Ringan (Light Vehicle / LV).
|
||||
* Helper query builder untuk memfilter data Light Vehicle (LV).
|
||||
*/
|
||||
public function indexLv(Request $request): View
|
||||
private function getFilteredLvQuery(Request $request): Builder
|
||||
{
|
||||
$query = Vehicle::lightVehicles();
|
||||
|
||||
@@ -101,7 +105,15 @@ class VehicleController extends Controller
|
||||
});
|
||||
}
|
||||
|
||||
$vehicles = $query->orderBy('code')->paginate(15)->withQueryString();
|
||||
return $query->orderBy('code');
|
||||
}
|
||||
|
||||
/**
|
||||
* Halaman khusus pengelolaan armada Kendaraan Ringan (Light Vehicle / LV).
|
||||
*/
|
||||
public function indexLv(Request $request): View
|
||||
{
|
||||
$vehicles = $this->getFilteredLvQuery($request)->paginate(15)->withQueryString();
|
||||
|
||||
// Statistik Ringkasan Khusus LV
|
||||
$totalLv = Vehicle::lightVehicles()->count();
|
||||
@@ -122,6 +134,140 @@ class VehicleController extends Controller
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor data Light Vehicle ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportLvExcel(Request $request): Response
|
||||
{
|
||||
$vehicles = $this->getFilteredLvQuery($request)->get();
|
||||
|
||||
$today = now()->translatedFormat('d F Y, H:i');
|
||||
$statusFilter = $request->filled('status') ? 'Status: ' . $request->input('status') : 'Semua Status Operasional';
|
||||
$searchFilter = $request->filled('search') ? 'Pencarian: "' . $request->input('search') . '"' : '';
|
||||
|
||||
$metaLines = [
|
||||
'Filter: ' . $statusFilter . ($searchFilter ? ' | ' . $searchFilter : ''),
|
||||
'Total Data: ' . $vehicles->count() . ' Unit LV | Tanggal Dicetak: ' . $today . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Kode LV' => 16,
|
||||
'Merek & Tipe' => 32,
|
||||
'No. Polisi' => 18,
|
||||
'Divisi' => 24,
|
||||
'Kepemilikan' => 20,
|
||||
'Odometer (KM)' => 20,
|
||||
'Status Operasional' => 22,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($vehicles as $idx => $v) {
|
||||
$odo = $v->current_km ? number_format($v->current_km, 0, ',', '.') . ' KM' : '0 KM';
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$v->code,
|
||||
$v->brand_type ?: '-',
|
||||
$v->license_plate ?: '-',
|
||||
$v->division ?: '-',
|
||||
$v->ownership ?: '-',
|
||||
$odo,
|
||||
$v->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'left', 'center', 'left', 'left', 'center', 'center'];
|
||||
|
||||
$fileName = 'Laporan_Data_Light_Vehicle_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
$binary = SimpleXlsxExporter::create(
|
||||
'LAPORAN DATA KENDARAAN RINGAN (LIGHT VEHICLE) - PT KERINCI MERANGIN HIDRO',
|
||||
$metaLines,
|
||||
$headers,
|
||||
$rows,
|
||||
$alignments
|
||||
);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor data Light Vehicle ke format PDF (Pratinjau Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportLvPdf(Request $request): Response
|
||||
{
|
||||
$vehicles = $this->getFilteredLvQuery($request)->get();
|
||||
|
||||
$today = now()->translatedFormat('d F Y, H:i');
|
||||
$statusFilter = $request->filled('status') ? $request->input('status') : 'Semua Status Operasional';
|
||||
|
||||
$totalOp = $vehicles->where('status', 'Operation')->count();
|
||||
$totalSt = $vehicles->where('status', 'Standby')->count();
|
||||
$totalBd = $vehicles->where('status', 'Breakdown')->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Kategori' => 'Light Vehicle (LV)',
|
||||
'Status Operasional' => $statusFilter,
|
||||
'Tanggal Cetak' => $today . ' WIB',
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Total Kendaraan' => $vehicles->count() . ' Unit LV',
|
||||
'Rincian Status' => "{$totalOp} Operation, {$totalSt} Standby, {$totalBd} Breakdown",
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 10, 'align' => 'C'],
|
||||
['title' => 'KODE LV', 'width' => 25, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'MEREK & TIPE', 'width' => 64, 'align' => 'L', 'bold' => true, 'max_char' => 38],
|
||||
['title' => 'NO. POLISI', 'width' => 28, 'align' => 'C'],
|
||||
['title' => 'DIVISI', 'width' => 45, 'align' => 'L', 'max_char' => 28],
|
||||
['title' => 'KEPEMILIKAN', 'width' => 35, 'align' => 'L', 'max_char' => 22],
|
||||
['title' => 'ODOMETER', 'width' => 30, 'align' => 'C'],
|
||||
['title' => 'STATUS', 'width' => 30, 'align' => 'C', 'is_status' => true],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($vehicles as $idx => $v) {
|
||||
$odo = $v->current_km ? number_format($v->current_km, 0, ',', '.') . ' KM' : '0 KM';
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$v->code,
|
||||
$v->brand_type ?: '-',
|
||||
$v->license_plate ?: '-',
|
||||
$v->division ?: '-',
|
||||
$v->ownership ?: '-',
|
||||
$odo,
|
||||
$v->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$subtitle = 'Armada Kendaraan Operasional Light Vehicle - ' . now()->translatedFormat('d F Y');
|
||||
$pdfBinary = VehiclePdfExporter::generate(
|
||||
'Laporan Data Light Vehicle (LV)',
|
||||
$subtitle,
|
||||
$metaLeft,
|
||||
$metaRight,
|
||||
$columns,
|
||||
$rows
|
||||
);
|
||||
|
||||
$fileName = 'Laporan_Light_Vehicle_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Halaman khusus pengelolaan Kategori Kendaraan Berat (Unit Support / Alat Berat).
|
||||
*/
|
||||
@@ -282,6 +428,453 @@ class VehicleController extends Controller
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper query builder untuk unit fisik alat berat (Unit Support Physical).
|
||||
*/
|
||||
private function getFilteredSupportPhysicalQuery(Request $request): Builder
|
||||
{
|
||||
$query = Vehicle::unitSupportPhysical()->with('parent');
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->input('status'));
|
||||
}
|
||||
|
||||
if ($request->filled('unit_category')) {
|
||||
$cat = $request->input('unit_category');
|
||||
$query->where(function ($q) use ($cat) {
|
||||
$q->where('unit_category', $cat)
|
||||
->orWhereHas('parent', fn ($pq) => $pq->where('unit_category', $cat));
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = trim($request->input('search'));
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('code', 'like', "%{$search}%")
|
||||
->orWhere('serial_number', 'like', "%{$search}%")
|
||||
->orWhere('brand_type', 'like', "%{$search}%")
|
||||
->orWhere('unit_category', 'like', "%{$search}%")
|
||||
->orWhere('location', 'like', "%{$search}%")
|
||||
->orWhere('license_plate', 'like', "%{$search}%")
|
||||
->orWhereHas('parent', function ($pq) use ($search) {
|
||||
$pq->where('code', 'like', "%{$search}%")
|
||||
->orWhere('unit_category', 'like', "%{$search}%")
|
||||
->orWhere('brand_type', 'like', "%{$search}%");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return $query->orderBy('code');
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper query builder untuk master kategori Unit Support.
|
||||
*/
|
||||
private function getFilteredSupportCategoriesQuery(Request $request): Builder
|
||||
{
|
||||
$query = Vehicle::unitSupportCategories()->withCount('units');
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->input('status'));
|
||||
}
|
||||
|
||||
if ($request->filled('unit_category')) {
|
||||
$query->where('unit_category', $request->input('unit_category'));
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = trim($request->input('search'));
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('code', 'like', "%{$search}%")
|
||||
->orWhere('brand_type', 'like', "%{$search}%")
|
||||
->orWhere('unit_category', 'like', "%{$search}%")
|
||||
->orWhere('location', 'like', "%{$search}%")
|
||||
->orWhere('division', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
return $query->orderBy('code');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor data Unit Support ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportSupportExcel(Request $request): Response
|
||||
{
|
||||
$hasPhysical = Vehicle::unitSupportPhysical()->exists();
|
||||
$today = now()->translatedFormat('d F Y, H:i');
|
||||
|
||||
if ($hasPhysical) {
|
||||
$units = $this->getFilteredSupportPhysicalQuery($request)->get();
|
||||
|
||||
$metaLines = [
|
||||
'Filter: ' . ($request->filled('status') ? 'Status: ' . $request->input('status') : 'Semua Status') .
|
||||
($request->filled('unit_category') ? ' | Kategori: ' . $request->input('unit_category') : '') .
|
||||
($request->filled('search') ? ' | Pencarian: "' . $request->input('search') . '"' : ''),
|
||||
'Total Data: ' . $units->count() . ' Unit Fisik Alat Berat | Tanggal Dicetak: ' . $today . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Kode Unit' => 16,
|
||||
'Kategori Alat' => 28,
|
||||
'No. Seri / Rangka' => 24,
|
||||
'Tahun' => 12,
|
||||
'No. Polisi' => 18,
|
||||
'Lokasi Kerja' => 24,
|
||||
'Odometer (HM/KM)' => 20,
|
||||
'Dokumen Izin' => 20,
|
||||
'Status Operasional' => 22,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($units as $idx => $u) {
|
||||
$categoryName = $u->parent?->unit_category ?: ($u->parent?->brand_type ?: ($u->unit_category ?: 'Unit Support'));
|
||||
$odo = $u->current_km ? number_format($u->current_km, 0, ',', '.') . ' HM' : '0 HM';
|
||||
$docs = trim(($u->silo_document ? 'SILO ' : '') . ($u->kir_document ? 'KIR ' : '')) ?: '-';
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$u->code,
|
||||
$categoryName,
|
||||
$u->serial_number ?: '-',
|
||||
$u->production_year ?: '-',
|
||||
$u->license_plate ?: '-',
|
||||
$u->location ?: '-',
|
||||
$odo,
|
||||
$docs,
|
||||
$u->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'left', 'left', 'center', 'center', 'left', 'center', 'center', 'center'];
|
||||
$title = 'LAPORAN DAFTAR UNIT FISIK ALAT BERAT (UNIT SUPPORT) - PT KERINCI MERANGIN HIDRO';
|
||||
$fileName = 'Laporan_Unit_Support_Lengkap_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
} else {
|
||||
// Fallback ke master kategori jika unit fisik belum diinput
|
||||
$categories = $this->getFilteredSupportCategoriesQuery($request)->get();
|
||||
|
||||
$metaLines = [
|
||||
'Filter: ' . ($request->filled('status') ? 'Status: ' . $request->input('status') : 'Semua Status') .
|
||||
($request->filled('unit_category') ? ' | Kategori: ' . $request->input('unit_category') : ''),
|
||||
'Total Data: ' . $categories->count() . ' Kategori Alat Berat | Tanggal Dicetak: ' . $today . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Kode Kategori' => 16,
|
||||
'Kategori Alat Berat' => 30,
|
||||
'Merek & Tipe Model' => 30,
|
||||
'Total Unit Fisik' => 18,
|
||||
'Lokasi Operasional' => 24,
|
||||
'Status Operasional' => 22,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($categories as $idx => $cat) {
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$cat->code,
|
||||
$cat->unit_category ?: $cat->brand_type,
|
||||
$cat->brand_type ?: '-',
|
||||
$cat->units_count . ' Unit',
|
||||
$cat->location ?: '-',
|
||||
$cat->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'left', 'left', 'center', 'left', 'center'];
|
||||
$title = 'LAPORAN MASTER KATEGORI ALAT BERAT (UNIT SUPPORT) - PT KERINCI MERANGIN HIDRO';
|
||||
$fileName = 'Laporan_Kategori_Unit_Support_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
}
|
||||
|
||||
$binary = SimpleXlsxExporter::create($title, $metaLines, $headers, $rows, $alignments);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor data Unit Support ke format PDF (Pratinjau Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportSupportPdf(Request $request): Response
|
||||
{
|
||||
$hasPhysical = Vehicle::unitSupportPhysical()->exists();
|
||||
$today = now()->translatedFormat('d F Y, H:i');
|
||||
$statusFilter = $request->filled('status') ? $request->input('status') : 'Semua Status Operasional';
|
||||
|
||||
if ($hasPhysical) {
|
||||
$units = $this->getFilteredSupportPhysicalQuery($request)->get();
|
||||
|
||||
$totalOp = $units->where('status', 'Operation')->count();
|
||||
$totalSt = $units->where('status', 'Standby')->count();
|
||||
$totalBd = $units->where('status', 'Breakdown')->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Kategori' => 'Unit Support (Alat Berat Fisik)',
|
||||
'Status Operasional' => $statusFilter,
|
||||
'Tanggal Cetak' => $today . ' WIB',
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Total Unit Fisik' => $units->count() . ' Unit Alat Berat',
|
||||
'Rincian Status' => "{$totalOp} Operation, {$totalSt} Standby, {$totalBd} Breakdown",
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 10, 'align' => 'C'],
|
||||
['title' => 'KODE UNIT', 'width' => 24, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'KATEGORI ALAT', 'width' => 45, 'align' => 'L', 'bold' => true, 'max_char' => 26],
|
||||
['title' => 'NO. SERI / RANGKA', 'width' => 42, 'align' => 'L', 'max_char' => 24],
|
||||
['title' => 'THN', 'width' => 14, 'align' => 'C'],
|
||||
['title' => 'NO. POLISI', 'width' => 24, 'align' => 'C'],
|
||||
['title' => 'LOKASI KERJA', 'width' => 50, 'align' => 'L', 'max_char' => 28],
|
||||
['title' => 'ODOMETER', 'width' => 28, 'align' => 'C'],
|
||||
['title' => 'STATUS', 'width' => 30, 'align' => 'C', 'is_status' => true],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($units as $idx => $u) {
|
||||
$categoryName = $u->parent?->unit_category ?: ($u->parent?->brand_type ?: ($u->unit_category ?: 'Unit Support'));
|
||||
$odo = $u->current_km ? number_format($u->current_km, 0, ',', '.') . ' HM' : '0 HM';
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$u->code,
|
||||
$categoryName,
|
||||
$u->serial_number ?: '-',
|
||||
$u->production_year ?: '-',
|
||||
$u->license_plate ?: '-',
|
||||
$u->location ?: '-',
|
||||
$odo,
|
||||
$u->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$title = 'Laporan Data Unit Support (Alat Berat)';
|
||||
$subtitle = 'Daftar Seluruh Unit Fisik Alat Berat Lapangan - ' . now()->translatedFormat('d F Y');
|
||||
$fileName = 'Laporan_Unit_Support_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
} else {
|
||||
$categories = $this->getFilteredSupportCategoriesQuery($request)->get();
|
||||
|
||||
$totalOp = $categories->where('status', 'Operation')->count();
|
||||
$totalSt = $categories->where('status', 'Standby')->count();
|
||||
$totalBd = $categories->where('status', 'Breakdown')->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Kategori' => 'Master Kategori Unit Support',
|
||||
'Status Operasional' => $statusFilter,
|
||||
'Tanggal Cetak' => $today . ' WIB',
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Total Kategori' => $categories->count() . ' Kategori Alat',
|
||||
'Rincian Status' => "{$totalOp} Operation, {$totalSt} Standby, {$totalBd} Breakdown",
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 10, 'align' => 'C'],
|
||||
['title' => 'KODE', 'width' => 25, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'KATEGORI ALAT BERAT', 'width' => 64, 'align' => 'L', 'bold' => true, 'max_char' => 38],
|
||||
['title' => 'MEREK & MODEL', 'width' => 55, 'align' => 'L', 'max_char' => 32],
|
||||
['title' => 'TOTAL UNIT', 'width' => 28, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'LOKASI OPERASIONAL', 'width' => 55, 'align' => 'L', 'max_char' => 32],
|
||||
['title' => 'STATUS', 'width' => 30, 'align' => 'C', 'is_status' => true],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($categories as $idx => $cat) {
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$cat->code,
|
||||
$cat->unit_category ?: $cat->brand_type,
|
||||
$cat->brand_type ?: '-',
|
||||
$cat->units_count . ' Unit',
|
||||
$cat->location ?: '-',
|
||||
$cat->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$title = 'Laporan Master Kategori Unit Support';
|
||||
$subtitle = 'Master 28 Kategori Alat Berat Proyek KMH - ' . now()->translatedFormat('d F Y');
|
||||
$fileName = 'Laporan_Kategori_Unit_Support_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
}
|
||||
|
||||
$pdfBinary = VehiclePdfExporter::generate($title, $subtitle, $metaLeft, $metaRight, $columns, $rows);
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor data unit fisik khusus kategori tertentu ke format Excel (.xlsx).
|
||||
*/
|
||||
public function exportSupportUnitsExcel(Request $request, int $id): Response
|
||||
{
|
||||
$category = Vehicle::unitSupportCategories()->withCount('units')->findOrFail($id);
|
||||
$query = $category->units();
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->input('status'));
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = trim($request->input('search'));
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('code', 'like', "%{$search}%")
|
||||
->orWhere('serial_number', 'like', "%{$search}%")
|
||||
->orWhere('license_plate', 'like', "%{$search}%")
|
||||
->orWhere('location', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
$units = $query->orderBy('code')->get();
|
||||
$today = now()->translatedFormat('d F Y, H:i');
|
||||
|
||||
$metaLines = [
|
||||
'Kategori: ' . ($category->unit_category ?: $category->brand_type) . ' (' . $category->code . ')' .
|
||||
($request->filled('status') ? ' | Status: ' . $request->input('status') : '') .
|
||||
($request->filled('search') ? ' | Pencarian: "' . $request->input('search') . '"' : ''),
|
||||
'Total Data: ' . $units->count() . ' Unit Fisik | Tanggal Dicetak: ' . $today . ' WIB',
|
||||
];
|
||||
|
||||
$headers = [
|
||||
'No' => 6,
|
||||
'Kode Unit' => 16,
|
||||
'No. Seri / Rangka' => 24,
|
||||
'Tahun' => 12,
|
||||
'No. Polisi' => 18,
|
||||
'Lokasi Kerja' => 24,
|
||||
'Odometer (HM/KM)' => 20,
|
||||
'Dokumen Izin' => 20,
|
||||
'Status Operasional' => 22,
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($units as $idx => $u) {
|
||||
$odo = $u->current_km ? number_format($u->current_km, 0, ',', '.') . ' HM' : '0 HM';
|
||||
$docs = trim(($u->silo_document ? 'SILO ' : '') . ($u->kir_document ? 'KIR ' : '')) ?: '-';
|
||||
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$u->code,
|
||||
$u->serial_number ?: '-',
|
||||
$u->production_year ?: '-',
|
||||
$u->license_plate ?: '-',
|
||||
$u->location ?: '-',
|
||||
$odo,
|
||||
$docs,
|
||||
$u->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$alignments = ['center', 'center', 'left', 'center', 'center', 'left', 'center', 'center', 'center'];
|
||||
$title = 'LAPORAN UNIT FISIK ' . strtoupper($category->unit_category ?: $category->brand_type) . ' - PT KERINCI MERANGIN HIDRO';
|
||||
$fileName = 'Laporan_Unit_' . preg_replace('/[^A-Za-z0-9_-]/', '_', $category->code) . '_' . now()->format('Ymd_Hi') . '.xlsx';
|
||||
|
||||
$binary = SimpleXlsxExporter::create($title, $metaLines, $headers, $rows, $alignments);
|
||||
|
||||
return response($binary, 200, [
|
||||
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($binary),
|
||||
'Cache-Control' => 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ekspor data unit fisik khusus kategori tertentu ke format PDF (Pratinjau Asli dengan Logo PT KMH).
|
||||
*/
|
||||
public function exportSupportUnitsPdf(Request $request, int $id): Response
|
||||
{
|
||||
$category = Vehicle::unitSupportCategories()->withCount('units')->findOrFail($id);
|
||||
$query = $category->units();
|
||||
|
||||
if ($request->filled('status')) {
|
||||
$query->where('status', $request->input('status'));
|
||||
}
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = trim($request->input('search'));
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('code', 'like', "%{$search}%")
|
||||
->orWhere('serial_number', 'like', "%{$search}%")
|
||||
->orWhere('license_plate', 'like', "%{$search}%")
|
||||
->orWhere('location', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
$units = $query->orderBy('code')->get();
|
||||
$today = now()->translatedFormat('d F Y, H:i');
|
||||
$statusFilter = $request->filled('status') ? $request->input('status') : 'Semua Status Operasional';
|
||||
|
||||
$totalOp = $units->where('status', 'Operation')->count();
|
||||
$totalSt = $units->where('status', 'Standby')->count();
|
||||
$totalBd = $units->where('status', 'Breakdown')->count();
|
||||
|
||||
$metaLeft = [
|
||||
'Kategori Alat' => ($category->unit_category ?: $category->brand_type) . ' (' . $category->code . ')',
|
||||
'Status Operasional' => $statusFilter,
|
||||
'Tanggal Cetak' => $today . ' WIB',
|
||||
];
|
||||
|
||||
$metaRight = [
|
||||
'Total Unit Fisik' => $units->count() . ' Unit Terdaftar',
|
||||
'Rincian Status' => "{$totalOp} Operation, {$totalSt} Standby, {$totalBd} Breakdown",
|
||||
];
|
||||
|
||||
$columns = [
|
||||
['title' => 'NO', 'width' => 10, 'align' => 'C'],
|
||||
['title' => 'KODE UNIT', 'width' => 28, 'align' => 'C', 'bold' => true],
|
||||
['title' => 'NO. SERI / RANGKA', 'width' => 50, 'align' => 'L', 'max_char' => 30],
|
||||
['title' => 'TAHUN', 'width' => 18, 'align' => 'C'],
|
||||
['title' => 'NO. POLISI', 'width' => 28, 'align' => 'C'],
|
||||
['title' => 'LOKASI KERJA', 'width' => 65, 'align' => 'L', 'max_char' => 38],
|
||||
['title' => 'ODOMETER', 'width' => 34, 'align' => 'C'],
|
||||
['title' => 'STATUS', 'width' => 34, 'align' => 'C', 'is_status' => true],
|
||||
];
|
||||
|
||||
$rows = [];
|
||||
foreach ($units as $idx => $u) {
|
||||
$odo = $u->current_km ? number_format($u->current_km, 0, ',', '.') . ' HM' : '0 HM';
|
||||
$rows[] = [
|
||||
$idx + 1,
|
||||
$u->code,
|
||||
$u->serial_number ?: '-',
|
||||
$u->production_year ?: '-',
|
||||
$u->license_plate ?: '-',
|
||||
$u->location ?: '-',
|
||||
$odo,
|
||||
$u->status ?: 'Standby',
|
||||
];
|
||||
}
|
||||
|
||||
$title = 'Laporan Unit Fisik ' . ($category->unit_category ?: $category->brand_type);
|
||||
$subtitle = 'Kategori ' . $category->code . ' - ' . now()->translatedFormat('d F Y');
|
||||
$fileName = 'Laporan_Unit_' . preg_replace('/[^A-Za-z0-9_-]/', '_', $category->code) . '_' . now()->format('Ymd_Hi') . '.pdf';
|
||||
|
||||
$pdfBinary = VehiclePdfExporter::generate($title, $subtitle, $metaLeft, $metaRight, $columns, $rows);
|
||||
|
||||
return response($pdfBinary, 200, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
'Content-Disposition' => 'inline; filename="' . $fileName . '"',
|
||||
'Content-Length' => strlen($pdfBinary),
|
||||
'Cache-Control' => 'private, max-age=0, must-revalidate',
|
||||
'Pragma' => 'public',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simpan penambahan unit fisik baru di bawah kategori Unit Support tertentu.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user