<?php
# Baca Variabel Form
$message 	   = array();
$Act	 	   = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	   = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$bomh_id 	   = isset($_REQUEST['bomh_id']) ? $_REQUEST['bomh_id'] : '';
$bomh_date 	   = isset($_REQUEST['bomh_date']) ? $_REQUEST['bomh_date'] : '';
$bomh_prodid   = isset($_REQUEST['bomh_prodid']) ? $_REQUEST['bomh_prodid'] : '';
$bomh_prodcode = isset($_REQUEST['bomh_prodcode']) ? $_REQUEST['bomh_prodcode'] : '';
$bomh_prodname = isset($_REQUEST['bomh_prodname']) ? $_REQUEST['bomh_prodname'] : '';
$bomh_prodsat  = isset($_REQUEST['bomh_prodsat']) ? $_REQUEST['bomh_prodsat'] : '';
$bomh_total    = isset($_REQUEST['bomh_total']) ? $_REQUEST['bomh_total'] : '';
$bomh_status   = isset($_REQUEST['bomh_status']) ? $_REQUEST['bomh_status'] : 10;
$id_det		   = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth	   = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
/*if(empty($bomh_id)){
	$bomh_no   = getbomno();
}else{*/
	$bomh_no   = isset($_REQUEST['bomh_no']) ? $_REQUEST['bomh_no'] : '';
#}
$bomd_prodid   = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$prodstock     = isset($_REQUEST['prodstock']) ? $_REQUEST['prodstock'] : '';
$bomd_qty      = isset($_REQUEST['bomd_qty']) ? $_REQUEST['bomd_qty'] : '';

if($Act =="SAVE"){
	if(count($message)==0){	
		if(empty($bomh_id)){
			$qrySave=mysql_query("INSERT INTO bom SET bomh_no='$bomh_no', bomh_date='$bomh_date', bomh_prodid='$bomh_prodid', bomh_total='$bomh_total', 	
			         bomh_inputby='$user_id',bomh_inputdate='$datetime', bomh_status='$bomh_status', bomh_level='FG'") or die ("Gagal query".mysql_error()); 
			if($qrySave){
				$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM bom"));
				$bomh_id = $data->id;
				$query = mysql_query("SELECT * FROM bom_temp WHERE bomd_inputby='$user_id' AND bomd_status=20 ");
				while($dt=mysql_fetch_object($query)){
					$qrySave=mysql_query("INSERT INTO bom_detail SET bomd_hid='$bomh_id', bomd_prodid='$dt->bomd_prodid', bomd_qty='$dt->bomd_qty', bomd_status=10") 
					         or die ("Gagal query".mysql_error());
				}
				$qd = mysql_query("DELETE FROM bom_temp WHERE bomd_inputby='$user_id' AND bomd_status=20");
			}	
			#Insert History User
			$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id',user_act='Menambahkan proses master BOM no : $bomh_no', user_date='$date', user_time='$time'");
		}elseif(!empty($bomh_id) && ($Act_Det <> "ADD_DET")){
			$qrySave=mysql_query("UPDATE bom SET bomh_date='$bomh_date', bomh_prodid='$bomh_prodid', bomh_total='$bomh_total', bomh_status='$bomh_status' WHERE id='$bomh_id'") 
			or die ("Gagal query".mysql_error());
			if($qrySave){
				foreach($id_det as $i => $id)
				{
					$bomd_qty		= $_REQUEST['bomdqty'][$id];
					mysql_query("UPDATE bom_detail SET bomd_qty='$bomd_qty' WHERE id='$id' ");
				}
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah proses master BOM no : $bomh_no', user_date='$date', user_time='$time'");
			}							  
		}				
		if($qrySave){
			$message[] = "Data Sukses tersimpan !";	
			$status = "OK";
		}
	}	
}
if($Act =="DETAIL"){ 
	$qd = mysql_query("SELECT a.*,b.prod_code,b.prod_name,b.prod_sat
					   FROM bom a
					   INNER JOIN product b ON b.id=a.bomh_prodid
					   WHERE a.id='$bomh_id'");
	$dd = mysql_fetch_object($qd);		
	$bomh_id 		= $dd->id;			
	$bomh_no	 	= $dd->bomh_no;
	$bomh_date  	= $dd->bomh_date;
	$bomh_prodid 	= $dd->bomh_prodid;
	$bomh_prodcode 	= $dd->prod_code;
	$bomh_prodname	= $dd->prod_name;
	$bomh_prodsat  	= $dd->prod_sat;
	$bomh_total  	= $dd->bomh_total;
	$bomh_status  	= $dd->bomh_status;
}	
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE bom SET bomh_status=100 WHERE id='$bomh_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus proses master BOM no : $bomh_no', 
		         user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
			
	}
}
if(($Act_Det == "ADD_DET") && (empty($bomh_id))){ 
	mysql_query("INSERT INTO bom_temp SET bomd_prodid='$bomd_prodid', bomd_qty='$bomd_qty', bomd_inputby='$user_id', bomd_status=20");
}elseif(($Act_Det == "ADD_DET") && (!empty($bomh_id))){
	mysql_query("INSERT INTO bom_detail SET bomd_hid='$bomh_id', bomd_prodid='$bomd_prodid', bomd_qty='$bomd_qty', bomd_status=10");
}
if(($Act_Det == "DELETE_DET") && (empty($bomh_id))){
	mysql_query("DELETE FROM bom_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($bomh_id))){
	mysql_query("UPDATE bom_detail SET bomd_status=100 WHERE id='$id_deth'");
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_bomfg.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-BOM-FG'>";
}
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-BOM-FG'>";	
		exit;
	}
}	
?>