<?php
//Rekap Penjualan Produk Per Karyawan (Eceran) -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='rekapprodukkaryawan'){
	echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Rekap Semua Data Penjualan (Eceran) oleh Karyawan<hr></b></div>
	
	<span style='float:right; margin-right:0px;'>
		<form method=POST action='rekap-produk-karyawan.html'>
				Periode : <select name='bulan'>
						<option value='' selected>- Pilih Bulan -</option>";
						$tampil=mysql_query("SELECT substring(tanggal_pesan,6,2) as bulan FROM rb_orders GROUP BY substring(tanggal_pesan,6,2)");
						while($r=mysql_fetch_array($tampil)){
						$bulan = Bulan($r[bulan]);
							if ($_POST[bulan]==$r[bulan]){
								echo "<option value=$r[bulan] selected>$bulan</option>";
							}else{
								echo "<option value=$r[bulan]>$bulan</option>";
							}
						}	
				echo "</select>
				<select name='tahun'>
						<option value='' selected>- Pilih Tahun -</option>";
						$tampil=mysql_query("SELECT substring(tanggal_pesan,1,4) as tahun FROM rb_orders GROUP BY substring(tanggal_pesan,1,4)");
						while($r=mysql_fetch_array($tampil)){
							if ($_POST[tahun]==$r[tahun]){
								echo "<option value=$r[tahun] selected>$r[tahun]</option>";
							}else{
								echo "<option value=$r[tahun]>$r[tahun]</option>";
							}
						}
				echo "</select>
			<input type='submit' name='submit' class='submitt' value='Lihat'>
		</form>
	</span><br><br>

	<div style='float:right;'>
					<form style='margin-top:-5px; margin-right:10px' method=POST action='rekap-produk-karyawan.html'>
							Tanggal : <select name='tgl'>
								<option value='".date("d")."' selected>".date("d")."</option>";
                                for($n=1; $n<=31; $n++){
                                	$lebar=strlen($n);
								    switch($lebar){ 
								    	case 1: { $g="0".$n; break; }
									    case 2: { $g=$n; break; }      
								    }
                                    if ($_POST[tgl]==$g){
                                    	echo "<option value='$g' selected>$g</option>";
                                	}else{
                                		echo "<option value='$g'>$g</option>";
                                	}
                                }
							echo "</select>

							<select name='bln'>";
								$bln = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
                                echo "<option value='".date("n")."' selected>".$bln[date("n")]."</option>";
                                for($n=1; $n<=12; $n++){
                                	if ($_POST[bln]==$n){
                                		echo "<option value='$n' selected>$bln[$n]</option>";
                                	}else{
                                    	echo "<option value='$n'>$bln[$n]</option>";
                                    }
                                }
							echo "</select>

							<select name='thn'>
								<option value='".date("Y")."' selected>".date("Y")."</option>";
                                for($n=2013; $n<=date("Y"); $n++){ 
                                    if ($_POST[thn]){
                                		echo "<option value='$n' selected>$n</option>";
                                	}else{
	                                    echo "<option value='$n'>$n</option>";
	                                }
                                } 
							echo "</select>

						<input type='submit' name='submit3' class='submitt' value='Lihat'>
					</form>
				</div>
			
			<div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
		<tr>
			<th width='40px'>No</th>
			<th>Nama Karyawan</th>
			<th>Jumlah Produk Terjual</th>
			<th>Total Penjualan Bersih</th>
			<th width='130px' align='center'>Action</th>
		</tr>
	</thead>
	<tfoot>
		<tr style='border: 2px solid #e3e3e3;'>
			<th style='border: none;'>No</th>
			<th style='border: none;'>Karyawan</th>
			<th style='border: none;'>Jumlah Terjual</th>
			<th style='border: none;'>Total Jual Bersih Bersih</th>
			<th style='border: none;'>Action</th>
		</tr>
	</tfoot>";
	$no=1;
	    $tampill=mysql_query("SELECT * FROM rb_users where (level='karyawan' AND status='biasa') OR level='admin'");
		echo "<tbody>";
    while($r=mysql_fetch_array($tampill)){	
    if (isset($_POST[submit])){
    	$periode = $_POST[tahun].'-'.$_POST[bulan];
    	$h = mysql_fetch_array(mysql_query("SELECT a.*, b.harga, c.karyawan, d.nama_lengkap, sum(b.harga - a.diskon - c.diskon) as tot_harga, sum(a.jumlah) as jumlah 
												FROM `rb_orders_detail` a JOIN rb_produk b ON a.id_produk=b.id_produk
													JOIN rb_orders c ON a.id_orders=c.id_orders 
														JOIN rb_users d ON c.karyawan=d.username where c.id_user='$r[id_user]' AND c.tanggal_pesan LIKE '$periode%'"));
    }elseif (isset($_POST[submit3])){
    	$bln=strlen($_POST[bln]);
		switch($bln){ 
			case 1: { $g="0".$_POST[bln]; break; }
			case 2: { $g=$_POST[bln]; break; }      
		}
		$periode = $_POST[thn].'-'.$g.'-'.$_POST[tgl];
    	$h = mysql_fetch_array(mysql_query("SELECT a.*, b.harga, c.karyawan, d.nama_lengkap, sum(b.harga - a.diskon - c.diskon) as tot_harga, sum(a.jumlah) as jumlah 
												FROM `rb_orders_detail` a JOIN rb_produk b ON a.id_produk=b.id_produk
													JOIN rb_orders c ON a.id_orders=c.id_orders 
														JOIN rb_users d ON c.karyawan=d.username where c.id_user='$r[id_user]' AND c.tanggal_pesan LIKE '%$periode%'"));
    }else{
    	$h = mysql_fetch_array(mysql_query("SELECT a.*, b.harga, c.karyawan, d.nama_lengkap, sum(b.harga - a.diskon - c.diskon) as tot_harga, sum(a.jumlah) as jumlah 
												FROM `rb_orders_detail` a JOIN rb_produk b ON a.id_produk=b.id_produk
													JOIN rb_orders c ON a.id_orders=c.id_orders 
														JOIN rb_users d ON c.karyawan=d.username where c.id_user='$r[id_user]'"));
	}
	if(($no % 2)==0){ $warna="#E1E1E1"; }else{ $warna="#ffffff"; }
	if ($h[jumlah] == ''){ $jumlah = 0; }else{ $jumlah = $h[jumlah]; }

	echo "<tr bgcolor='$warna'>
			<td class='read_only' style='color:blue; height:27px;'>$no</td>
			<td><span style='text-transform:capitalize;'>$r[nama_lengkap] </span><span style='text-transform:capitalize; color:blue'>($r[username])</span></td>
            <td><span style='color:red'>$jumlah</span></td>
			<td>Rp ".format_rupiah($h[tot_harga])."</td>
			<td><center><a class='linkkk' href='media.php?module=laporanproduk&a=$r[id_user]'>Lihat Detail</a></td>
		</tr>";
		$no++;
		}
		if (isset($_POST[submit])){
	    	$periode = $_POST[tahun].'-'.$_POST[bulan];
	    	$t = mysql_fetch_array(mysql_query("SELECT a.*, b.harga, c.karyawan, d.nama_lengkap, sum(b.harga - a.diskon - c.diskon) as tot_harga, sum(a.jumlah) as jumlah 
												FROM `rb_orders_detail` a JOIN rb_produk b ON a.id_produk=b.id_produk
													JOIN rb_orders c ON a.id_orders=c.id_orders 
														JOIN rb_users d ON c.karyawan=d.username where c.tanggal_pesan LIKE '$periode%'"));
	    }elseif (isset($_POST[submit3])){
	    	$bln=strlen($_POST[bln]);
			switch($bln){ 
				case 1: { $g="0".$_POST[bln]; break; }
				case 2: { $g=$_POST[bln]; break; }      
			}
			$periodee = $_POST[thn].'-'.$g.'-'.$_POST[tgl];
	    	$t = mysql_fetch_array(mysql_query("SELECT a.*, b.harga, c.karyawan, d.nama_lengkap, sum(b.harga - a.diskon - c.diskon) as tot_harga, sum(a.jumlah) as jumlah 
												FROM `rb_orders_detail` a JOIN rb_produk b ON a.id_produk=b.id_produk
													JOIN rb_orders c ON a.id_orders=c.id_orders 
														JOIN rb_users d ON c.karyawan=d.username where c.tanggal_pesan LIKE '%$periodee%'"));
	    }else{
			$t = mysql_fetch_array(mysql_query("SELECT a.*, b.harga, c.karyawan, d.nama_lengkap, sum(b.harga - a.diskon - c.diskon) as tot_harga, sum(a.jumlah) as jumlah 
												FROM `rb_orders_detail` a JOIN rb_produk b ON a.id_produk=b.id_produk
													JOIN rb_orders c ON a.id_orders=c.id_orders 
														JOIN rb_users d ON c.karyawan=d.username"));
	    }
		if ($t[jumlah] == ''){ $jumlah = 0; }else{ $jumlah = $t[jumlah]; }
		echo "<tr bgcolor='#9c9c9c'>
			<td class='read_only' style='background:#9c9c9c; color:#9c9c9c; height:27px;'>X</td>
			<td style='background:#9c9c9c; text-align:center; color:red; font-weight:bold;'>T o t a l</td>
            <td style='background:#9c9c9c; color:red;'>$jumlah</td>
			<td style='background:#9c9c9c; color:red;'>Rp ".format_rupiah($t[tot_harga])."</td>
			<td style='background:#9c9c9c; color:red;'></td>
		</tr>
</tbody>
</table></div></div>";
}