<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
require_once('include/global.php');
if (!isset($_SESSION)) session_start();

if(isset($_GET['norek']) && isset($_GET['dari']) && isset($_GET['sampai'])){
	$db=pDatabase::getInstance();
	$dari=$db->d_date($_GET['dari']);
	$sampai=$db->d_date($_GET['sampai']);
	$norek=$_GET['norek'];
	
	$rst=array();
	$rst['norekening']=$norek;
	$rst['tanggaldari']=$_GET['dari'];
	$rst['tanggalsampai']=$_GET['sampai'];
	$saldoawal=0;
	
	$query="select 'Saldo Awal' as keterangan,'' as nama,'' as no_rek,'' as tanggal,sum(if(jenis=0,jumlah,0)) as masuk,sum(if(jenis=1,jumlah,0)) as keluar,
				sum(if(jenis=0,jumlah,0))-sum(if(jenis=1,jumlah,0)) as saldo 
			from tb_tabungan where tanggal<? ";
	$rsawal=$db->loadAssoc($query,array($dari));
	
	$dataawal=array(array(keterangan=>'Saldo Awal',"masuk"=>0,"keluar"=>0,"saldo"=>0));
	if(isset($rsawal['saldo'])) {
		$saldoawal=$rsawal['saldo'];
	}
	$rsawal['saldo']=number_format($rsawal['saldo'],0,",",".");
	$dataawal=array($rsawal);
	
	$query="SELECT 	t1.keterangan,t1.tanggal,t2.no_rek,t2.nama,
					if(t1.jenis=0,t1.jumlah,0) as masuk,if(t1.jenis=1,t1.jumlah,0) as keluar 
			FROM tb_tabungan as t1
		inner join tb_siswa as t2 on t1.no_rek=t2.no_rek		
		WHERE t1.id>0 and t1.tanggal>=? and t1.tanggal<=? and t2.no_rek=?
		GROUP BY t1.id
		ORDER BY t1.tanggal ASC";
	$rs=$db->loadAll($query,array($dari,$sampai,$norek));
	
	if(sizeof($rs)>0) $rst['nama']=$rs[0]['nama'];
	$saldoberjalan=$saldoawal;
	for($i=0;$i<sizeof($rs);$i++){
		$saldoberjalan+=($rs[$i]['masuk']-$rs[$i]['keluar']);
		if($saldoberjalan>0) {
			$rs[$i]['saldo']=number_format($saldoberjalan,0,",",".");
		}else{
			$rs[$i]['saldo']="";
		}
		
		if($rs[$i]['masuk']>0){
			$rs[$i]['kredit']=number_format($rs[$i]['masuk'],0,",",".");
		}else{
			$rs[$i]['kredit']="";
		}
		
		if($rs[$i]['keluar']>0){
			$rs[$i]['debet']=number_format($rs[$i]['keluar'],0,",",".");
		}else{
			$rs[$i]['debet']="";
		}
		
	}
	
	$rsdata=array_merge($dataawal,$rs);
	
	$handle = fopen("cetak/transaksi.html", "r");
	$newline="";
	while(!feof($handle)){
	    $line = fgets($handle);
		$newline.=$line;
	}
	fclose($handle);
	

	$datafinal="";
	$searchtagbukukas = "/<@data>(.*?)<\/@data>/s";
	preg_match_all($searchtagbukukas, $newline, $matchdata);
	if(sizeof($matchdata)==2 && $matchdata[1][0]!=""){
		$strlampiran=$matchdata[0][0];
		$linelampiran=$matchdata[1][0];
		for($i=0;$i<sizeof($rsdata);$i++){
			$rsdata[$i]['no']=1+$i;
			if($rsdata[$i]['tanggal']!="") $rsdata[$i]['tanggal']=$db->sql_date($rsdata[$i]['tanggal']);
			$datacetak=$linelampiran;
			$searchdata = "/<data>(.*?)<\/data>/s";
			preg_match_all($searchdata, $linelampiran, $matchdatabukukas);
			
			if(sizeof($matchdatabukukas)==2 && $matchdatabukukas[1][0]!=""){
				for($x=0;$x<sizeof($matchdatabukukas[1]);$x++){
					$rpline="null";
					if(isset($rsdata[$i][$matchdatabukukas[1][$x]])){
						$rpline=$rsdata[$i][$matchdatabukukas[1][$x]];
					}
					$datacetak=str_replace($matchdatabukukas[0][$x], $rpline, $datacetak);
				}
			}
			$datafinal.=$datacetak;
		}
		
		$newline=str_replace($strlampiran, $datafinal, $newline);
	}
	
	$obj=(object) array("data"=>$newline);
	echo $db->renderdata($obj,$rst);
	
}

?>