<?php
# Baca Variabel Form
$message = array();
$Act	 	 = isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	 = isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$trf_id 	 = isset($_REQUEST['trf_id']) ? $_REQUEST['trf_id'] : '';
$trf_spbdate = isset($_REQUEST['trf_spbdate']) ? $_REQUEST['trf_spbdate'] : '';
$trf_reqno 	 = isset($_REQUEST['trf_reqno']) ? $_REQUEST['trf_reqno'] : '';
$trf_reqdate = isset($_REQUEST['trf_reqdate']) ? $_REQUEST['trf_reqdate'] : '';
$trf_deptid	 = isset($_REQUEST['trf_deptid']) ? $_REQUEST['trf_deptid'] : '';
$trf_typeid	 = isset($_REQUEST['trf_typeid']) ? $_REQUEST['trf_typeid'] : '';
$trf_pic  	 = isset($_REQUEST['trf_pic']) ? $_REQUEST['trf_pic'] : '';
$trf_desc  	 = isset($_REQUEST['trf_desc']) ? $_REQUEST['trf_desc'] : '';
$id_det		 = isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth	 = isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$val_date    = get_validationdate($trf_spbdate);
if(empty($trf_id)){ 
	$trf_spbno  = getspbno(3);
}else{
	$trf_spbno	= isset($_REQUEST['trf_spbno']) ? $_REQUEST['trf_spbno'] : '';
}
$trfd_prodid  = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$trfd_qty     = isset($_REQUEST['trfdqty']) ? $_REQUEST['trfdqty'] : '';
$trfd_desc 	  = isset($_REQUEST['trfddesc']) ? $_REQUEST['trfddesc'] : '';
$prodstock 	  = isset($_REQUEST['prodstock']) ? $_REQUEST['prodstock'] : '';
$trfddoctype  = isset($_REQUEST['trfddoctype']) ? $_REQUEST['trfddoctype'] : '';
$trfddocno    = isset($_REQUEST['trfddocno']) ? $_REQUEST['trfddocno'] : '';
$trfddocdate  = isset($_REQUEST['trfddocdate']) ? $_REQUEST['trfddocdate'] : '';
$trfdwhid     = isset($_REQUEST['trfdwhid']) ? $_REQUEST['trfdwhid'] : 0;
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($trf_id)){
				$qrySave=mysql_query("INSERT INTO transfer SET trf_spbno='$trf_spbno', trf_spbdate='$trf_spbdate', trf_reqno='$trf_reqno', 
								  trf_reqdate='$trf_reqdate', trf_deptid='$trf_deptid', trf_pic='$trf_pic', trf_typeid='$trf_typeid', 
								  trf_desc='$trf_desc', trf_inputby='$user_id',trf_inputdate='$datetime', trf_status=20, trf_category=3") 
								  or die ("Gagal query".mysql_error());
				if($qrySave){
					$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM transfer"));
					$trf_id = $data->id;
					$query = mysql_query("SELECT * FROM transfer_temp WHERE trfd_inputby='$user_id' AND trfd_category=3 ORDER BY id ASC ");
					while($dt=mysql_fetch_object($query)){
						/*$qrySave=mysql_query("INSERT INTO transfer_detail SET trfd_hid='$trf_id', trfd_prodid='$dt->trfd_prodid', 
											  trfd_qty='$dt->trfd_qty', trfd_desc='$dt->trfd_desc'")
											  or die ("Gagal query".mysql_error());*/
						#insert exbc
					    mysql_query("INSERT INTO transfer_detailexbc SET trfd_hid='$trf_id', trfd_prodid='$dt->trfd_prodid', trfd_qty='$dt->trfd_qty', 
								 trfd_desc='Return dari produksi', trfd_doctype='$dt->trfd_doctype', trfd_docno='$dt->trfd_docno', trfd_docdate='$dt->trfd_docdate',
								 trfd_price='0', trfd_status=10, trfd_whid='$dt->trfd_whid'");
					}
					$qd = mysql_query("DELETE FROM transfer_temp WHERE trfd_inputby='$user_id' AND trfd_category=3");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi pengembalian bahan baku No. $trf_spbno', 
									  user_date='$date', user_time='$time'");
							  
			}elseif(!empty($trf_id) && ($Act_Det <> "ADD_DET")){
				$qrySave=mysql_query("UPDATE transfer SET trf_spbno='$trf_spbno', trf_spbdate='$trf_spbdate', trf_reqno='$trf_reqno', 
									  trf_reqdate='$trf_reqdate', trf_deptid='$trf_deptid', trf_pic='$trf_pic', trf_typeid='$trf_typeid',
									  trf_desc='$trf_desc', trf_status=20 WHERE id='$trf_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
					#delete exbc
					#mysql_query("DELETE FROM transfer_detailexbc WHERE trfd_hid='$trf_id'");
					foreach($id_det as $i => $id){
						$trfd_qty		= $_REQUEST['trfd_qty'][$id];
						$trfd_desc		= $_REQUEST['trfd_desc'][$id];
						$trfd_prodid    = $_REQUEST['trfd_prodid'][$id];
						$trfd_doctype   = $_REQUEST['trfd_doctype'][$id];
						$trfd_docno     = $_REQUEST['trfd_docno'][$id];
						$trfd_docdate   = $_REQUEST['trfd_docdate'][$id];
						$trfd_whid		= $_REQUEST['trfd_whid'][$id];
						#mysql_query("UPDATE transfer_detail SET trfd_qty='$trfd_qty', trfd_desc='$trfd_desc' WHERE id='$id' ");
						#insert exbc
					    mysql_query("UPDATE transfer_detailexbc SET trfd_qty='$trfd_qty', trfd_doctype='$trfd_doctype', 
						trfd_docno='$trfd_docno', trfd_docdate='$trfd_docdate', trfd_whid='$trfd_whid' WHERE id='$id'");
					}
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi pengembalian bahan baku No. $trf_spbno', 
										  user_date='$date', user_time='$time'");
				}							  
			}			
			if($qrySave){
				$message[] = "Data Sukses tersimpan !";	
				$status = "OK";
			}
		}	
	}
}
if($Act =="DETAIL"){
	$qd = mysql_query("SELECT * FROM transfer WHERE id='$trf_id'");
	$dd = mysql_fetch_object($qd);		
	$trf_id 		= $dd->id;			
	$trf_spbno		= $dd->trf_spbno;
	$trf_spbdate	= $dd->trf_spbdate;
	$trf_reqno 		= $dd->trf_reqno;
	$trf_reqdate	= $dd->trf_reqdate;
	$trf_deptid		= $dd->trf_deptid;
	$trf_typeid     = $dd->trf_typeid;
	$trf_pic   		= $dd->trf_pic;
	$trf_desc	    = $dd->trf_desc;
}
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE transfer SET trf_status=100 WHERE id='$trf_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus transaksi pengembalian bahan baku No. $trf_spbno', user_date='$date', 
		         user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}
if($Act == "POSTING"){
	#Update Status
	$qd = mysql_query("UPDATE transfer SET trf_status=20 WHERE id='$trf_id'");
	#Insert History User
	$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting transaksi pengembalian bahan baku No. $trf_spbno', user_date='$date', 
			 user_time='$time'");
	$message[] = "Data Sukses diposting !";	
	$status = "OK";	
}
if(($Act_Det == "ADD_DET") && (empty($trf_id))){ 
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO transfer_temp SET trfd_prodid='$trfd_prodid', trfd_qty='$trfd_qty', trfd_doctype='$trfddoctype', trfd_whid='$trfdwhid',
						trfd_docno='$trfddocno', trfd_docdate='$trfddocdate', trfd_desc='$trfd_desc', trfd_inputby='$user_id', trfd_category=3");
	}
}elseif(($Act_Det == "ADD_DET") && (!empty($trf_id))){
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO transfer_detail SET trfd_hid='$trf_id', trfd_prodid='$trfd_prodid', trfd_qty='$trfd_qty', trfd_doctype='$trfddoctype', 
						trfd_docno='$trfddocno', trfd_docdate='$trfddocdate', trfd_desc='$trfd_desc', trfd_whid='$trfdwhid'");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($trf_id))){
	mysql_query("DELETE FROM transfer_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($trf_id))){
	mysql_query("DELETE FROM transfer_detail WHERE id='$id_deth'");
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_prod_inc.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Prod-Inc'>";
}
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-Prod-Inc'>";	
		exit;
	}
}	
?>