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

use Dompdf\Dompdf;
use Dompdf\Options;

/* ================= VALIDASI ================= */
$nobukti = trim($_GET['nobukti'] ?? '');
if ($nobukti === '') die("No bukti tidak valid");

/* ================= HEADER ================= */
$q = $conn->prepare("
    SELECT k.*, 
           s.nama_sekolah, s.kepala_sekolah, s.nip_kepsek,
           s.bendahara, s.nip_bendahara
    FROM kwitansi k
    LEFT JOIN sekolah s ON k.npsn = s.npsn
    WHERE k.nobukti = ?
    LIMIT 1
");
$q->bind_param("s", $nobukti);
$q->execute();
$d = $q->get_result()->fetch_assoc();
if (!$d) die("Data tidak ditemukan");

/* ================= DETAIL ================= */
$det = $conn->prepare("
    SELECT uraian, jumb, satuanb, pengeluaran, kode_kegiatan, kode_rekening
    FROM kwitansi
    WHERE nobukti = ?
");
$det->bind_param("s", $nobukti);
$det->execute();
$res = $det->get_result();

$detail_html = '';
$total_db = 0;
while ($r = $res->fetch_assoc()) {
    $detail_html .= "• {$r['uraian']} ({$r['jumb']} {$r['satuanb']})<br>";
    $total_db += (float)$r['pengeluaran'];
}

/* ================= NILAI ================= */
$nilai_pokok = ($_GET['nilai_pokok'] ?? '') !== ''
    ? (float) $_GET['nilai_pokok']
    : $total_db;

$dpp_lain = ($_GET['dpp_lain'] ?? '') !== ''
    ? (float) $_GET['dpp_lain']
    : 0;

/* ================= CEK PAJAK ================= */
function cek($k){
    return isset($_GET[$k]) && ($_GET[$k] == '1' || $_GET[$k] == 'on');
}

/* ================= DASAR PAJAK ================= */
// Semua pajak dihitung dari NILAI POKOK SAJA
$dpp_pajak = $nilai_pokok;

/* ================= HITUNG PAJAK ================= */
$ppn   = cek('ppn')   ? $dpp_pajak * 0.11  : 0;   // PPN 12%
$pph21 = cek('pph21') ? $dpp_pajak * 0.05  : 0;   // PPh 21 (5%)
$pph22 = cek('pph22') ? $dpp_pajak * 0.005 : 0;   // PPh 22 (0,5%)
$pph23 = cek('pph23') ? $dpp_pajak * 0.02  : 0;   // PPh 23 (2%)
$pemda = cek('pajak_daerah') ? $dpp_pajak * 0.10 : 0; // Pajak Daerah 10%

/* ================= TOTAL PENGELUARAN ================= */
// Sesuai ketentuan: hanya nilai pokok + PPN 12%
$total_pengeluaran = $nilai_pokok + $ppn;

/* ================= TANGGAL ================= */
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];
}

/* ================= TERBILANG ================= */
function terbilang($x){
    $x = abs((int)$x);
    $bil = ["","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan","sepuluh","sebelas"];

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


/* ================= HTML ================= */
$html = "
<style>
body{font-family:'Times New Roman';font-size:12px}
.box{border:1px solid #000;padding:6px}
.right{text-align:right}
.center{text-align:center}
.bold{font-weight:bold}

.ttd{
    width:100%;
    border-collapse:collapse;
    margin-top:20px;
    border-top:2px solid #000;     /* GARIS ATAS */
    border-bottom:2px solid #000;  /* GARIS BAWAH */
}

.ttd td{
    width:33%;
    text-align:center;
    vertical-align:top;
    padding:8px;

    /* GARIS VERTIKAL */
    border-left:1px solid #000;
    border-right:1px solid #000;
}

/* GARIS TENGAH */
.ttd .garis-tengah td{
    border-top:1px solid #000;
    border-left:1px solid #000;
    border-right:1px solid #000;
    padding:0;
    height:0;
}

.nama{
    font-weight:bold;
    text-decoration:underline;
}
</style>

<h2 class='center'>BUKTI PENGELUARAN</h2><hr>

<table width='100%'>
<tr>
<td>Nomor Bukti : <b>{$nobukti}</b></td>
<td class='right'>Tanggal : ".tgl_indo($d['tanggal'])."</td>
</tr>
</table><hr>

<table width='100%'>
<tr><td width='30%'>Sudah Terima Dari</td><td>: <b>BENDAHARA BOS {$d['nama_sekolah']}</b></td></tr>
<tr><td>Banyaknya Uang</td><td>: Rp ".number_format($total_pengeluaran,0,',','.')."</td></tr>
<tr><td></td><td class='box'><i>".ucwords(trim(terbilang((int)$total_pengeluaran)))." rupiah</i></td></tr>
</table><br>

<table width='100%'>
<tr><td width='30%'>Kode Kegiatan</td><td>: <b>{$d['kode_kegiatan']}</b></td></tr>
<tr><td width='30%'>Kode Rekening</td><td>: <b>{$d['kode_rekening']}</b></td></tr>
<tr><td width='30%'>Untuk Pembayaran</td><td>{$detail_html}</td></tr>
</table><br>

<table width='100%'>
<tr><td width='30%'>Nilai Sebelum Pajak</td><td>: <b>Rp ".number_format($nilai_pokok,0,',','.')."</td></tr>
<tr><td>DPP Nilai Lain</td><td>: Rp ".number_format($dpp_lain,0,',','.')."</td></tr>
<tr><td>PPN 12%</td><td>: Rp ".number_format($ppn,0,',','.')."</td></tr>
<tr><td>DPP PPh</td><td>: <b>Rp ".number_format($nilai_pokok,0,',','.')."</td></tr>
<tr><td>PPh Pasal 21 (5%)</td><td>: Rp ".number_format($pph21,0,',','.')."</td></tr>
<tr><td>PPh Pasal 22 (0.5%)</td><td>: Rp ".number_format($pph22,0,',','.')."</td></tr>
<tr><td>PPh Pasal 23 (2%)</td><td>: Rp ".number_format($pph23,0,',','.')."</td></tr>
<tr><td>Pajak Daerah (10%)</td><td>: Rp ".number_format($pemda,0,',','.')."</td></tr>
<tr><td><b>Total Pengeluaran</b></td><td>: <b>Rp ".number_format($total_pengeluaran,0,',','.')."</b></td></tr>
</table><br>

<table width='100%'>
<tr><td width='30%'>Tahun Anggaran</td><td>: <b>".date('Y')."</b></td></tr>
</table>

<!-- ================= TANDA TANGAN ================= -->
<table class='ttd'>
<tr>
    <td>
        Mengetahui,<br>
        Kepala Sekolah<br>
        {$d['nama_sekolah']}
    </td>

    <td>
        Dibayar Lunas Tgl, ".tgl_indo($d['tanggal'])."<br>
        Bendahara BOS<br>
        {$d['nama_sekolah']}
    </td>

    <td>
	<br>
        Yang Menerima
    </td>
</tr>

<tr class='garis-tengah'>
    <td></td>
    <td></td>
    <td></td>
</tr>

<tr>
    <td style='padding-top:95px'>
        <span class='nama'>{$d['kepala_sekolah']}</span><br>
        NIP {$d['nip_kepsek']}
    </td>

    <td style='padding-top:95px'>
        <span class='nama'>{$d['bendahara']}</span><br>
        NIP {$d['nip_bendahara']}
    </td>

    <td style='padding-top:95px'>
        <span class='nama'>{$d['yang_menerima']}</span>
    </td>
</tr>
</table>
";

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