<?php
die;
set_time_limit(0);
ini_set('memory_limit','1024M');
ini_set('post_max_size','32M');
ini_set('upload_max_filesize ','32M');

$destdir  = "live-data-migration-csvs/";
$name     = "gsi_data.csv";
$destfile = $destdir.$name;  

$batch_size = 3000;
$table_name = "job";
$records_inserted = 0;
$records_updated = 0;  
                                   
$flag = true;
$fp = fopen($destfile, 'r');
while (!feof ($fp)){
  $buffer = fgetcsv($fp, 1000000, ',');  
  if($flag) { 
	  if($buffer){
			$variable_array = array("company_name","address1","address2","address3","city","county","postcode","title","initial","first_name","last_name","job_function","job_title","phone_no","sector","email","note","renewal_dates","turnover");
			foreach($variable_array as $key=>$val){		
				${$val} = addslashes(trim(strtolower($buffer[$key])));
			}
			if($company_name =="company_name"){
				continue;
			}
			if(isset($renewal_dates) && $renewal_dates !=""){
				$renewal_date_array = explode("/",$renewal_dates);
				$renewal_dates = $renewal_date_array[2]."-".$renewal_date_array[0]."-".$renewal_date_array[1];
			}else{
				$renewal_dates = "0000-00-00 00:00:00";
			}
			$county_id = utility::getTableFieldById('county','id','county',$county);
			if(!(isset($county_id) && $county_id !="")){
				mysqli_query($conn,"INSERT INTO county(county) VALUE('$county')");
				$county_id = mysqli_insert_id($conn);
			}
			$sector_id = utility::getTableFieldByTwoCondition("keycode","id","name","sector","code",$sector);
			if(!(isset($sector_id) && $sector_id !="")){
				mysqli_query($conn,"INSERT INTO keycode(name,code) VALUE('sector','$sector')");
				$sector_id = mysqli_insert_id($conn);
			}
			$turnover = str_replace("œ","",$turnover);
			$turnover = str_replace(",","",$turnover);
			$note_date = date("Y-m-d");
			$enquiry_date = "0000-00-00 00:00:00";
			
			$records_inserted++;
			$sql =  "INSERT INTO $table_name(`is_enquiry`,`enquiry_date`,`company_name`,`address1`,`address2`,`address3`,`city`,`county`,`postcode`,`title`,`initial`,`first_name`,`last_name`,`job_function`,`job_title`,`phone_no`,`sector`,`email`,`renewal_dates`,`turnover`) VALUE('1','$enquiry_date','$company_name','$address1','$address2','$address3','$city','$county_id','$postcode','$title','$initial','$first_name','$last_name','$job_function','$job_title','$phone_no','$sector_id','$email','$renewal_dates','$turnover')";
			
			mysqli_query($conn,$sql);
			$job_id = mysqli_insert_id($conn);
			
			if(isset($note) && $note !=""){
				mysqli_query($conn,"INSERT INTO notes(job_id,note_text,note_date) VALUE('$job_id','$note','$note_date')");
			}
	  }
  }
}
?>
<?php 
if($records_inserted){    
  echo '<br><span style="font-family:calibri;color:red;">'.($records_inserted).' records has been inserted...</span>';
}?>
 <?php 
if($records_updated){    
  echo '<br><span style="font-family:calibri;color:red;">'.($records_updated).' records has been updated...</span>';
}?>