<?php
$message 	 	= array();
$Act         	= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$nomor          = isset($_REQUEST['nomor']) ? $_REQUEST['nomor'] : '';
$no_bpb   		= isset($_REQUEST['no_bpb']) ? $_REQUEST['no_bpb'] : '';
$tgl_bpb   		= isset($_REQUEST['tgl_bpb']) ? $_REQUEST['tgl_bpb'] : '';
$pemasok 		= isset($_REQUEST['pemasok']) ? $_REQUEST['pemasok'] : '';
$no_invoice     = isset($_REQUEST['no_invoice']) ? $_REQUEST['no_invoice'] : '';
$tgl_invoice 	= isset($_REQUEST['tgl_invoice']) ? $_REQUEST['tgl_invoice'] : '';
$no_do 		    = isset($_REQUEST['no_do']) ? $_REQUEST['no_do'] : '';
$catatan 		= isset($_REQUEST['catatan']) ? $_REQUEST['catatan'] : '';
$jenis_dokpab 	= isset($_REQUEST['jenis_dokpab']) ? $_REQUEST['jenis_dokpab'] : '';
$no_dokpab 		= isset($_REQUEST['no_dokpab']) ? $_REQUEST['no_dokpab'] : '';
$tgl_dokpab 	= isset($_REQUEST['tgl_dokpab']) ? $_REQUEST['tgl_dokpab'] : '';
$pilih			= isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
$pilih_reg		= isset($_REQUEST['pilih_reg']) ? $_REQUEST['pilih_reg'] : '';
$start_date		= isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : get_startdate();
$end_date		= isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : get_enddate();
$new_nomor 		= get_nomorinc(1);
$new_nobpb 		= get_nobpbinc(1);
//echo "$new_nomor==>$new_nobpb";
$Act_Det 	 	= isset($_REQUEST['Act_Det']) ? $_REQUEST['Act_Det'] : '';
$inch_id 	 	= isset($_REQUEST['inch_id']) ? $_REQUEST['inch_id'] : '';
$inch_bpbdate	= isset($_REQUEST['inch_bpbdate']) ? $_REQUEST['inch_bpbdate'] : '';
$sup_id			= isset($_REQUEST['sup_id']) ? $_REQUEST['sup_id'] : '';
$sup_name		= isset($_REQUEST['sup_name']) ? $_REQUEST['sup_name'] : '';
$inch_invno  	= isset($_REQUEST['inch_invno']) ? $_REQUEST['inch_invno'] : '';
$inch_invdate	= isset($_REQUEST['inch_invdate']) ? $_REQUEST['inch_invdate'] : '';
$inch_dono		= isset($_REQUEST['inch_dono']) ? $_REQUEST['inch_dono'] : '';
$inch_docid	 	= isset($_REQUEST['inch_docid']) ? $_REQUEST['inch_docid'] : '';
$inch_purpid	= isset($_REQUEST['inch_purpid']) ? $_REQUEST['inch_purpid'] : '';
$inch_docdate	= isset($_REQUEST['inch_docdate']) ? $_REQUEST['inch_docdate'] : '';
$inch_docno		= isset($_REQUEST['inch_docno']) ? $_REQUEST['inch_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'] : '';
$doc			= mysql_fetch_array(mysql_query("SELECT doc_type FROM document_type WHERE id='$inch_docid'"));
$sup			= mysql_fetch_array(mysql_query("SELECT sup_name FROM supplier WHERE id='$sup_id'"));
$val_date       = get_validationdate($inch_docdate);
$po_no	 	 	= isset($_REQUEST['po_no']) ? $_REQUEST['po_no'] : '';
$inch_currency 	= isset($_REQUEST['inch_currency']) ? $_REQUEST['inch_currency'] : '';
$inch_rate	 	= isset($_REQUEST['inch_rate']) ? $_REQUEST['inch_rate'] : get_rate($inch_currency,$inch_bpbdate);
$pilih			= isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
if(empty($inch_id)){ 
	$inch_bpbno = get_bpbno();
}else{
	$inch_bpbno	= isset($_REQUEST['inch_bpbno']) ? $_REQUEST['inch_bpbno'] : '';
}
$incd_prodid    	= isset($_REQUEST['prodid']) ? $_REQUEST['prodid'] : '';
$incd_qty   		= isset($_REQUEST['incdqty']) ? $_REQUEST['incdqty'] : '';
$incd_price 		= isset($_REQUEST['incdprice']) ? $_REQUEST['incdprice'] : '';
$incd_currency  	= isset($_REQUEST['incdcurrency']) ? $_REQUEST['incdcurrency'] : '';
$incd_weight  		= isset($_REQUEST['incdweight']) ? $_REQUEST['incdweight'] : '';
$incd_price_rate  	= isset($_REQUEST['n_rate']) ? $_REQUEST['n_rate'] : get_rate($incd_currency,$inch_bpbdate);
$incd_price_usd  	= 0;
//if($incd_currency=="IDR"){
	//$incd_amount	= isset($_REQUEST['incdamount']) ? $_REQUEST['incdamount'] : '';
//}else{
	$incd_amount	= $incd_qty * $incd_price;
//}
if($Act =="SAVE"){
	if(count($message)==0){	
		$qrySave=mysql_query("UPDATE incoming SET inch_status=20, inch_picterima='$user_id', inch_bpbdate='$inch_bpbdate', tgl_terimagudang='$datetime' WHERE id='$inch_id'");
		#Update Detail
		foreach($id_det as $i => $id){
			$incd_qty		    = $_REQUEST['incd_qty'][$id];
			$incd_qty_temp	    = $_REQUEST['incd_qty_temp'][$id];
			$price			    = $_REQUEST['incd_price'][$id];
			#$incd_whid		    = $_REQUEST['incd_whid'][$id];
			if($incd_qty != $incd_qty_temp){
				$incd_price		= ($price/$incd_qty)*$incd_qty_temp;
			}else{
				$incd_price		= $price; 
			}
			mysql_query("UPDATE incoming_detail SET incd_qty='$incd_qty_temp', incd_qty_temp='$incd_qty', incd_price='$incd_price', incd_price_temp='$price' WHERE id='$id' ");
		}		
		#Insert History User
		mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Terima barang No. $inch_bpbno', user_date='$date', user_time='$time'");
	}							  
	if($qrySave){
		$message[] = "Data Sukses tersimpan";	
		$status = "OK";
	}	
}
if($Act =="DETAIL"){
	$qd = mysql_query("SELECT a.*,CASE WHEN ifnull(a.inch_supid,0)=0 THEN nm_pemasok ELSE b.sup_name END as pemasok 
					   FROM incoming a LEFT JOIN supplier b ON b.id=a.inch_supid WHERE a.id='$inch_id'");
	$dd=mysql_fetch_array($qd);		
	$inch_id 		= $dd['id'];			
	$inch_bpbno		= $dd['inch_bpbno'];
	$inch_bpbdate	= $dd['inch_bpbdate'];
	$sup_id 		= $dd['inch_supid'];
	$sup_name		= $dd['pemasok'];
	$inch_invno		= $dd['inch_invno'];
	$inch_invdate   = $dd['inch_invdate'];
	$inch_doctype   = $dd['inch_doctype'];
	$inch_dono	    = $dd['inch_dono'];
	$inch_purpid	= $dd['inch_purpid'];
	$inch_docno		= $dd['inch_docno'];
	$inch_docdate	= $dd['inch_docdate'];
	$no_aju			= $dd['no_aju'];
	$tgl_aju		= $dd['tgl_aju'];
	$po_no			= $dd['po_no'];
}
if($Act=="DETAIL"){
	require "add_terima_barang.php";
}
if($Act=="CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Terima-Barang'>";
}
#Messages
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-Terima-Barang'>";	
		exit;
	}
}
?>