<?php
# Baca Variabel Form
$message = array();
$Act	 	 	= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$Act_Det 	 	= isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$outh_id 	 	= isset($_REQUEST['outh_id']) ? $_REQUEST['outh_id'] : '';
$outh_sjdate	= isset($_REQUEST['outh_sjdate']) ? $_REQUEST['outh_sjdate'] : '';
$cust_id    	= isset($_REQUEST['cust_id']) ? $_REQUEST['cust_id'] : '';
$cust_name   	= isset($_REQUEST['cust_name']) ? $_REQUEST['cust_name'] : '';
$outh_invno  	= isset($_REQUEST['outh_invno']) ? $_REQUEST['outh_invno'] : '';
$outh_invdate	= isset($_REQUEST['outh_invdate']) ? $_REQUEST['outh_invdate'] : '';
$outh_dono		= isset($_REQUEST['outh_dono']) ? $_REQUEST['outh_dono'] : '';
$outh_purpid	= isset($_REQUEST['outh_purpid']) ? $_REQUEST['outh_purpid'] : '';
$outh_docid	 	= isset($_REQUEST['outh_docid']) ? $_REQUEST['outh_docid'] : '';
$outh_docdate	= isset($_REQUEST['outh_docdate']) ? $_REQUEST['outh_docdate'] : '';
$outh_docno		= isset($_REQUEST['outh_docno']) ? $_REQUEST['outh_docno'] : '';
$id_det			= isset($_REQUEST['id_det']) ? $_REQUEST['id_det'] : '';
$id_deth		= isset($_REQUEST['id_deth']) ? $_REQUEST['id_deth'] : '';
$no_aju			= isset($_REQUEST['no_aju']) ? $_REQUEST['no_aju'] : '';
$tgl_aju		= isset($_REQUEST['tgl_aju']) ? $_REQUEST['tgl_aju'] : '';
$no_npe			= isset($_REQUEST['no_npe']) ? $_REQUEST['no_npe'] : '';
$tgl_npe		= isset($_REQUEST['tgl_npe']) ? $_REQUEST['tgl_npe'] : '';
$doc			= mysql_fetch_array(mysql_query("SELECT doc_type FROM document_type WHERE id='$outh_docid'"));
$cust			= mysql_fetch_array(mysql_query("SELECT cust_name FROM customer WHERE id='$cust_id'"));
$val_date       = get_validationdate($outh_sjdate);
if(empty($outh_id)){ 
	$outh_sjno = get_sjno();
}else{
	$outh_sjno = isset($_REQUEST['outh_sjno']) ? $_REQUEST['outh_sjno'] : '';
}
$outd_prodid      = isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$outd_qty   	  = isset($_REQUEST['outdqty']) ? $_REQUEST['outdqty'] : '';
$outd_price 	  = isset($_REQUEST['outdprice']) ? $_REQUEST['outdprice'] : '';
$price			  = $outd_qty*$outd_price;
$outd_currency    = isset($_REQUEST['outdcurrency']) ? $_REQUEST['outdcurrency'] : '';
$outd_weight  	  = isset($_REQUEST['outdweight']) ? $_REQUEST['outdweight'] : '';
$outd_stock  	  = 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($outh_id)){
				$qrySave=mysql_query("INSERT INTO outgoing SET outh_sjno='$outh_sjno', outh_sjdate='$outh_sjdate', outh_custid='$cust_id', 
								  outh_invno='$outh_invno', outh_invdate='$outh_invdate', outh_dono='$outh_dono',
								  outh_purpid='$outh_purpid', outh_docid='$outh_docid', outh_inputby='$user_id', sumber='MANUAL',
								  outh_inputdate='$datetime', outh_status=10, outh_docdate='$outh_docdate', outh_docno='$outh_docno',
								  no_aju='$no_aju', tgl_aju='$tgl_aju', no_npe='$no_npe', tgl_npe='$tgl_npe', outh_doctype='$doc[doc_type]', nm_penerima='$cust[cust_name]'") 
								  or die ("Gagal query".mysql_error());
				if($qrySave){
					$data = mysql_fetch_object(mysql_query("SELECT MAX(id) id FROM outgoing"));
					$outh_id = $data->id;
					$query = mysql_query("SELECT * FROM outgoing_temp WHERE outd_inputby='$user_id'");
					while($dt=mysql_fetch_object($query)){
						$qrySave=mysql_query("INSERT INTO outgoing_detail SET outd_hid='$outh_id', outd_prodid='$dt->outd_prodid', 
											  outd_qty='$dt->outd_qty', outd_price='$dt->outd_price', outd_currency='$dt->outd_currency', 
											  outd_weight='$dt->outd_weight', outd_wunit='$dt->outd_wunit'")
											  or die ("Gagal query".mysql_error());
					}
					$qd = mysql_query("DELETE FROM outgoing_temp WHERE outd_inputby='$user_id'");
				}	
				#Insert History User
				$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi pengeluaran No. $outh_sjno', 
						 user_date='$date', user_time='$time'");
			}elseif(!empty($outh_id) && ($Act_Det <> "ADD_DET")){
				$qrySave=mysql_query("UPDATE outgoing SET outh_sjno='$outh_sjno', outh_sjdate='$outh_sjdate', outh_custid='$cust_id', 
									  outh_invno='$outh_invno', outh_invdate='$outh_invdate', outh_dono='$outh_dono',
									  outh_purpid='$outh_purpid', outh_docid='$outh_docid', outh_docdate='$outh_docdate', outh_docno='$outh_docno',
									  no_aju='$no_aju', tgl_aju='$tgl_aju', no_npe='$no_npe', tgl_npe='$tgl_npe', outh_doctype='$doc[doc_type]', nm_penerima='$cust[cust_name]'
									  WHERE id='$outh_id'") or die ("Gagal query".mysql_error());
				if($qrySave){
				#Update Detail
					foreach($id_det as $i => $id)
					{
						$outd_qty		= $_REQUEST['outd_qty'][$id];
						$outd_price		= $_REQUEST['outd_price'][$id];
						$outd_weight	= $_REQUEST['outd_weight'][$id];
						mysql_query("UPDATE outgoing_detail SET outd_qty='$outd_qty', outd_price='$outd_price', outd_weight='$outd_weight' WHERE id='$id' ");
					}	
					#Insert History User
					$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Mengubah transaksi pengeluaran No. $outh_sjno', 
							 user_date='$date', user_time='$time'");
				}							  
			}				
			if($qrySave){
				$message[] = "Data Sukses tersimpan";	
				$status = "OK";
				
			}
		}	
	}
}		
if($Act =="DETAIL"){
	$qd = mysql_query("SELECT a.*,b.cust_name FROM outgoing a
					   INNER JOIN customer b ON b.id=a.outh_custid					   	   
					   WHERE a.outh_status <> 100 AND b.cust_status <> 100 
					   AND a.id='$outh_id'");
	$dd=mysql_fetch_object($qd);		
	$outh_id 		= $dd->id;			
	$outh_sjno		= $dd->outh_sjno;
	$outh_sjdate	= $dd->outh_sjdate;
	$cust_id 		= $dd->outh_custid;
	$cust_name		= $dd->cust_name;
	$outh_invno		= $dd->outh_invno;
	$outh_invdate   = $dd->outh_invdate;
	$outh_dono	    = $dd->outh_dono;
	$outh_purpid	= $dd->outh_purpid;
	$outh_docno		= $dd->outh_docno;
	$outh_docdate	= $dd->outh_docdate;
	$no_aju			= $dd->no_aju;
	$tgl_aju		= $dd->tgl_aju;
	$no_npe			= $dd->no_npe;
	$tgl_npe		= $dd->tgl_npe;
	if(isset($_REQUEST['flagdoc'])==1){
		$outh_docid		 = $_REQUEST['outh_docid'];
	}else{
		$outh_docid	    = $dd->outh_docid;
	}
}	
if($Act == "DELETE"){
	$qd = mysql_query("UPDATE outgoing SET outh_status=100 WHERE id='$outh_id'");
	if($qd){
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menghapus transaksi pengeluaran No. $outh_sjno', user_date='$date', user_time='$time'");
		$message[] = "Data Sukses terhapus !";	
		$status = "OK";
	}
}	
if($Act == "POSTING"){
	$q =mysql_query("SELECT a.outd_hid, b.outh_sjdate, a.outd_prodid, a.outd_qty FROM outgoing_detail a
					INNER JOIN outgoing b ON b.id=a.outd_hid
					WHERE a.outd_hid='$outh_id'");
	while($data=mysql_fetch_object($q)){
		$cekkat    = cek_kategori($data->outd_prodid);
		/*if($cekkat == 1 ){ #pengeluaran bahan baku
			mysql_query("INSERT INTO stock SET stock_trid='$data->outd_hid', stock_date='$data->outh_sjdate', stock_prodid='$data->outd_prodid', stock_qty='$data->outd_qty',   
			             stock_status=30, stock_loc=1");
		}elseif($cekkat == 2 ){ #pengeluaran barang modal
			mysql_query("INSERT INTO stock SET stock_trid='$data->outd_hid', stock_date='$data->outh_sjdate', stock_prodid='$data->outd_prodid', stock_qty='$data->outd_qty',   
			             stock_status=30, stock_loc=4");
		}elseif($cekkat == 3 ){ #pengeluaran barang jadi
			mysql_query("INSERT INTO stock SET stock_trid='$data->outd_hid', stock_date='$data->outh_sjdate', stock_prodid='$data->outd_prodid', stock_qty='$data->outd_qty',   
			             stock_status=30, stock_loc=3");
		}elseif($cekkat == 4 ){ #pengeluaran barang scrap
			mysql_query("INSERT INTO stock SET stock_trid='$data->outd_hid', stock_date='$data->outh_sjdate', stock_prodid='$data->outd_prodid', stock_qty='$data->outd_qty',   
			             stock_status=30, stock_loc=5");
		}elseif($cekkat == 5 ){ #pengeluaran wip
			mysql_query("INSERT INTO stock SET stock_trid='$data->outd_hid', stock_date='$data->outh_sjdate', stock_prodid='$data->outd_prodid', stock_qty='$data->outd_qty',   
			             stock_status=30, stock_loc=2");
		}*/
		$flag=1;
	}//exit;	
	if ($flag==1){
		#update status jadi posting
		$qd = mysql_query("UPDATE outgoing SET outh_status=20 WHERE id='$outh_id'");
		#Insert History User
		$itemp = mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Posting transaksi pengeluaran No. $outh_sjno', 
							  user_date='$date', user_time='$time'");
		$message[] = "Data Sukses diposting !";	
		$status = "OK";
	}
}
if(($Act_Det == "ADD_DET") && (empty($outh_id))){ 
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO outgoing_temp SET outd_prodid='$outd_prodid', outd_qty='$outd_qty', outd_price='$price', outd_currency='$outd_currency', 
					 outd_weight='$outd_weight', outd_wunit='Kg',outd_inputby='$user_id' ");
	}
}elseif(($Act_Det == "ADD_DET") && (!empty($outh_id))){
	if($val_date=="NO"){
		$message[] = "Periode :$start_date s/d $end_date belum closing";
		$status    = "NO";
	}else{
		mysql_query("INSERT INTO outgoing_detail SET outd_hid='$outh_id', outd_prodid='$outd_prodid', outd_qty='$outd_qty', outd_price='$price', 
					 outd_currency='$outd_currency', outd_weight='$outd_weight', outd_wunit='Kg'");
	}
}
if(($Act_Det == "DELETE_DET") && (empty($outh_id))){
	mysql_query("DELETE FROM outgoing_temp WHERE id='$id_deth'");
}elseif(($Act_Det == "DELETE_DET") && (!empty($outh_id))){
	mysql_query("DELETE FROM outgoing_detail WHERE id='$id_deth'");
}
if($Act == "ADD" || $Act == "DETAIL" || $Act_Det == "ADD_DET" || $Act_Det == "DELETE_DET"){
	require_once "add_pengeluaran.php";
}
if($Act == "CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Pengeluaran'>";
}
if (! count($message)==0 ){
	?>
		<script language="javascript">
			var pesan ='<?php 
							$Num=0;
							foreach ($message as $indeks=>$pesan_tampil){ 
								$Num++;
								echo $Num.'. '.$pesan_tampil.'\n';
							}
						?>';
			var status ='<?=$status;?>';			
			alert(pesan);
			//if(status == "SIMPAN"){
				//openwindow5('<?=$outh_id;?>');
			//}else{
				//self.history.back();
			//}
		</script>			
	<?php  
	if ($status=="OK"){	
		echo "<meta http-equiv='refresh' content='0; url=?page=Data-Pengeluaran'>";	
		exit;
	}
}	
?>