<?php
# Baca Variabel Form
$message = array();
$flag =0;
$month 		 = substr($start_date,5,2);
$year		 = substr($start_date,0,4);
$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'] : getreqno(1);
$trf_reqdate = isset($_REQUEST['trf_reqdate']) ? $_REQUEST['trf_reqdate'] : '';
$trf_deptid	 = isset($_REQUEST['trf_deptid']) ? $_REQUEST['trf_deptid'] : '';
$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'] : '';
$id_temp	 = isset($_REQUEST['id_temp']) ? $_REQUEST['id_temp'] : '';
$val_date = get_validationdate($trf_spbdate);
if(empty($trf_id)){ 
	$trf_spbno  = getspbno(1);
}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'] : '';
$prodprice 	  = isset($_REQUEST['prodprice']) ? $_REQUEST['prodprice'] : '';
$doctype 	  = isset($_REQUEST['doctype']) ? $_REQUEST['doctype'] : '';
$docno	 	  = isset($_REQUEST['docno']) ? $_REQUEST['docno'] : '';
$docdate 	  = isset($_REQUEST['docdate']) ? $_REQUEST['docdate'] : '';
$whid	 	  = isset($_REQUEST['whid']) ? $_REQUEST['whid'] : 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_desc='$trf_desc', trf_inputby='$user_id',trf_inputdate='$datetime', trf_status=20, trf_category=1") 
								  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=1 ORDER BY id ASC ");
					while($dt=mysql_fetch_object($query)){
						$qrySave=mysql_query("INSERT INTO transfer_detailexbc SET trfd_hid='$trf_id', trfd_prodid='$dt->trfd_prodid', 
											  trfd_qty='$dt->trfd_qty', trfd_desc='$dt->trfd_desc', trfd_price_satuan='$dt->trfd_price', trfd_whid='$dt->trfd_whid',
											  trfd_doctype='$dt->trfd_doctype', trfd_docno='$dt->trfd_docno', trfd_docdate='$dt->trfd_docdate', trfd_status=10")
											  or die ("Gagal query".mysql_error());
					}
					$qd = mysql_query("DELETE FROM transfer_temp WHERE trfd_inputby='$user_id' AND trfd_category=1");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi transfer 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_desc='$trf_desc', trf_status=20 WHERE id='$trf_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
					#delete temp
					#mysql_query("DELETE FROM transfer_temp WHERE trfd_hid='$trf_id'");
					foreach($id_det as $i => $id)
					{
						$trfd_prodid			= $_REQUEST['trfd_prodid'][$id];
						$trfd_qty				= $_REQUEST['trfd_qty'][$id];
						$trfd_price_satuan		= $_REQUEST['trfd_price_satuan'][$id];
						$trfd_desc				= $_REQUEST['trfd_desc'][$id];
						mysql_query("UPDATE transfer_detailexbc SET trfd_qty='$trfd_qty', trfd_desc='$trfd_desc', trfd_price_satuan='$trfd_price_satuan' WHERE id='$id' ");
						#insert temp
						#mysql_query("INSERT INTO transfer_temp SET trfd_qty='$trfd_qty', trfd_hid='$trf_id', trfd_prodid='$trfd_prodid', trfd_category=1");
					}
					#delete exbc
					#mysql_query("DELETE FROM transfer_detailexbc WHERE trfd_hid='$trf_id'");	
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi transfer 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_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 transfer bahan baku No. $trf_spbno', user_date='$date', 
		         user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}
if($Act == "POSTING"){
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "OK";
	}else{		
		$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 transfer 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{
		#Cek FIFO 1
		mysql_query("INSERT INTO transfer_temp SET trfd_prodid='$trfd_prodid', trfd_qty='$trfd_qty', trfd_desc='$trfd_desc', trfd_inputby='$user_id', 
		trfd_category=1, trfd_price='$prodprice', trfd_doctype='$doctype', trfd_docno='$docno', trfd_docdate='$docdate', trfd_whid='$whid'");
	}
}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_detailexbc SET trfd_hid='$trf_id', trfd_prodid='$trfd_prodid', trfd_qty='$trfd_qty', trfd_desc='$trfd_desc', 
		trfd_price_satuan='$prodprice',trfd_doctype='$doctype', trfd_docno='$docno', trfd_docdate='$docdate', trfd_status=10, trfd_whid='$whid'");
	}
}
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_detailexbc WHERE id='$id_deth'");
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_inc_prod.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Inc-Prod'>";
}
#Upload
if($Act == "UPL" ){
	require_once "upload_inc_prod.php";
}
if($Act == "PROCESS"){
	if(!empty($_FILES['userfile']['tmp_name'])){
		$data 	= new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
		$baris 	= $data->rowcount($sheet_index=0);
		for ($i=2; $i<=$baris; $i++)
		{
		  $id 		 	= $data->val($i, 1);
		  $trf_spbdate 	= date("Y-m-d",strtotime($data->val($i, 2)));
		  $trf_reqno 	= $data->val($i, 3);
		  $trf_reqdate 	= date("Y-m-d",strtotime($data->val($i, 4)));
		  $trf_pic 		= $data->val($i, 5);
		  $trfd_prodcode = $data->val($i, 6);
		  $trfd_qty  	= $data->val($i, 7);
		  $trf_desc 	= $data->val($i, 8);
		  $trf_nomor 	= $data->val($i, 9);
		  if(!empty($id)){
		  	  if($trf_reqdate=="1970-01-01"){
			  	$reqdate = "0000-00-00";
			  }else{
			  	$reqdate = $trf_reqdate;
			  }
			  mysql_query("INSERT INTO transfer_log SET trf_spbdate='$trf_spbdate',trf_reqno='$trf_reqno',trf_reqdate='$reqdate',trf_pic='$trf_pic',trf_deptid=1,trf_category=1,
						   trfd_prodcode='$trfd_prodcode',trfd_qty=$trfd_qty, trf_desc='$trf_desc', trf_status=10, trf_inputby='$user_id', 
						   trf_inputdate='$datetime', trf_nomor='$trf_nomor'");	
		  } 
		}
	}
	require_once "upload_inc_prod.php";
}
if($Act == "INV"){
	if(!empty($trf_id)){
		foreach ( $trf_id as $i => $val){
			mysql_query("UPDATE transfer_log SET trf_status=20 WHERE id='$val' ");
		}
	  $flag=1;			
	}else{
		$Act ="UPL";
		require_once "upload_inc_prod.php";
	}
	if($flag==1){
		$q = mysql_query("SELECT trf_nomor, trf_spbdate, trf_reqno, trf_reqdate, trf_deptid, trf_pic, trf_desc, trf_category
						  FROM transfer_log WHERE trf_status=20
						  GROUP BY trf_nomor
						  ORDER BY trf_nomor ASC");
		while($data=mysql_fetch_object($q)){
			$year 		= substr($data->trf_spbdate,0,4);
			$month 		= substr($data->trf_spbdate,5,2);
			$trf_spbno = get_uploadspbno(1,$year,$month);
			
			mysql_query("INSERT INTO transfer SET trf_spbno='$trf_spbno', trf_spbdate='$data->trf_spbdate', trf_reqno='$data->trf_reqno', 
						  trf_reqdate='$data->trf_reqdate', trf_deptid='$data->trf_deptid', trf_pic='$data->trf_pic',
						  trf_desc='$data->trf_desc', trf_inputdate='$datetime', trf_status=20, trf_category='$data->trf_category', 
						  trf_inputby='$user_id', trf_nomor='$data->trf_nomor'");
			#History
			mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi transfer bahan baku No. $trf_spbno', 
						 user_date='$date', user_time='$time'");
		}
		$flag=2;
	}
	if($flag==2){
		$qd = mysql_query("SELECT a.id, b.trfd_prodcode, b.trfd_qty, b.trfd_desc
							FROM transfer a
							INNER JOIN transfer_log b ON b.trf_nomor=a.trf_nomor
							WHERE b.trf_status=20
							ORDER BY a.id ASC");
		while($dt=mysql_fetch_object($qd)){
			#cek id
			$prod = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$dt->trfd_prodcode'"));
			#insert detail
			mysql_query("INSERT INTO transfer_detail SET trfd_hid='$dt->id',trfd_prodid='$prod[id]', trfd_qty='$dt->trfd_qty',trfd_desc='$dt->trfd_desc'");
		}		
		$flag=3;			
	}		
	if($flag==3){
		#update status
		mysql_query("UPDATE transfer_log SET trf_status=30 WHERE trf_status=20");
		#status
		$message[] = "Data Sukses diupload !";	
		$status ="OK";
	}
}
if($Act == "DEL_TEMP"){
	mysql_query("UPDATE transfer_log SET trf_status=100 WHERE trf_status=10");
	#redirect
	require_once "upload_inc_prod.php";
}
if($Act == "DELDET_TEMP"){
	mysql_query("UPDATE transfer_log SET trf_status=100 WHERE id='$id_temp'");
	#redirect
	require_once "upload_inc_prod.php";
}
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-Inc-Prod'>";	
		exit;
	}
}	
?>
