Files
accounting_dev/application/libraries/PDF_Neracasaldo.php
T
2026-07-12 22:10:02 +07:00

313 lines
11 KiB
PHP

<?php
require_once(APPPATH . 'third_party/fpdf/fpdf.php');
class PDF extends FPDF
{
// ----------------------------------------------------------------
// PROPERTI HEADER
// ----------------------------------------------------------------
private $nama;
private $alamat;
private $logo;
private $periodeStr = '';
// ----------------------------------------------------------------
// KONFIGURASI TABEL
// ----------------------------------------------------------------
// A4 Portrait efektif = 190mm, margin 10+10
// Kolom: Kode 20 | Nama Akun 90 | Debit 40 | Kredit 40 → TOTAL 190
private $colWidths = [20, 90, 40, 40];
// ----------------------------------------------------------------
// SETTER
// ----------------------------------------------------------------
public function setHeaderData($nama, $alamat, $logo = '')
{
$this->nama = $nama;
$this->alamat = $alamat;
$this->logo = $logo;
}
public function setInfoLaporan($periodeStr)
{
$this->periodeStr = $periodeStr;
}
// ----------------------------------------------------------------
// HEADER HALAMAN
// ----------------------------------------------------------------
function Header()
{
// Header lengkap hanya di halaman pertama
if ($this->PageNo() === 1) {
// Logo (opsional)
if (!empty($this->logo) && file_exists($this->logo)) {
$this->Image($this->logo, 10, 6, 18);
$this->SetXY(31, 10);
} else {
$this->SetXY(10, 10);
}
// Nama perusahaan
$this->SetFont('Arial', 'B', 12);
$this->Cell(0, 6, $this->nama, 0, 1, 'L');
// Alamat
$this->SetFont('Arial', '', 9);
$x = !empty($this->logo) && file_exists($this->logo) ? 31 : 10;
$this->SetX($x);
$this->Cell(0, 5, $this->alamat, 0, 1, 'L');
// Garis pemisah (A4 Portrait: x=10 s/d x=200)
$this->Ln(2);
$this->SetLineWidth(0.6);
$this->Line(10, $this->GetY(), 200, $this->GetY());
$this->SetLineWidth(0.2);
$this->Line(10, $this->GetY() + 1, 200, $this->GetY() + 1);
$this->Ln(5);
// Judul
$this->SetFont('Arial', 'B', 13);
$this->SetTextColor(245, 140, 0);
$this->Cell(0, 7, 'LAPORAN NERACA SALDO', 0, 1, 'C');
$this->SetTextColor(0, 0, 0);
// Periode
$this->SetFont('Arial', '', 10);
$this->Cell(0, 5, $this->periodeStr, 0, 1, 'C');
// Tanggal cetak
$this->SetFont('Arial', '', 9);
$this->Cell(0, 5, 'Di Cetak Pada : ' . date('d-m-Y') . ' - ' . date('H:i:s'), 0, 1, 'C');
$this->Ln(4);
} else {
// Halaman berikutnya tanpa header laporan
$this->SetY(10);
}
// Header kolom tabel tetap tampil di setiap halaman
$this->_drawTableHeader();
}
// ----------------------------------------------------------------
// FOOTER HALAMAN
// ----------------------------------------------------------------
function Footer()
{
$this->SetY(-10);
$this->SetFont('Arial', 'I', 8);
$this->Cell(0, 5, 'Halaman ' . $this->PageNo(), 0, 0, 'C');
}
// ----------------------------------------------------------------
// HELPER: posisi X awal — TABEL RATA TENGAH KERTAS
// ----------------------------------------------------------------
private function _startX()
{
$tableW = array_sum($this->colWidths);
return ($this->GetPageWidth() - $tableW) / 2;
}
// ----------------------------------------------------------------
// HELPER: Gambar header kolom tabel
// ----------------------------------------------------------------
private function _drawTableHeader()
{
$startX = $this->_startX();
$this->SetX($startX);
$this->SetFillColor(245, 140, 0);
$this->SetTextColor(255, 255, 255);
$this->SetFont('Arial', 'B', 9);
$this->Cell($this->colWidths[0], 8, 'Kode', 1, 0, 'C', true);
$this->Cell($this->colWidths[1], 8, 'Nama Akun', 1, 0, 'C', true);
$this->Cell($this->colWidths[2], 8, 'Debit', 1, 0, 'C', true);
$this->Cell($this->colWidths[3], 8, 'Kredit', 1, 1, 'C', true);
$this->SetTextColor(0, 0, 0);
}
// ----------------------------------------------------------------
// TABEL NERACA SALDO (mendukung multiline Nama Akun)
// ----------------------------------------------------------------
public function NeracaSaldoTable($rows, $totalDebit, $totalKredit)
{
$startX = $this->_startX();
$lineH = 6;
$this->SetFont('Arial', '', 9);
if (empty($rows)) {
$this->SetX($startX);
$this->SetFont('Arial', 'B', 10);
$this->Cell(array_sum($this->colWidths), 8, 'Tidak ada data', 1, 1, 'C');
} else {
foreach ($rows as $row) {
$namaAkun = isset($row->nama_akun) ? $row->nama_akun : (isset($row['nama_akun']) ? $row['nama_akun'] : '');
$kodeAkun = isset($row->kode_akun) ? $row->kode_akun : (isset($row['kode_akun']) ? $row['kode_akun'] : '');
$debit = isset($row->debit) ? (float)$row->debit : (isset($row['debit']) ? (float)$row['debit'] : 0);
$kredit = isset($row->kredit) ? (float)$row->kredit : (isset($row['kredit']) ? (float)$row['kredit'] : 0);
$nbLines = $this->NbLines($this->colWidths[1], $namaAkun);
$rowH = max(1, $nbLines) * $lineH;
if ($this->GetY() + $rowH > 270) {
$this->AddPage('P', 'A4');
}
$y = $this->GetY();
$this->SetFont('Arial', '', 9);
$this->SetXY($startX, $y);
$this->Cell($this->colWidths[0], $rowH, $kodeAkun, 1, 0, 'C');
$xNama = $this->GetX();
$this->MultiCell($this->colWidths[1], $lineH, $namaAkun, 1, 'L');
$this->SetXY($xNama + $this->colWidths[1], $y);
$this->Cell($this->colWidths[2], $rowH, $debit > 0 ? number_format($debit, 0, ',', '.') : '-', 1, 0, 'R');
$this->Cell($this->colWidths[3], $rowH, $kredit > 0 ? number_format($kredit, 0, ',', '.') : '-', 1, 0, 'R');
$this->SetXY($startX, $y + $rowH);
}
}
// ---- BARIS TOTAL ----
if ($this->GetY() + 8 > 270) {
$this->AddPage('P', 'A4');
}
$this->SetX($startX);
$this->SetFillColor(255, 204, 102);
$this->SetFont('Arial', 'B', 9);
$this->Cell($this->colWidths[0] + $this->colWidths[1], 8, 'TOTAL', 1, 0, 'C', true);
$this->Cell($this->colWidths[2], 8, number_format($totalDebit, 0, ',', '.'), 1, 0, 'R', true);
$this->Cell($this->colWidths[3], 8, number_format($totalKredit, 0, ',', '.'), 1, 1, 'R', true);
}
// ----------------------------------------------------------------
// RINGKASAN: STATUS BALANCE
// ----------------------------------------------------------------
public function NeracaSaldoRingkasan($totalDebit, $totalKredit)
{
if ($this->GetY() + 9 > 270) {
$this->AddPage('P', 'A4');
}
$startX = $this->_startX();
$tableW = array_sum($this->colWidths);
$labelW = $tableW - $this->colWidths[3];
$selisih = $totalDebit - $totalKredit;
$this->Ln(2);
$this->SetX($startX);
$this->SetFont('Arial', 'B', 10);
if (abs($selisih) < 1) {
$this->SetFillColor(198, 239, 206); // hijau, balance
$label = 'SELISIH (BALANCE)';
} else {
$this->SetFillColor(255, 199, 206); // merah, tidak balance
$label = 'SELISIH (TIDAK BALANCE)';
}
$this->Cell($labelW, 8, $label, 1, 0, 'L', true);
$this->Cell($this->colWidths[3], 8, number_format($selisih, 0, ',', '.'), 1, 1, 'R', true);
}
// ----------------------------------------------------------------
// TANDA TANGAN
// ----------------------------------------------------------------
public function Pembuat($nama, $jabatan = '')
{
$startX = $this->_startX();
$tableW = array_sum($this->colWidths);
$signWidth = 70;
$signX = $startX + $tableW - $signWidth; // sejajar sisi kanan tabel
$this->Ln(3);
// $this->SetX($signX);
$this->SetFont('Arial', 'I', 9);
$this->Cell($signWidth, 6, '*Mengetahui, (Finance Departement)', 0, 1, 'L');
// $this->Cell($signWidth, 6, $nama, 0, 1, 'L');
// if (!empty($jabatan)) {
// $this->Cell($signWidth, 5, '(' . $jabatan . ')', 0, 1, 'L');
// }
// $this->Ln(0);
// $this->SetX($signX);
// $this->SetFont('Arial', 'B', 10);
// $this->Cell($signWidth, 6, $nama, 0, 1, 'R');
// if (!empty($jabatan)) {
// $this->SetX($signX);
// $this->SetFont('Arial', '', 9);
// $this->Cell($signWidth, 5, '(' . $jabatan . ')', 0, 1, 'R');
// }
}
// ----------------------------------------------------------------
// HELPER: Hitung jumlah baris yang dibutuhkan MultiCell
// ----------------------------------------------------------------
private function NbLines($w, $txt)
{
$cw = &$this->CurrentFont['cw'];
if ($w == 0) {
$w = $this->w - $this->rMargin - $this->x;
}
$wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
$s = str_replace("\r", '', $txt);
$nb = strlen($s);
if ($nb > 0 && $s[$nb - 1] == "\n") {
$nb--;
}
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$nl = 1;
while ($i < $nb) {
$c = $s[$i];
if ($c == "\n") {
$i++;
$sep = -1;
$j = $i;
$l = 0;
$nl++;
continue;
}
if ($c == ' ') {
$sep = $i;
}
$l += isset($cw[ord($c)]) ? $cw[ord($c)] : 600;
if ($l > $wmax) {
if ($sep == -1) {
if ($i == $j) {
$i++;
}
} else {
$i = $sep + 1;
}
$sep = -1;
$j = $i;
$l = 0;
$nl++;
} else {
$i++;
}
}
return $nl;
}
}