<?php
//Rekap Penjualan Produk Per Karyawan Grosir-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='rekapprodukkaryawanx'){
	echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Rekap Semua Data Penjualan (Grosir) oleh Karyawan<hr></b></div>
	
	<span style='float:right; margin-right:0px;'>
		<form method=POST action='rekap-produk-karyawanx.html'>
				Periode : <select name='bulan'>
						<option value=0 selected>- Pilih Bulan -</option>";
						$tampil=mysql_query("SELECT substring(tanggal_pesan,6,2) as bulan FROM rb_orders_grosir 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=0 selected>- Pilih Tahun -</option>";
						$tampil=mysql_query("SELECT substring(tanggal_pesan,1,4) as tahun FROM rb_orders_grosir 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>
			
			<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='grosir') 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 sum(b.jumlah) as total_jumlah, sum((b.jumlah*c.harga_grosir)-((b.diskon/100)*(b.jumlah*c.harga_grosir)))-a.diskon as total_harga_bersih
											FROM `rb_orders_grosir` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk where a.karyawan='$r[username]' AND a.tanggal_pesan LIKE '$periode%'"));
	}else{
		$h = mysql_fetch_array(mysql_query("SELECT sum(b.jumlah) as total_jumlah, sum((b.jumlah*c.harga_grosir)-((b.diskon/100)*(b.jumlah*c.harga_grosir)))-a.diskon as total_harga_bersih
											FROM `rb_orders_grosir` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk where a.karyawan='$r[username]'"));
	}
	if(($no % 2)==0){ $warna="#E1E1E1"; }else{ $warna="#ffffff"; }
	if ($h[total_jumlah] == ''){ $jumlah = 0; }else{ $jumlah = $h[total_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[total_harga_bersih])."</td>
			<td><center><a class='linkkk' href='media.php?module=laporanprodukx&a=$r[username]'>Lihat Detail</a></td>
		</tr>";
		$no++;
	}
	if (isset($_POST[submit])){
	    $periode = $_POST[tahun].'-'.$_POST[bulan];
		$t = mysql_fetch_array(mysql_query("SELECT sum(b.jumlah) as total_jumlah, sum(((b.jumlah*c.harga_grosir)-b.diskon)) as total_harga FROM `rb_orders_grosir` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk where a.tanggal_pesan LIKE '$periode%'"));
		$d = mysql_fetch_array(mysql_query("SELECT sum(diskon) as diskon FROM  rb_orders_grosir where tanggal_pesan LIKE '$periode%'"));
	}else{
		$t = mysql_fetch_array(mysql_query("SELECT sum(b.jumlah) as total_jumlah, sum(((b.jumlah*c.harga_grosir)-b.diskon)) as total_harga FROM `rb_orders_grosir` a JOIN rb_orders_detail b ON a.id_orders=b.id_orders JOIN rb_produk c ON b.id_produk=c.id_produk"));
		$d = mysql_fetch_array(mysql_query("SELECT sum(diskon) as diskon FROM  rb_orders_grosir"));
	}
	if ($t[total_jumlah] == ''){ $jumlah = 0; }else{ $jumlah = $t[total_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[total_harga]-$d[diskon])."</td>
			<td style='background:#9c9c9c; color:red;'></td>
		</tr>
	</tbody>
</table></div></div>";
}