<?php
# Baca Variabel Form
$message 	   = array();
$Act	 	   = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	   = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$kv_id 	   	   = isset($_REQUEST['kv_id']) ? $_REQUEST['kv_id'] : '';
$kv_date 	   = isset($_REQUEST['kv_date']) ? $_REQUEST['kv_date'] : '';
$kv_prodid     = isset($_REQUEST['kv_prodid']) ? $_REQUEST['kv_prodid'] : '';
$kv_prodcode   = isset($_REQUEST['kv_prodcode']) ? $_REQUEST['kv_prodcode'] : '';
$kv_prodname   = isset($_REQUEST['kv_prodname']) ? $_REQUEST['kv_prodname'] : '';
$kv_prodsat    = isset($_REQUEST['kv_prodsat']) ? $_REQUEST['kv_prodsat'] : '';
$kv_qty        = isset($_REQUEST['kv_qty']) ? $_REQUEST['kv_qty'] : '';
$kv_pic        = isset($_REQUEST['kv_pic']) ? $_REQUEST['kv_pic'] : '';
$kv_desc       = isset($_REQUEST['kv_desc']) ? $_REQUEST['kv_desc'] : '';
$id_det		   = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth	   = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
if(empty($kv_id)){
	$kv_no  = get_kvextno();
}else{
	$kv_no  = isset($_REQUEST['kv_no']) ? $_REQUEST['kv_no'] : '';
}
$kvd_prodid = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$prodstock  = isset($_REQUEST['prodstock']) ? $_REQUEST['prodstock'] : '';
$kvd_qty    = isset($_REQUEST['kvd_qty']) ? $_REQUEST['kvd_qty'] : '';
$val_date 	= get_validationdate($kv_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_konversi_external.php"; 
		exit;
	}else{
		if(count($message)==0){	
			if(empty($kv_id)){
				$qrySave=mysql_query("INSERT INTO konversi_external SET kv_no='$kv_no', kv_date='$kv_date', kv_prodid='$kv_prodid', kv_qty='$kv_qty', kv_desc='$kv_desc', kv_pic='$kv_pic',	
						 kv_inputby='$user_id',kv_inputdate='$datetime', kv_status=20, kv_type='KONVERSI'") or die ("Gagal query".mysql_error()); 
				if($qrySave){
					$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM konversi_external"));
					$kv_id = $data->id;
					$query = mysql_query("SELECT * FROM konversi_external_temp WHERE kvd_inputby='$user_id' AND kvd_type='KONVERSI'");
					while($dt=mysql_fetch_object($query)){
						$qrySave=mysql_query("INSERT INTO konversi_external_detail SET kvd_hid='$kv_id', kvd_prodid='$dt->kvd_prodid', kvd_qty='$dt->kvd_qty'") 
								 or die ("Gagal query".mysql_error());
					}
					$qd = mysql_query("DELETE FROM konversi_external_temp WHERE kvd_inputby='$user_id' AND kvd_type='KONVERSI'");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id',user_act='Menambahkan proses konversi ext no : $kv_no', user_date='$date', user_time='$time'");
			}elseif(!empty($kv_id) && ($Act_Det <> "ADD_DET")){
				$qrySave=mysql_query("UPDATE konversi_external SET kv_date='$kv_date', kv_prodid='$kv_prodid', kv_qty='$kv_qty', kv_desc='$kv_desc', kv_pic='$kv_pic', kv_status=20 WHERE id='$kv_id'") 
				or die ("Gagal query".mysql_error());
				if($qrySave){
					foreach($id_det as $i => $id)
					{
						$kvd_qty		= $_REQUEST['kvdqty'][$id];
						mysql_query("UPDATE konversi_external_detail SET kvd_qty='$kvd_qty' WHERE id='$id' ");
					}
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah proses konversi ext no : $kv_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 konversi_external a
					   INNER JOIN product b ON b.id=a.kv_prodid
					   WHERE a.kv_status <> 100 AND a.id='$kv_id'");
	$dd = mysql_fetch_object($qd);		
	$kv_id 		= $dd->id;			
	$kv_no	 	= $dd->kv_no;
	$kv_date  	= $dd->kv_date;
	$kv_prodid 	= $dd->kv_prodid;
	$kv_prodcode 	= $dd->prod_code;
	$kv_prodname	= $dd->prod_name;
	$kv_prodsat  	= $dd->prod_sat;
	$kv_qty  	= $dd->kv_qty;
	$kv_pic  	= $dd->kv_pic;
	$kv_desc  	= $dd->kv_desc;
}	
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE konversi_external SET kv_status=100 WHERE id='$kv_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus proses konversi ext no : $kv_no', 
		         user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
			
	}
}
if($Act == "POSTING"){
	#Update Status
	$qd = mysql_query("UPDATE konversi_external SET kv_status=20 WHERE id='$kv_id'");
	#Insert History User
	$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting proses konversi ext no. $kv_no', user_date='$date', user_time='$time'");
	$message[] = "Data Sukses diposting !";	
	$status = "OK";				
}
if(($Act_Det == "ADD_DET") && (empty($kv_id))){ 
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_konversi_external.php";
		exit;
	}else{
		mysql_query("INSERT INTO konversi_external_temp SET kvd_prodid='$kvd_prodid', kvd_qty='$kvd_qty', kvd_inputby='$user_id', kvd_type='KONVERSI'");
	}
}elseif(($Act_Det == "ADD_DET") && (!empty($kv_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_konversi_external.php";
		exit;
	}else{
		mysql_query("INSERT INTO konversi_external_detail SET kvd_hid='$kv_id', kvd_prodid='$kvd_prodid', kvd_qty='$kvd_qty'");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($kv_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_konversi_external.php";
		exit;
	}else{
		mysql_query("DELETE FROM konversi_external_temp WHERE id='$id_deth' AND kvd_type='KONVERSI'");
	}
}elseif(($Act_Det == "DELETE_DET") && (!empty($kv_id))){
	if($val_date=="NO"){
		echo "<font color='red'>Periode :$start_date s/d $end_date belum closing</font><br>";
		#reload
		require_once "add_konversi_external.php";
		exit;
	}else{
		mysql_query("DELETE FROM konversi_external_detail WHERE id='$id_deth'");
	}
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_konversi_external.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Konversi-External'>";
}
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-Konversi-External'>";	
		exit;
	}
}	
?>