<?php
// Transaksi Grosir -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='transaksigrosir'){
	$sid = $_SESSION[namauser];
	$sql = mysql_query("SELECT * FROM rb_orders_temp, rb_produk 
			                WHERE id_session='$sid' AND rb_orders_temp.id_produk=rb_produk.id_produk");
  $ketemu=mysql_num_rows($sql);
  if($ketemu < 0){
    echo "<script>window.alert('Keranjang Belanja Anda Masih Kosong.');
        window.location=('index.php')</script>";
    }
  else{
	    $temp=mysql_query("SELECT * FROM rb_grosir_temp where id_session='$sid'");
		$t=mysql_fetch_array($temp);
    echo "<form method=post action=aksii.php?module=keranjang&act=update>
			<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Transaksi Penjualan Grosir - Form Pembelian/Pemesanan<hr></b></div>

		<div id='content_transaksi'>
		<div id='kiri'><table width='90%'>"; 
		if ($_SESSION[id_pesanan_grosir]==''){ ?>
			<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="<?php echo kdauto("rb_orders_grosir","G") ; ?>"> <?php echo " &nbsp;&nbsp;Tgl : <input type=text name='tanggal_pesan' value='$tgl_sekarang' style='width:30%' readonly='on'></td></tr> ";
		}else{
			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='$_SESSION[id_pesanan_grosir]'> &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>
		
		<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%' 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=60px>Stok</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 "<input type=hidden name='stok[$no]' value='$r[stok]' size=4>
			  <td class='tdd'><span style='color:red;'>$r[stok]</span></td>
              <td class='tdd'><center><input style='margin-left:-8px' type=text name='jml[$no]' value='$r[jumlah]' maxlength='3' size=1 onkeypress=\"return harusangka(event)\"></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++; 
  } 
  $sql = mysql_query("SELECT * FROM rb_orders_temp, rb_produk 
			                WHERE id_session='$sid' AND rb_orders_temp.id_orders='$_SESSION[id_pesanan_grosir]' AND rb_orders_temp.id_produk=rb_produk.id_produk");
  $r=mysql_fetch_array($sql);
        
echo "<tr>
  		<td colspan=5 align=right>
  			<a style=' margin-bottom:3px;' class='linkkk' title='Cari dan Tambahkan Pembelian Produk' href='semua-produkk.html'>+</a>
			<input style='  margin-bottom:3px;' class='linkkk' type=submit value='Update Data'> 
  			<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;'>Sub Total </p><br/>
			<p style='float:right; margin-top:13px;'>Diskon</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;'><br/><br/><br/>TERBILANG : </span><span style=' font-size: 13px;'>"; echo ucwords(Terbilang($total));echo " Rupiah</span>
		</td>
		<td colspan=4><br><b><span style='color:red; font-size: 18px;'><p>"; ?>
			<script>
			function kalkulatorTambah(type1,type2,diskonn){
				var dis = diskonn.replace(".", "");
				var hasil = eval(type2) + (eval(dis) - 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 "$total"; ?>' 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 "$total_rp"; ?>' class=disabled readonly=on></p>
				
			<?php
				$sqlll=mysql_query("SELECT * FROM rb_bayar_temp where id_orders='$_SESSION[id_pesanan_grosir]'");
				$b=mysql_fetch_array($sqlll);
				$jmldata=mysql_num_rows($sqlll);
				if ($jmldata==0){
			?>
				<input type=hidden  name='subtotalbayar' value='0' class=disabled readonly=on id="type1" onkeyup="kalkulatorTambah(this.value,getElementById('type2').value);"/>
				<input name='subtotalbayar' style='width:88%; font-weight:bold; font-size:16px; margin-top:3px; margin-left:4px; padding:4px' type=text></p>
				<input type=hidden id="diskonn"  value='<?php echo $b[diskon]; ?>' onkeyup="kalkulatorTambah(getElementById('type1').value,getElementById('type2').value,this.value);"/></p>
				<p><input style='width:177px; font-weight:bold; font-size:16px; margin-bottom:1px;' type=text class=input name='diskona' value='<?php echo format_rupiah($b[diskon]); ?>'>
				<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,getElementById('diskonn').value);"/></p>
			<?php }else{ ?>
				<input name='subtotalbayar' style='width:88%; font-weight:bold; font-size:16px; margin-top:3px; margin-left:4px; padding:4px' type=text value='<?php echo format_rupiah($b[sub_total_bayar]); ?>'></p>
				<input type=hidden id="diskonn"  value='<?php echo $b[diskon]; ?>' onkeyup="kalkulatorTambah(getElementById('type1').value,getElementById('type2').value,this.value);"/></p>
				<p><input style='width:177px; font-weight:bold; font-size:16px; margin-bottom:1px;' type=text class=input name='diskona' value='<?php echo format_rupiah($b[diskon]); ?>'>
				<p><input style='width:89%; font-weight:bold; font-size:16px; margin-bottom:1px;' type=text  name='jml_bayar' value='<?php echo format_rupiah($b[jml_bayar]); ?>' class=input id="type2" onkeyup="kalkulatorTambah(getElementById('type1').value,this.value,getElementById('diskonn').value);"/></p>
			<?php } ?>
				<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/>
		
		<a class='button-reply' title='Simpan Transaksi ini ke Database' href='media.php?module=simpantransaksigrosir'>Transaksi Baru</a>
		<input style='width:170px; padding-bottom:8px;' class='button-reply' type=submit value='Simpan Transaksi'> 
		<a class='button-reply' title='Hapus Data Transaksi pada Halaman Ini' href='aksii.php?module=keranjang&act=reset' onclick=\"return confirm('Anda yakin ingin reset transaksi ini?')\">Reset Transaksi</a>
		<a class='button-reply' style='float:left;' title='Print Nota Transaksi Penjulan Barang' href='print_nota_grosir.php?id=$_SESSION[id_pesanan_grosir]' target='_BLANK'>Print Nota</a>
		</form>";
	}
}

// Modul simpan transaksi Grosir ----------------------------------------------------------------------------------------------------------------------------------------------------
elseif ($_GET[module]=='simpantransaksigrosir'){
// fungsi untuk mendapatkan isi keranjang belanja
function isi_keranjang(){
	$isikeranjang = array();
	$sid = $_SESSION[namauser];
	$sql = mysql_query("SELECT * FROM rb_orders_temp WHERE id_session='$sid'");
	
	while ($r=mysql_fetch_array($sql)) {
		$isikeranjang[] = $r;
	}
	return $isikeranjang;
}
  
$id_orders=$_SESSION[id_pesanan_grosir];

// Simpan Data pemesanan
  $sm = mysql_fetch_array(mysql_query("SELECT * FROM rb_grosir_temp where id_orders='$id_orders'"));
  $st = mysql_fetch_array(mysql_query("SELECT * FROM rb_bayar_temp where id_orders='$id_orders'"));

if ($st[sub_total_bayar] == '0'){
	$subtotall = $st[sub_total]-$st[diskon];
}else{
	$subtotall = $st[sub_total_bayar];
}

if (($st[jml_bayar]-$subtotall) < 0){
	$status = 'Hutang';
}else{
	$status = 'Lunas';
}

	$sisa = $st[jml_bayar]-$subtotall;

mysql_query("INSERT INTO rb_orders_grosir( id_orders,
									nama_pemesan, 
									alamat, 
									telpon, 
									tanggal_pesan, 
									tanggal_selesai,
									perusahaan,
									karyawan,
									status_pesanan,
									sisa,
									jumlah_bayar,
									diskon,
									keterangan) 
							VALUES( '$sm[id_orders]',
									'$sm[nama_pemesan]',
									'$sm[alamat]',
									'$sm[telpon]',
									'$tgl_orders',
									'$sm[tanggal_selesai]',
									'$sm[perusahaan]',
									'$_SESSION[namauser]',
									'$status',
									'$sisa',
									'$st[jml_bayar]',
									'$st[diskon]',
									'$sm[keterangan]')");

// panggil fungsi isi_keranjang dan hitung jumlah produk yang dipesan
$isikeranjang = isi_keranjang();
$jml          = count($isikeranjang);

// simpan data detail pemesanan  
for ($i = 0; $i < $jml; $i++){
  mysql_query("INSERT INTO rb_orders_detail(id_orders, id_produk, jumlah, diskon) 
               VALUES('$id_orders',{$isikeranjang[$i]['id_produk']}, {$isikeranjang[$i]['jumlah']}, '".$isikeranjang[$i]['diskon']."')");
}
  
// update/kurangi stok produk
for ($i = 0; $i < $jml; $i++) {
	$cekr = mysql_num_rows(mysql_query("SELECT * FROM rb_produk WHERE id_kategori='16' AND id_produk = {$isikeranjang[$i]['id_produk']}"));
	if ($cekr <= 0){
		mysql_query("UPDATE rb_produk SET stok = stok - {$isikeranjang[$i]['jumlah']}
							    WHERE id_produk = {$isikeranjang[$i]['id_produk']}");
	}
}

// setelah data pemesanan tersimpan, hapus data pemesanan di tabel pemesanan sementara (orders_temp)
        mysql_query("TRUNCATE rb_grosir_temp");
		mysql_query("TRUNCATE rb_orders_temp");
		mysql_query("TRUNCATE rb_bayar_temp");
		unset($_SESSION[id_pesanan_grosir]);
				echo "<script>document.location='transaksi-grosir.html';</script>";
}