<?php
// Transaksi Belanja Atau Pembelian ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
if ($_GET[module]=='transaksibelanja'){
	$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");
 
	    $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_pemesan_temp 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='$_SESSION[id_pesanan_eceran]'");
		$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=aksi.php?module=keranjang&act=update>
			<div class='post_title' style='margin-bottom:10px; position:relative;'><b>Transaksi Penjualan Eceran - Form Pembelian/Pemesanan<hr></b></div>
  		<div id='content_transaksi'><div id='kiri'><table width='90%'>"; 
  		if ($_SESSION[id_pesanan_eceran]==''){ 
			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='".kdauto('rb_orders','E')."'> &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_eceran]'> &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 value='$t[no_ref]' name='nomor_ref' style='width:50%' autocomplete='off'></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=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;
  $ketemu=mysql_num_rows($sql);
 
  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'><span style='color:red;'>$r[stok]</span></td>
              <td class='tdd'><input type=text name='jml[$no]' value='$r[jumlah]' maxlength='3' size=6 onkeypress=\"return harusangka(event)\"></td>
			  <td class='tdd'><input type=text name='hrg[$no]' value='$r[harga]' maxlength='6' size=6 onkeypress=\"return harusangka(event)\"></td>
              <td class='tdd'>Rp $subtotal_rp</td>
              <td class='tdd'><a class='linkkk' title='Hapus Produk ini' href='aksi.php?module=keranjang&act=hapus&id=$r[id_orders_temp]'>Delete</a></td>
          </tr>";
    $no++; 
  } 
  $r=mysql_fetch_array(mysql_query("SELECT * FROM rb_orders_temp, rb_produk 
			                WHERE id_session='$sid' AND rb_orders_temp.id_orders='$_SESSION[id_pesanan_eceran]' AND rb_orders_temp.id_produk=rb_produk.id_produk"));
  echo "<tr><td colspan=5 align=right>
  		<a style=' margin-bottom:3px;' class='linkkk' title='Cari dan Tambahkan Pembelian Produk' href='semua-produk.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;'>Bayar Instansi </p><br/>
		<p style='float:right; margin-top:13px;'>Jumlah Bayar </p><br/>
		<p style='float:right; margin-top:7px;'><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,inst,diskon){
						var typ1 = type1.replace(".", "");
						var res = type2.replace(".", "");
						var instansi = inst.replace(".", "");
						var disk = diskon.replace(".", "");
						var hasil = (eval(res) + eval(disk)) - (eval(typ1) - eval(instansi));
						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,getElementById('inst').value,getElementById('diskon').value);"/>
					<input style='width:175px; 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
			$b=mysql_fetch_array(mysql_query("SELECT * FROM rb_bayar_temp where id_orders='$_SESSION[id_pesanan_eceran]'"));
			$jmld=mysql_fetch_array(mysql_query("SELECT count(*) as total FROM rb_bayar_temp where id_orders='$_SESSION[id_pesanan_eceran]'"));
			$hitinst = mysql_num_rows(mysql_query("SELECT * FROM rb_instansi where id_instansi='$t[id_instansi]'"));
				if ($hitinst == 0){ $instan = 0; }else{
					$inst = mysql_fetch_array(mysql_query("SELECT * FROM rb_instansi where id_instansi='$t[id_instansi]'"));
					$instan = $inst[harga];
				}

			if ($jmld[total] == '0'){ ?>
				<p><input type=text class='input-tot' name='subtotalbayar' value='0'>
					<input type=hidden name='bayarinstansi' value='<?php echo $instan; ?>' id="inst" onkeyup="kalkulatorTambah(getElementById('type1').value,getElementById('type2').value,this.value,getElementById('diskon').value);"/>
				<p><input type=text class='input-tot' value='0' name='diskona'>
					<input type=hidden id="diskon" onkeyup="kalkulatorTambah(getElementById('type1').value,getElementById('type2').value,getElementById('inst').value,this.value);"/></p>
				<p><input type=text class='input-tot-disable' id="type2" value='<?php echo format_rupiah($instan); ?>' disabled/></p>
				<p><input autocomplete='off' type=text  name='jml_bayar' value='0' class='input-tot' id="type2" onkeyup="kalkulatorTambah(getElementById('type1').value,this.value,getElementById('inst').value,getElementById('diskon').value);"/></p>
			<?php }else{ ?>
				<p><input type=text class='input-tot' name='subtotalbayar' value='<?php echo format_rupiah($b[sub_total_bayar]); ?>'>
					<input type=hidden name='bayarinstansi' value='<?php echo $instan; ?>' id="inst" onkeyup="kalkulatorTambah(,getElementById('type1').value,getElementById('type2').value,this.value,getElementById('diskon').value);"/>
				<p><input type=text class='input-tot' name='diskona' value='<?php echo format_rupiah($b[diskon]); ?>'>
					<input type=hidden id="diskon" value='<?php echo $b[diskon]; ?>' onkeyup="kalkulatorTambah(getElementById('type1').value,getElementById('type2').value,getElementById('inst').value,this.value);"/></p>
				<p><input type=text class='input-tot-disable' value='<?php echo format_rupiah($instan); ?>' id="type2" disabled/></p>
				<p><input type=text autocomplete='off' class='input-tot'  name='jml_bayar' value='<?php echo format_rupiah($b[jml_bayar]); ?>' id="type2" onkeyup="kalkulatorTambah(getElementById('type1').value,this.value,getElementById('inst').value,getElementById('diskon').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='simpan-transaksi.html'>Transaksi Baru (F5)</a>
		<input style='width:185px; padding-bottom:8px;' class='button-reply' type=submit value='Simpan Transaksi (F4)'> 
		<a style='width:185px;' class='button-reply' title='Hapus Data Transaksi pada Halaman Ini' href='aksi.php?module=keranjang&act=reset' onclick=\"return confirm('Anda yakin ingin reset transaksi ini?')\">Reset Transaksi (F3)</a>
		<a class='button-reply' style='float:left;' title='Print untuk Resep Dokter' href='print_resep.php?id=$_SESSION[id_pesanan_eceran]' target='_BLANK'>Print Resep (F1)</a>
		<a class='button-reply' style='float:left;' title='Print Nota Transaksi Penjulan Barang' href='print_nota.php?id=$_SESSION[id_pesanan_eceran]' target='_BLANK'>Print Nota (F2)</a>
		</form>";
}

// Modul simpan transaksi ----------------------------------------------------------------------------------------------------------------------------------------------------
elseif ($_GET[module]=='simpantransaksi'){
// 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_eceran];
  $sm = mysql_fetch_array(mysql_query("SELECT * FROM rb_pemesan_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] + $st[bayar_inst]) - $subtotall) < 0){
	$status = 'Hutang';
}else{
	$status = 'Lunas';
}

	$sisa = (($st[jml_bayar] + $st[bayar_inst]) - $subtotall);

$totalbaya = $st[jml_bayar];
mysql_query("INSERT INTO rb_orders( id_orders, 
									no_ref, 
									nama_pemesan, 
									alamat, 
									telpon, 
									tanggal_pesan, 
									tanggal_selesai, 
									id_instansi, 
									id_dokter, 
									diameter, 
									warna, 
									id_user, 
									tanggal_ambil, 
									karyawan,
									status_pesanan,
									bayar_instansi,
									sisa,
									jumlah_bayar,
									diskon) 
							VALUES( '$sm[id_orders]',
									'$sm[no_ref]',
									'$sm[nama_pemesan]',
									'$sm[alamat]',
									'$sm[telpon]',
									'$tgl_orders',
									'$sm[tanggal_selesai]',
									'$sm[id_instansi]',
									'$sm[id_dokter]',
									'$sm[diameter]',
									'$sm[warna]',
									'$sm[id_user]',
									'$sm[tanggal_ambil]',
									'$_SESSION[namauser]',
									'$status',
									'$st[bayar_inst]',
									'$sisa',
									'$totalbaya',
									'$st[diskon]')");

// Simpan Data Resep									
  $sql = mysql_query("SELECT * FROM rb_resep_temp where id_orders='$id_orders'");
  $rs=mysql_fetch_array($sql);

mysql_query("INSERT INTO rb_resep( id_orders, 
									R_SPH, 
									L_SPH, 
									R_CYL, 
									L_CYL, 
									R_AXS, 
									L_AXS, 
									R_ADD, 
									L_ADD, 
									PD,
									PD2) 
							VALUES( '$rs[id_orders]',
									'$rs[R_SPH]',
									'$rs[L_SPH]',
									'$rs[R_CYL]',
									'$rs[L_CYL]',
									'$rs[R_AXS]',
									'$rs[L_AXS]',
									'$rs[R_ADD]',
									'$rs[L_ADD]',
									'$rs[PD]',
									'$rs[PD2]')");
  
  
// panggil fungsi isi_keranjang dan hitung jumlah produk yang dipesan
$isikeranjang = isi_keranjang();
$jml          = count($isikeranjang);
$hrg          = count($isikeranjang);

// simpan data detail pemesanan  
for ($i = 0; $i < $hrg; $i++){
  mysql_query("INSERT INTO rb_orders_detail(id_orders, id_produk, jumlah, diskon, harga) 
               VALUES('$id_orders',{$isikeranjang[$i]['id_produk']}, {$isikeranjang[$i]['jumlah']}, '".$isikeranjang[$i]['diskon']."','".$isikeranjang[$i]['harga']."')");
}

  
// update/kurangi stok produk
for ($i = 0; $i < $hrg; $i++) {
	$cekr = mysql_num_rows(mysql_query("SELECT * FROM rb_produk a JOIN rb_kategori_produk b ON a.id_kategori=b.id_kategori where b.nama_kategori LIKE '%pesanan%' AND a.id_produk = {$isikeranjang[$i]['id_produk']}"));
	if ($cekr <= 0){
		mysql_query("UPDATE rb_produk SET stok = stok - {$isikeranjang[$i]['harga']}
							    WHERE id_produk = {$isikeranjang[$i]['id_produk']}");
	}
}


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