<script type="text/javascript">
	window.print();
</script>

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

$user	    = $_GET['user'];
$tglawal    = $_GET['tglawal'];
$tglakhir   = $_GET['tglakhir'];
$kodecab    = $_GET['kodecab'];
$jamawal	= $_GET['jamawal'];
$jamakhir	= $_GET['jamakhir'];

if($tglakhir==$tglawal){
    if(!empty($jamawal))
        $sql = mysql_query("SELECT kode_pj,tanggal,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglawal' AND concat(substr(jam,1,5)) BETWEEN '$jamawal' AND '$jamakhir' AND batal='' AND posting='P' GROUP BY kode_pj");
    else
        $sql = mysql_query("SELECT kode_pj,tanggal,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglawal' AND batal='' AND posting='P' GROUP BY kode_pj");

    $r= mysql_fetch_array($sql);
    $jam_aw	= $r['jam'];

    if(!empty($jamawal))
        $sql1 = mysql_query("SELECT kode_pj,tanggal,jenis_by,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglawal' AND concat(substr(jam,1,5)) BETWEEN '$jamawal' AND '$jamakhir' AND !(jenis_by='') AND batal='' AND posting='P' GROUP BY kode_pj");
    else
        $sql1 = mysql_query("SELECT kode_pj,tanggal,jenis_by,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglawal' AND !(jenis_by='') AND batal='' AND posting='P' GROUP BY kode_pj");
    
    $tjual 	= 0;
    $tpotong= 0;
    $tpottun= 0;
    $tpotedc= 0;
    $tnotajl= 0;
    $tunitjl= 0;
    $ttunai	= 0;
    $tretur	= 0;
    $tunitrt= 0;
    $tedc	= 0;
    $tqris  = 0;
    $tgrab  = 0;
    $tcharge= 0;

    while($r1=mysql_fetch_array($sql1)){
        $kodepj = $r1['kode_pj'];
        $jenisby= $r1['jenis_by'];
        $jam_ak	= $r1['jam'];
	
        $sql    = 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($r=mysql_fetch_array($sql)){
            $item++;
            $jual   = $jual+$r['jumlah'];
		    $potong	= $potong+$r['potongan'];
            $bayar  = $bayar+$r['bayar'];
            $charge = $charge+$r['charge_edc'];
            $unit   = $unit+$r['jml_unit'];
            $jam_ak = $r['jam'];
        }
	    $tjual	= $tjual+$jual;
	    $tunitjl= $unitjl+$unit;
	    $tpotong= $tpotong+$potong;
	    $ttot	= $tjual+$tcharge;
	
	    if($unit<0){
            $tretur	= $tretur+$r['jumlah'];
	        $tunitrt= $tunitrt+$r['jml_unit'];
	        echo "retur $tretur ";
        }
        
	    if($jenisby=='TUNAI'){
		    $ttunai	= $ttunai+$jual;
		    $tpottun= $tpottun+$potong;
	    }else{
	        if($jenisby=='EDC'){
		        $tedc	= $tedc+$jual;
		        $tpotedc= $tpotedc+$potong;
		        $tcharge= $tcharge+$charge;
		        $tsedc	= $tedc-$tpotedc+$tcharge;
	        }else{
	            if($jenisby=='QRIS'){
		            $tqris  = $tqris+$jual;
		            $tpotedc= $tpotedc+$potong;
		            $tcharge= $tcharge+$charge;
		            $tsqris = $tqris-$tpotedc+$tcharge;
	            }else{
	                if($jenisby=='GRAB') $tgrab = $tgrab+$jual;
	            }
	        }
	    }
	    $tnotajl++;
    }
}
else{
    $sql = mysql_query("SELECT kode_pj,tanggal,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglawal' AND concat(substr(jam,1,5))>='$jamawal' AND batal='' AND posting='P' GROUP BY kode_pj");
    
    $r= mysql_fetch_array($sql);
    $jam_aw	= $r['jam'];

    $sql1 = mysql_query("SELECT kode_pj,tanggal,jenis_by,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglawal' AND concat(substr(jam,1,5))>='$jamawal' AND !(jenis_by='') AND batal='' AND posting='P' GROUP BY kode_pj");
    
    $tjual 	= 0;
    $tpotong= 0;
    $tpottun= 0;
    $tpotedc= 0;
    $tnotajl= 0;
    $tunitjl= 0;
    $ttunai	= 0;
    $tretur	= 0;
    $tunitrt= 0;
    $tedc	= 0;
    $tqris  = 0;
    $tgrab  = 0;
    $tcharge= 0;

    while($r1=mysql_fetch_array($sql1)){
        $kodepj = $r1['kode_pj'];
        $jenisby= $r1['jenis_by'];
        $jam_ak	= $r1['jam'];
	
        $sql    = 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($r=mysql_fetch_array($sql)){
            $item++;
            $jual   = $jual+$r['jumlah'];
		    $potong	= $potong+$r['potongan'];
            $bayar  = $bayar+$r['bayar'];
            $charge = $charge+$r['charge_edc'];
            $unit   = $unit+$r['jml_unit'];
            $jam_ak = $r['jam'];
        }
	    $tjual	= $tjual+$jual;
	    $tunitjl= $unitjl+$unit;
	    $tpotong= $tpotong+$potong;
	    $ttot	= $tjual+$tcharge;
	    
	    if($unit<0){
            $tretur	= $tretur+$r['jumlah'];
	        $tunitrt= $tunitrt+$r['jml_unit'];
        }
        
	    if($jenisby=='TUNAI'){
		    $ttunai	= $ttunai+$jual;
		    $tpottun= $tpottun+$potong;
	    }else{
	        if($jenisby=='EDC'){
		        $tedc	= $tedc+$jual;
		        $tpotedc= $tpotedc+$potong;
		        $tcharge= $tcharge+$charge;
		        $tsedc	= $tedc-$tpotedc+$tcharge;
	        }else{
	            if($jenisby=='QRIS'){
		            $tqris  = $tqris+$jual;
		            $tpotedc= $tpotedc+$potong;
		            $tcharge= $tcharge+$charge;
		            $tsqris = $tqris-$tpotedc+$tcharge;
	            }else{
	                if($jenisby=='GRAB') $tgrab = $tgrab+$jual;
	            }
	        }
	    }
	    $tnotajl++;
    }
    
    $sql = mysql_query("SELECT kode_pj,tanggal,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglakhir' AND concat(substr(jam,1,5))<='$jamakhir' AND batal='' AND posting='P' GROUP BY kode_pj");
    
    $r= mysql_fetch_array($sql);
    $jam_aw	= $r['jam'];

    $sql1 = mysql_query("SELECT kode_pj,tanggal,jenis_by,jam FROM faktur_pj WHERE user='$user' AND tanggal='$tglakhir' AND concat(substr(jam,1,5))<='$jamakhir' AND !(jenis_by='') AND batal='' AND posting='P' GROUP BY kode_pj");
    
    while($r1=mysql_fetch_array($sql1)){
        $kodepj = $r1['kode_pj'];
        $jenisby= $r1['jenis_by'];
        $jam_ak	= $r1['jam'];
	
        $sql    = 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($r=mysql_fetch_array($sql)){
            $item++;
            $jual   = $jual+$r['jumlah'];
		    $potong	= $potong+$r['potongan'];
            $bayar  = $bayar+$r['bayar'];
            $charge = $charge+$r['charge_edc'];
            $unit   = $unit+$r['jml_unit'];
            $jam_ak = $r['jam'];
            
            if($unit<0){
                $tretur	= $tretur+$r['jumlah'];
	            $tunitrt= $tunitrt+$r['jml_unit'];
            }
        }
	    $tjual	= $tjual+$jual;
	    $tunitjl= $unitjl+$unit;
	    $tpotong= $tpotong+$potong;
	    $ttot	= $tjual+$tcharge;
	
	    if($jenisby=='TUNAI'){
		    $ttunai	= $ttunai+$jual;
		    $tpottun= $tpottun+$potong;
	    }else{
	        if($jenisby=='EDC'){
		        $tedc	= $tedc+$jual;
		        $tpotedc= $tpotedc+$potong;
		        $tcharge= $tcharge+$charge;
		        $tsedc	= $tedc-$tpotedc+$tcharge;
	        }else{
	            if($jenisby=='QRIS'){
		            $tqris  = $tqris+$jual;
		            $tpotedc= $tpotedc+$potong;
		            $tcharge= $tcharge+$charge;
		            $tsqris = $tqris-$tpotedc+$tcharge;
	            }else{
	                if($jenisby=='GRAB') $tgrab = $tgrab+$jual;
	            }
	        }
	    }
	    $tnotajl++;
    }
}
$tretur	=$tretur*-1;
$tretedc=$tretedc*-1;

$tnetto		=$tjual-$tpotong-$tretur+$tcharge;
$tnetjual	=$tnetto+$tretur;

$tsetor	=$ttunai-$tpottun-$tretur;
$tnota	=$tnotajl+$tnotart;
$tunit	=$tunitjl+$tunitrt;

$sql=mysql_query("SELECT * FROM cabang WHERE kode_cab='$kodecab'");
$r  =mysql_fetch_assoc($sql);
$kop1=$r['nama_cab'];
$kop2=$r['alamat'];

echo "<table width='100%'>
		<tr>
			<td align='center' style='font:15px verdana'><b>$kop1</b></td>
		</tr>
		<tr>
		    <td align='center' style='font:11px verdana'>$kop2</td>
		</tr>
    </table>
    <hr>
    
    <table width='100%' style='font:11px verdana'>
		<tr>
			<td>Nama Kasir</td>
			<td>: $user</td>
			<td></td>
		</tr>
		<tr>
			<td>Tanggal Penjualan</td>
			<td>: ".jin_date_str($tglawal)."</td>
			<td></td>
		</tr>
		<tr>
			<td>Jam Pelayanan</td>
			<td>: $jam_aw-$jam_ak</td>
			<td></td>
		</tr>
		<tr>
			<td>Jumlah Nota</td>
			<td>: $tnota</td>
			<td></td>
		</tr>
		<tr>
			<td>Jumlah Barang</td>
			<td>: ".number_format($tunit,2)." unit</td>
			<td></td>
		</tr>
		<tr>
			<td>Penjualan Tunai</td>
			<td>: Rp ".number_format($ttunai,0)."</td>
			<td></td>
		</tr>
		<tr>
			<td>Penjualan EDC</td>
			<td>: Rp ".number_format($tsedc,0)."</td>
			<td> +</td>
		</tr>
		<tr>
			<td>Penjualan QRIS</td>
			<td>: Rp ".number_format($tsqris,0)."</td>
			<td> +</td>
		</tr>
		<tr>
			<td>Penjualan GRAB</td>
			<td style='border-bottom:double 1px' >: Rp ".number_format($tgrab,0)."</td>
			<td> +</td>
		</tr>
		<tr>
			<td>Total Penjualan</td>
			<td>: Rp ".number_format($ttot,0)."</td>
		</tr>
		<tr>
			<td>Retur Tunai</td>
			<td>: Rp ".number_format($tretur,0)."</td>
			<td> -</td>
		</tr>
		<tr>
			<td>Retur EDC</td>
			<td style='border-bottom:1px double'>: Rp ".number_format($tretedc,0)."</td>
			<td> -</td>
		</tr>
		<tr>
			<td>Potongan Tunai</td>
			<td>: Rp ".number_format($tpottun,0)."</td>
			<td> -</td>
		</tr>
		<tr>
			<td>Potongan EDC</td>
			<td style='border-bottom:1px double'>: Rp ".number_format($tpotedc,0)."</td>
			<td> -</td>
		</tr>
		<tr>
			<td>Penjualan Netto</td>
			<td>: Rp ".number_format($tnetto,0)."</td>
			<td></td>
		</tr>
		<tr>
			<td>Charge EDC/QRIS</td>
			<td>: Rp ".number_format($tcharge,0)."</td>
			<td></td>
		</tr>
		<tr>
			<td>Setoran Tunai</td>
			<td>: Rp ".number_format($tsetor,0)."</td>
			<td></td>
		</tr>
	</table>
	<hr>";
?>
<script type="text/javascript">	
	history.back();
</script>