<?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'] : '';
$pilih		   = isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
$pilih_reg	   = isset($_REQUEST['pilih_reg']) ? $_REQUEST['pilih_reg'] : '';
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='PRODUKSI'") 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=10 ");
				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=10");
			}	
			#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_no='$bomh_no', 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=10");
}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_bom.php";
}
#proses upload by excel
if($Act=="GET"){
	$data 	= new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
	$baris 	= $data->rowcount($sheet_index=0);
	for ($i=2; $i<=$baris; $i++){
	  $message = array();
	  $no 		 	= $data->val($i, 1);
	  $prod_codeh 	= $data->val($i, 2);
	  $prod_nameh 	= $data->val($i, 3);
	  $prod_sath 	= $data->val($i, 4);
	  $qtyh 		= $data->val($i, 5);
	  $prod_coded  	= $data->val($i, 6);
	  $prod_named 	= $data->val($i, 7);
	  $prod_satd  	= $data->val($i, 8);
	  $qtyd  		= $data->val($i, 9);
	  
	  if(!empty($no)){
		  mysql_query("INSERT INTO bom_temp SET prod_codeh='$prod_codeh', prod_nameh='$prod_nameh', prod_sath='$prod_sath', 
					   qtyh='$qtyh', prod_coded='$prod_coded', prod_named='$prod_named', prod_satd='$prod_satd', qtyd='$qtyd',
					   bomd_status=1000, bomd_inputby='$user_id'"); 
	  }
	}
}
if($Act=="REG"){
	if(!empty($pilih_reg)){	
		foreach ( $pilih_reg as $i => $val){
			$prod_codeh   = isset($_REQUEST['prod_codeh'][$val])?$_REQUEST['prod_codeh'][$val] :'';
			$prod_nameh   = isset($_REQUEST['prod_nameh'][$val])?$_REQUEST['prod_nameh'][$val] :'';
			$prod_sath    = isset($_REQUEST['prod_sath'][$val])?$_REQUEST['prod_sath'][$val] :'';
			$kd_kategorih = isset($_REQUEST['kd_kategorih'][$val])?$_REQUEST['kd_kategorih'][$val] :'';
			$prod_coded   = isset($_REQUEST['prod_coded'][$val])?$_REQUEST['prod_coded'][$val] : '';
			$prod_named   = isset($_REQUEST['prod_named'][$val]) ?$_REQUEST['prod_named'][$val] : '';
			$prod_satd	  = isset($_REQUEST['prod_satd'][$val])?$_REQUEST['prod_satd'][$val] :'';
			$kd_kategorid = isset($_REQUEST['kd_kategorid'][$val])?$_REQUEST['kd_kategorid'][$val] :'';
			#Insert master barang
			if($kd_kategorih != "BLANK"){
				#Update kode branag ditemp
				mysql_query("UPDATE bomd_temp SET prod_codeh='$prod_codeh', prod_nameh='$prod_nameh', prod_sath='$prod_sath' WHERE id='$val'");
				#Insert ke master barang				
				mysql_query("INSERT INTO product SET prod_code='$prod_codeh', prod_name='$prod_nameh', prod_sat='$prod_sath', prod_cat='$kd_kategorih', prod_status=10");
				#Aktivitas user					
				mysql_query("INSERT INTO user_activity SET user_id='".$_SESSION['USER_ID']."', user_act='Menambahkan data barang - $prod_codeh', 
				             user_date='$date', user_time='$time'");
			}
			if($kd_kategorid != "BLANK"){
				#Update kode branag ditemp
				mysql_query("UPDATE bomd_temp SET prod_coded='$prod_coded', prod_named='$prod_named', prod_satd='$prod_satd' WHERE id='$val'");
				#Insert ke master barang				
				mysql_query("INSERT INTO product SET prod_code='$prod_coded', prod_name='$prod_named', prod_sat='$prod_satd', prod_cat='$kd_kategorid', prod_status=10");
				#Aktivitas user					
				mysql_query("INSERT INTO user_activity SET user_id='".$_SESSION['USER_ID']."', user_act='Menambahkan data barang - $prod_coded', 
				             user_date='$date', user_time='$time'");
			}
		}
		require "add_upload_bom.php";
	}else{
		require "add_upload_bom.php";
	}	
}
if($Act=="PRC"){
	if(!empty($pilih)){	
		foreach ( $pilih as $i => $val){
			#Update status temp
			$bomh_prodid   = isset($_REQUEST['bomh_prodid'][$val])?$_REQUEST['bomh_prodid'][$val] :'';
			$bomd_prodid   = isset($_REQUEST['bomd_prodid'][$val])?$_REQUEST['bomd_prodid'][$val] :'';
			mysql_query("UPDATE bom_temp SET bomd_status=2000, bomh_prodid='$bomh_prodid', bomd_prodid='$bomd_prodid' WHERE id='$val' ");
		}
		require "add_upload_bom.php";
	}else{
		require "add_upload_bom.php";
	}	
}
if($Act=="INV"){
	#Insert header pemasukan
	$qh=mysql_query("SELECT * FROM bom_temp WHERE bomd_status =2000 GROUP BY bomh_prodid ORDER BY bomh_prodid ASC");	
	while($dh=mysql_fetch_array($qh)){
		$bomh_no = getbomno();
		#Insert Pemasukan Headar
		$cekbom = mysql_fetch_array(mysql_query("SELECT id FROM bom WHERE bomh_prodid='$dh[bomh_prodid]'"));
		if(empty($cekbom['id'])){
			mysql_query("INSERT INTO bom SET bomh_no='$bomh_no', bomh_prodid='$dh[bomh_prodid]', bomh_level='PRODUKSI', 
						 bomh_total='$dh[qtyh]', bomh_inputby='$user_id', bomh_inputdate='$datetime', bomh_status=5");
			#Aktivitas User
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi master BOM. $bomh_no', user_date='$date', user_time='$time'");
		}
		$status = 1;
	}	
	#exit;
	if($status == 1){
		$qd = mysql_query("SELECT b.id,a.bomd_prodid,a.qtyd  FROM bom_temp a 
						  INNER JOIN bom b ON b.bomh_prodid=a.bomh_prodid				 
						  WHERE a.bomd_status=2000 AND b.bomh_status=5");
		while($dd = mysql_fetch_array($qd)){
			#Insert Detail
			mysql_query("INSERT INTO bom_detail SET bomd_hid='$dd[id]', bomd_prodid='$dd[bomd_prodid]', bomd_qty='$dd[qtyd]', bomd_status=10");
			
		}
		$status = 2;
	}
	//exit;
	if($status == 2){	
		#update status log
		mysql_query("UPDATE bom_temp SET bomd_status=3000 WHERE bomd_status=2000");
		#update status log
		mysql_query("UPDATE bom SET bomh_status=10 WHERE bomh_status =5");
		$message[] = "Data Sukses dikirim.";	
		$status    = "OK";
	}		 
}
if($Act=="DEL_UPL"){
	#update status
	mysql_query("UPDATE bom_temp SET bomd_status=5000 WHERE bomd_status=1000");
	require "add_upload_bom.php";
}
if($Act=="DEL_UPL_DET"){
	#update status
	mysql_query("UPDATE bom_temp SET bomd_status=1000 WHERE id='".$_REQUEST['id_temp']."' ");
	require "add_upload_bom.php";
}
if($Act == "UPL" || $Act == "GET" || $Act == "TEMP" || $Act == "PRC" || $Act == "INV" || $Act == "REG" || $Act == "DEL_UPL" || $Act_Det == "DEL_UPL_DET"){
	require_once "add_upload_bom.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-BOM'>";
}
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'>";	
		exit;
	}
}	
?>