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

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

	function lihat_detail(ID){
		var kode = ID;	
		$.ajax({
			type	: "POST",
			url		: "modul/setor_bbm/cari_kode.php",
			data	: "kode="+kode,
			dataType: "json",
			success	: function(data){
				$("#t_kodesb").val(kode);
				$("#t_tanggal").val(data.tgl);
				$("#t_shift").val(data.shift);
				$("#t_kodetl").val(data.kodetl);
				$("#t_jenis").val(data.jenis);
				$("#t_petugas").val(data.petugas);
				$("#t_telleraw").val(data.telleraw);
				$("#t_tellerak").val(data.tellerak);
				$("#t_jmlkeluar").val(data.jmlkeluar);
				$("#t_jmltera").val(data.jmltera);
				$("#t_jmlbon").val(data.jmlbon);
				$("#t_jmltunai").val(data.jmltunai);
				$("#t_niltunai").val(data.niltunai);
				$("#pencarian").hide();
				$("#tampil_data").hide();
				$("#info_bbm").show();
				$("#tampil_detail").show();
				$("#tampil_detail").load('modul/setor_bbm/tampil_data_bon.php?kode='+kode);
			}
		});		
	}
</script>

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

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

echo "<table id='theList' width='100%'>
		<tr>
			<th>No.</th>
			<th>Kode Setoran</th>			
			<th>No.Teller</th>
			<th>Jenis BBM</th>			
			<th>Teller Awal</th>
			<th>Teller Akhir</th>
			<th>Liter Tera</th>
			<th>Liter Bon</th>
			<th>Liter Tunai</th>
			<th>Liter Jual</th>
			<th>Nilai Bon</th>
			<th>Setor Tunai</th>
			<th>Penjualan</th>
			<th>Lihat</th>
		</tr>";
		
		if(!empty($cari))
			$sql1 = "SELECT tanggal,shift FROM setor_bbm WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' AND (jenis LIKE '%$cari%') GROUP BY tanggal,shift ";
		else
			$sql1 = "SELECT * FROM setor_bbm WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND batal='' GROUP BY tanggal,shift ";
		$query1 = mysql_query($sql1);
		
		$jml=0;
		$ttunai=0;
		$ttera=0;
		$tbon=0;
		$tltun=0;
		$tljual=0;
		$tnilbon=0;
		$tniltun=0;
		$tjual=0;
		while($r1=mysql_fetch_array($query1)){
			$tanggal=$r1[tanggal];
			$shift=$r1[shift];
			
			echo "<tr>
				<td></td>
				<td><b>Tanggal :</b></td>
				<td><b>".jin_date_str($r1[tanggal])."</b></td>
				<td align='center'><b>Shift : $r1[shift]</b></td>
			</tr>";
			
			if(!empty($cari))
				$sql = "SELECT * FROM setor_bbm WHERE tanggal='$tanggal' AND shift='$shift' AND batal='' AND (jenis LIKE '%$cari%') GROUP BY kode_sb ";
			else
				$sql = "SELECT * FROM setor_bbm WHERE tanggal='$tanggal' AND shift='$shift' AND batal='' GROUP BY kode_sb ";
			$query = mysql_query($sql);
			
			$no=1;
			$stunai=0;
			$stera=0;
			$sbon=0;
			$sltun=0;
			$sljual=0;
			$snilbon=0;
			$sniltun=0;
			$sjual=0;
			while($r_data=mysql_fetch_array($query)){
				$kodesb = $r_data[kode_sb];
				
				$sql2 = "SELECT kode_sb,kode_tl,jenis,petugas,teller_aw,teller_ak,jml_tera,jml_bon,jml_tunai,nilai_tunai,sum(nilai_nopol) as nilbon FROM setor_bbm WHERE kode_sb='$kodesb' AND batal='' GROUP BY kode_sb ";
				$query2 = mysql_query($sql2);
				
				while($r2=mysql_fetch_array($query2)){
					$ljual = $r2[jml_bon]+$r2[jml_tunai];
					$stunai = $stunai+$r2[nilai_tunai];
					$stera = $stera+$r2[jml_tera];
					$sbon = $sbon+$r2[jml_bon];
					$sltun = $sltun+$r2[jml_tunai];					
					$jual = $r2[nilai_tunai]+$r2[nilbon];
					$snilbon = $snilbon+$r2[nilbon];
					$sniltun = $sniltun+$r2[nilai_tunai];
					$sjual = $sjual+$jual;
					$sljual = $sljual+$ljual;
					
					echo "<tr>
						<td align='center'>$no</td>
						<td align='center'>$r2[kode_sb]</td>
						<td align='center'>$r2[kode_tl]</td>
						<td>$r2[jenis]</td>						
						<td align='right'>$r2[teller_aw]</td>
						<td align='right'>$r2[teller_ak]</td>
						<td align='right'>".number_format($r2[jml_tera],2)."</td>
						<td align='right'>".number_format($r2[jml_bon],2)."</td>
						<td align='right'>".number_format($r2[jml_tunai],2)."</td>
						<td align='right'>".number_format($ljual,2)."</td>
						<td align='right'>".number_format($r2[nilbon])."</td>
						<td align='right'>".number_format($r2[nilai_tunai])."</td>
						<td align='right'>".number_format($jual)."</td>
						<td align='center'>
						<a href='javascript:void(0)' onClick=\"lihat_detail('$r2[kode_sb]')\">
							<img src='images/view.png' border='0' id='edit' title='Lihat detail' width='12' height='12' >
						</a>
						</td>
					</tr>";
					$no++;
				}
			}
			$tljual = $tljual+$sljual;
			$ttera = $ttera+$stera;
			$tbon = $tbon+$sbon;
			$tltun = $tltun+$sltun;
			$tniltun = $tniltun+$sniltun;
			$tnilbon = $tnilbon+$snilbon;
			$tjual = $tjual+$sjual;
			echo "<tr>
				<td></td>
				<td></td>
				<td></td>				
				<td></td>
				<td></td>				
				<td align='right'>Jumlah</td>
				<td align='right'>".number_format($stera,2)."</td>
				<td align='right'>".number_format($sbon,2)."</td>
				<td align='right'>".number_format($sltun,2)."</td>
				<td align='right'>".number_format($sljual,2)."</td>
				<td align='right'>".number_format($snilbon)."</td>
				<td align='right'>".number_format($sniltun)."</td>
				<td align='right'>".number_format($sjual)."</td>
			</tr>";
		}
		
		echo "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>				
				<td align='right'>Total</td>
				<td align='right'>".number_format($ttera,2)."</td>
				<td align='right'>".number_format($tbon,2)."</td>
				<td align='right'>".number_format($tltun,2)."</td>
				<td align='right'>".number_format($tljual,2)."</td>
				<td align='right'>".number_format($tnilbon)."</td>
				<td align='right'>".number_format($tniltun)."</td>
				<td align='right'>".number_format($tjual)."</td>
			</tr>";
	echo "</table>";
?>