<?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 = "bom";
$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(strtolower($buffer[$key])));
}

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

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

$typee= utility::getTableFieldById(
    "item_management",   // table name
    "usual_source",                // field to get
    "part_number",       // lookup field
    $Component_Part_Number // lookup value
);

// If no record found, then insert

    $records_inserted++;

    $sql = "INSERT INTO $table_name (`main_product_id`,`sub_part_id`,`sub_part_name`,`quantity_required`,`type`) VALUES ('$part_id_main','$part_id_com','$Component_Part_Number','$Quantity_Required','$typee')";
    mysqli_query($conn, $sql);

    // get the new id
    $part_id = mysqli_insert_id($conn);


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