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

session_start();

$tgl_awal	= $_GET['tgl_awal'];
$tgl_akhir	= $_GET['tgl_akhir'];
$kasir		= $_GET['kasir'];
$jamawal	= $_GET['jamawal'];
$jamakhir	= $_GET['jamakhir'];
$caraby		= $_GET['caraby'];
$caribrg	= $_GET['caribrg'];
$kodedt 	= $_GET['kodedt'];
$kodecab	= $_GET['kodecab'];
$cabang 	= $_SESSION['namacab'];

echo "<div id='info'>
<table id='theList' width='100%'>
	<tr>
		<th>No.</th>
		<th>Kode</th>
		<th>Nama Barang</th>
		<th>Barcode</th>
		<th>Satuan</th>
		<th>Jumlah Unit</th>
		<th>Harga Jual</th>
		<th>Total Harga</th>
	</tr>";
	
	if(!empty($kasir)){
		if(!($caraby=='')){
		    if(!empty($jamawal))
			    $query1	= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND user ='$kasir' AND jam BETWEEN '$jamawal' AND '$jamakhir' AND jenis_by='$caraby' AND posting='P' AND batal='' GROUP BY kode_pj";
            else
                $query1	= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND user ='$kasir' AND jenis_by='$caraby' AND posting='P' AND batal='' GROUP BY kode_pj";
		}else{
		    if(!empty($jamawal))
		        $query1	= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND user ='$kasir' AND jam BETWEEN '$jamawal' AND '$jamakhir' AND posting='P' AND !(jenis_by='') AND batal='' GROUP BY kode_pj";
            else{
                $query1	= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND user ='$kasir' AND posting='P' AND !(jenis_by='') AND batal='' GROUP BY kode_pj";
            }
		}
	}else{
		if(!empty($caribrg))
			$query1= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND (nama_st LIKE '%$caribrg%' OR kode_st LIKE '%$caribrg%') AND posting='P' AND batal='' GROUP BY kode_pj";
		else{
		    if(!empty($kodedt))
                $query1	= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND kode_dt='$kodedt' AND posting='P' AND batal='' GROUP BY kode_pj";
			else{
			    if(!($caraby=='')){
			        if(!empty($jamawal))
			            $query1= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND jam BETWEEN '$jamawal' AND '$jamakhir' AND jenis_by='$caraby' AND posting='P' AND batal='' GROUP BY kode_pj";
			        else
    				    $query1= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND jenis_by='$caraby' AND posting='P' AND batal='' GROUP BY kode_pj";
			    }		    
			    else{
			        if(!empty($jamawal))
				        $query1= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND jam BETWEEN '$jamawal' AND '$jamakhir' AND posting='P' AND !(jenis_by='') AND batal='' GROUP BY kode_pj";
		            else
		                $query1= "SELECT kode_pj,tanggal,kode_cab,nama_pl,nama_dt,pasien,jenis_by,nobank,jenis_kt,nokartu,count(kode_st) as totitem,jam,user FROM faktur_pj WHERE tanggal BETWEEN '$tgl_awal' AND '$tgl_akhir' AND kode_cab='$kodecab' AND posting='P' AND !(jenis_by='') AND batal='' GROUP BY kode_pj";
			    }
			}
		}
	}
	$data1= mysql_query($query1);
	
	$tjumlah=0;
	$tpotong=0;
	$tedc=0;
	$tcharge=0;
	
	while($r1=mysql_fetch_array($data1)){
		$kodepj =$r1['kode_pj'];
		$jenisby=$r1['jenis_by'];
		$nobank =$r1['nobank'];
		$jeniskt=$r1['jenis_kt'];
		$nokartu=$r1['nokartu'];

		echo "<tr>
			<td align='center'><b>Faktur</b></td>
			<td><b>$r1[kode_pj]</b></td>				
			<td><b>".jin_date_str($r1['tanggal'])." $cabang</b></td>
			<td><b>Pelanggan : $r1[nama_pl]</b></td>
			<td><b>Kasir : $r1[user]</b></td>
			<td><b>Jam : $r1[jam]</b></td>
			<td><b>Dokter : $r1[nama_dt]</b></td>
			<td><b>Pasien : $r1[pasien]</b></td>
		</tr>";
		if(!empty($caribrg))
			$query = mysql_query("SELECT * FROM faktur_pj WHERE kode_pj='$kodepj' AND (nama_st LIKE '%$caribrg%' OR kode_st LIKE '%$caribrg%')");
		else
			$query = mysql_query("SELECT * FROM faktur_pj WHERE kode_pj='$kodepj'");
		
		$no=1;
		$sjumlah=0;
		$spotong=0;
		$sedc=0;
		$scharge=0;
		while($r=mysql_fetch_array($query)){
			echo "<tr>
				<td align='center'>$no</td>
				<td align='center'>$r[kode_st]</td>
				<td>$r[nama_st]</td>
				<td>$r[barcode]</td>
				<td align='center'>$r[satuan]</td>
				<td align='right'>$r[jml_unit]</td>
				<td align='right'>".number_format($r['hg_jual'])."</td>
				<td align='right'>".number_format($r['jumlah'])."</td>
			</tr>";
			$sjumlah	=$sjumlah+$r['jumlah'];
			$spotong	=$spotong+$r['potongan'];
			$sbayar	=$sbayar+$r['bayar'];
			
			if(!($jenisby=='TUNAI')) $scharge=$scharge+$r['charge_edc'];
			$tsedc=$tedc-$tspotong+$tcharge;
			$no++;			
		}
		$netto=$sjumlah-$spotong;
		
		if(!($jenisby=='TUNAI')) $sedc=$netto;
			
		echo "<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Jumlah</td>
			<td align='right'>".number_format($sjumlah)."</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Potongan</td>
			<td align='right'>".number_format($spotong)."</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>			
			<td align='right'>Netto</td>
			<td align='right'>".number_format($netto)."</td>
		</tr>
		<tr>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td></td>
			<td align='right'>Pembayaran</td>
			<td>$jenisby</td>
		</tr>";
			
		if(!($jenisby=='TUNAI')){
			echo "<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Rekening EDC</td>
				<td>$nobank</td>
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Jenis Kartu</td>
				<td>$nokartu</td>
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td align='right'>Nomor Kartu</td>
				<td>$nokartu</td>
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>				
				<td align='right'>Charge EDC</td>
				<td align='right'>".number_format($scharge)."</td>
			</tr>";
		}
		$tjumlah=$tjumlah+$sjumlah;
		$tedc   =$tedc+$sedc;
		$tcharge=$tcharge+$scharge;
		$tpotong=$tpotong+$spotong;
		$ttot	=$tjumlah+$tcharge;
	}
	$ttunai=$tjumlah-$tedc-$tpotong;
	$tsetor=$ttunai;
	$tnetto=$tjumlah-$tpotong;
	
	echo "<tr></tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Total</td>
		<td align='right'>".number_format($ttot)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Potongan</td>
		<td align='right'>".number_format($tpotong)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Netto</td>
		<td align='right'>".number_format($tnetto)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Tunai</td>
		<td align='right'>".number_format($ttunai)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>EDC</td>
		<td align='right'>".number_format($tedc)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Charge EDC</td>
		<td align='right'>".number_format($tcharge)."</td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td align='right'>Total Setoran</td>
		<td align='right'>".number_format($tsetor)."</td>
	</tr>
</table>";
echo "</div>";
?>