<?php
include '../../inc/inc.koneksi.php';	
include '../../inc/fungsi_tanggal.php';
	
$cari	= $_GET[cari];
$tglawal = $_GET[tglawal];
$tglakhir= $_GET[tglakhir];
$gudang= $_GET[gudang];
	
$content = "<b><h2>REKAPITULASI MUTASI BARANG</h2>		
	<li>TANGGAL  ".jin_date_str($tglawal)." S/D ".jin_date_str($tglakhir)."</li>
	<li>GUDANG $gudang</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>Koreksi Opname</th>
			<th>Total Masuk</th>
			<th>Penjualan</th>
			<th>Koreksi Opname</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];
			if($gudang=='1')
				$jmlstok=$r_data[jml_gd1];
			else if($gudang=='2')
				$jmlstok=$r_data[jml_gd2];
			else
				$jmlstok=$r_data[jml_gd3];
			
			$sql1 	 = "SELECT kode_st,gudang,masuk,keluar FROM rekap_mbr WHERE tanggal>='$tglawal' AND kode_st='$kodest' AND gudang='$gudang' 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 gudang='$gudang' AND batal='' ORDER BY kode_st AND tanggal ";
			$query3= mysql_query($sql3);
			
			$jual=0;
			$beli=0;			
			$opnplus=0;
			$opnmin=0;
			
			while($r=mysql_fetch_array($query3)){
	
				if($r[jurnal]=='BBM'){
					$jual=$jual+$r[keluar]; }
				else if($r[jurnal]=='PNB'){
					$beli=$beli+$r[masuk]; }				
				else if($r[jurnal]=='OPN'){
					if($r[masuk]==0){
						$opnmin=$opnmin+$r[keluar];}
					else 
						$opnplus=$opnplus+$r[masuk];
				}
			}
			$tmasuk=$beli+$opnplus;
			$tkeluar=$jual+$opnmin;
			$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($opnplus,2)."</td>
					<td align='right'>".number_format($tmasuk,2)."</td>
					<td align='right'>".number_format($jual,2)."</td>					
					<td align='right'>".number_format($opnplus,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;
?>
