<?php
# Baca Variabel Form
$message 	 = array();
$Act	 	 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] :'';
$Act_Det 	 = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$adjh_id 	 = isset($_REQUEST['adjh_id']) ? $_REQUEST['adjh_id'] :'';
$adjh_date   = isset($_REQUEST['adjh_date']) ? $_REQUEST['adjh_date'] : '';
$adjh_desc   = isset($_REQUEST['adjh_desc']) ? $_REQUEST['adjh_desc'] : '';
$id_det		 = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth	 = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$val_date    = get_validationdate($adjh_date);
if(empty($adjh_id)){ 
	$adjh_no   = getadjno();
}else{
	$adjh_no   = isset($_REQUEST['adjh_no']) ? $_REQUEST['adjh_no'] : '';
}
$adjd_prodid   = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$adjd_qty      = isset($_REQUEST['adjd_qty']) ? $_REQUEST['adjd_qty'] : '';
		
if($Act=="SAVE"){
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		if(count($message)==0){	
			if(empty($adjh_id)){
				$qrySave=mysql_query("INSERT INTO adjusment SET adjh_no='$adjh_no', adjh_date='$adjh_date', adjh_desc='$adjh_desc',
								  adjh_inputby='$user_id',adjh_inputdate='$datetime', adjh_status=10") or die ("Gagal query".mysql_error());
				if($qrySave){
					$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM adjusment"));
					$adjh_id = $data->id;
					$query = mysql_query("SELECT * FROM adjusment_temp WHERE adjd_inputby='$user_id' ");
					while($dt=mysql_fetch_object($query)){
						$qrySave=mysql_query("INSERT INTO adjusment_detail SET adjd_hid='$adjh_id', adjd_prodid='$dt->adjd_prodid', 
											  adjd_qty='$dt->adjd_qty'") or die ("Gagal query".mysql_error());
					}
					$qd = mysql_query("DELETE FROM adjusment_temp WHERE adjd_inputby='$user_id'");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan data penyesuaian No. $adjh_no', user_date='$date', user_time='$time'");
							  
			}elseif(!empty($adjh_id) AND ($_REQUEST['Add_Detail'] <> "Tambah")){
				$qrySave=mysql_query("UPDATE adjusment SET adjh_date='$adjh_date', adjh_desc='$adjh_desc' WHERE id='$adjh_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
					foreach($id_det as $i => $id)
					{
						$adjd_qty		= $_REQUEST['adjdqty'][$id];
						mysql_query("UPDATE adjusment_detail SET adjd_qty='$adjd_qty' WHERE id='$id' ");
					}
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah data penyesuaian No. $adjh_no', user_date='$date', user_time='$time'");
				}							  
			}				
			if($qrySave){
				$message[] = "Data Sukses tersimpan !";	
				$status = "OK";
			}
		}
	}	
}	
if($Act =="DETAIL"){	
	$qd = mysql_query("SELECT * FROM adjusment WHERE adjh_status <> 100 AND id='$adjh_id'");
	$dd = mysql_fetch_object($qd);		
	$adjh_id 		= $dd->id;			
	$adjh_no		= $dd->adjh_no;
	$adjh_date		= $dd->adjh_date;
	$adjh_desc  	= $dd->adjh_desc;
}
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE adjusment SET adjh_status=100 WHERE id='$adjh_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus data penyesuaian No. $adjh_no', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}	
}	
if($Act == "POSTING"){
	#Update Status
	$qd = mysql_query("UPDATE adjusment SET adjh_status=20 WHERE id='$adjh_id'");
	#Insert History User
	$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting data penyesuaian No. $adjh_no', user_date='$date', user_time='$time'");
	$message[] = "Data Sukses diposting !";	
	$status = "OK";	
}
if(($Act_Det == "ADD_DET") && (empty($inch_id))){ 
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO adjusment_temp SET adjd_prodid='$adjd_prodid', adjd_qty='$adjd_qty', adjd_inputby='$user_id' ");
	}
}elseif(($Act_Det == "ADD_DET") && (!empty($inch_id))){
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO adjusment_detail SET adjd_hid='$adjh_id', adjd_prodid='$adjd_prodid', adjd_qty='$adjd_qty'");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($inch_id))){
	mysql_query("DELETE FROM adjusment_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($inch_id))){
	mysql_query("DELETE FROM adjusment_detail WHERE id='$id_deth'");
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_penyesuaian.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Penyesuaian'>";
}
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-Penyesuaian'>";	
		exit;
	}
}	
?>
