<?php
	include '../../inc/inc.koneksi.php';
	include '../../inc/fungsi_tanggal.php';
	
	$tgl_awal = $_GET['tgl_awal'];
	$tgl_akhir= $_GET['tgl_akhir'];
	$sql2= mysql_query("SELECT * FROM company");
	while($r2=mysql_fetch_array($sql2)){
		$kop1 = $r2[nama1];
		$kop2 = $r2[nama2];
		$kop3 = $r2[alamat];
	}
	
	$content = "<b><li>$kop1</li></b>
		<li>$kop2</li>
		<li>$kop3</li>
		<br>
		<b><li>DAFTAR STOK OPNAME</li>
		<li>TANGGAL : ".jin_date_str($tgl_awal)." S/D ".jin_date_str($tgl_akhir)."</li></b>
		<br>
	
		<table width='390' border='1' style='border-collapse:collapse'>
		<tr>
			<th>No.</th>
			<th>Kode</th>
			<th>Nama Barang</th>
			<th>Satuan</th>
			<th>Harga Pokok</th>
			<th>Jml. Stok</th>			
			<th>Jml. Opname</th>
			<th>Selisih</th>
			<th>Nilai Selisih</th>
			<th>Posting</th>
		</tr>";
	
		$sql1 = "SELECT * FROM st_opname WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' GROUP BY kode_op";
		$query1 = mysql_query($sql1);
		
		$jml=0;
		$tnilai=0;
		
		while($data1=mysql_fetch_array($query1)){
			$kodeop = $data1[kode_op];
		
			$content .= "<tr>
				<td></td>
				<td></td>
				<td><b>Kode Opn.: $data1[kode_op] - ".jin_date_str($data1[tanggal])."</b></td>
			</tr>";
				
			$sql = "SELECT * FROM st_opname WHERE kode_op='$kodeop' ORDER BY kode_op,kode_st";
			$query = mysql_query($sql);
			$snilai = 0;
			$no=1;
			
			while($data=mysql_fetch_array($query)){
			
				$content .= "<tr>
					<td align='center'>$no</td>
					<td align='center'>$data[kode_st]</td>
					<td>$data[nama_st]</td>
					<td>$data[satuan]</td>
					<td align='right'>".number_format($data[hg_beli],2)."</td>
					<td align='right'>".number_format($data[jml_stok],2)."</td>
					<td align='right'>".number_format($data[jml_opn],2)."</td>
					<td align='right'>".number_format($data[jml_sls],2)."</td>					
					<td align='right'>".number_format($data[nilai_sls],2)."</td>
					<td align='center'>$data[posting]</td>
				</tr>";
				$no++;
				$jml++;
				$snilai=$snilai+$data[nilai_sls];
			}
			$content .= "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jumlah</td>
				<td align='right'>".number_format($snilai,2)."</td>
			</tr>";	
			$tnilai=$tnilai+$snilai;
		}
		
		$content .= "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
			
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Total</td>
				<td align='right'>".number_format($tnilai,2)."</td>
			</tr>";
		$content .= "</table>";
		
		$content .= "<table>
			<tr>
				<td>Jumlah Data : $jml</td>
			</tr>
		</table>";
				
		header("Content-type: application/x-msdownload");
		header("Content-Disposition: attachment; filename=daftar_so.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		echo $content;
?>