<script type="text/javascript">
    $(function() {
        $("#theList tr:even").addClass("stripe1");
        $("#theList tr:odd").addClass("stripe2");

        $("#theList tr").hover(
            function() {
                $(this).toggleClass("highlight");
            },
            function() {
                $(this).toggleClass("highlight");
            }
        );
    });
</script>

<?php
include '../../inc/inc.koneksi.php';
include '../../inc/fungsi_tanggal.php';

$tgl_awal  = $_GET['tgl_awal'];
$tgl_akhir = $_GET['tgl_akhir'];
$kodesl	   = $_GET['kodesl'];

echo "<div id='info'>	
	<table id='theList' width='100%'>
	<tr>
		<th>No.</th>
		<th>Kode Barang</th>
		<th>Nama Barang</th>
		<th>Satuan</th>
		<th>Jumlah Unit</th>
		<th>Harga Netto</th>
		<th>Harga Pokok</th>
		<th>Laba Kotor</th>
		<th>Margin</th>
	</tr>";
		
	if (!empty($kodesl))
		$query= "SELECT kode_st,nama_st,satuan,sum(jml_unit) as tunit,sum(jumlah) as tjumlah,sum(hpp) as thpp FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' AND kode_sl='$kodesl' GROUP BY kode_st";
	else
		$query= "SELECT kode_st,nama_st,satuan,sum(jml_unit) as tunit,sum(jumlah) as tjumlah,sum(hpp) as thpp FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' GROUP BY kode_st";
	
	$data= mysql_query($query);
	$no=1;
	$gtotal=0;
	$gthpp=0;
	$gtlaba=0;
	
	while($r_data=mysql_fetch_array($data)){
		$laba=$r_data[tjumlah]-$r_data[thpp];
		$hpp=$r_data[thpp];
		$kodest=$r_data[kode_st];
		$junit=$r_data[junit];
		$netto=$r_data[tjumlah]-$r_data[tpot];
		
		if (!empty($kodesl))
			$sql= mysql_query("SELECT kode_st,sum(jml_retur) as jretur,sum(jumlah) as nretur,sum(hpp) as jhpp FROM retur_pj WHERE kode_st='$kodest' AND tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_sl='$kodesl' AND batal='' GROUP BY kode_st");
		else
			$sql= mysql_query("SELECT kode_st,sum(jml_retur) as jretur,sum(jumlah) as nretur,sum(hpp) as jhpp FROM retur_pj WHERE kode_st='$kodest' AND tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' GROUP BY kode_st ");
		
		$row=mysql_num_rows($sql);
		if($row>0){
			$r=mysql_fetch_assoc($sql);
			$junit=$junit-$r[jretur];
			$netjual=$netto-$r[nretur];
			$hpp=$hpp-$r[jhpp];
			$laba=$netjual-$hpp;
		}else{
			$netjual=$netto;
		}
		if($hpp==0)
			$margin=100;
		else
			$margin=($laba/$hpp)*100;
		
		echo "<tr>	
			<td align='center'>$no</td>
			<td align='center'>$r_data[kode_st]</td>
			<td>$r_data[nama_st]</td>
			<td align='center'>$r_data[satuan]</td>
			<td align='right'>$r_data[tunit]</td>
			<td align='right'>".number_format($netjual,0)."</td>
			<td align='right'>".number_format($hpp,0)."</td>
			<td align='right'>".number_format($laba,0)."</td>
			<td align='right'>".number_format($margin,2)." %</td>
		</tr>";
		$gtotal=$gtotal+$netjual;
		$gthpp=$gthpp+$hpp;
		$gtlaba=$gtlaba+$laba;
		$no++;
	}
	$margin=($gtlaba/$gthpp)*100;
		
	echo "<tr>
		<td></td>
		<td></td>
		<td></td>			
		<td></td>
		<td align='right'>Total</td>
		<td align='right'>".number_format($gtotal,0)."</td>
		<td align='right'>".number_format($gthpp,0)."</td>
		<td align='right'>".number_format($gtlaba,0)."</td>
		<td align='right'>".number_format($margin,2)." %</td>
	</tr>
	</table>";
echo "</div>";
?>
