<script type="text/javascript">
	window.print();
</script>
<?php
	include '../../inc/inc.koneksi.php';	
	include '../../inc/fungsi_tanggal.php';

	$tgl_awal	= $_GET['tgl_awal'];
	$tgl_akhir	= $_GET['tgl_akhir'];
	$kodesp  	= $_GET['kodesp'];
	$namasp	    = $_GET['namasp'];
	$kodegr  	= $_GET['kodegr'];
	$namagr	    = $_GET['namagr'];
	$kodecab  	= $_GET['kodecab'];
	$urut     	= $_GET['urut'];
	$user		= $_GET['user'];
	$kodedt 	= $_GET['kodedt'];
	$namadt 	= $_GET['namadt'];
	$nafile 	= 'labapj_'.$user;

	$sql2	= mysql_query("SELECT * FROM company");
	$r2		= mysql_fetch_assoc($sql2);
	$kop1 	= $r2['nama1'];
	$kop2 	= $r2['nama2'];
	$kop3 	= $r2['alamat'];
	
	echo "<table width='100%' style='font:11px arial'><b>
		<tr>
			<td>$kop1</td>
		</tr>
		<tr>
			<td>$kop2</td>
		</tr>
		<tr>
			<td>$kop3</td>
		</tr>
	</table>
	
	<table width='100%'>
		<tr>
			<td align='center' style='font:14px arial'><b>LAPORAN LABA/RUGI PENJUALAN PER BARANG</b></td>
		</tr>
		<tr>
			<td align='center' style='font:14px arial'><b>TANGGAL : ".jin_date_str($tgl_awal)." S/D ".jin_date_str($tgl_akhir)."</b></td>
		</tr>";
		if(!$kodecab==''){
		    echo "<tr>
			    <td align='center' style='font:14px arial'><b>CABANG : $namacab</b></td>
		    </tr>";
		}
		if(!$kodesp==''){
			echo "<tr>
				<td align='center' style='font:14px arial'><b>SUPPLIER : $namasp</b></td>
			</tr>";
		}
		if(!$kodegr==''){
			echo "<tr>
				<td align='center' style='font:14px arial'><b>KELOMPOK : $namagr</b></td>
			</tr>";
		}
		if(!empty($kodedt)){
			echo "<tr>
				<td align='center' style='font:15px arial'><b>DOKTER : $namadt ($kodedt)</b></td>
			</tr>";
		}
	echo "</table>
	<br>
	
	<table width='100%' border='1' style='border-collapse:collapse;font:11px arial'>
		<tr>
			<th>No.</th>
			<th>Kode Barang</th>
			<th>Nama Barang</th>
			<th>Barcode</th>
			<th>Satuan</th>
			<th>Jumlah Unit</th>
			<th>Penjualan</th>
			<th>Harga Pokok</th>
			<th>Laba/Rugi</th>
			<th>Margin %</th>
		</tr>";
				
		if($urut=='kode')
			$sql= mysql_query("SELECT * FROM $nafile ORDER BY kode_st ");
		else
			$sql= mysql_query("SELECT * FROM $nafile ORDER BY laba desc ");
		
		$no=1;
		$tjumlah=0;$thpp=0;$tlaba=0;
		while($r=mysql_fetch_array($sql)){
			$jumlah=$r['jumlah'];
			$hpp=$r['hpp'];
			
			echo "<tr>
				<td align='center'>$no</td>
				<td align='center'>$r[kode_st]</td>
				<td>$r[nama_st]</td>
				<td>$r[barcode]</td>
				<td>$r[satuan]</td>
				<td align='right'>$r[jml_unit]</td>			
				<td align='right'>".number_format($jumlah)."</td>
				<td align='right'>".number_format($hpp,0)."</td>
				<td align='right'>".number_format($r['laba'],0)."</td>
				<td align='right'>".number_format($r['margin'],2)."</td>
			</tr>";
			$tjumlah=$tjumlah+$jumlah;
			$thpp	=$thpp+$hpp;
			$tlaba	=$tlaba+$r['laba'];
			$no++;
		}
		$margin	=($tlaba/$thpp)*100;
		
		echo "<tr>
			<td></td>
			<td></td>
			<td></td>			
			<td></td>
			<td></td>
			<td align='right'>Total</td>
			<td align='right'>".number_format($tjumlah)."</td>
			<td align='right'>".number_format($thpp,0)."</td>
			<td align='right'>".number_format($tlaba,0)."</td>
			<td align='right'>".number_format($margin,2)."</td>
		</tr>
	</table>";
?>