<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';

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

	mysql_query("CREATE TABLE $nafile LIKE lap_jual");
	mysql_query("DELETE FROM $nafile WHERE 1");

	if(!$kodecab==''){
	    if(!$kodesp=='')
		    $sql1= mysql_query("SELECT kode_cab,kode_st,nama_st,barcode,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 kode_cab='$kodecab' AND kode_sp='$kodesp' AND posting='P' AND batal='' GROUP BY kode_st");
		else{
		    if(!$kodegr=='')
		        $sql1= mysql_query("SELECT kode_cab,kode_st,nama_st,barcode,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 kode_cab='$kodecab' AND concat(substr(kode_st,1,2))='$kodegr' AND posting='P' AND batal='' GROUP BY kode_st");
		    else
		        $sql1= mysql_query("SELECT kode_cab,kode_st,nama_st,barcode,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 kode_cab='$kodecab' AND posting='P' AND batal='' AND posting='P' GROUP BY kode_st");
		}
	}
	else{
	    if(!empty($kodedt))
	        $sql1= mysql_query("SELECT kode_cab,kode_st,nama_st,barcode,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 kode_dt='$kodedt' AND posting='P' AND batal='' AND posting='P' GROUP BY kode_st");
	    else{
	        if(!$kodesp=='')
		        $sql1= mysql_query("SELECT kode_cab,kode_st,nama_st,barcode,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 kode_sp='$kodesp' AND posting='P' AND batal='' AND posting='P' GROUP BY kode_st");
		    else{
		        if(!$kodegr=='')
    		        $sql1= mysql_query("SELECT kode_cab,kode_st,nama_st,barcode,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 concat(substr(kode_st,1,2))='$kodegr' AND batal='' AND posting='P' GROUP BY kode_st");
	    	    else
		            $sql1= mysql_query("SELECT kode_cab,kode_st,nama_st,barcode,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 posting='P' GROUP BY kode_st");
		    }
	    }
	}
	$row1=mysql_num_rows($sql1);
	
	if($row1>0){	
		while($r1=mysql_fetch_array($sql1)){
			$kodecab=$r1['kode_cab'];
			$kodest	=$r1['kode_st'];
			$namast	=$r1['nama_st'];
			$barcode=$r1['barcode'];
			$satuan	=$r1['satuan'];
			$tunit	=$r1['tunit'];
			$tjumlah=$r1['tjumlah'];
			$thpp 	=$r1['thpp'];
			$laba	=$tjumlah-$thpp;
			
			if($thpp==0) $margin=0;
			else $margin=($laba/$thpp)*100;
			$margin=round($margin,2);
			
			mysql_query("INSERT INTO $nafile (kode_st,nama_st,barcode,satuan,tgl_awal,tgl_akhir,jml_unit,jumlah,hpp,laba,margin) VALUES('$kodest','$namast','$barcode','$satuan','$tgl_awal','$tgl_akhir','$tunit','$tjumlah','$thpp','$laba','$margin')");
		}
	}
	
echo "<div id='info'>	
	<table id='theList' width='100%'>
		<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']+$r['jumlah1']+$r['jumlah2']+$r['jumlah3'];
			$hpp=$r['hpp']+$r['hpp1']+$r['hpp2']+$r['hpp3'];
			
			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++;
		}
		if(!$thpp==0)
			$margin	=($tlaba/$thpp)*100;
		else
			$margin	= 0;
		
		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>";
echo "</div>";
?>