<?php
error_reporting(0);
session_start();
include "../config/koneksi.php";
include "../config/library.php";
include "../config/fungsi_indotgl.php";
include "../config/session_admin.php";

$module=$_GET[module];
$act=$_GET[act];
	$sid = $_SESSION[namauser];

if ($module=='keranjang' AND $act=='tambah'){
	$sql2 = mysql_query("SELECT stok, id_kategori FROM rb_produk 
			WHERE id_produk='$_GET[id]'");

	$r=mysql_fetch_array($sql2);
	$cekp = mysql_num_rows(mysql_query("SELECT * FROM rb_kategori_produk where nama_kategori LIKE '%pesanan%' AND id_kategori='$r[id_kategori]'"));
  if ($r[stok] <= 0 AND $cekp <= 0){
	echo "<script>window.alert('Maaf, Produk ini sudah Habis.');
        window.location=('javascript:history.go(-1)')</script>";
  }
  else{
	// check if the product is already
	// in cart table for this session
	$sql = mysql_query("SELECT id_produk FROM rb_orders_temp
			WHERE id_produk='$_GET[id]' AND id_session='$sid'");
	$ketemu=mysql_num_rows($sql);
		 
	if ($ketemu==0){
		// put the product in cart table
		mysql_query("INSERT INTO rb_orders_temp (id_produk, jumlah, id_session, tgl_order_temp, jam_order_temp)
				VALUES ('$_GET[id]', '1', '$sid', '$tgl_orders', '$jam_sekarang')");
	} else {
		// update product quantity in cart table
		mysql_query("UPDATE rb_orders_temp 
		        SET jumlah = jumlah + 1
				WHERE id_session ='$sid' AND id_produk='$_GET[id]'");		
	}	
	header('Location:keranjang-belanja.html');
  }				
}

elseif ($module=='keranjang' AND $act=='hapus'){
	mysql_query("DELETE FROM rb_orders_temp WHERE id_orders_temp='$_GET[id]'");
	header('Location:keranjang-belanja.html');				
}
	
elseif ($module=='keranjang' AND $act=='update'){	
    $sqle=mysql_query("SELECT * FROM rb_pemesan_temp where id_orders='$_POST[id_orders]'");
	$jmldata=mysql_num_rows($sqle);
	$in = explode(' - ',$_POST[instansi]);
	$do = explode(' - ',$_POST[dokter]);
	$sa = explode(' - ',$_POST[sales]);

	$tgl_pesan = tgl_standard($_POST[tanggal_pesan]);
	$tgl_selesai = tgl_standard($_POST[tanggal_selesai]);
	$tgl_ambil = tgl_pesan($_POST[sponsor]);
	
	if ($jmldata==0){
		mysql_query("INSERT INTO rb_pemesan_temp ( id_orders,
											 no_ref, 
											 nama_pemesan,
											 alamat,
											 telpon,
											 tanggal_pesan,
											 tanggal_selesai, 
											 id_instansi, 
											 id_dokter, 
											 diameter,
											 warna,
											 id_user,
											 tanggal_ambil,
											 status_pesanan,
											 id_session)
									VALUES ('$_POST[id_orders]',
											'$_POST[nomor_ref]', 
											'$_POST[nama_pemesan]',
											'$_POST[alamat]',
											'$_POST[phone]',
											'$tgl_pesan',
											'$tgl_selesai', 
											'$in[0]', 
											'$do[0]', 
											'$_POST[diameter]', 
											'$_POST[warna]', 
											'$sa[0]', 
											'$tgl_ambil', 
											'Pending',
											'$sid')");
	}else{								
		mysql_query("UPDATE rb_pemesan_temp SET id_orders = '$_POST[id_orders]',
											 no_ref = '$_POST[nomor_ref]', 
											 nama_pemesan = '$_POST[nama_pemesan]',
											 alamat = '$_POST[alamat]',
											 telpon = '$_POST[phone]',
											 tanggal_pesan = '$tgl_pesan',
											 tanggal_selesai = '$tgl_selesai',
											 id_instansi = '$in[0]', 
											 id_dokter = '$do[0]', 
											 diameter = '$_POST[diameter]', 
											 warna = '$_POST[warna]', 
											 id_user = '$sa[0]', 
											 tanggal_ambil = '$tgl_ambil', 
											 status_pesanan = 'Pending' where id_orders='$_POST[id_orders]'");
	}
	
	$_SESSION[id_pesanan_eceran] = $_POST[id_orders];
	$sql=mysql_query("SELECT * FROM rb_resep_temp");
	$jmldata=mysql_num_rows($sql);
	if ($jmldata==0){
		mysql_query("INSERT INTO rb_resep_temp ( id_orders,
												 R_SPH,
												 L_SPH, 
												 R_CYL,
												 L_CYL,
												 R_AXS,
												 L_AXS,
												 R_ADD, 
												 L_ADD, 
												 PD,
												 PD2,
												 id_session)
										VALUES ('$_POST[id_orders]',
												'$_POST[sph_r]',
												'$_POST[sph_l]', 
												'$_POST[cyl_r]',
												'$_POST[cyl_l]',
												'$_POST[axs_r]',
												'$_POST[axs_l]',
												'$_POST[add_r]', 
												'$_POST[add_l]', 
												'$_POST[pdr]',
												'$_POST[pdl]',
												'$_SESSION[namauser]')");
	}else{								
		mysql_query("UPDATE rb_resep_temp SET id_orders = '$_POST[id_orders]',
												 R_SPH = '$_POST[sph_r]', 
												 L_SPH = '$_POST[sph_l]',
												 R_CYL = '$_POST[cyl_r]',
												 L_CYL = '$_POST[cyl_l]',
												 R_AXS = '$_POST[axs_r]',
												 L_AXS = '$_POST[axs_l]',
												 R_ADD = '$_POST[add_r]', 
												 L_ADD = '$_POST[add_l]', 
												 PD = '$_POST[pdr]',
												 PD2 = '$_POST[pdl]'");
	}
	
	
	$sqlll=mysql_query("SELECT * FROM rb_bayar_temp where id_orders='$_SESSION[id_pesanan_eceran]'");
	$jmldata=mysql_num_rows($sqlll);
	
	$jml_bayar = str_replace(".","",$_POST[jml_bayar]);
	$subtotalbayar = str_replace(".","",$_POST[subtotalbayar]);
	$diskonn = $_POST[sub_total]-$subtotalbayar;
	if ($subtotalbayar == 0){
		$diskontotal = str_replace(".","",$_POST[diskona]);
	}else{
		$diskontotal = $_POST[sub_total]-$subtotalbayar;
	}

	if ($jmldata==0){
		mysql_query("INSERT INTO rb_bayar_temp ( id_orders,
												 sub_total,
												 sub_total_bayar,
												 jml_bayar,
												 bayar_inst,
												 diskon,
												 id_session)
										VALUES ('$_POST[id_orders]',
												'$_POST[sub_total]',
												'$subtotalbayar',
												'$jml_bayar',
												'$_POST[bayarinstansi]',
												'$diskontotal',
												'$_SESSION[namauser]')");
	}else{								
		mysql_query("UPDATE rb_bayar_temp SET id_orders = '$_POST[id_orders]',
												 sub_total = '$_POST[sub_total]', 
												 sub_total_bayar = '$subtotalbayar', 
												 jml_bayar = '$jml_bayar',
												 bayar_inst = '$_POST[bayarinstansi]',
												 diskon = '$diskontotal' where id_orders='$_SESSION[id_pesanan_eceran]'");
	}
				
  $id       = $_POST[id];
  $jml_data = count($id);
  $jumlah   = $_POST[jml]; 
  $stok   = $_POST[stok]; 
  $diskon   = $_POST[diskon]; 
  $harga   = $_POST[harga]; 
  for ($i=1; $i <= $jml_data; $i++){
  	if ($jumlah[$i] > $stok[$i]){
		echo "<script>document.location='keranjang-belanja.html';</script>";
	}else{
		mysql_query("UPDATE rb_orders_temp SET jumlah = '".$jumlah[$i]."',
											diskon = '".$diskon[$i]."',
											harga = '".$harge[$i]."'
										  WHERE id_orders_temp = '".$id[$i]."'");
	}
}
	header('Location:keranjang-belanja.html');				
}

elseif ($module=='keranjang' AND $act=='reset'){	
	mysql_query("DELETE FROM rb_pemesan_temp where id_session='$_SESSION[namauser]'");
    mysql_query("DELETE FROM rb_orders_temp where id_session='$_SESSION[namauser]'");
	mysql_query("DELETE FROM rb_resep_temp where id_session='$_SESSION[namauser]'");
	mysql_query("DELETE FROM rb_bayar_temp where id_session='$_SESSION[namauser]'");
	unset($_SESSION[id_pesanan_eceran]);
		
	header('Location:keranjang-belanja.html');	
}

elseif ($module=='keranjang' AND $act=='resep'){	
	mysql_query("DELETE FROM rb_resep_temp where id_session='$_SESSION[namauser]'");
		
		header('Location:keranjang-belanja.html');	
}

elseif ($module=='keranjang' AND $act=='empty'){	
	mysql_query("DELETE FROM rb_orders_temp where id_session='$_SESSION[namauser]'");
		header('Location:keranjang-belanja.html');	
}
?>
