<?php
include '../../inc/inc.koneksi.php';	
include '../../inc/fungsi_tanggal.php';
	
$kode	= $_GET['kode'];
$sql2 	 = "SELECT * FROM stock WHERE kode_st='$kode' ORDER BY kode_st";
$query2= mysql_query($sql2);
$rows	= mysql_num_rows($query2);
if($rows>0){
while($r2	=mysql_fetch_array($query2)){
	$namast=$r2[nama_st];
	$satuan=$r2[satuan];
	$jmlstok=$r2[jml_stok];
	$tglawal=$r2[tgl_awal];
	$tglakhir=$r2[tgl_akhir];
}
}else{
    $namast='';
	$satuan='';
	$jmlstok=0;
	$tglawal='';
	$tglakhir='';
}

$sql1 	 = "SELECT kode_st,sum(masuk) as jmasuk,sum(keluar) as jkeluar FROM rekap_mbr WHERE tanggal>='$tglawal' AND kode_st='$kode' AND batal='' GROUP BY kode_st ";
$query1= mysql_query($sql1);
while($r1=mysql_fetch_array($query1)){			
	$jmasuk=$r1[jmasuk];
	$jkeluar=$r1[jkeluar];
}
$stawal=$jmlstok-$jmasuk+$jkeluar;
$sisa 	=$stawal;
$no = 0;
	
$content = "<b><h2 align='center'>KARTU PERSEDIAAN BARANG</h2>		
		<li align='center'>Periode  ".jin_date_str($tglawal)." S/D ".jin_date_str($tglakhir)."</li>
		<br>
		<li>Barang		:  $kode - $namast</li>
		<li>Stok Awal	:  ".number_format($stawal,2)." $satuan</li>
		<br>
	
		<table width='390' border='1' style='border-collapse:collapse'>
		<tr>
			<th>No.</th>
			<th>Tanggal</th>
			<th>Nomor Bukti</th>
			<th>Kode Trans</th>
			<th>Keterangan</th>
			<th>Masuk</th>
			<th>Keluar</th>
			<th>Sisa</th>			
		</tr>";
	
		$jmasuk=0;
		$jkeluar=0;		
			
		$sql		 = "SELECT * FROM rekap_mbr WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_st='$kode' AND batal='' ORDER BY tanggal ";
		$query	= mysql_query($sql);
		while($r=mysql_fetch_array($query)){
			$sisa=$sisa+$r[masuk]-$r[keluar];		
			
			$content .= "<tr>					
					<td>$no</td>
					<td>".jin_date_str($r[tanggal])."</td>
					<td>$r[no_bukti]</td>
					<td align='center'>$r[kode_tr]</td>
					<td>$r[keterangan]</td>
					<td align='right'>".number_format($r[masuk],2)."</td>
					<td align='right'>".number_format($r[keluar],2)."</td>
					<td align='right'>".number_format($sisa,2)."</td>
				</tr>";			
				$jmasuk=$jmasuk+$r[masuk];
				$jkeluar=$jkeluar+$r[keluar];				
				$no++;
		}
				
		$content .= "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jumlah </td>
				<td align='right'>".number_format($jmasuk,2)."</td>
				<td align='right'>".number_format($jkeluar,2)."</td>
			</tr>";
			
		$content .= "</table>";
				
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=ks_$kode.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>
