<?php
// Laporan Penjualan ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='laporanproduk'){
	echo "<div class='post_title' style='margin-bottom:20px; position:relative;'><b>Laporan Data Penjualan Eceran Pada Optik<hr></b></div>";
		if (isset($_POST[tgl])){
			$lebar=strlen($_POST[bulan]);
			switch($lebar){
		      case 1:{ $g="0".$_POST[bulan]; break; }
		      case 2:{ $g=$_POST[bulan]; break; }      
		    } 
		    $tgl_pesann = $_POST[tahun].'-'.$g.'-'.$_POST[tanggal];
	}	
	echo "<form style='float:right; width:390px' method=POST action='laporan-terjual.html'>
					  Tanggal : <select style='width:23%' name='tanggal'><option value='' selected>- Tanggal -</option>";
													for($n=1; $n<=31; $n++){
															if ($_POST[tanggal]==$n){
																echo "<option value='$n' selected>$n</option>";
															}else{
																echo "<option value='$n'>$n</option>";
															}
														}
				echo "</select>
						-
				<select style='width:25%' name='bulan'> <option value='' selected>- Bulan -</option>";
													$bln = array('','Januari','Februari','Maret','April','Mei','Juni','Juli','Agustus','September','Oktober','November','Desember');
														for($n=1; $n<=12; $n++){
															if ($_POST[bulan]==$n){
																echo "<option value='$n' selected>$bln[$n]</option>";
															}else{
																echo "<option value='$n'>$bln[$n]</option>";
															}
														}
				echo "</select>
						-
				<select style='width:20%' name='tahun'>
									<option value=0 selected>- 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' class='submitt' name='tgl' value='Lihat'>
				</form>

				<form method=GET action='media.php'>
					<input type='hidden' name='module' value='laporanproduk'>
					Filter : <select name='inst' style='margin-right:3px; margin-bottom:5px;'>";
							echo "<option value='' selected>- Pilih Instansi -</option>";
							$tampil=mysql_query("SELECT * FROM rb_instansi ORDER BY id_instansi DESC");
							while($r=mysql_fetch_array($tampil)){
								if ($_GET[inst]==$r[id_instansi]){
									echo "<option value='$r[id_instansi]' selected>$r[nama_instansi]</option>";
								}else{
									echo "<option value='$r[id_instansi]'>$r[nama_instansi]</option>";
								}
							}
				  	echo "</select>
				  
					<select name='dok' style='margin-right:3px; margin-bottom:5px;'>";
							echo "<option value='' selected>- Pilih Dokter -</option>";
							$tampil=mysql_query("SELECT * FROM rb_dokter ORDER BY id_dokter DESC");
							while($r=mysql_fetch_array($tampil)){
								if ($_GET[dok]==$r[id_dokter]){
									echo "<option value='$r[id_dokter]' selected>$r[nama_dokter]</option>";
								}else{
									echo "<option value='$r[id_dokter]'>$r[nama_dokter]</option>";
								}
							}
				  	echo "</select>
				  		  <input type='submit' class='submitt' value='Lihat'>
				</form>
				<div style='clear:both'></div>

			<div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
		<tr>
			<th>No Orders</th>
			<th>No Ref</th>
			<th>Nama Pemesan</th>
			<th>Telepon</th>
			<th>Tanggal Pesan</th>
			<th>Karyawan</th>";
		if ($status == 'admin'){
			echo "<th>Total Modal </th>";
		}
			echo "<th>Total Jual </th>
				  <th>Total Bayar </th>";
		if ($status == 'admin'){
			echo "<th>Keuntungan </th>";
		}

			echo "<th style='text-align:center;'>Action</th>
		</tr>
	</thead>
	<tfoot>
		<tr style='border: 2px solid #e3e3e3;'>
			<th style='border: none;'>No Orders</th>
			<th style='border: none;'>No Ref</th>
			<th style='border: none;'>Nama Pemesan</th>
			<th style='border: none;'>Telepon</th>
			<th style='border: none;'>Tanggal Pesan</th>
			<th style='border: none;'>Karyawan</th>";
		if ($status == 'admin'){
			echo "<th style='border: none;'>Total Modal </th>";
		}
			echo "<th style='border: none;'>Total Jual </th>
				  <th style='border: none;'>Total Bayar </th>";

		if ($status == 'admin'){
			echo "<th style='border: none;'>Keuntungan </th>";
		}
			echo "<th style='border: none;'></th>
		</tr>
	</tfoot>";
if (isset($_GET[a])){
		$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders a JOIN rb_users b ON a.id_user=b.id_user where a.id_user='$_GET[a]' ORDER BY a.no_ref DESC");
}else{
	if (isset($_POST[tgl])){
		$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders a JOIN rb_users b ON a.id_user=b.id_user where a.tanggal_pesan='$tgl_pesann' ORDER BY a.no_ref DESC");
	}elseif($_GET[dok]!='' AND $_GET[inst]!=''){
		$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders a JOIN rb_users b ON a.id_user=b.id_user where a.id_instansi='$_GET[inst]' AND a.id_dokter='$_GET[dok]' ORDER BY a.no_ref DESC");
	}elseif($_GET[dok]!=''){
		$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders a JOIN rb_users b ON a.id_user=b.id_user where a.id_dokter='$_GET[dok]' ORDER BY a.no_ref DESC");
	}elseif($_GET[inst]!=''){
		$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders a JOIN rb_users b ON a.id_user=b.id_user where a.id_instansi='$_GET[inst]' ORDER BY a.no_ref DESC");
	}else{
		$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders a JOIN rb_users b ON a.id_user=b.id_user ORDER BY a.no_ref DESC");
	}
}
	    
	echo "<tbody>";
    while($r=mysql_fetch_array($tampil)){
	$tanggal_pesan=tgl_indo($r[tanggal_pesan]);
	  if(($no % 2)==0){ $warna="#ffffff"; }else{ $warna="#E1E1E1"; }
		if ($r[status_pesanan]=='Hutang'){
			$color = 'red';
		}elseif($r[status_pesanan]=='Lunas'){
			$color = 'blue';
		}else{
			$color = 'green';
		}

	$tot = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah*b.harga_pokok) as total, sum((a.jumlah*b.harga)) as sub_total FROM  rb_orders_detail a left join rb_produk b on a.id_produk=b.id_produk where a.id_orders='$r[id_orders]'"));
	if ($r[sisa] > 0){
		$jumlah_bayar = $r[jumlah_bayar] - $r[sisa];
	}else{
		$jumlah_bayar = $r[jumlah_bayar];
	}

	echo "<tr bgcolor='$warna'>
			<td class='read_only' style='color:red'>$r[id_orders]</td>
            <td class='linkSysteM'><a alt='Total Rp ".format_rupiah($tot[total])." (Diskon Rp. ".format_rupiah($r[diskon]).")' href=''>$r[no_ref]</a></td>
			<td>$r[nama_pemesan]</td>
            <td>$r[telpon]</td>
			<td>$tanggal_pesan</td>
			<td><span style='text-transform:capitalize;'>$r[nama_lengkap]</span></td>";
		if ($status == 'admin'){
			echo "<td>Rp ".format_rupiah($tot[total])."</td>";
		}
			echo "<td>Rp ".format_rupiah($tot[sub_total]-$r[diskon])."</td>
				  <td style='color:red'>Rp ".format_rupiah($jumlah_bayar)."</td>";
		if ($status == 'admin'){
			echo "<td style='color:blue'>Rp ".format_rupiah(($tot[sub_total]-$r[diskon])-$tot[total])."</td>";
		}
			echo "<td width=100px><center><a class='linkkk' href='detail-laporan-$r[id_orders].html'>Lihat Detail</a> ";
			echo "</center></td>
		</tr>";
		}

	$totall = mysql_fetch_array(mysql_query("SELECT sum(a.jumlah*b.harga_pokok) as total, sum((a.jumlah*b.harga)-z.diskon) as sub_total FROM  rb_orders z JOIN rb_orders_detail a ON z.id_orders=a.id_orders left join rb_produk b on a.id_produk=b.id_produk"));
	$besar = mysql_fetch_array(mysql_query("SELECT sum(jumlah_bayar-sisa) as total FROM `rb_orders` where sisa > 0"));
	$kecil = mysql_fetch_array(mysql_query("SELECT sum(jumlah_bayar) as total FROM `rb_orders` where sisa < 0"));
	echo "<tr>
			<td style='background:yellow; color:yellow' class='read_only'>0</td>
            <td style='background:yellow'></td>
			<td style='background:yellow'></td>
			<td style='background:yellow'></td>
			<td style='background:yellow'></td>
			<td style='background:yellow'></td>";
		if ($status == 'admin'){
			echo "<td style='background:yellow'>Rp ".format_rupiah($totall[total])."</td>";
		}
			echo "<td style='background:yellow'>Rp ".format_rupiah($totall[sub_total])."</td>
				  <td style='background:yellow'>Rp ".format_rupiah($kecil[total]+$besar[total])."</td>";
		if ($status == 'admin'){
			echo "<td style='background:yellow'>Rp ".format_rupiah($totall[sub_total]-$totall[total])."</td>";
		}
			echo "<td style='background:yellow'></td>
		</tr>";

echo "	</tbody>
 </table>
</div></div>";
}