<?php
// ****************************************************************************
// Carregar os produtos na tabela categorias_artigos
// ****************************************************************************
// Inclui a conexão

//$host1    = $_SESSION['HOST'];
//$user1    = $_SESSION['USER'];
//$pass1    = $_SESSION['PASS'];
//$dbname1  = $_SESSION['DBNAME'];

$host1    = "localhost";
$user1    = "root";
$pass1    = "";
$dbname1  = "eed";

$host1    = "mysql.portalediouro.com.br";
$user1    = "portaled02_add1";
$pass1    = "ediouro123";
$dbname1  = "portalediouro02";


$pasta = "../ftp/remessa/arquivos/";
$arquivos = glob("$pasta{*.csv}", GLOB_BRACE);


foreach($arquivos as $img){

   $tipo="";
   $nome=""; 
   $nomearq="";  
    
   echo "$img <br>" ;
   // tipo
   $rest = substr($img, -3);    // retorna o tipo do arquivo
   echo "$rest <br>" ;
   $tipo = $rest;
   //  nome
   $rest = substr($img, -12, 8); // retorna o nome cliente 
   echo "$rest <br>" ;
   $nome = $rest;

   //  nome
   $rest = substr($img, -19, 19); // retorna o nome do arquivo 
   echo "$rest <br>" ;
   $nomearq = $rest;   

   if ($nome == "clientes") {


         
$mysqli = new mysqli($host1, $user1, $pass1, $dbname1);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
exit();
     }

$handle = fopen("../ftp/remessa/arquivos/$nomearq", "r");
 

 // Excluir tabela de clientes
        
    $query = "delete from contatos";
          
         
    if ($result = $mysqli->query($query)) {
       }




$row = 0;
$ll = 0;
while ($line = fgetcsv($handle, 100000, ";")) {
	if ($row++ == 0) {
		continue;
	}
	
	$people[] = [
		 'customercode'          => $line[0],
		 'distributorcode'       => $line[1],
		 'channel'               => $line[2],
		 'cnpj'                  => $line[3],
                 'customer_name'         => $line[4],
                 'Address'               => $line[5],
                 'email'                 => $line[6]
                
                
	];
        echo "customercode = "            . $people[$ll]['customercode']      . " " ; 
        $customercode                     = $people[$ll]['customercode'];
        
        echo "distributorcode = "         . $people[$ll]['distributorcode']     . " " ; 
        $distributorcode                  = $people[$ll]['distributorcode'];
        
        echo "channel = "                 . $people[$ll]['channel']        . " " ; 
        $channel                          = $people[$ll]['channel'];
        
        echo "cnpj = "                    . $people[$ll]['cnpj']        . " " ; 
        $cnpj                             = $people[$ll]['cnpj'];
        
        echo "customer_name = "           . $people[$ll]['customer_name']        . " " ; 
        $customer_name                    = $people[$ll]['customer_name'];  
        
        echo "Address = "                 . $people[$ll]['Address']        . " " ; 
        $Address                          = $people[$ll]['Address'];  
                
        echo "email = "                    . $people[$ll]['email'] 
                . "</br>" ; 
         $email                             = $people[$ll]['email'];
         
        date_default_timezone_set('America/Sao_Paulo');
        $date = date('Y-m-d H:i:s');
        
        
        
        
         // incluir dados no  Banco de dados
        $query = "INSERT INTO contatos (nome_cliente, email_cliente,
            `created`,customercode,distributorcode,channel,
            cnpj,customer_name,Address,email)
            VALUES ('$email','$email','$date','$customercode','$distributorcode','$channel','$cnpj'"
               
                . ",'$customer_name','$Address', '$email')";
          
         
         if ($result = $mysqli->query($query)) {
         
          }
        
        
        $ll++;
}
 
//print_r($people);

 fclose($handle);

 $origem = "../ftp/remessa/arquivos/";
 $origem .=  $nomearq;
 echo "$origem <br>" ;
 $destino = "../ftp/remessa/arquivos/processados/";
 $destino .= $nomearq; 
 echo "$destino <br>" ;
 echo " <br>" ;
 copy($origem, $destino);
 unlink($origem);

}
}

?>
