<?php
// Transaksi Belanja Atau Pembelian ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='transaksihutang'){
	$sid = $_SESSION[namauser];
	$sql = mysql_query("SELECT * FROM rb_orders_detail, rb_produk 
			                WHERE id_orders='$_GET[id]' AND rb_orders_detail.id_produk=rb_produk.id_produk");
	    $temp=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($temp);
		if ($t[tanggal_ambil]==$tgl_sekarang OR $t[tanggal_ambil] == ''){
			$tglambil = $tgl_sekarang;
		}else{
			$tglambil = $t[tanggal_ambil];
		}
    echo "<form name='submitecer' method=post action=''>
			<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Transaksi Pembayaran Sisa<hr></b></div>
  		<div id='content_transaksi'><div id='kiri'><table width='90%'>"; 
        echo "<tr><td class='td'><b>Nomor Pesanan</b></td><td class='td' width='20px'></td><td class='td'>  <input type=text name='id_orders' value='$_GET[id]'> &nbsp;&nbsp;Tgl : <input type=text name='tanggal_pesan' value='$tgl_sekarang' style='width:30%' readonly='on'></td></tr> ";
        echo "<tr><td class='td'><b>Nomor Ref</b></td>	<td class='td'></td><td class='td' colspan=1><input type=text name='nomor_ref' style='width:50%' autocomplete='off' required></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%' autocomplete='off'></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;'>$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%' autocomplete='off'></td></tr>
		<tr><td class='td'><b>Tanggal Selesai</b></td>	<td class='td'></td><td class='td' colspan=1><input type=text value='".tgl_indo($t[tanggal_selesai])."' id='tgl1' name='tanggal_selesai' style='width:35%' autocomplete='off'></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 id='instansi' type=text value='$t[id_instansi] - $t[nama_instansi]' name='instansi' class='disabled' placeholder=' Ketik Nama Instansi' style='width:73%'></td></tr>
        <tr><td class='td'><b>Dokter/RO</b></td><td class='td'></td>	<td class='td' colspan=1><input id='dokter' type=text value='$t[id_dokter] - $t[nama_dokter]' name='dokter' class='disabled' placeholder=' Ketik Nama Dokter' style='width:73%'></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' style='width:20%;margin-right:10px;' autocomplete='off'> 
																		<b> Warna</b> : <input type=text value='$t[warna]' name='warna' style='width:20%;margin-right:10px;' autocomplete='off'> </td></tr>
		<tr><td class='td'><b>Karyawan</b></td><td class='td'></td>		<td class='td' colspan=1><input id='sales' value='$t[id_user] - $t[nama_user]' type=text name='sales' class='disabled' placeholder=' Ketik Nama Karyawan' style='width:73%'></td></tr>
		<tr><td class='td'><b>Tgl Ambil</b></td><td class='td'></td>	<td class='td' colspan=1><input type=text id='tgl3' name='sponsor' style='width:53%' value='".tgl_indo($t[tanggal_ambil])."' autocomplete='off'></td></tr>";
			
		include "transaksi/resep.php";

		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=80px>Jumlah Beli</th>
				<th class='th' width=100px>Harga</th>
				<th class='th' width=100px>Total Harga</th>
			</tr>";  
  
  $no=1;
  while($r=mysql_fetch_array($sql)){
	$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><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>
            								 <a href='#' alt='SPH ($d[R_SPH]), CYL ($d[R_CYL]), AXS ($d[R_AXS]), ADD ($d[R_ADD]), PD ($d[PD])' style='background:#000; padding:2px 10px 2px 10px; color:#fff; text-decoration:none'>R</a>
            								 <a href='#' alt='SPH ($d[L_SPH]), CYL ($d[L_CYL]), AXS ($d[L_AXS]), ADD ($d[L_ADD]), PD ($d[PD2])' style='background:#000; padding:2px 10px 2px 10px; color:#fff; text-decoration:none'>L</a></td>";
			}else{
				echo "<td class='tdd'>$r[nama_produk]</td>";
			}
			echo "<input type=hidden name='stok[$no]' value='$r[stok]' size=4>
				  <input type=hidden name='kategori[$no]' value='$r[id_kategori]' size=4>
              <td class='tdd'>$r[jumlah]</td>
			  <input type=hidden name='diskon[$no]' value='$r[diskon]' maxlength='10' size=10>
			  <td class='tdd'>Rp $harga</td>
              <td class='tdd'>Rp $subtotal_rp</td>
             
          </tr>";
    $no++; 
  } 
  $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]'"));
  $bhu = mysql_fetch_array(mysql_query("SELECT sum(bayar_sisa) as byrhutang FROM rb_sisa where id_orders='$_GET[id]'"));
  $sisa = (($t[bayar_instansi] + $t[jumlah_bayar] + $t[diskon]) - $tot[total]) + $bhu[byrhutang];
  echo "<tr><td colspan=3 align=right>
  <br><br><span style='font-weight:bold; float:right; font-size: 15px;'><p style='float:right; margin-top:-10px;'>Total Sisa</p><br/>
		<p style='float:right; margin-top:8px;'>Jumlah Bayar </p><br/>
		<p style='float:right; margin-top:1px;'><br/>Sisa </p></span>
		<span style='font-weight:bold; font-size: 13px; color:red;'>TERBILANG : </span><span style=' font-size: 13px;'>"; echo ucwords(Terbilang(str_replace('-', '', $sisa)));echo " Rupiah</span></td>
		<td colspan=4><br><b><span style='color:red; font-size: 18px;'><p>"; ?>
				<script>
					function kalkulatorTambah(type1,type2){
						var typ1 = type1.replace(".", "");
						var res = type2.replace(".", "");
						var hasil = eval(typ1) + eval(res);
						document.getElementById('result').innerHTML = hasil;
					if (isNaN(hasil)) return 0; else return hasil; }
				</script>
					<input type=hidden  name='sub_total' value='<?php echo "$sisa"; ?>' class=disabled readonly=on id="type1" onkeyup="kalkulatorTambah(this.value,getElementById('type2').value);"/>
				<input style='width:175px; font-weight:bold; font-size:16px; margin-top:3px; margin-left:4px; padding:4px' type=text value='<?php echo format_rupiah($sisa); ?>' class=disabled readonly=on></p>
				<p><input type=text autocomplete='off' class='input-tot'  name='jml_bayar' id="type2" onkeyup="kalkulatorTambah(getElementById('type1').value,this.value);"/></p>
				<p><div style='color:blue; background:#e3e3e3; height:23px; width:178px; margin-left:5px; border:1px solid #cecece; margin-top:5px; padding-top:4px; padding-left:4px'><span id="result"></span></div></p>
		<?php echo "<br/></b></span></td></tr>			
        </table><br/>
        <input type='hidden' name='id_orders' value='$_GET[id]'>
		<input style='width:185px; padding-bottom:8px;' name='simpanhutang' class='button-reply' type=submit value='Simpan Transaksi'> 
		</form>";

// Modul simpan transaksi ----------------------------------------------------------------------------------------------------------------------------------------------------
if (isset($_POST[simpanhutang])){
	mysql_query("INSERT INTO rb_sisa VALUES('','$_POST[id_orders]','$_POST[nomor_ref]','$_POST[jml_bayar]','$_SESSION[idu]','".date('Y-m-d')."')");
	echo "<script>document.location='detail-laporan-".$_POST[id_orders].".html';</script>";
}

}elseif ($_GET[module]=='laporansisa'){
	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 Pembayaran Sisa Pesanan Pada Optik</b><hr>
	</div>
				<form style='float:right; width:390px' method=POST action=''>
					  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>
				<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 Bayar</th>
			<th>Jumlah Bayar</th>
			<th>Karyawan</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 Bayar</th>
			<th style='border: none;'>Jumlah Bayar</th>
			<th style='border: none;'>Karyawan</th>
			<th style='border: none;'></th>
		</tr>
	</tfoot>";

if ($status == 'grosir' OR $status == 'admin'){
	if (isset($_POST[tgl])){
		$tampil = mysql_query("SELECT a.*,  b.nama_lengkap FROM rb_sisa a 
								JOIN rb_users b ON a.id_user=b.id_user where a.waktu='$tgl_pesann' ORDER BY a.id_sisa DESC");
	}else{
		$tampil = mysql_query("SELECT a.*,  b.nama_lengkap FROM rb_sisa a 
								JOIN rb_users b ON a.id_user=b.id_user ORDER BY a.id_sisa DESC");
	}
}else{
	if (isset($_POST[tgl])){
		$tampil = mysql_query("SELECT a.*,  b.nama_lengkap FROM rb_sisa a 
								JOIN rb_users b ON a.id_user=b.id_user
								JOIN rb_orders c ON a.id_orders=c.id_orders where a.waktu='$tgl_pesann' ORDER BY a.id_sisa DESC");
	}else{
		$tampil = mysql_query("SELECT a.*,  b.nama_lengkap FROM rb_sisa a JOIN rb_users b ON a.id_user=b.id_user 
									JOIN rb_orders c ON a.id_orders=c.id_orders ORDER BY a.id_sisa DESC");
	}
}

	    
		echo "<tbody>";
    while($r=mysql_fetch_array($tampil)){
    	$hitung = mysql_num_rows(mysql_query("SELECT * FROM rb_orders where id_orders='$r[id_orders]'"));
    	if ($hitung <= '0'){
    		$u = mysql_fetch_array(mysql_query("SELECT * FROM rb_orders_grosir where id_orders='$r[id_orders]'"));
    	}else{
    		$u = mysql_fetch_array(mysql_query("SELECT * FROM rb_orders where id_orders='$r[id_orders]'"));
    	}

	$tanggal_pesan=tgl_indo($u[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';
		}

	echo "<tr bgcolor='$warna'>
			<td class='read_only' style='color:red'>$r[id_orders]</td>
            <td>$r[no_ref]</td>
			<td>$u[nama_pemesan]</td>
            <td>$u[alamat]</td>
            <td>$u[telpon]</td>
			<td>$tanggal_pesan</td>
			<td>".tgl_indo($r[waktu])."</td>
			<td>Rp ".format_rupiah($r[bayar_sisa])."</td>
			<td><span style='text-transform:capitalize;'>$r[nama_lengkap]</span></td>";
			if ($hitung <= '0'){
				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='media.php?module=laporansisa&delete=$r[id_sisa]' onclick=\"return confirm('Anda yakin ingin hapus data ini?')\">X</a>";
				}
				echo "</center></td>";
			}else{
				echo "<td width=120px><center><a class='linkkk' href='detail-laporan-$r[id_orders].html'>Lihat Detail</a> ";
				if ($status == 'admin'){ 
					echo "<a class='linkkk' style='color:red' href='media.php?module=laporansisa&delete=$r[id_sisa]' onclick=\"return confirm('Anda yakin ingin hapus data ini?')\">X</a>";
				}
				echo "</center></td>";
			}
		echo "</tr>";
		}

		if (isset($_GET[delete])){
			mysql_query("DELETE FROM rb_sisa where id_sisa='$_GET[delete]'");
			echo "<script>document.location='media.php?module=laporansisa';</script>";
		}
echo "	</tbody>
 </table>
</div></div>";
}