<?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_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);
$trf_id2 	 = isset($_REQUEST['trf_id2']) ? $_REQUEST['trf_id2'] : '';
if(empty($trf_id)){ 
	$trf_spbno  = getspbno(4);
}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'] : '';
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=10, trf_category=4") 
								  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=4 ");
					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());
					}
					$qd = mysql_query("DELETE FROM transfer_temp WHERE trfd_inputby='$user_id' AND trfd_category=4");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi transfer barang jadi 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' WHERE id='$trf_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
					foreach($id_det as $i => $id)
					{
						$trfd_qty		= $_REQUEST['trfd_qty'][$id];
						$trfd_desc		= $_REQUEST['trfd_desc'][$id];
						mysql_query("UPDATE transfer_detail SET trfd_qty='$trfd_qty', trfd_desc='$trfd_desc' WHERE id='$id' ");
					}
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi transfer barang jadi 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 barang jadi No. $trf_spbno', user_date='$date', 
		         user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}
if($Act == "POSTING"){
	/*$q =mysql_query("SELECT a.trfd_hid, b.trf_spbdate, a.trfd_prodid, a.trfd_qty 
		FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
		WHERE a.trfd_hid='$trf_id'");
	while($data=mysql_fetch_object($q)){
		#Insert pengeluaran bahan baku
		mysql_query("INSERT INTO stock SET stock_trid='$data->trfd_hid', stock_date='$data->trf_spbdate', stock_prodid='$data->trfd_prodid', stock_qty='$data->trfd_qty',   
			         stock_status=30, stock_loc=1");
		#Insert pemasukan wip
		mysql_query("INSERT INTO stock SET stock_trid='$data->trfd_hid', stock_date='$data->trf_spbdate', stock_prodid='$data->trfd_prodid', stock_qty='$data->trfd_qty',   
			         stock_status=20, stock_loc=2");			 
		$sts =1;
	}
	if($sts==1){
		#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 transfer bahan baku No. $trf_spbno', user_date='$date', 
		         user_time='$time'");
		$message[] = "Data Sukses diposting !";	
		$status = "OK";				
	}*/
	#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 transfer barang jadi No. $trf_spbno', 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 transfer_temp SET trfd_prodid='$trfd_prodid', trfd_qty='$trfd_qty', trfd_desc='$trfd_desc', trfd_inputby='$user_id', trfd_category=4");
	}
}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 transfer_detail SET trfd_hid='$trf_id', trfd_prodid='$trfd_prodid', trfd_qty='$trfd_qty', trfd_desc='$trfd_desc'");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($inch_id))){
	mysql_query("DELETE FROM transfer_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($inch_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_outs.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Prod-OutS'>";
}
#Upload
if($Act == "UPL" ){
	require_once "upload_prod_out.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_pic 		= $data->val($i, 3);
		  $trfd_prodcode = $data->val($i, 4);
		  $trfd_qty  	= $data->val($i, 5);
		  $trf_desc 	= $data->val($i, 6);
		  $trf_nomor 	= $data->val($i, 7);
		  if(!empty($id)){
			  mysql_query("INSERT INTO transfer_log SET trf_spbdate='$trf_spbdate',trf_pic='$trf_pic',trf_deptid=4,trf_category=4,
						   trfd_prodcode='$trfd_prodcode',trfd_qty=$trfd_qty, trf_desc='$trf_desc', trf_status=1000, trf_inputby='$user_id', 
						   trf_inputdate='$datetime', trf_nomor='$trf_nomor'");	
		  } 
		}
	}
	require_once "upload_prod_out.php";
}
/*if($Act == "INV"){
	if(!empty($trf_id)){
		foreach ( $trf_id as $i => $val){
			mysql_query("UPDATE transfer_log SET trf_status=2000 WHERE id='$val' ");
		}
	  $flag=1;			
	}else{
		$Act ="UPL";
		require_once "upload_prod_out.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=2000
						  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(4,$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 barang jadi 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=2000
							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=3000 WHERE trf_status=2000");
		#status
		$message[] = "Data Sukses diupload !";	
		$status ="OK";
	}
}*/
if($Act == "INV"){
	if(!empty($trf_id2)){
		foreach ( $trf_id2 as $i => $val){
			//mysql_query("UPDATE transfer_log SET trf_status=2000 WHERE id='$val' AND trf_category=4 ");
			 $trf_spbdate2	= $_REQUEST['trf_spbdate2'][$val];
			 $trfd_prodid2	= $_REQUEST['trfd_prodid2'][$val];
			 $trfd_qty2		= $_REQUEST['trfd_qty2'][$val];
			 $trfd_price2	= 0; //$_REQUEST['trfd_price2'][$val];
			 $rmn_qty2		= $_REQUEST['rmn_qty2'][$val];
			 //$baris			= $_REQUEST['baris'][$val];
			 $val_date2      = get_validationdate($trf_spbdate2);
			 if($val_date2=="NO"){
				$message[] = "Baris :$val Periode :$start_date s/d $end_date belum closing";
				$status = "NO";
				$Act ="ADD";
				$flag=0;
			 }
			 if($trfd_qty2 <= 0) {
				 $message[] = "Baris :$val Jumlah transfer kosong/minus";
				 $status = "NO";
				 $Act ="ADD";
				 $flag=0;
			 }	
			 if($trfd_qty2 > $rmn_qty2){
				 $message[] = "Baris :$val Jumlah transfer lebih besar dari jumlah sisa";
				 $status = "NO";
				 $Act ="ADD";
				 $flag=0;
			 }	   
		}
		if (count($message)==0 ){
			foreach ( $trf_id2 as $i => $val){
				 $trf_spbdate2	= $_REQUEST['trf_spbdate2'][$val];
				 $trfd_prodid2	= $_REQUEST['trfd_prodid2'][$val];
				 $trfd_qty2		= $_REQUEST['trfd_qty2'][$val];
				 $trfd_price2	= 0; //$_REQUEST['trfd_price2'][$val];
				 $rmn_qty2		= $_REQUEST['rmn_qty2'][$val];
				 $trfd_price_satuan2	= 0; //$_REQUEST['trfd_price_satuan2'][$val];
				 $trfd_price_rate2		= 0; //$_REQUEST['trfd_price_rate2'][$val];
				 $trfd_price_usd2		= 0; //$_REQUEST['trfd_price_usd2'][$val];
				 $trfd_currency2		= 0; //$_REQUEST['trfd_currency2'][$val];
				 $trf_nomor2 	= (int)("".substr($trf_spbdate2,0,4)."".substr($trf_spbdate2,5,2)."".substr($trf_spbdate2,8,2)."");
				 mysql_query("INSERT INTO transfer_log SET trf_spbdate='$trf_spbdate2',trf_pic='$user_name',trf_deptid=8,trf_category=4,
							   trfd_prodid='$trfd_prodid2',trfd_qty='$trfd_qty2', trf_desc='Transfer otomatis', trf_status=2000, trf_inputby='$user_id', 
							   trf_inputdate='$datetime', trf_nomor='$trf_nomor2', trfd_lapid='$val', trfd_price='$trfd_price2',
							   trfd_price_satuan='$trfd_price_satuan2', trfd_price_rate='$trfd_price_rate2', trfd_price_usd='$trfd_price_usd2',
							   trfd_currency='$trfd_currency2'  ");	
				 $flag=1;		
			}
		}
	}else{
		$Act ="ADD";
		require_once "add_prod_outs.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=2000 AND trf_category=4
						  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(4,$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=10, 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 barang jadi No. $trf_spbno', 
						 user_date='$date', user_time='$time'");
		}
		$flag=2;
	}
	if($flag==2){
		$qd = mysql_query("SELECT a.id, b.trfd_prodid, b.trfd_qty, b.trfd_desc, b.trfd_lapid, a.trf_spbno, b.trf_nomor, b.trfd_price,
							b.trfd_price_satuan, b.trfd_price_rate, b.trfd_price_usd, b.trfd_currency
							FROM transfer a
							INNER JOIN transfer_log b ON b.trf_nomor=a.trf_nomor
							WHERE b.trf_status=2000 AND a.trf_status=10 AND b.trf_category=4 
							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='$dt->trfd_prodid', trfd_qty='$dt->trfd_qty',trfd_desc='$dt->trfd_desc',
			trfd_lapid='$dt->trfd_lapid', trfd_price='$dt->trfd_price', trfd_price_satuan='$dt->trfd_price_satuan', trfd_price_rate='$dt->trfd_price_rate',
			trfd_price_usd='$dt->trfd_price_usd', trfd_currency='$dt->trfd_currency'");			
		}		
		$flag=3;			
	}		
	if($flag==3){
		#update status
		mysql_query("UPDATE transfer_log SET trf_status=3000 WHERE trf_status=2000  AND trf_category=4");
		#update status
		mysql_query("UPDATE transfer SET trf_status=20 WHERE trf_status=10");
		#status
		$message[] = "Data Sukses ditransfer !";	
		$status ="OK";
	}
}
if($Act == "DEL_TEMP"){
	mysql_query("UPDATE transfer_log SET trf_status=100 WHERE trf_status=1000");
	#redirect
	require_once "upload_prod_out.php";
}
if($Act == "DELDET_TEMP"){
	mysql_query("UPDATE transfer_log SET trf_status=100 WHERE id='$id_temp'");
	#redirect
	require_once "upload_prod_out.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-Prod-OutS'>";	
		exit;
	}elseif ($status=="NO"){	
		#redirect
		require_once "add_prod_outs.php";
	}
}	
?>