<?php
# Baca Variabel Form
$message = array();
$Act	 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$pb_id 	 = isset($_REQUEST['pb_id']) ? $_REQUEST['pb_id'] : '';
$pb_date = isset($_REQUEST['pb_date']) ? $_REQUEST['pb_date'] : '';
$pb_pic	 = isset($_REQUEST['pb_pic']) ? $_REQUEST['pb_pic'] : '';
$pb_desc = isset($_REQUEST['pb_desc']) ? $_REQUEST['pb_desc'] : '';
$id_det	 = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
if(empty($pb_id)){ 
	$pb_no   = getpbno();
}else{
	$pb_no	 = isset($_REQUEST['pb_no']) ? $_REQUEST['pb_no'] : ''; 
}
$pbd_prodid  = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$pbd_qty	 = isset($_REQUEST['pbd_qty']) ? $_REQUEST['pbd_qty'] : '';
$val_date 	 = get_validationdate($pb_date);	
if($Act =="SAVE"){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_permintaan_barang.php"; 
		exit;
	}else{
		if(count($message)==0){	
			if(empty($pb_id)){
				$qrySave=mysql_query("INSERT INTO permintaan_barang SET pb_no='$pb_no', pb_date='$pb_date', pb_pic='$pb_pic', pb_desc='$pb_desc',
									  pb_inputby='$user_id', pb_inputdate='$datetime', pb_status=20") or die ("Gagal query".mysql_error());
				if($qrySave){
					$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM permintaan_barang"));
					$pb_id = $data->id;
					$query = mysql_query("SELECT * FROM permintaan_barang_temp WHERE pbd_inputby='$user_id' ORDER BY id ASC");
					while($dt=mysql_fetch_object($query)){
						$qrySave=mysql_query("INSERT INTO permintaan_barang_detail SET pbd_hid='$pb_id', pbd_prodid='$dt->pbd_prodid', 
											  pbd_qty='$dt->pbd_qty'") or die ("Gagal query".mysql_error());
					}
					$qd = mysql_query("DELETE FROM permintaan_barang_temp WHERE pbd_inputby='$user_id'");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan permintaan barang No. $pb_no', user_date=$date, user_time='$time'");
							  
			}elseif(!empty($pb_id) && ($Act_Det <> "ADD_DET")){
				$qrySave=mysql_query("UPDATE permintaan_barang SET pb_date='$pb_date', pb_pic='$pb_pic', pb_desc='$pb_desc', pb_status=20 WHERE id='$pb_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
					foreach($id_det as $i => $id)
					{
						$pbd_qty = $_REQUEST['scrdqty'][$id];
						mysql_query("UPDATE permintaan_barang_detail SET pbd_qty='$pbd_qty' WHERE id='$id' ");
					}
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah permintaan barang No. $pb_no', user_date='$date', user_time='$time'");
				}							  
			}				
			if($qrySave){
				$message[] = "Data Sukses tersimpan !";	
				$status = "OK";
			}
		}	
	}
}	
if($Act =="DETAIL"){ 
	$qd = mysql_query("SELECT * FROM permintaan_barang WHERE id='$pb_id'");
	$dd = mysql_fetch_object($qd);		
	$pb_id 		= $dd->id;			
	$pb_no		= $dd->pb_no;
	$pb_date	= $dd->pb_date;
	$pb_pic		= $dd->pb_pic;
	$pb_desc  	= $dd->pb_desc;
}
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE permintaan_barang SET pb_status=100 WHERE id='$pb_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus permintaan barang No. $pb_no', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
			
	}
}
if($Act == "POSTING"){
		#Update Status
		$qd = mysql_query("UPDATE permintaan_barang SET pb_status=20 WHERE id='$pb_id'");
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting permintaan barang No. $pb_no', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses diposting !";	
		$status = "OK";				
}
if(($Act_Det == "ADD_DET") && (empty($pb_id))){ 
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_permintaan_barang.php"; 
		exit;
	}else{
	mysql_query("INSERT INTO permintaan_barang_temp SET pbd_prodid='$pbd_prodid', pbd_qty='$pbd_qty', pbd_inputby='$user_id'");
	}
}elseif(($Act_Det == "ADD_DET") && (!empty($pb_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_permintaan_barang.php"; 
		exit;
	}else{
	mysql_query("INSERT INTO permintaan_barang_detail SET pbd_hid='$pb_id', pbd_prodid='$pbd_prodid', pbd_qty='$pbd_qty'");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($pb_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_permintaan_barang.php"; 
		exit;
	}else{
	mysql_query("DELETE FROM permintaan_barang_temp WHERE id='$id_deth'");
	}
}elseif(($Act_Det == "DELETE_DET") && (!empty($pb_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_permintaan_barang.php"; 
		exit;
	}else{
	mysql_query("DELETE FROM permintaan_barang_detail WHERE id='$id_deth'");
	}
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_permintaan_barang.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Permintaan-Barang'>";
}
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-Permintaan-Barang'>";	
		exit;
	}
}	
?>
