<?php
// Laporan Penjualan Grosir ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='laporanprodukx'){
	echo "<div class='post_title' style='margin-bottom:20px; position:relative;'><b>Laporan Data Penjualan Grosir 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 "<div style='float:right;'>
					<form style='float:right; width:390px; margin-right:10px' method=POST action='laporan-terjualx.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_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' class='submitt' name='tgl' value='Lihat'>
				</form></div><br><br>

				<div style='float:right;'>
				<form style='margin-top:-5px; margin-right:10px' method=POST action='laporan-terjualx.html'>
				Filter : <select name='a'>
					<option value='' selected>- Nama Perusahaan -</option>";
					$perusahaan = mysql_query("SELECT perusahaan FROM rb_orders_grosir GROUP BY perusahaan");
					while ($p = mysql_fetch_array($perusahaan)){
						echo "<option value='$p[perusahaan]'>$p[perusahaan]</option>";
					}
				echo "</select>
				<input type='submit' class='submitt' name='perusahaan' value='Lihat'>
				</form>
				</div>
				<div style='clear:both'></div>

			<div id='container'>
			<div id='demo'>
<table class='display' id='example'>
	<thead>
		<tr>
			<th>No Orders</th>
			<th>Nama Pemesan</th>
			<th>Perusahaan</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;'>Nama Pemesan</th>
			<th style='border: none;'>Perusahaan</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_grosir a JOIN rb_users b ON a.karyawan=b.username where a.karyawan='$_GET[a]' ORDER BY a.id_orders DESC");
	}else{
		if (isset($_POST[tgl])){
			$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders_grosir a JOIN rb_users b ON a.karyawan=b.username where a.tanggal_pesan='$tgl_pesann' ORDER BY a.id_orders DESC");
		}elseif (isset($_POST[perusahaan])){
			$tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders_grosir a JOIN rb_users b ON a.karyawan=b.username where a.perusahaan='$_POST[a]' ORDER BY a.id_orders DESC");
		}else{
		    $tampil = mysql_query("SELECT a.*, b.nama_lengkap FROM rb_orders_grosir a JOIN rb_users b ON a.karyawan=b.username ORDER BY a.id_orders 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_grosir)) 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[nama_pemesan]</a></td>
			<td>$r[perusahaan]</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=120px><center><a class='linkkk' href='detail-grosir-$r[id_orders].html'>Lihat Detail</a> ";
			if ($status == 'admin'){ 
				echo "<a class='linkkk' style='color:Red' href='grosir-hapus-laporan-$r[id_orders].html' onclick=\"return confirm('Anda yakin ingin hapus data ini?')\">X</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_grosir) as sub_total FROM  rb_orders_grosir 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_grosir` where sisa > 0"));
	$kecil = mysql_fetch_array(mysql_query("SELECT sum(jumlah_bayar) as total FROM `rb_orders_grosir` where sisa < 0"));
	$dis = mysql_fetch_array(mysql_query("SELECT sum(diskon) as diskon FROM `rb_orders_grosir`"));
	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]-$dis[diskon])."</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]-$dis[diskon])-$totall[total])."</td>";
		}
			echo "<td style='background:yellow'></td>
		</tr>";

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