<?php

include '../../../config/koneksi.php';
session_start();

   
		//upload data excel kedalam folder uploads
		
		if(empty(($_FILES['file']['name'])))
		{
			echo "Belum Pilih File";
		}
	else
	{
		require('spreadsheet-reader-master/php-excel-reader/excel_reader2.php');
		require('spreadsheet-reader-master/SpreadsheetReader.php');

		$target_dir = "uploads/".basename($_FILES['file']['name']);
      $bulan=$_POST['bulan'];
      	  $favcolor = "green";


		move_uploaded_file($_FILES['file']['tmp_name'],$target_dir);
		 mysqli_query($koneksi, "delete from realisasi where bulan ='$bulan'");

		$Reader = new SpreadsheetReader($target_dir);

		foreach ($Reader as $Key => $Row)
		{
			// import data excel mulai baris ke-2 (karena ada header pada baris 1)
			if ($Key < 1) continue;
			$rowr1 = str_replace(',00', '', $Row[27]);	
			$rowr2 = str_replace('.', '', $rowr1);
			if($Row[0] == 'Total'){

			}
			else
			{

				switch ($bulan) {
  case date("Y").'-01-01':
    $bulan1='Januari '.date("Y");
     break;
  case date("Y").'-02-01':
    $bulan1='Februari '.date("Y");
    break;
  case date("Y").'-03-01':
    $bulan1='Maret '.date("Y");
  
    break;
   case date("Y").'-04-01':
    $bulan1='April '.date("Y");
   
    break;
    case date("Y").'-05-01':
    $bulan1='Mei '.date("Y");
    
    break;
    case date("Y").'-06-01':
    $bulan1='Juni '.date("Y");
    
    break;
    case date("Y").'-07-01':
    $bulan1='Juli '.date("Y");
   
    break;
    case date("Y").'-08-01':
    $bulan1='Agustus '.date("Y");
    
    break;
    case date("Y").'-09-01':
    $bulan1='September '.date("Y");
    
    break;
    case date("Y").'-10-01':
    $bulan1='Oktober '.date("Y");
    
    break;
    case date("Y").'-11-01':
    $bulan1='November '.date("Y");
    
    break;
    case date("Y").'-12-01':
    $bulan1='Desember '.date("Y");
    
    break;
  default:
    echo "?";
}
			 
$poss=strstr($Row[25], $bulan1);

echo $bulan1;
	if ($poss == $bulan1) { 
 
		

			$query=mysqli_query($koneksi,"INSERT INTO realisasi
				(
				nomor,
				kode_skpd,
				skpd,
				kode_sub_skpd,
				sub_skpd,
				kode_fungsi,
				fungsi,
				kode_sub_fungsi,
				sub_fungsi,
				kode_urusan,
				urusan,
				kode_bidang,
				bidang,
				kode_program,
				program,
				kode_kegiatan,
				kegiatan,
				kode_sub_kegiatan,
				sub_kegiatan,
				kode_rekening,
				rekening,
				nomor_dokumen,
				jenis_dokumen,
				jenis_transaksi,
				nomor_dpt,
				tanggal_dokumen,
				keterangan_dokumen,
				realisasi,
				nomor_spp,
				tanggal_spp,
				nomor_spm,
				tanggal_spm,
				nomor_sp2d,
				tanggal_sp2d,
				tanggal_transfer,
				bulan

			    ) VALUES (
			    '".$Row[0]."', 
			    '".$Row[1]."',
			    '".$Row[2]."',
			    '".$Row[3]."',
			    '".$Row[4]."',
			    '".$Row[5]."',
			    '".$Row[6]."',
			    '".$Row[7]."',
			    '".$Row[8]."',
			    '".$Row[9]."',
			    '".$Row[10]."',
			    '".$Row[11]."',
			    '".$Row[12]."',
			    '".$Row[13]."',
			    '".$Row[14]."',
			    '".$Row[15]."',
			    '".$Row[16]."',
			    '".$Row[17]."',
			    '".$Row[18]."',
			    '".$Row[19]."',
			    '".$Row[20]."',
			    '".$Row[21]."',
			    '".$Row[22]."',
			    '".$Row[23]."',
			    '".$Row[24]."',
			    '".$Row[25]."',
			    '".$Row[26]."',
			    '".$rowr2."',
			    '".$Row[38]."',
			    '".$Row[39]."',
			    '".$Row[40]."',
			    '".$Row[41]."',
			    '".$Row[42]."',
			    '".$Row[43]."',
			    '".$Row[44]."',
			    '".$bulan."'
			)");
		  		}
else
{
	
}
	
		}
		}
		if ($query) {
				echo "Import data berhasil";
				unlink($target_dir);
			}else{
				echo mysql_error();
			}
		}
	?>