<?php
die("hii");
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     = "supplier_details.csv";
$destfile = $destdir.$name;  

$batch_size = 3000;
$table_name = "supplier_management";
$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("Supplier_Code","Supplier_Name","Address_1","Address_2","Address_3","Address_4","Postcode","Telephone","Fax_Number","Email","Country_Code");
			foreach($variable_array as $key=>$val){		
				//${$val} = addslashes(trim(strtolower($buffer[$key])));
				${$val} = addslashes(trim($buffer[$key]));
			}
			if($Supplier_Code =="Supplier_Code"){
				continue;
			}
			
			if(isset($Telephone) && $Telephone !=""){
				$Telephone=utility::app_crypt($Telephone,'e');
			}
			if(isset($Email) && $Email !=""){
				$Email=utility::app_crypt($Email,'e');	
			}
			
			$Full_Address = "";

			if(!empty($Address_1) && !empty($Address_2)){
				$Full_Address = $Address_1 . ", " . $Address_2;
			}elseif(!empty($Address_1)){
				$Full_Address = $Address_1;
			}elseif(!empty($Address_2)){
				$Full_Address = $Address_2;
			}
			
			$country_id = utility::getTableFieldById(
				"country",  
				"id",                     
				"country_code",          
				$Country_Code           
			);
			$created_by					= $_SESSION['user_id'];
        	$created_on					= date("Y-m-d H:i:s");	
        	$updated_by					= $_SESSION['user_id'];
			
			$records_inserted++;
			$sql =  "INSERT INTO $table_name(`supplier_code`,`supplier_name`,`address_1`,`address_2`,`address_3`,`supplier_postcode`,`contact_1_phone_no`,`contact_1_email_address`,`default_country_code`) VALUE('$Supplier_Code','$Supplier_Name','$Full_Address','$Address_3','$Address_4','$Postcode','$Telephone','$Email','$country_id')";
			
			mysqli_query($conn,$sql);
	  }
  }
}
?>
<?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>';
}?>