<?php
session_start();
error_reporting(0);
include "../config/koneksi.php";
include "../config/library.php";
include "../config/session_karyawan.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]'");		
	}	
	deleteAbandonedCart();
	header('Location:transaksi-grosir.html');
  }				
}

elseif ($module=='keranjang' AND $act=='hapus'){
	mysql_query("DELETE FROM rb_orders_temp WHERE id_orders_temp='$_GET[id]'");
	header('Location:transaksi-grosir.html');				
}
	
elseif ($module=='keranjang' AND $act=='update'){	
    $sqle=mysql_query("SELECT * FROM rb_grosir_temp where id_orders='$_POST[id_orders]'");
	$jmldata=mysql_num_rows($sqle);
	if ($jmldata==0){
		mysql_query("INSERT INTO rb_grosir_temp ( id_orders,
											 nama_pemesan,
											 alamat,
											 telpon,
											 tanggal_pesan,
											 tanggal_selesai,
											 perusahaan,
											 keterangan,
											 id_session)
									VALUES ('$_POST[id_orders]',
											'$_POST[nama_pemesan]',
											'$_POST[alamat]',
											'$_POST[phone]',
											'$_POST[tanggal_pesan]',
											'$_POST[tanggal_selesai]',
											'$_POST[perusahaan]', 
											'$_POST[keterangan]',
											'$sid')");
	}else{								
		mysql_query("UPDATE rb_grosir_temp SET id_orders = '$_POST[id_orders]',
												 nama_pemesan = '$_POST[nama_pemesan]',
												 alamat = '$_POST[alamat]',
												 telpon = '$_POST[phone]',
												 tanggal_pesan = '$_POST[tanggal_pesan]',
												 tanggal_selesai = '$_POST[tanggal_selesai]',
												 perusahaan = '$_POST[perusahaan]',
												 keterangan = '$_POST[keterangan]',
												 id_session = '$sid' where id_orders='$_POST[id_orders]'");
	}	
	$_SESSION[id_pesanan_grosir] = $_POST[id_orders];
	$sqlll=mysql_query("SELECT * FROM rb_bayar_temp where id_orders='$_SESSION[id_pesanan_grosir]'");
	$jmldata=mysql_num_rows($sqlll);
	$jml_bayar = str_replace(".","",$_POST[jml_bayar]);
	$subtotalbayar = str_replace(".","",$_POST[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)
										VALUES ('$_POST[id_orders]',
												'$_POST[sub_total]',
												'$subtotalbayar',
												'$jml_bayar',
												'0',
												'$diskontotal')");
	}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',
												 diskon    = '$diskontotal' where id_orders='$_SESSION[id_pesanan_grosir]'");
	}
				
  $id       = $_POST[id];
  $jml_data = count($id);
  $jumlah   = $_POST[jml]; 
  $stok   = $_POST[stok]; 
  $diskon   = $_POST[diskon]; 
  for ($i=1; $i <= $jml_data; $i++){
	if ($jumlah[$i] > $stok[$i]){
		echo "<script>document.location='transaksi-grosir.html';</script>";
	}else{
    mysql_query("UPDATE rb_orders_temp SET jumlah = '".$jumlah[$i]."',
										diskon = '".$diskon[$i]."'
                                      WHERE id_orders_temp = '".$id[$i]."'");
	}
}
	header('Location:transaksi-grosir.html');				
}

elseif ($module=='keranjang' AND $act=='reset'){	
	mysql_query("TRUNCATE rb_grosir_temp");
    mysql_query("TRUNCATE rb_orders_temp");
	mysql_query("TRUNCATE rb_bayar_temp");
		
		header('Location:transaksi-grosir.html');	
}

elseif ($module=='keranjang' AND $act=='empty'){	
	mysql_query("TRUNCATE rb_orders_temp");
		
		header('Location:transaksi-grosir.html');	
}

function deleteAbandonedCart(){
	$kemarin = date('Y-m-d', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
	mysql_query("DELETE FROM rb_orders_temp 
	        WHERE tgl_order_temp < '$kemarin'");
}
?>
