<?php
include '../../inc/inc.koneksi.php';	
include '../../inc/fungsi_tanggal.php';
	
$cari	= $_GET[cari];
$tglawal = $_GET[tglawal];
$tglakhir= $_GET[tglakhir];
	
$content = "<b><h2>REKAPITULASI MUTASI BARANG</h2>		
<li>TANGGAL  ".jin_date_str($tglawal)." S/D ".jin_date_str($tglakhir)."</li>
<br>
	
<table width='500' border='1' style='border-collapse:collapse'>
	<tr>
		<th>No.</th>
		<th>Kode</th>
		<th>Nama Barang</th>
		<th>Stok Awal</th>
		<th>Pembelian</th>
		<th>Retur Penjualan</th>
		<th>Stok Opname</th>
		<th>Total Masuk</th>
		<th>Penjualan</th>
		<th>Retur Pembelian</th>
		<th>Total Keluar</th>
		<th>Stok Akhir</th>			
	</tr>";
	
	if (!empty($cari)){
		$query2	= "SELECT * FROM st_group WHERE (nama_gr LIKE '%$cari%' or kode_gr LIKE '%$cari%') ORDER BY kode_gr";
	}
	else{
		$query2	= "SELECT * FROM st_group ORDER BY kode_gr";
	}
		
	$data2	= mysql_query($query2);
	while($r_data2=mysql_fetch_array($data2)){
		$kodegr=$r_data2[kode_gr];
		
		$content .= "<tr>
			<td></td>
			<td><b>$r_data2[kode_gr]</b></td>					
			<td><b>$r_data2[nama_gr]</b></td>
		</tr>";
		
		$sql = "SELECT * FROM stock WHERE kode_gr='$kodegr' ORDER BY kode_st ";
		$query = mysql_query($sql);
		
		$no=1+$hal;
		while($r_data=mysql_fetch_array($query)){
			$kodest=$r_data[kode_st];
			$jmlstok=$r_data[jml_stok];
	
			$sql1 	 = "SELECT kode_st,masuk,keluar FROM rekap_mbr WHERE tanggal>='$tglawal' AND kode_st='$kodest' AND batal='' ORDER BY kode_st AND tanggal ";
			$query1= mysql_query($sql1);
			$jmasuk=0;
			$jkeluar=0;
		
			while($r1=mysql_fetch_array($query1)){			
				$jmasuk=$jmasuk+$r1[masuk];
				$jkeluar=$jkeluar+$r1[keluar];
			}
			$stawal=$jmlstok-$jmasuk+$jkeluar;
			
			$sql3 = "SELECT * FROM rekap_mbr WHERE tanggal BETWEEN '$tglawal' AND '$tglakhir' AND kode_st='$kodest' AND batal='' ORDER BY kode_st AND tanggal ";
			$query3= mysql_query($sql3);
		
			$jual=0;
			$retjual=0;
			$beli=0;			
			$retbeli=0;
			$opname=0;
			
			while($r=mysql_fetch_array($query3)){
				if($r[jurnal]=='PJL')
					$jual=$jual+$r[keluar];
				else if($r[jurnal]=='PBL')
					$beli=$beli+$r[masuk];
				else if($r[jurnal]=='RPB')
					$retbeli=$retbeli+$r[keluar];
				else if($r[jurnal]=='RPJ')
					$retjual=$retjual+$r[masuk];
				else if($r[jurnal]=='SO')
					$opname=$opname+$r[masuk];
			}
			$tmasuk=$beli+$retjual+$opname;
			$tkeluar=$jual+$retbeli;
			$stakhir=$stawal+$tmasuk-$tkeluar;
				
			$content .= "<tr>
				<td>$no</td>
				<td>$r_data[kode_st]</td>
				<td>$r_data[nama_st]</td>
				<td align='right'>".number_format($stawal,2)."</td>
				<td align='right'>".number_format($beli,2)."</td>
				<td align='right'>".number_format($retjual,2)."</td>
				<td align='right'>".number_format($opname,2)."</td>
				<td align='right'>".number_format($tmasuk,2)."</td>
				<td align='right'>".number_format($jual,2)."</td>
				<td align='right'>".number_format($retbeli,2)."</td>
				<td align='right'>".number_format($tkeluar,2)."</td>
				<td align='right'>".number_format($stakhir,2)."</td>
			</tr>";
			$no++;
			$jml++;
		}
	}
			
	$content .= "</table>";
	$content .= "<table width='100%'>
		<tr>
			<td>Jumlah Data : $jml</td>
		</tr>
	</table>";
				
	header("Content-type: application/x-msdownload");
	header("Content-Disposition: attachment; filename=rekap_mb.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
echo $content;
?>