Files
rkm-kmh/app/Services/SimperPdfExporter.php
T

239 lines
8.3 KiB
PHP
Raw Normal View History

2026-10-16 11:02:28 +07:00
<?php
namespace App\Services;
use FPDF;
use Illuminate\Support\Collection;
class SimperPdfExporter extends FPDF
{
public string $reportTitle = 'Laporan Data SIMPER';
public string $subtitle = '';
public array $metaLeft = [];
public array $metaRight = [];
public int $totalRows = 0;
public string $logoPath = '';
public function Header(): void
{
// 1. Logo Perusahaan di Kiri Atas
if (!empty($this->logoPath) && file_exists($this->logoPath)) {
$this->Image($this->logoPath, 15, 10, 44);
} else {
$this->SetXY(15, 10);
$this->SetFont('Arial', 'B', 14);
$this->SetTextColor(27, 94, 32);
$this->Cell(60, 8, 'KERINCI HYDROPOWER', 0, 0, 'L');
}
// 2. Branding Sistem di Kanan Atas
$this->SetXY(170, 10);
$this->SetFont('Arial', 'B', 18);
$this->SetTextColor(27, 94, 32); // #1b5e20 Green
$this->Cell(112, 6, 'RKM-KMH', 0, 1, 'R');
$this->SetXY(170, 16);
$this->SetFont('Arial', 'B', 7.5);
$this->SetTextColor(71, 85, 105); // #475569 Slate
$this->Cell(112, 4, 'KERINCI HYDROPOWER 350 MW', 0, 1, 'R');
// 3. Banner Biru Gelap Navy (Sesuai Referensi it.allrole.web.id)
$this->SetY(24);
$this->SetFillColor(6, 33, 63); // #06213f Dark Navy
$this->Rect(15, 24, 267, 16, 'F');
// Teks Judul di Kiri Banner
$this->SetXY(18, 26);
$this->SetFont('Arial', 'B', 13);
$this->SetTextColor(255, 255, 255);
$this->Cell(140, 6, $this->reportTitle, 0, 1, 'L');
$this->SetXY(18, 32);
$this->SetFont('Arial', '', 8);
$this->SetTextColor(203, 213, 225); // #cbd5e1 Slate
$this->Cell(140, 5, $this->subtitle, 0, 1, 'L');
// Teks Sub-sistem di Kanan Banner
$this->SetXY(160, 26);
$this->SetFont('Arial', 'B', 9);
$this->SetTextColor(255, 255, 255);
$this->Cell(119, 6, 'SISTEM KENDARAAN RKM-KMH', 0, 1, 'R');
$this->SetXY(160, 32);
$this->SetFont('Arial', 'I', 7.5);
$this->SetTextColor(203, 213, 225);
$this->Cell(119, 5, 'Dicetak otomatis dari sistem', 0, 1, 'R');
// 4. Bagian Ringkasan Metadata (2 Kolom)
$metaY = 44;
$this->SetTextColor(51, 65, 85);
// Kolom Kiri
foreach ($this->metaLeft as $k => $v) {
$this->SetXY(15, $metaY);
$this->SetFont('Arial', '', 8);
$this->Cell(28, 4.5, $k, 0, 0);
$this->Cell(5, 4.5, ':', 0, 0);
$this->SetFont('Arial', 'B', 8);
$this->Cell(85, 4.5, $v, 0, 1);
$metaY += 5;
}
// Kolom Kanan
$metaY2 = 44;
foreach ($this->metaRight as $k => $v) {
$this->SetXY(175, $metaY2);
$this->SetFont('Arial', '', 8);
$this->Cell(28, 4.5, $k, 0, 0);
$this->Cell(5, 4.5, ':', 0, 0);
$this->SetFont('Arial', 'B', 8);
$this->Cell(74, 4.5, $v, 0, 1);
$metaY2 += 5;
}
// Garis Pembatas Tipis
$divY = max($metaY, $metaY2) + 2;
$this->SetDrawColor(226, 232, 240);
$this->Line(15, $divY, 282, $divY);
// 5. Header Tabel Solid Navy
$tableY = $divY + 3;
$this->SetXY(15, $tableY);
$this->SetFillColor(6, 33, 63);
$this->SetTextColor(255, 255, 255);
$this->SetFont('Arial', 'B', 7.5);
$cols = [
['NO', 8, 'C'],
['NIK', 25, 'C'],
['NAMA KARYAWAN', 52, 'L'],
['JABATAN', 38, 'L'],
['UNIT / DIVISI', 34, 'L'],
['DRIVER', 22, 'C'],
['JENIS SIMPER', 34, 'C'],
['MASA BERLAKU', 25, 'C'],
['STATUS', 29, 'C'],
2026-10-16 11:02:28 +07:00
];
foreach ($cols as $col) {
$this->Cell($col[1], 7.5, $col[0], 0, 0, $col[2], true);
}
$this->Ln();
}
public function Footer(): void
{
$this->SetY(-12);
$this->SetFont('Arial', '', 7.5);
$this->SetTextColor(148, 163, 184);
$this->Cell(
180,
5,
'Menampilkan ' . $this->totalRows . ' data - Dokumen ini digenerate secara otomatis oleh Sistem RKM-KMH pada ' . now()->translatedFormat('d F Y') . '.',
0,
0,
'L'
);
$this->Cell(87, 5, 'Halaman ' . $this->PageNo() . ' dari {nb}', 0, 0, 'R');
}
/**
* Generate real PDF binary string.
*/
public static function generate(Collection $employees, array $metaLeft, array $metaRight, string $subtitle): string
{
$pdf = new self('L', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->reportTitle = 'Laporan Data SIMPER';
$pdf->subtitle = $subtitle;
$pdf->metaLeft = $metaLeft;
$pdf->metaRight = $metaRight;
$pdf->totalRows = $employees->count();
$pdf->logoPath = public_path('images/logo-kerinci.png');
$pdf->SetMargins(15, 10, 15);
$pdf->AddPage();
$pdf->SetAutoPageBreak(true, 15);
$pdf->SetFont('Arial', '', 7.5);
$fill = false;
$soon = now()->addDays(30);
foreach ($employees as $idx => $emp) {
$isDriver = $emp->is_operator;
$expDate = $emp->simper_expired_at ? $emp->simper_expired_at->format('d/m/Y') : '-';
if (! $isDriver) {
$statusText = 'Bukan Driver';
} elseif (! $emp->simper_expired_at) {
$statusText = 'Belum Ada SIMPER';
} elseif ($emp->simper_expired_at->isPast() && ! $emp->simper_expired_at->isToday()) {
$statusText = 'Kedaluwarsa';
} elseif ($emp->simper_expired_at->lte($soon)) {
$statusText = 'Segera Habis';
} else {
$statusText = 'Masih Berlaku';
}
$pdf->SetFillColor(248, 250, 252);
$pdf->SetDrawColor(241, 245, 249);
$pdf->SetTextColor(15, 23, 42);
// Kolom 1: No
$pdf->SetFont('Arial', '', 7.5);
$pdf->Cell(8, 6.5, (string) ($idx + 1), 'B', 0, 'C', $fill);
2026-10-16 11:02:28 +07:00
// Kolom 2: NIK (Bold)
$pdf->SetFont('Arial', 'B', 7.5);
$pdf->Cell(25, 6.5, $emp->nik, 'B', 0, 'C', $fill);
2026-10-16 11:02:28 +07:00
// Kolom 3: Nama Lengkap (Bold, Truncate jika > 30 karakter)
$nama = mb_strimwidth($emp->name, 0, 30, '...');
$pdf->Cell(52, 6.5, ' ' . $nama, 'B', 0, 'L', $fill);
2026-10-16 11:02:28 +07:00
// Kolom 4: Jabatan
$pdf->SetFont('Arial', '', 7.5);
$jabatan = mb_strimwidth($emp->position ?: '-', 0, 22, '...');
$pdf->Cell(38, 6.5, ' ' . $jabatan, 'B', 0, 'L', $fill);
2026-10-16 11:02:28 +07:00
// Kolom 5: Unit / Divisi
$unit = mb_strimwidth($emp->unit_kerja ?: ($emp->division ?: '-'), 0, 20, '...');
$pdf->Cell(34, 6.5, ' ' . $unit, 'B', 0, 'L', $fill);
2026-10-16 11:02:28 +07:00
// Kolom 6: Status Driver
$pdf->Cell(22, 6.5, $isDriver ? 'Driver' : 'Non-Driver', 'B', 0, 'C', $fill);
2026-10-16 11:02:28 +07:00
// Kolom 7: Rincian Jenis SIMPER
$simperList = $emp->simpers->map(function ($s) {
$code = $s->simperType?->code ?: 'SIMPER';
return str_replace('SIMPER-', '', $code);
})->filter()->unique()->values();
$simperTypesText = $simperList->isNotEmpty() ? $simperList->join(', ') : ($isDriver ? 'LV' : '-');
$pdf->SetFont('Arial', 'B', 7);
$pdf->Cell(34, 6.5, $simperTypesText, 'B', 0, 'C', $fill);
2026-10-16 11:02:28 +07:00
// Kolom 8: Masa Berlaku
$pdf->SetFont('Arial', '', 7.5);
$pdf->Cell(25, 6.5, $expDate, 'B', 0, 'C', $fill);
// Kolom 9: Status SIMPER (Berwarna)
2026-10-16 11:02:28 +07:00
if ($statusText === 'Masih Berlaku') {
$pdf->SetTextColor(4, 120, 87); // #047857 Green
} elseif ($statusText === 'Segera Habis') {
$pdf->SetTextColor(180, 83, 9); // #b45309 Amber
} elseif ($statusText === 'Kedaluwarsa' || $statusText === 'Belum Ada SIMPER') {
$pdf->SetTextColor(190, 18, 60); // #be123c Rose
} else {
$pdf->SetTextColor(100, 116, 139); // #64748b Slate
}
$pdf->SetFont('Arial', 'B', 7.5);
$pdf->Cell(29, 6.5, $statusText, 'B', 0, 'C', $fill);
2026-10-16 11:02:28 +07:00
$pdf->Ln();
$fill = ! $fill;
}
return $pdf->Output('S');
}
}