<?php
//include '../session.php';
include "module/excel_reader2.php";
@$data = new Spreadsheet_Excel_Reader($_FILES['fileexcel']['tmp_name']);
@$hasildata = $data->rowcount($sheet_index=0);
$sukses = 0;
$gagal = 0;

for ($i=2; $i<=$hasildata; $i++)
{
  
  //$record['nipd']=trim($data->val($i,1);
  $nm_mhs=trim($data->val($i,2)); 
  
  $record['nm_pd'] = $nm_mhs;  
  $record['tmpt_lahir'] = trim($data->val($i,3));
  $record['tgl_lahir'] = trim($data->val($i,4));
  $record['jk']  = trim($data->val($i,5));
  $record['id_agama'] =trim($data->val($i,6));
  $record['id_kk']=trim($data->val($i,7));
  //$record['id_jns_daftar'] = trim($data->val($i,8);
  //$record['tgl_masuk_sp'] = trim($data->val($i,9);
  //$record['mulai_smt'] = trim($data->val($i,10); 
  $record['jln'] = trim($data->val($i,11));
  $record['rt'] = trim($data->val($i,12));
  $record['rw'] = trim($data->val($i,13));
  $record['nm_dsn'] = trim($data->val($i,14));
  $record['ds_kel'] = trim($data->val($i,15));
  $record['id_wil']='000000';
  $record['a_terima_kps'] = trim($data->val($i,21)); 
  $record['stat_pd'] = trim($data->val($i,23));
  $record['nm_ayah'] = trim($data->val($i,24));
  $record['id_jenjang_pendidikan_ayah'] = trim($data->val($i,26));
  $record['id_pekerjaan_ayah'] = trim($data->val($i,27));
  $record['id_penghasilan_ayah'] = trim($data->val($i,28));
  $record['id_kebutuhan_khusus_ayah'] = 0;
  $record['nm_ibu_kandung']  = trim($data->val($i,29));
  $record['id_jenjang_pendidikan_ibu'] = trim($data->val($i,31));
  $record['id_pekerjaan_ibu'] = trim($data->val($i,32));
  $record['id_penghasilan_ibu'] = trim($data->val($i,33));
  $record['id_kebutuhan_khusus_ibu'] = 0;
  $record['nik'] = trim($data->val($i,39));
  //$record['kode_prodi'] = trim($data->val($i,40);
  $record['kewarganegaraan']='ID';
  $records[]=$record;
}
  
 $j_encode=json_encode($records);
 $mhs_json=$j_encode;
// $j_decode=json_decode($j_encode,true);
// $mhs_json1=$j_decode;
//echo "parameter yang masuk ke tabel mahasiswa sebagai berikut <br />";
	//echo "<br />";
//	print_r($mhs_json)."<br />";
//	echo "----------------------------------<br>";
//	
//	echo "<br />";
//	print_r($mhs_json1)."<br />";
//	echo "----------------------------------<br>";
	
	
	$result=$proxy->InsertRecordset($gettoken,'mahasiswa',$mhs_json);    // fix insert banyak records
	//var_dump($result)."<br>";

if ($result['result']) {
	for ($i=2; $i<=$hasildata; $i++){
		//foreach($mhs_json1 as $row){
		
			$idsp="id_sp='".$id_sp."'";
			$prodi=trim($data->val($i,40));      
			//echo "kode prodi : ".trim($data->val($i,2)."<br>";
			$kode_prodi="kode_prodi ILIKE '%$prodi%'";
			//$kode_prodi="id_jur='".trim(trim($data->val($i,40))."'";
			$filter=$kode_prodi." AND ".$idsp;
		//echo $filter."<br>";
			$result=$proxy->GetRecord($gettoken,'sms',$filter);
			$coba=$result['result'];
			$id_sms=$coba['id_sms'];
		//echo "id_sms = '".$id_sms."'<br>";

		// Kebutuhan tabel mahasiswa_pt
		
			$record1['id_reg_pd']='';
			$record1['id_sms']=$id_sms;//'851ef9fb-ee0a-4772-b868-43d9370c21df';//$coba['id_sms'];
		  
	  
	  		$nm_mhs=trim($data->val($i,2)); 
			//$nm_mhs1=pg_escape_string(utf8_encode($nm_mhs));
			$filt_nm="nm_pd='".$nm_mhs."'";
			$filt_tpl="tmpt_lahir='".trim($data->val($i,3))."'";
			$filt_tgl="tgl_lahir='".trim($data->val($i,4))."'";
			$filter1=$filt_nm." AND ".$filt_tpl." AND ".$filt_tgl;
			  //echo $filter1;
			  $result=$proxy->GetRecord($gettoken,'mahasiswa',$filter1);
			  $coba1=$result['result'];
			  $id_pd=$coba1['id_pd'];
			  //echo "<br />";
//			  print_r ("id_pd ='".$id_pd."'<br>");
	  
			$record1['id_pd']=$id_pd;
			$record1['id_sp']=$id_sp;
			//					
			//if($row['StatusAwalID']=='B'){$id_daftar=1;}else{$id_daftar=2;}
			$id_daft=trim($data->val($i,8));
			$record1['tgl_masuk_sp']=trim($data->val($i,9));
			  $mulai_smt=trim($data->val($i,10));
			$record1['mulai_smt']=$mulai_smt;
			$nipd=trim($data->val($i,1));
			$record1['nipd']=$nipd;			
			if ($id_daft=='2'){
			$record1['id_jns_daftar']=$id_daft;

			//$record1['id_pd']=$id_pd;
			$record1['sks_diakui']=$data->val($i,41);
				$pt_as=$data->val($i,42);
				$asal=trim($pt_as);
				$filt="npsn like '%$asal%'";
				$result=$proxy->GetRecord($gettoken,'satuan_pendidikan',$filt);
				$id_pt_asal=$result['result']['id_sp'];
				//echo "PT Asal = ".$id_pt_asal."<br />";
			$record1['id_pt_asal']=$id_pt_asal;
			    $pro_as=$data->val($i,43);
				$pr_asal=trim($pro_as);
				$filt_pro="p.id_sp='$id_pt_asal' AND kode_prodi like '%$pr_asal%'";
				$result=$proxy->GetRecord($gettoken,'sms',$filt_pro);
				$id_prodi_asal=$result['result']['id_sms'];
				//echo "Prodi asal= ".$id_prodi_asal."<br />";
			$record1['id_prodi_asal']=$id_prodi_asal;
			}
			else {
			$record1['id_jns_daftar']=$id_daft;			
			}
			
			$record1s[]=$record1;
		//}
		//if ($hasildata) $sukses++;
		//else $gagal++;
	}
		$j_encode1=json_encode($record1s);
		$mhs_pt_json=$j_encode1;
		//print_r($mhs_pt_json);
		//$j_decode1=json_decode($j_encode1,true);
		//$mhs_pt_json1=$j_decode1;
	
	$result1=$proxy->InsertRecordset($gettoken,'mahasiswa_pt',$mhs_pt_json);    // fix insert banyak record
	//var_dump($result1);
	echo "<pre style='background-color:#1699C9'>";
	$x=0;
	if ($result['result']) {
	for ($i=2; $i<=$hasildata; $i++){
		$x++;
			$nm_mhs=trim($data->val($i,2)); 
			//$nm_mhs1=pg_escape_string(utf8_encode($nm_mhs));
			$filt_nm="nm_pd like '%$nm_mhs%'";
			$filt_tpl="tmpt_lahir='".trim($data->val($i,3))."'";
			$filt_tgl="tgl_lahir='".trim($data->val($i,4))."'";
			$filter1=$filt_nm." AND ".$filt_tpl." AND ".$filt_tgl;
			  //echo $filter1;
			  $result=$proxy->GetRecord($gettoken,'mahasiswa_pt',$filter1);
			  $coba1=$result['result'];
			  $id_pd=$coba1['id_pd'];
			  $id_reg_pd=$coba1['id_reg_pd'];
			  $nm=$result['result']['nm_pd'];
			  $nim=$result['result']['nipd'];
			  $mulai_smt=$result['result']['mulai_smt'];
			  $prodi=$result['result']['fk__sms'];
	
			echo "<br>$x. | NIM : ".$nim." | Nama : ".$nm." | Semester Awal = ".$mulai_smt." | Prodi = ".$prodi."<br>";
			if($id_reg_pd != '')$sukses++;
			else
			$gagal++;
	}
	}
	echo "</pre>";
}

echo "<b>import data selesai.</b> <br>";
echo "Data yang berhasil di import : " . $sukses .  "<br>";
echo "Data yang gagal diimport : ".$gagal .  "<br>";
echo "kembali ke <a href='?module=home'>depan</a>";

?>