<?php
// Laporan Penjualan Grosir ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='laporanpenjualangrosir'){
	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 class='post_title' style='margin-bottom:10px; position:relative;'>
				<b>Laporan Semua Data Pemesanan (Grosir) Pada Optik</b><hr>
			</div>";
			
			echo "<div style='float:right;'>
					<form style='float:right; width:390px; margin-right:10px' method=POST action='laporan-penjualan-grosir.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-penjualan-grosir.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>Alamat</th>
			<th>Telepon</th>
			<th>Tanggal Pesan</th>
			<th>Karyawan</th>
			<th>Status Pesanan</th>
			<th>Tgl Ambil </th>
			<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;'>Alamat</th>
			<th style='border: none;'>Telepon</th>
			<th style='border: none;'>Tanggal Pesan</th>
			<th style='border: none;'>Karyawan</th>
			<th style='border: none;'>Status Pesanan</th>
			<th style='border: none;'>Tgl Ambil </th>
			<th style='border: none;'></th>
		</tr>
	</tfoot>";
		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_grosir) - a.diskon) as 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]'"));
	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[alamat]</td>
            <td>$r[telpon]</td>
			<td>$tanggal_pesan</td>
			<td><span style='text-transform:capitalize;'>$r[nama_lengkap]</span></td>
			<td style='color:$color'>$r[status_pesanan]</td>
			<td style='color:red'>$r[tanggal_selesai]</td>
			<td width=100px><center><a class='linkkk' href='detail-grosir-$r[id_orders].html'>Lihat Detail</a> ";
			echo "</center></td>
		</tr>";
		}
echo "	</tbody>
 </table>
</div></div>";
}


// Detail Laporan Penjualan Grosir ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
elseif ($_GET[module]=='detailgrosir'){

	    $detail=mysql_query("SELECT * FROM rb_orders_grosir where id_orders='$_GET[id]'");
		$t=mysql_fetch_array($detail);
		$tanggal_pesan=tgl_indo($t[tanggal_pesan]);
		
		echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Detail Laporan Penjualan Grosir pada : <span style='color:red'>$t[perusahaan]</span><hr></b></div>
		<div id='content_transaksi'>
		<div id='kiri'><table width='90%'>"; ?>
		<tr><td class='td'><b>Nomor Pesanan</b></td><td class='td' width='20px'></td><td class='td'>                <input class='disabled' type=text name='id_orders' value="<?php echo "$t[id_orders]"; ?>" readonly=on> <?php echo " &nbsp;&nbsp;Tgl : <input type=text name='tanggal_pesan' value='$tanggal_pesan' style='width:30%' readonly='on' class='disabled1'></td></tr> 
        <tr><td class='td'><b>Nama Pemesan</b></td><td class='td'></td><td class='td' colspan=1><input type=text value='$t[nama_pemesan]' name='nama_pemesan' style='width:93%' class='disabled1'></td></tr>
        <tr><td class='td'><b>Alamat</b></td><td class='td'></td><td class='td' colspan=1><textarea name='alamat' style='margin-left:-1px; width:93%; height:45px;' class='disabled1'>$t[alamat]</textarea></td></tr>
		<tr><td class='td'><b>Phone/HP</b></td><td class='td'></td><td class='td' colspan=1><input type=text value='$t[telpon]' name='phone' style='width:93%' class='disabled1'></td></tr>
		<tr><td class='td'><b>Tgl Ambil/Selesai</b></td><td class='td'></td><td class='td' colspan=1><input type=text value='$t[tanggal_selesai]' name='phone' style='width:43%' class='disabled1'><br><br></td></tr>
		</table>
		</div>
		
		<div id='kanan'><table width='100%' >
		<tr><td class='td' width='100px'><b>Perusahaan</b></td><td class='td'></td><td class='td' colspan=1><input type=text value='$t[perusahaan]' name='perusahaan' placeholder=' Nama Perusahaan' style='width:73%' class='disabled1'></td></tr>
		 <tr><td class='td'><b>Keterangan</b></td><td class='td'></td><td class='td' colspan=1><textarea name='keterangan' style='margin-left:-1px; width:73%; height:105px;' class='disabled1'>$t[keterangan]</textarea></td></tr>
        </table></div>
		
		
		</div>
		
          <table cellpadding=8 width=100%>
		  		<tr bgcolor=#2e2e2e>
				
				<th class='th' width=30px>No</th>
				<th class='th'>Nama Produk</th>
				<th class='th' width=80px>Jumlah Beli</th>
				<th class='th' width=100px>Harga</th>
				<th class='th' width=235px>Total Harga</th>
				<th class='th' width=40px></th>
			</tr>";  
  
  $no=1;
    $sql=mysql_query("SELECT * FROM rb_orders_detail 
							left join rb_produk on rb_orders_detail.id_produk=rb_produk.id_produk 
									where rb_orders_detail.id_orders='$_GET[id]'");
  while($r=mysql_fetch_array($sql)){
	$subtotall   = $r[harga_grosir] * $r[jumlah];
    $subtotalll   = $r[diskon]/100 * $r[harga_grosir] * $r[jumlah];
	$subtotal 	 =  $subtotall -  $subtotalll;
    $total       = $total + $subtotal;  
    $subtotal_rp = format_rupiah($subtotal);
    $total_rp    = format_rupiah($total);
    $harga       = format_rupiah($r[harga_grosir]);
    if(($no % 2)==0){
		$warna="#E1E1E1";
	}else{
		$warna="#ffffff";
	}
    echo "<tr bgcolor=$warna><td class='tdd'>$no</td><input type=hidden name=id[$no] value=$r[id_orders_temp]>";
			$cekd = mysql_num_rows(mysql_query("SELECT * FROM rb_produk_detail where id_produk='$r[id_produk]'"));
			if ($cekd >= 1){
				$d = mysql_fetch_array(mysql_query("SELECT * FROM rb_produk_detail a 
													join rb_jenis b on a.id_jenis=b.id_jenis
	    												join rb_type c on a.id_type=c.id_type where a.id_produk='$r[id_produk]'"));
            	echo "<td class='linkSysteM'><a href='#' alt='Type : $d[type] - Jenis : $d[jenis]'>$r[nama_produk]</a></td>";
			}else{
				echo "<td class='tdd'>$r[nama_produk]</td>";
			}
			echo "<input type=hidden name='stok[$no]' value='$r[stok]' size=4>
              <td class='tdd'><center>$r[jumlah]</center></td>
			  <td class='tdd'>Rp $harga</td>
              <td class='tdd'>Rp $subtotal_rp</td>
              <td class='tdd'><a class='linkkk' style='color:red' href='media.php?module=detailgrosir&aksi=hapus&id=$r[id_orders_detail]&psn=$_GET[id]&idp=$r[id_produk]&jml=$r[jumlah]'  onclick=\"return confirm('Anda yakin ingin batalkan pesanan ini?')\">X</a></td>
          </tr>";
    $no++; 
  } 

  if ($_GET[aksi]=='hapus'){
  		mysql_query("UPDATE rb_produk set stok = stok+$_GET[jml] where id_produk='$_GET[idp]'");
  		mysql_query("DELETE FROM rb_orders_detail where id_orders_detail='$_GET[id]'");
  		   echo "<script>window.alert('Sukses Batalkan Pesanan');
        window.location=('detail-grosir-$_GET[psn].html')</script>";
  }	

  $tot = mysql_fetch_array(mysql_query("SELECT sum((a.jumlah*b.harga_grosir) - ((a.diskon/100)* (a.jumlah*b.harga_grosir))) as total FROM  rb_orders_detail a left join rb_produk b on a.id_produk=b.id_produk where a.id_orders='$_GET[id]'"));
  $bhu = mysql_fetch_array(mysql_query("SELECT sum(bayar_sisa) as byrhutang FROM rb_sisa where id_orders='$_GET[id]'"));
  $sisa = ($t[jumlah_bayar] - $tot[total])+$bhu[byrhutang]+$t[diskon];


  if ($sisa >= 0 AND $t[status_pesanan] != 'Diambil'){
  		mysql_query("UPDATE rb_orders_grosir SET status_pesanan='Lunas' where id_orders='$_GET[id]'");
  }else{
  		mysql_query("UPDATE rb_orders_grosir SET status_pesanan='Hutang' where id_orders='$_GET[id]'");
  }

$cekbelanja = mysql_num_rows(mysql_query("SELECT * FROM rb_orders_detail where id_orders='$_GET[id]'"));
if ($cekbelanja <= 0){
  if($tot[total]=='0' OR $tot[total]==''){
  	mysql_query("DELETE FROM rb_orders_grosir where id_orders='$_GET[id]'");
  	mysql_query("DELETE FROM rb_orders_detail where id_orders='$_GET[id]'");
  	echo "<script>document.location='laporan-penjualan-grosir.html';</script>";
  }
}

  echo "<tr>
  			<td colspan=4 style='text-align:right'>Total Rp</td>
  			<td colspan=5 align=right><b style='color:Red'>".format_rupiah($tot[total])."</b></td>
  		</tr>
  		<tr>
  			<td colspan=4 style='text-align:right'>Sub Total Rp</td>
  			<td colspan=5 align=right><b style='color:Red'>".format_rupiah($tot[total]-$t[diskon])."</b></td>
  		</tr>
  		<tr>
  			<td colspan=4 style='text-align:right'>Diskon</td>
  			<td colspan=5 align=right><b style='color:Red'>".format_rupiah($t[diskon])."</b></td>
  		</tr>
  		<tr>
  			<td colspan=4 style='text-align:right'>Jumlah Bayar Rp</td>
  			<td colspan=5 align=right><b style='color:Red'>".format_rupiah($t[jumlah_bayar])."</b></td>
  		</tr>
  		
  		<tr>
  			<td colspan=4 style='text-align:right'>Sisa Rp</td>
  			<td colspan=5 align=right><b style='color:Red'>".format_rupiah($sisa)."</b></td>
  		</tr>";
	
	echo "</table><br/><br/>
		
		<a class='button-reply' href='laporan-penjualan-grosir.html'>Kembali</a>";
		if ($t[status_pesanan]!='Diambil'){	
			echo "<a class='button-reply' title='Barang Sudah Diambil' href='media.php?module=sudahambilgrosir&idg=$_GET[id]'>Sudah di Ambil</a>";
		}
		if ($sisa < 0){
  			echo "<a class='button-reply' href='media.php?module=transaksihutanggrosir&id=$_GET[id]'>Bayar Sisa</a>";
  		}
		echo "<a class='button-reply' style='float:left;' title='Print Nota Transaksi' href='print_nota_grosir_detail.php?id=$_GET[id]' target='_BLANK'>Print Nota</a>";
}

elseif ($_GET[module]=='bayarsisagrosir'){
	mysql_query("UPDATE rb_orders_grosir SET jumlah_bayar=jumlah_bayar+$_POST[total] where id_orders='$_POST[id]'");
	header('location:detail-grosir-'.$_POST[id].'.html');
}

elseif ($_GET[module]=='sudahambilgrosir'){
	$tglambil = date("d/m/Y");
    mysql_query("UPDATE rb_orders_grosir SET status_pesanan='Diambil', tanggal_selesai='$tglambil' WHERE id_orders='$_GET[idg]'");
  	header('location:detail-grosir-'.$_GET[idg].'.html');
}