<?php
$message 	 	= array();
$Act         	= isset($_REQUEST['Act']) ? $_REQUEST['Act'] : '';
$pilih			= isset($_REQUEST['pilih']) ? $_REQUEST['pilih'] : '';
$pilih_reg		= isset($_REQUEST['pilih_reg']) ? $_REQUEST['pilih_reg'] : '';
#Proses
if($Act=="GET"){
	$data 	= new Spreadsheet_Excel_Reader($_FILES['userfile']['tmp_name']);
	$baris 	= $data->rowcount($sheet_index=0);
	for ($i=2; $i<=$baris; $i++)
	{
	  $message = array();
	  $no 		 	= $data->val($i, 1);
	  $no_bpb 		= $data->val($i, 2);
	  $tgl_bpb 		= date("Y-m-d",strtotime($data->val($i,3)));
	  $no_aju 		= $data->val($i, 4);
	  $tgl_aju 		= date("Y-m-d",strtotime($data->val($i,5)));
	  $nm_pemasok  = $data->val($i, 6);
	  $jenis_dokpab = $data->val($i, 7);
	  $no_dokpab  	= $data->val($i, 8);
	  $tgl_dokpab  	= date("Y-m-d",strtotime($data->val($i,9)));
	  $no_invoice	= $data->val($i, 10);
	  $tgl_invoice 	= date("Y-m-d",strtotime($data->val($i,11)));
	  $no_sppb		= $data->val($i, 12);
	  $tgl_sppb 	= date("Y-m-d",strtotime($data->val($i,13)));
	  $kode  		= $data->val($i, 14);
	  $nm_barang	= $data->val($i, 15);	  
	  $satuan  		= $data->val($i, 16);
	  $jumlah  		= $data->val($i, 17);
	  $nilai_nidr	= $data->val($i, 18);
	  $nilai_idr	= $data->val($i, 19);
	  $valuta  		= $data->val($i, 20);
	  $berat  		= $data->val($i, 21);
	  $no_urut  	= $data->val($i, 22);
	  $tujuan	  	= $data->val($i, 23);
	  $no_kontrak  	= $data->val($i, 24);
	  $tgl_kontrak 	= date("Y-m-d",strtotime($data->val($i,25)));
	  $tipe  		= $data->val($i, 26);
	  $ukuran  		= $data->val($i, 27);
	  $no_dokpab_asal  	= $data->val($i, 28);
	  $tgl_dokpab_asal  = $data->val($i, 29);
	  
	  if(!empty($no)){
		  mysql_query("INSERT INTO log_incoming SET no_bpb='$no_bpb', tgl_bpb='$tgl_bpb', kode='$kode', jumlah='$jumlah', valuta='$valuta', nilai='$nilai_idr', 
		  			   log_status=1000, log_input=now(), log_user='$user_id', nm_pemasok='".mysql_real_escape_string($nm_pemasok)."', jenis_dokpab='$jenis_dokpab', no_dokpab='$no_dokpab',
					   tgl_dokpab='$tgl_dokpab', berat='$berat', no_aju='$no_aju', tgl_aju='$tgl_aju', no_sppb='$no_sppb', tgl_sppb='$tgl_sppb',
					   no_invoice='$no_invoice', tgl_invoice='$tgl_invoice', no_urut='$no_urut', nm_barang='".mysql_real_escape_string($nm_barang)."', satuan='$satuan', non_rupiah='$nilai_nidr', 
					   tujuan='$tujuan', no_kontrak='$no_kontrak', tgl_kontrak='$tgl_kontrak', tipe='$tipe', ukuran='$ukuran', no_dokpab_asal='$no_dokpab_asal',
					   tgl_dokpab_asal='$tgl_dokpab_asal'"); 
	  }
	}
}
if($Act=="PROCESS"){
	if(!empty($pilih)){	
		foreach ( $pilih as $i => $val)
		{
			#Update status temp
			mysql_query("UPDATE log_incoming SET log_status=2000 WHERE id='$val' ");
		}
		require "add_upload_pemasukan.php";
	}else{
		require "add_upload_pemasukan.php";
	}	
}elseif($Act=="INV"){
	#Insert header pemasukan
	$qh=mysql_query("SELECT * FROM log_incoming WHERE log_status =2000 AND log_user='$user_id' GROUP BY no_dokpab, jenis_dokpab");	
	while($dh=mysql_fetch_array($qh)){
		$ary_nomor 		= get_nomorinc(1);
		$ary_nobpb 		= get_nobpbinc(1);		
		if(!empty($dh['no_bpb'])){
			$no_bpb = $dh['no_bpb'];
		}else{
			$no_bpb = $ary_nobpb;
		}
		#Insert Pemasukan Headar
		mysql_query("INSERT INTO incoming SET inch_bpbno='$no_bpb', inch_bpbdate='$dh[tgl_bpb]', inch_invno='$dh[no_invoice]', 
		             inch_invdate='$dh[tgl_invoice]', inch_dono='$dh[no_do]', inch_docno='$dh[no_dokpab]', 
					 inch_docdate='$dh[tgl_dokpab]', inch_inputby='$user_id', inch_inputdate='$datetime', inch_status=13, 
					 inch_doctype='$dh[jenis_dokpab]', nm_pemasok='".mysql_real_escape_string($dh['nm_pemasok'])."', no_aju='$dh[no_aju]', tgl_aju='$dh[tgl_aju]', no_sppb='$dh[no_sppb]', tgl_sppb='$dh[tgl_sppb]',                     
					 no_hbl='$dh[no_hbl]', tgl_hbl='$dh[tgl_hbl]', vesel='$dh[vesel]', eta_jkt='$dh[eta_jkt]', no_kontainer='$dh[no_kontainer]', 
					 ukuran_kontainer='$dh[ukuran_kontainer]', tipe_kontainer='$dh[tipe_kontainer]', jumlah_kemasan='$dh[jumlah_kemasan]', kode_kemasan='$dh[kode_kemasan]',
					 jenis_kemasan='$dh[jenis_kemasan]', pelabuhan='$dh[pelabuhan]', no_izin='$dh[no_izin]', tgl_izin='$dh[tgl_izin]', no_dokpab_asal='$dh[no_dokpab_asal]',
					 tgl_dokpab_asal='$dh[tgl_dokpab_asal]', jatuh_tempo='$dh[jatuh_tempo]', valuta='$dh[valuta]', status_dokpab='$dh[status_dokpab]', sumber='UPLOAD', tujuan='$dh[tujuan]', no_kontrak='$dh[no_kontrak]', tgl_kontrak='$dh[tgl_kontrak]'");
		#Aktivitas User
		mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='Menambahkan transaksi pemasukan No. $dh[no_bpb]', user_date='$date', user_time='$time'");
		$status = 1;
	}	
	//exit;
	if($status == 1){
		$qd = mysql_query("SELECT b.id,a.kode,a.jumlah,a.valuta,a.nilai,a.berat,a.no_urut,a.kode_hs,a.tarif,a.nilai_inv, a.jenis_dokpab, a.no_dokpab, a.tgl_dokpab, a.tgl_bpb,
					      a.non_rupiah
						  FROM log_incoming a 
						  INNER JOIN incoming b ON b.inch_docno=a.no_dokpab	AND b.inch_doctype=a.jenis_dokpab				 
						  WHERE a.log_status=2000 AND b.inch_status=13 AND a.log_user='$user_id'");
		while($dd = mysql_fetch_array($qd))
		{
			$prd2 = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$dd[kode]' AND prod_cat<>100"));
			#Insert Detail
			if($dd['valuta'] =="IDR"){$unit_price=$dd['nilai']/$dd['jumlah'];}else{$unit_price=$dd['non_rupiah']/$dd['jumlah'];}
			mysql_query("INSERT INTO incoming_detail SET incd_hid='$dd[id]', incd_prodid='$prd2[id]', incd_qty='$dd[jumlah]', 
			             incd_currency='$dd[valuta]', incd_price='$dd[nilai]', incd_weight='$dd[berat]', incd_wunit='KG', no_urut='$dd[no_urut]', kode_hs='$dd[kode_hs]',
						 tarif='$dd[tarif]', nilai_inv='$dd[nilai_inv]', incd_price_usd='$dd[non_rupiah]', incd_price_satuan='$unit_price'");
			
		}
		$status = 2;
	}
	//exit;
	if($status == 2){	
		#update status log
		mysql_query("UPDATE log_incoming SET log_status=3000 WHERE log_status =2000 AND log_user='$user_id'");
		#update status log
		mysql_query("UPDATE incoming SET inch_status=15 WHERE inch_status =13 AND inch_inputby='$user_id'");
		$message[] = "Data Sukses dikirim.";	
		$status    = "OK";
	}		 
}elseif($Act=="REG"){
	if(!empty($pilih_reg)){	
		foreach ( $pilih_reg as $i => $val)
		{
			$kd_barang   = $_REQUEST['kd_barang'][$val];
			$nm_barang   = $_REQUEST['nm_barang'][$val];
			$satuan      = $_REQUEST['satuan'][$val];
			$kd_kategori = $_REQUEST['kd_kategori'][$val];
			$tipe        = $_REQUEST['tipe'][$val];
			$ukuran      = $_REQUEST['ukuran'][$val];
			#Insert master barang
			if($kd_kategori != "BLANK"){
				#Update kode branag ditemp
				mysql_query("UPDATE log_incoming SET kode='$kd_barang', nm_barang='".mysql_real_escape_string($nm_barang)."', satuan='$satuan', tipe='$tipe', ukuran='$ukuran' WHERE id='$val'");
				#Insert ke master barang				
				mysql_query("INSERT INTO product SET prod_code='$kd_barang', prod_name='".mysql_real_escape_string($nm_barang)."', prod_sat='$satuan', prod_cat='$kd_kategori', prod_status=10, prod_type='$tipe', prod_spec='$ukuran'");
				#Aktivitas user					
				mysql_query("INSERT INTO user_activity SET user_id='".$_SESSION['USER_ID']."', user_act='Menambahkan data barang - $kd_barang', 
				             user_date='$date', user_time='$time'");
			}
		}
		require "add_upload_pemasukan.php";
	}else{
		require "add_upload_pemasukan.php";
	}	
}elseif($Act=="DEL_TEMP"){
	#update status
	mysql_query("UPDATE log_incoming SET log_status=5000 WHERE log_status=1000 AND log_user='$user_id'");
	require "add_upload_pemasukan.php";
}elseif($Act=="DELDET_TEMP"){
	#update status
	mysql_query("UPDATE log_incoming SET log_status=1000 WHERE id='".$_REQUEST['id_temp']."' ");
	require "add_upload_pemasukan.php";
}elseif(($Act=="ADD") || ($Act=="GET") || ($Act=="TEMP") ){
	require "add_upload_pemasukan.php";
}elseif($Act=="CANCEL" || $Act=="CLOSE"){
	echo "<meta http-equiv='refresh' content='0; url=?page=Data-Upload-Pemasukan'>";	
	exit;
}
#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-Upload-Pemasukan'>";	
		exit;
	}else{
		echo "<meta http-equiv='refresh' content='0; url=?page=Proses-Upload-Pemasukan&Act=TEMP'>";
	}
}
?>