<?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     = "part_number_list.csv";
$destfile = $destdir.$name;  

$batch_size = 3000;
$table_name = "item_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("Assembly_Part_Number","Sequence_Number","Component_Part_Number","Quantity_Required");

foreach($variable_array as $key=>$val){		
    ${$val} = addslashes(trim($buffer[$key]));
}

if ($Assembly_Part_Number == "Assembly_Part_Number") {
    continue;
}

// use the $type variable here
$part_id = utility::getTableFieldById(
    "item_management",   // table name
    "id",                // field to get
    "part_number",       // lookup field
    $Component_Part_Number // lookup value
);

// If no record found, then insert
if (empty($part_id)) {
    $records_inserted++;

$sql = "INSERT INTO $table_name (`part_number`,`usual_source`) 
        VALUES ('$Component_Part_Number','Bought In')";
mysqli_query($conn, $sql);
$partt_id = mysqli_insert_id($conn);

$sql_product_name = "INSERT INTO product_name 
        (`pro_id`,`group_number`,`group_code`,`group_name`,`qty`) 
        VALUES ('$partt_id','$Component_Part_Number','Bought In','$Component_Part_Number','1')";
mysqli_query($conn, $sql_product_name);
$partt_idd = mysqli_insert_id($conn);

$sql_update = "UPDATE item_management 
               SET part_number_id = '$partt_idd' 
               WHERE id = '$partt_id'";
mysqli_query($conn, $sql_update);


	
}

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