<?php
# Baca Variabel Form
$message 	 = array();
$Act		 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] :'';
$stock_id	 = isset($_REQUEST['stock_id']) ? $_REQUEST['stock_id'] :'';
$prod_code	 = isset($_REQUEST['prod_code']) ? $_REQUEST['prod_code'] :'';
$id_temp	 = isset($_REQUEST['id_temp']) ? $_REQUEST['id_temp'] :'';
#input manual
$prodid	 	 = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] :'';
$prodcode	 = isset($_REQUEST['prodcode']) ? $_REQUEST['prodcode'] :'';
$prod_cat	 = isset($_REQUEST['prod_cat']) ? $_REQUEST['prod_cat'] :'';
$prodsat	 = isset($_REQUEST['prodsat']) ? $_REQUEST['prodsat'] :'';
$stock_date	 = isset($_REQUEST['stock_date']) ? $_REQUEST['stock_date'] :'';
$stock_qty	 = isset($_REQUEST['stock_qty']) ? $_REQUEST['stock_qty'] :'';
$stock_doctype	 = isset($_REQUEST['stock_doctype']) ? $_REQUEST['stock_doctype'] :'';
$stock_docno	 = isset($_REQUEST['stock_docno']) ? $_REQUEST['stock_docno'] :'';
$stock_docdate	 = isset($_REQUEST['stock_docdate']) ? $_REQUEST['stock_docdate'] :'';
$stock_price	 = isset($_REQUEST['stock_price']) ? $_REQUEST['stock_price'] :'';
$stock_currency	 = isset($_REQUEST['stock_currency']) ? $_REQUEST['stock_currency'] :'';
$stock_loc	 = isset($_REQUEST['stock_loc']) ? $_REQUEST['stock_loc'] :'';
$stock_whid	 = isset($_REQUEST['stock_whid']) ? $_REQUEST['stock_whid'] :0;
/*if($prod_cat==1 || $prod_cat==6){
	$stock_loc=1;
}elseif($prod_cat==5 ){
	$stock_loc=2;
}elseif($prod_cat==3 || $prod_cat==4 ){
	$stock_loc=3;
}elseif($prod_cat==2 ){
	$stock_loc=4;
}*/
#Upload
if($Act == "UPL" ){
	require_once "upload_stock_awal.php";
}
if($Act == "PROCESS"){
	if(!empty($_FILES['userfile']['tmp_name'])){
		$data 	= new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
		$baris 	= $data->rowcount($sheet_index=0);
		for ($i=2; $i<=$baris; $i++)
		{
		  $id 		 		= $data->val($i, 1);
		  $stock_date 		= date("Y-m-d",strtotime($data->val($i, 2)));
		  $stock_prodcode 	= $data->val($i, 3);
		  $stock_qty 		= $data->val($i, 4);
		  if(!empty($id)){
			  mysql_query("INSERT INTO stock_log SET stock_date='$stock_date',stock_prodcode='$stock_prodcode',stock_qty='$stock_qty', stock_status=10");	
		  } 
		}
	}
	require_once "upload_stock_awal.php";
}
if($Act == "INV"){
	if(!empty($stock_id)){
		foreach ( $stock_id as $i => $val){
			mysql_query("UPDATE stock_log SET stock_status=20 WHERE id='$val' ");
		}
	  $flag=1;			
	}else{
		$Act ="UPL";
		require_once "upload_stock_awal.php";
	}
	if($flag==1){
		$q = mysql_query("SELECT * FROM stock_log WHERE stock_status=20 ORDER BY id ASC");
		while($data=mysql_fetch_array($q)){
			$prod = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$data[stock_prodcode]'"));
			mysql_query("INSERT INTO stock SET stock_prodid='$prod[id]', stock_date='$data[stock_date]', stock_qty='$data[stock_qty]', stock_status=10 ");
			#History
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi saldo awal : $data[stock_prodcode] ==> $data[stock_qty] ', 
						 user_date='$date', user_time='$time'");
		}
		$flag=2;
	}
	if($flag==2){
		#update status
		mysql_query("UPDATE stock_log SET stock_status=30 WHERE stock_status=20");
		#status
		$message[] = "Data Sukses diupload !";	
		$status ="OK";
	}
}
if($Act == "DEL_TEMP"){
	mysql_query("UPDATE stock_log SET stock_status=100 WHERE stock_status=10");
	#redirect
	require_once "upload_stock_awal.php";
}
if($Act == "DELDET_TEMP"){
	mysql_query("UPDATE stock_log SET stock_status=100 WHERE id='$id_temp'");
	#redirect
	require_once "upload_stock_awal.php";
}
if($Act == "DELETE"){
	mysql_query("DELETE FROM stock WHERE id='$stock_id'");
	#History
	mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengahpus data saldo awal : item code :$prod_code ', 
				 user_date='$date', user_time='$time'");
	$message[] = "Data Sukses dihapus !";	
	$status ="OK";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Stock-Awal'>";	
}
if($Act == "ADD"){
	$prod_cat		 = 1;
	
	require_once "add_stock_awal.php";
}
if($Act == "SAVE"){
	if(empty($stock_id)){
		mysql_query("INSERT INTO stock SET stock_prodid='$prodid', stock_date='$stock_date', stock_qty='$stock_qty', stock_status=10, stock_doctype='$stock_doctype',
		stock_docno='$stock_docno', stock_docdate='$stock_docdate', stock_price='$stock_price', stock_currency='$stock_currency', stock_loc='$stock_loc', stock_whid='$stock_whid' ");
		#History
		mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi saldo awal : $prodcode ==> $stock_qty', 
					 user_date='$date', user_time='$time'");
	}else{
		mysql_query("UPDATE stock SET stock_prodid='$prodid', stock_date='$stock_date', stock_qty='$stock_qty',stock_doctype='$stock_doctype',
		stock_docno='$stock_docno', stock_docdate='$stock_docdate', stock_price='$stock_price', stock_currency='$stock_currency', stock_loc='$stock_loc', stock_whid='$stock_whid' 
		WHERE id='$stock_id'");
		#History
		mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Merubah transaksi saldo awal : $prodcode ==> $stock_qty', 
					 user_date='$date', user_time='$time'");
	}	
	$message[] = "Data Sukses disimpan !";	
	$status ="OK";			 
}
if($Act == "DETAIL"){
	$dt=mysql_fetch_array(mysql_query("SELECT a.*, b.prod_code, b.prod_name, b.prod_sat, b.prod_cat FROM stock a INNER JOIN product b ON b.id=a.stock_prodid
	    WHERE a.id='$stock_id'"));
	$stock_id	 = $dt['id'];
	$prodid	 	 = $dt['stock_prodid'];
	$prodcode	 = "$dt[prod_code] - $dt[prod_name]";
	$prodsat	 = $dt['prod_sat'];
	$stock_date	 = $dt['stock_date'];
	$stock_qty	 = $dt['stock_qty'];
	$stock_doctype	 = $dt['stock_doctype'];
	$stock_docno	 = $dt['stock_docno'];
	$stock_docdate	 = $dt['stock_docdate'];
	$stock_price	 = $dt['stock_price'];
	$stock_currency	 = $dt['stock_currency'];
	$prod_cat		 = $dt['prod_cat'];
	$stock_loc		 = $dt['stock_loc'];
	$stock_whid		 = $dt['stock_whid'];
	require_once "add_stock_awal.php";	
}
# Popup pesan
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil) { 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			alert(pesan);
		</script>
	<?php 		
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Stock-Awal'>";	
	}
}
?>