<?php
// Laporan Penjualan ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='laporanpenjualan'){
	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 (Eceran) Barang Pada Optik</b><hr>
	</div>
				<form style='float:right; width:390px' method=POST action='laporan-penjualan.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='laporanpenjualan'>
					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>Alamat</th>
			<th>Telepon</th>
			<th>Tanggal Pesan</th>
			<th>Tanggal Selesai</th>
			<th>Karyawan</th>
			<th>Status </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;'>No Ref</th>
			<th style='border: none;'>Nama Pemesan</th>
			<th style='border: none;'>Alamat</th>
			<th style='border: none;'>Telepon</th>
			<th style='border: none;'>Tanggal Pesan</th>
			<th style='border: none;'>Tanggal Selesai</th>
			<th style='border: none;'>Karyawan</th>
			<th style='border: none;'>Status </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 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) - 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[no_ref]</a></td>
			<td>$r[nama_pemesan]</td>
            <td>$r[alamat]</td>
            <td>$r[telpon]</td>
			<td>$tanggal_pesan</td>
			<td>".tgl_indo($r[tanggal_selesai])."</td>
			<td><span style='text-transform:capitalize;'>$r[nama_lengkap]</span></td>
			<td style='color:$color'>$r[status_pesanan]</td>
			<td style='color:red'>".tgl_indo($r[tanggal_ambil])."</td>
			<td width=100px><center><a class='linkkk' href='detail-laporan-$r[id_orders].html'>Lihat Detail</a> ";
			echo "</center></td>
		</tr>";
		}
echo "	</tbody>
 </table>
</div></div>";
}


// Detail Laporan ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
elseif ($_GET[module]=='detaillaporan'){
	    $detail=mysql_query("SELECT a.*, b.id_dokter,b.nama_dokter, c.id_instansi,c.nama_instansi, d.id_user, d.nama_lengkap as nama_user 
		    					FROM rb_orders a 
		    						LEFT JOIN rb_dokter b ON a.id_dokter=b.id_dokter 
		    							LEFT JOIN rb_instansi c ON a.id_instansi=c.id_instansi 
		    								LEFT JOIN rb_users d ON a.id_user=d.id_user 
		    									where a.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 Pemesanan oleh Bpk/Ibk : $t[nama_pemesan]<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='$t[id_orders]' readonly=on> &nbsp;&nbsp;Tgl : <input class='disabled' type=text name='tanggal_pesan' value='$tanggal_pesan' style='width:30%' readonly=on></td></tr> 
        <tr><td class='td'><b>Nomor Ref</b></td><td class='td'></td><td class='td' colspan=1><input class='disabled' type=text value='$t[no_ref]' name='nomor_ref' style='width:50%' readonly=on></td></tr>
        <tr><td class='td'><b>Nama Pemesan</b></td><td class='td'></td><td class='td' colspan=1><input class='disabled' type=text value='$t[nama_pemesan]' name='nama_pemesan' style='width:93%' readonly=on></td></tr>
        <tr><td class='td'><b>Alamat</b></td><td class='td'></td><td class='td' colspan=1><textarea class='disabled' name='alamat' style='margin-left:-1px; width:93%; height:45px;'readonly=on>$t[alamat]</textarea></td></tr>
		<tr><td class='td'><b>Phone/HP</b></td><td class='td'></td><td class='td' colspan=1><input class='disabled' type=text value='$t[telpon]' name='phone' style='width:93%' readonly=on></td></tr>
		<tr><td class='td'><b>Tanggal Selesai</b></td><td class='td'></td><td class='td' colspan=1><input class='disabled' type=text value='".tgl_indo($t[tanggal_selesai])."'  name='tanggal_selesai' style='width:35%' readonly=on></td></tr>
		</table>
		</div>
		
		<div id='kanan'><table width='100%' >
		<tr><td class='td' width='100px'><b>Nama Instansi</b></td><td class='td'></td><td class='td' colspan=1><input type=text value='$t[id_instansi] - $t[nama_instansi]' name='instansi' class='disabled' style='width:73%' readonly=on></td></tr>
        <tr><td class='td'><b>Dokter/RO</b></td><td class='td'></td><td class='td' colspan=1><input type=text value='$t[id_dokter] - $t[nama_dokter]' name='dokter' class='disabled' style='width:73%' readonly=on></td></tr>
        <tr><td class='td'><b>Diameter</b></td><td class='td'></td><td class='td'><input type=text value='$t[diameter]' name='diameter' class='disabled' style='width:20%;margin-right:10px;' readonly=on> 
												<b> Warna</b> : <input type=text value='$t[warna]' class='disabled' name='warna' style='width:20%;margin-right:10px;' readonly=on> </td></tr>
		<tr><td class='td'><b>Karyawan</b></td><td class='td'></td><td class='td' colspan=1><input class='disabled' value='$t[id_user] - $t[nama_user]' type=text name='sales' class='disabled' style='width:73%' readonly=on></td></tr>
		<tr><td class='td'><b>Tgl Ambil</b></td><td class='td'></td><td class='td' colspan=1><input type=text class='disabled' value='".tgl_indo($t[tanggal_ambil])."' name='sponsor' style='width:53%' readonly=on></td></tr>";
			
			$sqle=mysql_query("SELECT * FROM rb_resep where id_orders='$_GET[id]'");
			$p=mysql_fetch_array($sqle);
		
	echo "<tr><td class='td'></td><td class='td'></td><td class='td'>
				<input type=text value='R' style='width:16%; background:#000; color:#fff; font-weight:bold; text-align:center; margin-right:4px;' disabled>
				<select style='width:66px' name='sph_r'>
					<option value='$p[R_SPH]'>$p[R_SPH]</option>
				</select>
				<select style='width:66px' name='cyl_r'>
					<option value='$p[R_CYL]'>$p[R_CYL]</option>
				</select>
				<select style='width:66px' name='axs_r'>
					<option value='$p[R_AXS]'>$p[R_AXS]</option>
				</select>
				<select style='width:66px' name='add_r'>
					<option value='$p[R_ADD]'>$p[R_ADD]</option>
				</select>
				<input style='width:30px' type=text name='pdr' value='$p[PD]' placeholder='PD' style='width:5%'>
				</td></tr>
				
		<tr><td class='td'></td><td class='td'></td><td class='td'>
				<input type=text value='L' style='width:16%; background:#000; color: #fff; font-weight:bold; text-align:center; margin-right:4px;' disabled>
				<select style='width:66px' name='sph_l'>
					<option value='$p[L_SPH]'>$p[L_SPH]</option>
				</select>
				<select style='width:66px' name='cyl_l'>
					<option value='$p[L_CYL]'>$p[L_CYL]</option>
				</select>
				<select style='width:66px' name='axs_l'>
					<option value='$p[L_AXS]'>$p[L_AXS]</option>
				</select>
				<select style='width:66px' name='add_l'>
					<option value='$p[L_ADD]'>$p[L_ADD]</option>
				</select>
				<input style='width:30px' type=text name='pdl' value='$p[PD2]' placeholder='PD' style='width:5%'>
				</td></tr>";
				
		echo "</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=60px>Jumlah</th>
				<th class='th' width=130px>Harga</th>
				<th class='th' width=145px>Total Harga</th>
				<th class='th' width=40px></th>
			</tr>";  
 
 
  $no=1;
  $sqle=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($sqle)){
	$subtotall   = $r[harga] * $r[jumlah];
    $subtotalll   = $r[diskon];
	$subtotal 	 =  $subtotall -  $subtotalll;
    $total       = $total + $subtotal;
    $subtotal_rp = format_rupiah($subtotal);
    $total_rp    = format_rupiah($total);
    $harga       = format_rupiah($r[harga]);
    if(($no % 2)==0){
		$warna="#E1E1E1";
	}else{
		$warna="#ffffff";
	}
    echo "<tr bgcolor=$warna><td class='tdd'>$no</td>";
			$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 "<td class='tdd'>$r[jumlah]</td>
			  <input type=hidden value='$r[diskon]' size='10' maxlength='10'>
			  <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=detaillaporan&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-laporan-$_GET[psn].html')</script>";
  }	

  $bhu = mysql_fetch_array(mysql_query("SELECT sum(bayar_sisa) as byrhutang FROM rb_sisa where id_orders='$_GET[id]'"));
  $tot = mysql_fetch_array(mysql_query("SELECT sum((a.jumlah*b.harga) - 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='$_GET[id]'"));
  $sisa = (($t[bayar_instansi] + $t[jumlah_bayar] + $t[diskon]) - $tot[total])+$bhu[byrhutang];
  
  if ($sisa >= '0' AND $t[status_pesanan] != 'Diambil'){
  		mysql_query("UPDATE rb_orders SET status_pesanan='Lunas' where id_orders='$_GET[id]'");
  }else{
  		mysql_query("UPDATE rb_orders 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 where id_orders='$_GET[id]'");
  	mysql_query("DELETE FROM rb_resep where id_orders='$_GET[id]'");
  	mysql_query("DELETE FROM rb_orders_detail where id_orders='$_GET[id]'");
  	echo "<script>document.location='laporan-penjualan.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 Rp</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'>Bayar Instansi Rp</td>
  			<td colspan=5 align=right><b style='color:Red'>".format_rupiah($t[bayar_instansi])."</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]+$bhu[byrhutang])."</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' title='Kembali ke List Laporan Pemesanan' href='laporan-penjualan.html'>Kembali</a>";
		if ($t[status_pesanan]!='Diambil'){	
			echo "<a class='button-reply' title='Ganti Status Menjadi Sudah Di Ambil' href='sudah-diambil-$t[id_orders].html'>Sudah Di Ambil</a>";
		}
		if ($sisa < 0){
  			echo "<a class='button-reply' href='media.php?module=transaksihutang&id=$_GET[id]'>Bayar Sisa</a>";
  		}
		echo "<a class='button-reply' style='float:left' title='Print untuk Resep Dokter' href='print_resepp.php?id=$t[id_orders]' target='_BLANK'>Print Resep</a>
		<a class='button-reply' style='float:left' title='Print Nota Transaksi Penjulan Barang' href='print_notaa.php?id=$t[id_orders]' target='_BLANK'>Print Nota</a>";
}

elseif ($_GET[module]=='sudahdiambil'){
	
    mysql_query("UPDATE rb_orders SET status_pesanan='Diambil', tanggal_ambil='$tgl_barat' WHERE id_orders='$_GET[diambil]'");
  	header('location:detail-laporan-'.$_GET[diambil].'.html');
}

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