<?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');

$kode_sub_skpd=$_POST['kode_sub_skpd'];
 if (!file_exists("uploads/".$kode_sub_skpd))
    mkdir("uploads/".$kode_sub_skpd);
		$target_dir = "uploads/".$kode_sub_skpd.'/'.basename($_FILES['file']['name']);

      	  $favcolor = "green";


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

		$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;
			$rowrb1 = str_replace(',00', '', $Row[7]);	
			$rowrb2 = str_replace('.', '', $rowrb1);
			$rowrp1 = str_replace(',00', '', $Row[8]);	
			$rowrp2 = str_replace('.', '', $rowrp1);
			$rowrn1 = str_replace(',00', '', $Row[9]);	
			$rowrn2 = str_replace('.', '', $rowrn1);
			if($Row[0] == 'Total'){

			}
			else
			{

				
}
			 


			$query=mysqli_query($koneksi,"INSERT INTO sp2d
				(
				kode_sub_skpd,
				nomor,
				tanggal_sp2d,
				nomor_sp2d,
				unit_skpd,
				nama_penerima,
				keterangan,
				jenis_sp2d,
				bruto,
				potongan,
				neto
			    ) VALUES (
			    '$kode_sub_skpd', 
			    '".$Row[0]."',
			    '".$Row[1]."',
			    '".$Row[2]."',
			    '".$Row[3]."',
			    '".$Row[4]."',
			    '".$Row[5]."',
			    '".$Row[6]."',
			    '".$rowrb2."',
			    '".$rowrp2."',
			    '".$rowrn2."'
			)");
		  		

		}
		}
		if ($query) {
				echo "Import data berhasil";
				unlink($target_dir);
				
			}else{
				echo mysql_error();
			}
		
	?>