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

$tanggal    = $_GET['tanggal'];
$kodecab    = $_GET['kodecab'];
$jamawal	= $_GET['jamawal'];
$jamakhir	= $_GET['jamakhir'];

echo "<table id='theList' width='100%'>
	<tr>
		<th>No.</th>
		<th>Cabang</th>
		<th>Kasir</th>
        <th>Jam Layanan</th>
		<th>Jumlah Nota</th>
		<th>Jumlah Item</th>
        <th>Jumlah Unit</th>
		<th>Jumlah Tunai</th>
		<th>Jumlah EDC</th>
		<th>Jumlah QRIS</th>
		<th>Jumlah GRAB</th>
		<th>Chg. EDC/QRIS</th>
		<th>Jumlah Omset</th>
	</tr>";
	if(!empty($kodecab)){
	    if(!empty($jamawal)){
	        $sql2= mysql_query("SELECT kode_cab,user FROM faktur_pj WHERE kode_cab='$kodecab' AND tanggal='$tanggal' AND concat(substr(jam,1,5)) BETWEEN '$jamawal' AND '$jamakhir' AND batal='' AND posting='P' GROUP BY user");
	    }else
	        $sql2= mysql_query("SELECT kode_cab,user FROM faktur_pj WHERE kode_cab='$kodecab' AND tanggal='$tanggal' AND batal='' AND posting='P' GROUP BY user");
    }
    else{
        if(!empty($jamawal))
            $sql2= mysql_query("SELECT kode_cab,user FROM faktur_pj WHERE tanggal='$tanggal' AND concat(substr(jam,1,5)) BETWEEN '$jamawal' AND '$jamakhir' AND batal='' AND posting='P' GROUP BY user");
        else
            $sql2= mysql_query("SELECT kode_cab,user FROM faktur_pj WHERE tanggal='$tanggal' AND batal='' AND posting='P' GROUP BY user");
    }
    
	$tjual 	= 0;
	$tnota	= 0;
	$ttunai	= 0;
	$tedc	= 0;
	$tqris  = 0;
	$tgrab  = 0;
	$tunit  = 0;
	$titem  = 0;
	$tcharge= 0;
	$tpotong= 0;
	$no		= 1;
	
	while($r2=mysql_fetch_array($sql2)){
        $user    = $r2['user'];
        $kodecab1= $r2['kode_cab'];
        
        $jam_aw	 = '';
        $jam_ak	 = '';
        
	    $sjual 	= 0;
		$snota	= 0;
		$stunai	= 0;
		$sedc	= 0;
		$sqris  = 0;
		$sgrab  = 0;
		$sunit  = 0;
		$sitem  = 0;
		$scharge= 0;
		$spotong= 0;
		
	    if(!empty($jamawal))
	        $sql1 = mysql_query("SELECT kode_cab,jam,user FROM faktur_pj WHERE user='$user' AND tanggal='$tanggal' AND concat(substr(jam,1,5)) BETWEEN '$jamawal' AND '$jamakhir' AND batal='' AND posting='P' GROUP BY kode_pj");
	    else
	        $sql1 = mysql_query("SELECT kode_cab,jam,user FROM faktur_pj WHERE user='$user' AND tanggal='$tanggal' AND batal='' AND posting='P' ORDER BY kode_pj");
	        
	    $r1     = mysql_fetch_array($sql1);
	    $jam_aw	= $r1['jam'];

        if(!empty($jamawal))
            $sql = mysql_query("SELECT kode_pj,tanggal,kode_cab,jenis_by,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tanggal' AND concat(substr(jam,1,5)) BETWEEN '$jamawal' AND '$jamakhir' AND !(jenis_by='') AND batal='' AND posting='P' GROUP BY kode_pj");
        else
            $sql = mysql_query("SELECT kode_pj,tanggal,kode_cab,jenis_by,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tanggal' AND !(jenis_by='') AND batal='' AND posting='P' GROUP BY kode_pj");
        
		while($r=mysql_fetch_array($sql)){
            $kodepj = $r['kode_pj'];
            $jenisby= $r['jenis_by'];
			
            $sql3   = mysql_query("SELECT * FROM faktur_pj WHERE kode_pj='$kodepj' AND batal='' AND posting='P'");
            $jual   = 0;
            $bayar  = 0;
            $charge = 0;
			$item   = 0;
            $unit   = 0;
            $tunai  = 0;
			$potong	= 0;
			
            while($r3=mysql_fetch_array($sql3)){
                $item++;
                $jual   = $jual+$r3['jumlah'];
				$potong	= $potong+$r3['potongan'];
                $bayar  = $bayar+$r3['bayar'];
                $charge = $charge+$r3['charge_edc'];
                $unit   = $unit+$r3['jml_unit'];
                $jam_ak = $r3['jam'];
            }
	        $sjual	= $sjual+$jual;
	        $sunit  = $sunit+$unit;
			$sitem	= $sitem+$item;
			$spotong= $spotong+$potong;
	
	        if($jenisby=='EDC'){
		        $sedc	= $sedc+$jual-$potong;
		        $scharge= $scharge+$charge;
	        }else{
	            if($jenisby=='QRIS'){
		            $sqris  = $sqris+$jual-$potong;
		            $scharge= $scharge+$charge;
	            }else{
	                if($jenisby=='GRAB') $sgrab= $sgrab+$jual-$potong;
	                else $stunai = $stunai+$jual-$potong;
	            }
	        }
	        $snota++;
	    }
	    $sjual=$sjual+$scharge-$spotong;

		echo "<tr>
	        <td align='center'>$no</td>
    	    <td align='center'>$kodecab1</td>
    		<td>$user</td>
	    	<td>$jam_aw - $jam_ak</td>
		    <td align='right'>$snota</td>
			<td align='right'>$sitem</td>
		    <td align='right'>$sunit</td>
    		<td align='right'>".number_format($stunai,0)."</td>
		    <td align='right'>".number_format($sedc,0)."</td>
		    <td align='right'>".number_format($sqris,0)."</td>
		    <td align='right'>".number_format($sgrab,0)."</td>
	        <td align='right'>".number_format($scharge,0)."</td>
    		<td align='right'>".number_format($sjual,0)."</td>
	    </tr>";
		$tjual	= $tjual+$sjual;
		$tnota	= $tnota+$snota;
		$titem	= $titem+$sitem;
		$tunit  = $tunit+$sunit;
		$ttunai	= $ttunai+$stunai;
		$tedc 	= $tedc+$sedc;
		$tqris	= $tqris+$sqris;
		$tgrab	= $tgrab+$sgrab;
		$tcharge= $tcharge+$scharge;
		$no++;
	}
    
echo "<tr></tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Total</td>
		<td align='right'>$tnota</td>
		<td align='right'>$titem</td>
		<td align='right'>$tunit</td>
		<td align='right'>".number_format($ttunai,0)."</td>		
		<td align='right'>".number_format($tedc,0)."</td>
		<td align='right'>".number_format($tqris,0)."</td>
		<td align='right'>".number_format($tgrab,0)."</td>
	    <td align='right'>".number_format($tcharge,0)."</td>
		<td align='right'>".number_format($tjual,0)."</td>		
	</tr>
</table>";
?>