TIF_NGANJUK_E41212035/forms/cetak_ikhtisarkas.php

117 lines
3.7 KiB
PHP

<?php
require_once('../vendor/tecnickcom/tcpdf/tcpdf.php'); // Pastikan path benar
include '../config/database.php';
// Pastikan tidak ada output sebelum header dikirim
ob_start();
// Atur local time ke bahasa Indonesia
setlocale(LC_TIME, 'id_ID.utf8', 'Indonesian_indonesia.1252');
// Fungsi untuk mengubah format bulan ke bahasa Indonesia
function ubahBulanKeIndonesia($tanggal) {
return strftime('%B %Y', strtotime($tanggal . "-01")); // Format "Maret 2024"
}
// Ambil bulan dari parameter GET
$search_bulan = isset($_GET['bulan']) ? mysqli_real_escape_string($conn, $_GET['bulan']) : '';
if (empty($search_bulan)) {
die("Bulan tidak ditemukan.");
}
// Query untuk mengambil data berdasarkan bulan
$query = "SELECT * FROM dana_kas WHERE DATE_FORMAT(kas_tanggal, '%Y-%m') = '$search_bulan' ORDER BY kas_tanggal ASC";
$result = mysqli_query($conn, $query);
// Konversi bulan ke format bahasa Indonesia
$bulanIndonesia = ubahBulanKeIndonesia($search_bulan);
// Buat instance PDF
$pdf = new TCPDF();
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Masjid-E');
$pdf->SetTitle('Laporan Ikhtisar Kas ' . $bulanIndonesia);
$pdf->SetMargins(10, 10, 10);
$pdf->SetAutoPageBreak(TRUE, 10);
$pdf->AddPage();
// Judul Laporan
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 10, 'Rekapitulasi Dana Kas', 0, 1, 'C');
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Periode: ' . $bulanIndonesia, 0, 1, 'C');
$pdf->Ln(5);
// Buat tabel agar sejajar
$pdf->SetFont('helvetica', '', 10);
$html = '
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 5px;
text-align: center;
}
th {
background-color: #f2f2f2;
font-weight: bold;
}
</style>
<table>
<thead>
<tr>
<th width="7%">No</th>
<th width="20%">Kas Pemasukan</th>
<th width="20%">Kas Pengeluaran</th>
<th width="25%">Kas Keterangan</th>
<th width="13%">Kas Jenis</th>
<th width="15%">Kas Tanggal</th>
</tr>
</thead>
<tbody>';
$no = 1;
$total_pemasukan = 0;
$total_pengeluaran = 0;
while ($row = mysqli_fetch_assoc($result)) {
$total_pemasukan += $row['kas_pemasukan'];
$total_pengeluaran += $row['kas_pengeluaran'];
$html .= '<tr>
<td width="7%">' . $no . '</td>
<td width="20%">Rp ' . number_format($row['kas_pemasukan'], 0, ',', '.') . '</td>
<td width="20%">Rp ' . number_format($row['kas_pengeluaran'], 0, ',', '.') . '</td>
<td width="25%" style="text-align: left;">' . htmlspecialchars($row['kas_keterangan']) . '</td>
<td width="13%">' . htmlspecialchars($row['kas_jenis']) . '</td>
<td width="15%">' . date('d-m-Y', strtotime($row['kas_tanggal'])) . '</td>
</tr>';
$no++;
}
// Tambahkan total pemasukan & pengeluaran
$html .= '<tr>
<th colspan="2" style="text-align: right;">Total Pemasukan:</th>
<th style="text-align: center;"><b>Rp ' . number_format($total_pemasukan, 0, ',', '.') . '</b></th>
<th colspan="2" style="text-align: right;">Total Pengeluaran:</th>
<th style="text-align: center;"><b>Rp ' . number_format($total_pengeluaran, 0, ',', '.') . '</b></th>
</tr>';
$html .= '</tbody></table>';
// Tambahkan tabel ke PDF
$pdf->writeHTML($html, true, false, true, false, '');
// Pastikan tidak ada output sebelum header dikirim
ob_end_clean();
// **Menampilkan PDF di browser, bukan mendownload langsung**
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="Ikhtisar_Kas_' . $search_bulan . '.pdf"');
$pdf->Output('Ikhtisar_Kas_' . $search_bulan . '.pdf', 'I');
?>