<?php
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$id = (int)($_GET['id'] ?? 0);
$q = $conn->prepare("
    SELECT 
        k.*,
        s.nama_sekolah,
        s.kepala_sekolah,
        s.nip_kepsek,
        s.bendahara,
        s.nip_bendahara
    FROM kwitansi k
    JOIN sekolah s ON k.npsn = s.npsn
    WHERE k.id_kw = ?
");
$q->bind_param("i", $id);
$q->execute();
$d = $q->get_result()->fetch_assoc();

if (!$d) {
    die("Data kwitansi / sekolah tidak ditemukan");
}
if(!$d) die("Data tidak ditemukan");

/* ================= TERBILANG ================= */
function terbilang($x){
    $x = abs((int)$x);

    $bil = array(
        "",
        "satu",
        "dua",
        "tiga",
        "empat",
        "lima",
        "enam",
        "tujuh",
        "delapan",
        "sembilan",
        "sepuluh",
        "sebelas"
    );

    if ($x < 12)
        return " " . $bil[$x];
    elseif ($x < 20)
        return terbilang($x - 10) . " belas";
    elseif ($x < 100)
        return terbilang($x / 10) . " puluh" . terbilang($x % 10);
    elseif ($x < 200)
        return " seratus" . terbilang($x - 100);
    elseif ($x < 1000)
        return terbilang($x / 100) . " ratus" . terbilang($x % 100);
    elseif ($x < 2000)
        return " seribu" . terbilang($x - 1000);
    elseif ($x < 1000000)
        return terbilang($x / 1000) . " ribu" . terbilang($x % 1000);
    elseif ($x < 1000000000)
        return terbilang($x / 1000000) . " juta" . terbilang($x % 1000000);
    elseif ($x < 1000000000000)
        return terbilang($x / 1000000000) . " miliar" . terbilang($x % 1000000000);
    else
        return "terlalu besar";
}

/* =============== TANGGAL INDONESIA =============== */
function tgl_indo($tgl){
    $b = [1=>"Januari","Februari","Maret","April","Mei","Juni",
          "Juli","Agustus","September","Oktober","November","Desember"];
    $p = explode("-", $tgl);
    return $p[2]." ".$b[(int)$p[1]]." ".$p[0];
}

/* ================= HITUNG PAJAK ================= */
$pajakAktif = ($_GET['pajak'] ?? 'on') === 'on';

$nilai_sebelum_pajak = (float)$d['pengeluaran'];

/* DPP */
$dpp_nilai_lain = $nilai_sebelum_pajak;
$dpp_pph        = $nilai_sebelum_pajak;

/* Pajak */
$ppn  = $pajakAktif ? ($dpp_nilai_lain * 0.12)  : 0;
$pph21 = $pajakAktif ? ($dpp_pph * 0.05)        : 0;
$pph22 = $pajakAktif ? ($dpp_pph * 0.005)       : 0;
$pph23 = $pajakAktif ? ($dpp_pph * 0.02)        : 0;

/* TOTAL */
$total_pengeluaran = $nilai_sebelum_pajak + $ppn + $pph21 + $pph22 + $pph23;

$html = "
<style>
body{font-family:'Times New Roman'; font-size:12px;}
h3{text-align:center; margin:0;}
.line{border-bottom:2px solid #000; margin:6px 0 10px;}
.tbl{width:100%; border-collapse:collapse;}
.tbl td{padding:4px; vertical-align:top;}
.box{border:1px solid #000; padding:6px;}
.pajak td{padding:3px;}
.pajak .label{background:#eee;}


/* ===== TABEL TTD BOS ===== */
.ttd-bos{
    width:100%;
    border-collapse:collapse;
    margin-top:10px;
    border-top:2px solid #000;
    border-bottom:2px solid #000;
}

.ttd-bos td{
    border-left:1px solid #000;
    border-right:1px solid #000;
	text-align:center;
    vertical-align:top;
    padding:6px;
    font-size:12px;
}

.ttd-judul{
    height:50px;
}

.ttd-jarak{
    height:85px;
}

.nama{
    font-weight:bold;
    text-decoration:underline;
}
/* garis tengah utuh di tabel TTD */
.ttd-garis-tengah{
    height:0;
}

.ttd-garis-tengah td{
    padding:0;
    height:0;
    border-bottom:1px solid #000;
}
.bg-grey {
    background: #eee;
}
.total {
    font-weight: bold;
    border-top: 2px solid #000;
}
</style>


<h3>BUKTI PENGELUARAN</h3>
<div class='line'></div>
<table class='tbl'>
<tr>
  <td width='20%'>Nomor Bukti</td><td width='50%'>:   {$d['nobukti']}</td>
  <td width='20%'>Tanggal</td><td width='40%'>:   ".tgl_indo($d['tanggal'])."</td>
</tr>
</table>
<div class='line'></div>
<br>

<table class='tbl'>
<tr>
  <td width='25%'>Sudah Terima Dari</td>
  <td>: <b>BENDAHARA ".strtoupper($d['nama_sekolah'])."</b></td>
</tr>
<tr>
  <td>Banyaknya Uang</td>
  <td>: <b>Rp ".number_format($total_pengeluaran,0,',','.')."</b></td>
</tr>
<tr>
  <td>Terbilang</td>
  <td class='box'>".ucwords(terbilang($total_pengeluaran))." rupiah</td>
</tr>
</table>

<br>

<table class='tbl'>
<tr>
    <td width='25%'>Kode Kegiatan</td>
    <td width='3%'>:</td>
    <td width='72%'>".$d['kode_kegiatan']."</td>
</tr>
<tr>
    <td>Kode Rekening</td>
    <td>:</td>
    <td>".$d['kode_rekening']."</td>
</tr>
<tr>
    <td>Untuk Pembayaran</td>
    <td>:</td>
    <td>
        ".$d['uraian']." jumlah ".$d['jumb']." ".$d['satuanb']."
    </td>
</tr>
</table>

<br>

".($pajakAktif ? "
<br>
<table class='tbl pajak' style='width:50%;'>
<tr class='bg-grey'>
    <td width='65%'><b>Nilai Sebelum Pajak</b></td>
    <td width='15%'>:</td>
    <td width='50%' align='right'>Rp ".number_format($nilai_sebelum_pajak,0,',','.')."</td>
</tr>

<tr class='bg-grey'>
    <td>DPP Nilai Lain</td>
    <td>:</td>
    <td align='right'>Rp ".number_format($dpp_nilai_lain,0,',','.')."</td>
</tr>

<tr>
    <td>PPN (12%)</td>
    <td>:</td>
    <td align='right'>Rp ".number_format($ppn,0,',','.')."</td>
</tr>

<tr class='bg-grey'>
    <td>DPP PPh</td>
    <td>:</td>
    <td align='right'>Rp ".number_format($dpp_pph,0,',','.')."</td>
</tr>

<tr>
    <td>PPh Pasal 21 (5%)</td>
    <td>:</td>
    <td align='right'>Rp ".number_format($pph21,0,',','.')."</td>
</tr>

<tr>
    <td>PPh Pasal 22 (0,5%)</td>
    <td>:</td>
    <td align='right'>Rp ".number_format($pph22,0,',','.')."</td>
</tr>

<tr>
    <td>PPh Pasal 23 (2%)</td>
    <td>:</td>
    <td align='right'>Rp ".number_format($pph23,0,',','.')."</td>
</tr>

<tr class='total'>
    <td><b>Total Pengeluaran</b></td>
    <td>:</td>
    <td align='right'><b>Rp ".number_format($total_pengeluaran,0,',','.')."</b></td>
</tr>
</table>
" : "")."

<br>
<table class='tbl'>
<tr>
  <td width='25%'>Tahun Anggaran</td>
  <td>: " .date('Y')."</td>
</tr>
</table>
<table class='ttd-bos'>
<tr>
    <td class='ttd-judul' width='33%'>
        Menyetujui,<br>
    Kepala Sekolah<br>
    ".strtoupper($d['nama_sekolah'])."
    </td>

    <td class='ttd-judul' width='33%'>
        Dibayar lunas tgl, ".tgl_indo($d['tanggal'])."<br>
    Oleh Bendahara BOS<br>
    ".strtoupper($d['nama_sekolah'])."
    </td>

    <td class='ttd-judul' width='33%'>
	<br>
        Yang Menerima
		<br>
    </td>
</tr>

<!-- GARIS TENGAH UTUH -->
<tr class='ttd-garis-tengah'>
    <td colspan='3'></td>
</tr>

<tr>
    <td class='ttd-jarak'></td>
    <td class='ttd-jarak'></td>
    <td class='ttd-jarak'></td>
</tr>

<tr>
    <td>
        <span class='nama'>".($d['kepala_sekolah'])."</span><br>
NIP. ".$d['nip_kepsek']."
    </td>

    <td>
        <span class='nama'>".($d['bendahara'])."</span><br>
NIP. ".$d['nip_bendahara']."
    </td>

    <td>
        <span class='nama'>".strtoupper($d['yang_menerima'])."</span>
    </td>
</tr>
</table>
";

$options = new Options();
$options->set('isRemoteEnabled', true);
$pdf = new Dompdf($options);
$pdf->loadHtml($html);
$pdf->setPaper('A4','portrait');
$pdf->render();
$pdf->stream("BUKTI_PENGELUARAN_{$d['nobukti']}.pdf", ["Attachment"=>false]);
exit;
