<?php
session_start();
error_reporting(0);
include "config/koneksi.php";
include "config/library.php";
include "config/fungsi_indotgl.php";
include "config/fungsi_seo.php";
if (isset($_SESSION[id])) {
	if ($_SESSION['level'] == 'admin') {
		$iden = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM users where username='$_SESSION[id]'"));
		$nama =  $iden['nama_lengkap'];
		$level = 'Administrator';
		$foto = 'dist/img/user.png';
	}

	$ta = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM tahun_ajaran where idTahunAjaran='$_GET[tahun]'"));
	$idTahun = $ta['idTahunAjaran'];
	$idt = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM identitas where npsn='10700295'"));
	$pos = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM pos_bayar where idPosBayar='$_GET[pos]'"));

	header("Content-type: application/vnd-ms-excel");
	header("Content-Disposition: attachment; filename=laporan_pembayaran_pos_" . str_replace(" ", "_", $pos['nmPosBayar']) . "_" . date('dmyHis') . ".xls");
?>
	<h3 align="center"><?php echo $idt['nmSekolah']; ?><br />LAPORAN PEMBAYARAN</h3>
	<h3 class="box-title">Pos Pembayaran <?php echo $pos['nmPosBayar']; ?> <?php echo $ta['nmTahunAjaran']; ?></h3>
	<table border="1" class="table table-bordered table-striped">
		<thead>
			<tr>
				<th>No.</th>
				<th>POS Bayar</th>
				<th>Jenis Pembayaran</th>
				<th>Jumlah Pembayaran</th>
				<th>Jumlah Tagihan</th>
			</tr>
		</thead>
		<tbody>
			<?php
			$no = 1;
			$totDibayar = 0;
			$totTagihan = 0;
			$sqlJenisBayar = mysqli_query($conn,"SELECT * FROM jenis_bayar WHERE idTahunAjaran='$idTahun' AND idPosBayar='$pos[idPosBayar]'");
			while ($djb = mysqli_fetch_array($sqlJenisBayar)) {
				if ($djb['tipeBayar'] == 'bulanan') {
					//menghitung semua tagihan bulanan
					$tgbul 	=	mysqli_fetch_array(mysqli_query($conn,"SELECT
									jenis_bayar.idPosBayar,
									pos_bayar.nmPosBayar,
									Sum(tagihan_bulanan.jumlahBayar) AS TotalSemuaTagihanBulanan,
									 sum(tagihan_bulanan.dispensasiBayar) as dispen
									FROM
									tagihan_bulanan
									INNER JOIN jenis_bayar ON tagihan_bulanan.idJenisBayar = jenis_bayar.idJenisBayar
									INNER JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
									WHERE tagihan_bulanan.idJenisBayar='$djb[idJenisBayar]'
									GROUP BY
									jenis_bayar.idPosBayar"));
					$semuaTagihan = $tgbul['TotalSemuaTagihanBulanan'] - $tgbul['dispen'];

					$dbayar = mysqli_fetch_array(mysqli_query($conn,"SELECT
									jenis_bayar.idPosBayar,
									pos_bayar.nmPosBayar,
									jenis_bayar.idTahunAjaran,
									tahun_ajaran.nmTahunAjaran,
									jenis_bayar.nmJenisBayar,
									Sum(tagihan_bulanan.jumlahBayar) AS TotalPembayaranPerJenis,
									 sum(tagihan_bulanan.dispensasiBayar) as dispens,
									tagihan_bulanan.statusBayar
									FROM
									tagihan_bulanan
									INNER JOIN jenis_bayar ON tagihan_bulanan.idJenisBayar = jenis_bayar.idJenisBayar
									INNER JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
									INNER JOIN tahun_ajaran ON jenis_bayar.idTahunAjaran = tahun_ajaran.idTahunAjaran
									WHERE tagihan_bulanan.idJenisBayar='$djb[idJenisBayar]' AND tagihan_bulanan.statusBayar='1'
									GROUP BY
									tagihan_bulanan.idJenisBayar"));
					$jBayar 	= $dbayar['TotalPembayaranPerJenis'] - $dbayar['dispens'];
					$tagihan 	= $semuaTagihan - $jBayar;
				} else {
					//menghitung semua tagihan bebas
					$tgb 	= 	mysqli_fetch_array(mysqli_query($conn,"SELECT
										tagihan_bebas.idTagihanBebas,
										jenis_bayar.idPosBayar,
										pos_bayar.nmPosBayar,
										SUM(tagihan_bebas.totalTagihan) As TotalSemuaTagihanBebas
										 
										FROM
										tagihan_bebas
										INNER JOIN jenis_bayar ON tagihan_bebas.idJenisBayar = jenis_bayar.idJenisBayar
										INNER JOIN pos_bayar ON jenis_bayar.idPosBayar = pos_bayar.idPosBayar
										WHERE tagihan_bebas.idJenisBayar='$djb[idJenisBayar]'
										GROUP BY
										jenis_bayar.idPosBayar"));
					$semuaTagihan = $tgb['TotalSemuaTagihanBebas'];

					$dbayar = mysqli_fetch_array(mysqli_query($conn,"SELECT
									tagihan_bebas.idJenisBayar,
									jenis_bayar.nmJenisBayar,
									jenis_bayar.idTahunAjaran,
									tahun_ajaran.nmTahunAjaran,
									tagihan_bebas_bayar.idTagihanBebas,
									Sum(tagihan_bebas_bayar.jumlahBayar) AS TotalPembayaranPerJenis,
									 sum(tagihan_bebas_bayar.dispensasiBayar) as dispens,
									tagihan_bebas_bayar.ketBayar
									FROM
									tagihan_bebas_bayar
									INNER JOIN tagihan_bebas ON tagihan_bebas_bayar.idTagihanBebas = tagihan_bebas.idTagihanBebas
									INNER JOIN jenis_bayar ON tagihan_bebas.idJenisBayar = jenis_bayar.idJenisBayar
									INNER JOIN tahun_ajaran ON jenis_bayar.idTahunAjaran = tahun_ajaran.idTahunAjaran
									WHERE tagihan_bebas_bayar.idTagihanBebas='$tgb[idTagihanBebas]'
									GROUP BY
									tagihan_bebas_bayar.idTagihanBebas"));
					$jBayar 	= $dbayar['TotalPembayaranPerJenis'] - $dbayar['dispens'];
					$jBayars 	= $dbayar['TotalPembayaranPerJenis'];
					$tagihan 	= $semuaTagihan - $jBayars;
				}
				echo "<tr>
							<td align='center'>$no</td>
							<td>$pos[nmPosBayar]</td>
							<td>$djb[nmJenisBayar]</td>
							<td align='right'>" . buatRp($jBayar) . "</td>
							<td align='right'>" . buatRp($tagihan) . "</td>
						</tr>";
				$no++;
				$totDibayar += $jBayar;
				$totTagihan += $tagihan;
			}
			?>
			<tr>
				<td></td>
				<td colspan="2">Jumlah Pembayaran</td>
				<td align="right"><b><?php echo buatRp($totDibayar); ?></b></td>
				<td align="right"><b><?php echo buatRp($totTagihan); ?></b></td>
			</tr>
		</tbody>
	</table>

	<br />
	<table width="100%">
		<tr>
			<td align="center"></td>
			<td align="center" width="400px">
				<?php echo $idt['kabupaten']; ?>, <?php echo tgl_raport(date("Y-m-d")); ?>
				<br />Kepala Tata Usaha,<br /><br /><br /><br />
				<b><u><?php echo $idt['nmKaTU']; ?></u><br /><?php echo $idt['nipKaTU']; ?></b>
			</td>
		</tr>
	</table>
<?php
} else {
	include "login.php";
}
?>