<?php
# Baca Variabel Form
$message 	   = array();
$Act	 	   = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	   = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$wip_id 	   = isset($_REQUEST['wip_id']) ? $_REQUEST['wip_id'] : '';
$wip_date 	   = isset($_REQUEST['wip_date']) ? $_REQUEST['wip_date'] : '';
$wip_pic       = isset($_REQUEST['wip_pic']) ? $_REQUEST['wip_pic'] : '';
$wip_desc	   = isset($_REQUEST['wip_desc']) ? $_REQUEST['wip_desc'] : '';
$id_det		   = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth	   = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
if(empty($wip_id)){
	$wip_no   = getwipno();
}else{
	$wip_no   = isset($_REQUEST['wip_no']) ? $_REQUEST['wip_no'] : '';
}
$wipd_prodid   = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$wipd_qty      = isset($_REQUEST['wipd_qty']) ? $_REQUEST['wipd_qty'] : '';

if($Act =="SAVE"){
	if(count($message)==0){	
		if(empty($wip_id)){
			$qrySave=mysql_query("INSERT INTO wip SET wip_no='$wip_no', wip_date='$wip_date', wip_pic='$wip_pic', wip_desc='$wip_desc',
			         wip_inputby='$user_id', wip_inputdate='$datetime', wip_status=10") or die ("Gagal query".mysql_error());
			if($qrySave){
				$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM wip"));
				$wip_id = $data->id;
				$query = mysql_query("SELECT * FROM wip_log WHERE wip_inputby='$user_id' ");
				while($dt=mysql_fetch_object($query)){
					$qrySave=mysql_query("INSERT INTO wip_detail SET wipd_hid='$wip_id', wipd_prodid='$dt->wipd_prodid', wipd_qty='$dt->wipd_qty'") 
					         or die ("Gagal query".mysql_error());
				}
				$qd = mysql_query("DELETE FROM wip_log WHERE wip_inputby='$user_id'");
			}	
			#Insert History User
			$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id',user_act='Menambahkan data wip no : $wip_no', user_date='$date', user_time='$time'");
		}elseif(!empty($wip_id) && ($Act_Det <> "ADD_DET")){
			$qrySave=mysql_query("UPDATE wip SET wip_date='$wip_date', wip_pic='$wip_pic', wip_desc='$wip_desc' WHERE id='$wip_id'") 
			or die ("Gagal query".mysql_error());
			if($qrySave){
				foreach($id_det as $i => $id)
				{
					$wipd_qty		= $_REQUEST['wipdqty'][$id];
					mysql_query("UPDATE wip_detail SET wipd_qty='$wipd_qty' WHERE id='$id' ");
				}
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah data wip no : $wip_no', user_date='$date', user_time='$time'");
			}							  
		}				
		if($qrySave){
			$message[] = "Data Sukses tersimpan !";	
			$status = "OK";
		}
	}	
}
if($Act =="DETAIL"){ 
	$qd = mysql_query("SELECT * FROM wip WHERE id='$wip_id'");
	$dd = mysql_fetch_object($qd);		
	$wip_id 	= $dd->id;			
	$wip_no	 	= $dd->wip_no;
	$wip_date  	= $dd->wip_date;
	$wip_pic 	= $dd->wip_pic;
	$wip_desc 	= $dd->wip_desc;
}	
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE wip SET wip_status=100 WHERE id='$wip_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus data no : $wip_id', 
		         user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
			
	}
}
if($Act == "POSTING"){
	$qd = mysql_query("UPDATE wip SET wip_status=20 WHERE id='$wip_id'");
	#Insert History User
	$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting data wip no. $wip_id', user_date='$date', user_time='$time'");
	$message[] = "Data Sukses diposting !";	
	$status = "OK";				
}
if(($Act_Det == "ADD_DET") && (empty($wip_id))){ 
	mysql_query("INSERT INTO wip_log SET wipd_prodid='$wipd_prodid', wipd_qty='$wipd_qty', wip_inputby='$user_id', wip_status=10");
}elseif(($Act_Det == "ADD_DET") && (!empty($inch_id))){
	mysql_query("INSERT INTO wip_detail SET wipd_hid='$wip_id', wipd_prodid='$wipd_prodid', wipd_qty='$wipd_qty'");
}
if(($Act_Det == "DELETE_DET") && (empty($wip_id))){
	mysql_query("DELETE FROM wip_log WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($wip_id))){
	mysql_query("DELETE FROM wip_detail WHERE id='$id_deth'");
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_wip.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-WIP'>";
}
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-WIP'>";	
		exit;
	}
}	
?>