<?php
// Transaksi Belanja Atau Pembelian ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='transaksihutanggrosir'){
		if (isset($_POST[id_orders])){
				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-grosir-".$_POST[id_orders].".html';</script>";
		}
	$sid = $_GET[id];
	$sql = mysql_query("SELECT * FROM rb_orders_detail, rb_produk 
			                WHERE id_orders='$sid' AND rb_orders_detail.id_produk=rb_produk.id_produk");
  		$ketemu=mysql_num_rows($sql);
	    $temp=mysql_query("SELECT * FROM rb_orders_grosir where id_orders='$sid'");
		$t=mysql_fetch_array($temp);
    echo "<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Transaksi Pembayarana Sisa Pesanan Grosir<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='$sid'> &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>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 Diambil</b></td>	<td class='td'></td><td class='td' colspan=1><input type=text value='$t[tanggal_selesai]' id='tgl2' name='tanggal_selesai' style='width:35%' autocomplete='off'><br><br></td></tr>
		</table>
		</div>
		<form method='POST' action=''>
		<div id='kanan'><table width='100%' >
		<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'></td></tr>
		<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%' autocomplete='off'></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;'>$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=100px>Total Harga</th>
				<th class='th' align=center width=85px>Action</th>
			</tr>";  
  
  $no=1;
  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 "
              <td class='tdd'><center>$r[jumlah]</center></td>
			  <input type=hidden name='diskon[$no]' value='$r[diskon]' maxlength='3' size=3>
			  <td class='tdd'>Rp $harga</td>
              <td class='tdd'>Rp $subtotal_rp</td>
              <td class='tdd'><a class='linkkk' title='Hapus Produk ini' href='aksii.php?module=keranjang&act=hapus&id=$r[id_orders_temp]'>Delete</a></td>
          </tr>";
    $no++; 
}
  $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_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]'"));
  $sisa = (($t[jumlah_bayar] - $tot[total]) + $bhu[byrhutang])+$t[diskon];

echo "<tr>
  		<td colspan=4 align=right>
  			<br><span style='font-weight:bold; float:right; font-size: 15px;'>
  			<p style='float:right; margin-top:-2px;'>Total </p><br/>
			<p style='float:right; margin-top:13px;'>Jumlah Bayar </p><br/>
			<p style='float:right; margin-top:0px;'><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 hasil = eval(type2) + eval(type1);
				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:88%; font-weight:bold; font-size:16px; margin-top:3px; margin-left:4px; padding:4px' type=text value='<?php echo "$sisa"; ?>' class=disabled readonly=on></p>
				<p><input autocomplete='off' style='width:89%; font-weight:bold; font-size:16px; margin-bottom:1px;' type=text  name='jml_bayar' class=input 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:170px; name='simpanhutang' padding-bottom:8px;' class='button-reply' type='submit' value='Simpan Transaksi'> 
		</form>";

}