<?php
/* if(empty($wonb_id)){
	$wonb_no	 = get_wonb($bulan,$tahun);
}else{
	$wonb_no	 = isset($_REQUEST['wonb_no']) ? $_REQUEST['wonb_no'] : '';
} */
?>
<?php
# Baca Variabel Form
$message = array();
$fromdate	 = $start_date;
$todate      = $end_date;
$Act	 	 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	 = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$wonb_id 	 	 = isset($_REQUEST['wonb_id']) ? $_REQUEST['wonb_id'] : '';
$wonb_date 	 = isset($_REQUEST['wonb_date']) ? $_REQUEST['wonb_date'] : '';
$wonb_deptid	 = isset($_REQUEST['wonb_deptid']) ? $_REQUEST['wonb_deptid'] : '';
$wonb_pic  	 = isset($_REQUEST['wonb_pic']) ? $_REQUEST['wonb_pic'] : '';
$wonb_desc  	 = isset($_REQUEST['wonb_desc']) ? $_REQUEST['wonb_desc'] : '';
$id_det		 = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : 0;
$id_deth	 = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$id_temp	 = isset($_REQUEST['id_temp']) ? $_REQUEST['id_temp'] : '';
$val_date 	 = get_validationdate($wonb_date);
$tahun       = substr($wonb_date,0,4);
$bulan		 = substr($wonb_date,5,2);
if(empty($wonb_id)){
	$wonb_no	 = get_wonb($bulan,$tahun);
}else{
	$wonb_no	 = isset($_REQUEST['wonb_no']) ? $_REQUEST['wonb_no'] : '';
}

$wonbdprodid 	 = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : 0;
$wonbdqty 	 = isset($_REQUEST['wonbdqty']) ? $_REQUEST['wonbdqty'] : 0;

if($Act =="GET_BOM"){
	$bom = mysql_fetch_array(mysql_query("SELECT id FROM bom WHERE bomh_prodid='$wo_prodid' AND bomh_level='PRODUKSI' AND bomh_status=10"));
	$bom_id = $bom['id'];
	#reload
	require_once "add_workordernonbom.php";
}
if($Act =="SAVE"){
		if(count($message)==0){	
			if(empty($wonb_id)){
				$qrySave=mysql_query("INSERT INTO workorder_nonbom SET wonb_no='$wonb_no', wonb_date='$wonb_date', wonb_deptid='$wonb_deptid', wonb_pic='$wonb_pic', wonb_desc='$wonb_desc', wonb_inputby='$user_id', wonb_inputdate='$datetime', wonb_status=20") or die ("Gagal query".mysql_error());
				if($qrySave){
					$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM workorder_nonbom"));
					$wonb_id = $data->id;
					$qd=mysql_query("SELECT * FROM workorder_nonbom_temp WHERE wonbd_inputby='$user_id' AND wonbd_status=10 ORDER BY id ASC");
					while($dd=mysql_fetch_array($qd)){
						#Insert bom
						mysql_query("INSERT INTO workorder_nonbom_detail SET wonbd_prodid='$dd[wonbd_prodid]', wonbd_qty='$dd[wonbd_qty]', wonbd_hid='$wonb_id'");
					}	
					#delete temp
					mysql_query("DELETE FROM workorder_nonbom_temp WHERE wonbd_inputby='$user_id' AND wonbd_status=10");
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi workorder non bom No. $wonb_no', 
					user_date='$date', user_time='$time'");		
				}
			}else{
				$qrySave=mysql_query("UPDATE workorder_nonbom SET wonb_no='$wonb_no', wonb_date='$wonb_date', wonb_pic='$wonb_pic', wonb_desc='$wonb_desc', wonb_status=20 WHERE id='$wonb_id'") 
				or die ("Gagal query".mysql_error());
				if($qrySave){
					foreach($id_det as $i => $id){
						$wonbd_qty	= $_REQUEST['wonbd_qty'][$id];
						mysql_query("UPDATE workorder_nonbom_detail SET wonbd_qty='$wonbd_qty' WHERE id='$id'");
					}
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi workorder non bom No. $wonb_no', user_date='$date', user_time='$time'");
				}							  
			}				
			if($qrySave){
				$message[] = "Data Sukses tersimpan !";	
				$status = "OK";
			}
		}	
}
if($Act =="DETAIL"){
	$dd = mysql_fetch_object(mysql_query("SELECT * FROM workorder_nonbom  
	      WHERE id='$wonb_id'"));		
	$wonb_id 		= $dd->id;			
	$wonb_no		= $dd->wonb_no;
	$wonb_date	= $dd->wonb_date;
	$wonb_deptid	= $dd->wonb_deptid;
	$wonb_pic   	= $dd->wonb_pic;
	$wonb_desc	= $dd->wonb_desc;
	
}
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE workorder_nonbom SET wonb_status=100 WHERE id='$wonb_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus transaksi workorder non bom No. $wonb_no', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}
if($Act == "POSTING"){
	#Update Status
	$qd = mysql_query("UPDATE workorder_nonbom SET wonb_status=20 WHERE id='$wonb_id'");
	#Insert History User
	$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting transaksi workorder non bom No. $wonb_no', user_date='$date', user_time='$time'");
	$message[] = "Data Sukses diposting !";	
	$status = "OK";	
}
if(($Act_Det == "ADD_DET") && (empty($wonb_id))){ 	
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO workorder_nonbom_temp SET wonbd_prodid='$wonbdprodid', wonbd_qty='$wonbdqty', wonbd_inputby='$user_id', wonbd_status=10");
	}
}elseif(($Act_Det == "ADD_DET") && (!empty($wonb_id))){
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO workorder_nonbom_detail SET wonbd_prodid='$wonbdprodid', wonbd_qty='$wonbdqty', wonbd_hid='$wonb_id' ");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($wonb_id))){
	mysql_query("DELETE FROM workorder_nonbom_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($wonb_id))){
	mysql_query("DELETE FROM workorder_nonbom_detail WHERE id='$id_deth'");
}

if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_workordernonbom.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Work-Order-Non-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-Work-Order-Non-Bom'>";	
		exit;
	}
}
?>

