<?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";


// ***************************************************************************
// buscar no diretorio os arquivos de produtos que vem do totvs.
// ***************************************************************************

$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 == "produtos") {


$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 produtos
        
    //$query = "delete from categorias_artigos where id <> 1 and id <> 2 and id <> 3";
          
         
    //if ($result = $mysqli->query($query)) {
    //   }


$row = 0;
$ll = 0;
while ($line = fgetcsv($handle, 100000, ";")) {
	if ($row++ == 0) {
		continue;
	}
	
	$people[] = [
		 'id_product'            => $line[0],
		 'id_collection'         => $line[1],
		 'id_edition'            => $line[2],
		 'edition_number'        => $line[3],
                 'id_prod_Planeta'       => $line[4],
                 'description'           => $line[5],
                 'price'                 => $line[6],
                 'reordable'             => $line[7],
                 'returnable'            => $line[8],
                 'packet'                => $line[9],
                 'cde_number'            => $line[10],
                 'id_ce'                 => $line[11],
                
	];
        echo "id_product = "              . $people[$ll]['id_product']      . " " ; 
        $id_product                       = $people[$ll]['id_product'];
        
        echo "id_collection = "           . $people[$ll]['id_collection']     . " " ; 
        $id_collection                    = $people[$ll]['id_collection'];
        
        echo "id_edition = "              . $people[$ll]['id_edition']        . " " ; 
        $id_edition                       = $people[$ll]['id_edition'];
        
        echo "edition_number = "          . $people[$ll]['edition_number']        . " " ; 
        $edition_number                   = $people[$ll]['edition_number'];
        
        echo "id_prod_Planeta = "         . $people[$ll]['id_prod_Planeta']        . " " ; 
        $id_prod_Planeta                  = $people[$ll]['id_prod_Planeta'];  
        
        echo "description = "             . $people[$ll]['description']    . " " ; 
        $description                      = $people[$ll]['description']; 
        
        echo "price = "                   . $people[$ll]['price']   . " " ;
        $price                           = $people[$ll]['price'];
        
        echo "reordable = "               . $people[$ll]['reordable']       . " " ;
        $reordable                        = $people[$ll]['reordable'];   
        
        echo "returnable = "              . $people[$ll]['returnable']       . " " ; 
        $returnable                       = $people[$ll]['returnable'];
        
        echo "packet = "                  . $people[$ll]['packet']     . " " ;  
        $packet                           = $people[$ll]['packet'];  
        
        echo "cde_number = "              . $people[$ll]['cde_number']       . " " ; 
        $cde_number                       = $people[$ll]['cde_number'];
        
        echo "id_ce = "                   . $people[$ll]['id_ce']      
         . "</br>" ; 
        
        $id_ce                           = $people[$ll]['id_ce'];
        
        date_default_timezone_set('America/Sao_Paulo');
        $date = date('Y-m-d H:i:s');
        
        $usuario="Administrador";
        
         // incluir dados no  Banco de dados
         $query = "insert into categorias_artigos (nome_cat_artigo"
                 . ",created,id_product"
                 . ",id_collection"
                 . ",id_edition"
                 . ",edition_number"
                 . ",id_prod_Planeta"
                 . ",description"
                 . ",price"
                 . ",reordable"
                 . ",returnable"
                 . ",packet"
                 . ",cde_number"
                 . ",id_ce"
                 . ",user) "
                 . "VALUES ('x'"
                 . ",'$date'"
                 . ",'$id_product'"
                 . ",'$id_collection'"
                 . ",'$id_edition'"
                 . ",'$edition_number'"
                 . ",'$id_prod_Planeta'"
                 . ",'$description'"
                 . ",'$price'"
                 . ",'$reordable'"
                 . ",'$returnable'"
                 . ",'$packet'"
                 . ",'$cde_number'"
                 . ",'$id_ce'"
                 . ",'$usuario' )";
          
         
         echo $query; 
         
         
         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);

}
}


?>
