<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['kodecab'];
	$urut     	= $_GET['urut'];
	$user		= $_GET['user'];
	$kodedt 	= $_GET['kodedt'];
	$nafile 	= 'rekappj_'.$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 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 FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND kode_gr='$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 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 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 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 FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_gr='$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 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'];
			
			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','0','0','0')");
		}
	}
	
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 Jual</th>
			<th>Jumlah Harga</th>
		</tr>";
				
		if($urut=='kode')
			$sql= mysql_query("SELECT * FROM $nafile ORDER BY kode_st ");
		else{
		    if($urut=='nominal')
			    $sql= mysql_query("SELECT * FROM $nafile ORDER BY jumlah desc ");
		    else
		        $sql= mysql_query("SELECT * FROM $nafile ORDER BY jml_unit desc ");
		}
		
		$no=1;
		$tjumlah=0;
		while($r=mysql_fetch_array($sql)){
			$jumlah=$r['jumlah'];
			
			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>
			</tr>";
			$tjumlah=$tjumlah+$jumlah;
			$no++;
		}
		echo "<tr>
			<td></td>
			<td></td>
			<td></td>			
			<td></td>
			<td></td>
			<td align='right'>Total</td>
			<td align='right'>".number_format($tjumlah)."</td>
		</tr>
	</table>";
	
echo "</div>";
?>