<?php
// ****************************************************************************
// Carregar Chamada de encalhe na tabela 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    = "portaled01_add1";
$pass1    = "ediouro123";
$dbname1  = "portalediouro01";

$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, -6, 2); // retorna o nome cliente 
   echo "$rest <br>" ;
   $nome = $rest;

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

   if ($nome == "ce") {


         
$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");
 
$row = 0;
$ll = 0;
while ($line = fgetcsv($handle, 100000, ";")) {
	if ($row++ == 0) {
		continue;
	}
	
	$people[] = [
		'customercode'           => $line[0],
		 'cnpj'                  => $line[1],
		 'cde_number'            => $line[2],
		 'id_edition'            => $line[3],
                 'id_product'            => $line[4],
                 'edition_number'        => $line[5],
                 'Id_prod_Planeta'       => $line[6],
                 'recall_type'           => $line[7],
                 'recall_date'           => $line[8],
                 'line_number'           => $line[9],
                 'total_distributed'     => $line[10],
                 'return'                => $line[11],
                 'sales'                 => $line[12],
                 'Send'                  => $line[13],
                 'discount'              => $line[14],
                 'nota_ponto'            => $line[15],
                 'invoice_number'        => $line[16]
                
	];
        echo "customercode = "            . $people[$ll]['customercode']      . " " ; 
        $customercode                     = $people[$ll]['customercode'];
        echo "cnpj = "                    . (string) $people[$ll]['cnpj']     . " " ; 
        $cnpj                             = (string) $people[$ll]['cnpj'];
        echo "cde_number = "              . $people[$ll]['cde_number']        . " " ; 
        $cde_number                       = $people[$ll]['cde_number'];
        echo "id_edition = "              . $people[$ll]['id_edition']        . " " ; 
        $id_edition                       = $people[$ll]['id_edition'];
        echo "id_product = "              . $people[$ll]['id_product']        . " " ; 
        $id_product                       = $people[$ll]['id_product'];  
        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 "recall_type = "             . $people[$ll]['recall_type']       . " " ;
        $recall_type                      = $people[$ll]['recall_type'];        
        echo "recall_date = "             . $people[$ll]['recall_date']       . " " ; 
        $recall_date                      = $people[$ll]['recall_date'];
        echo "line_number = "             . $people[$ll]['line_number']       . " " ; 
        $line_number                      = $people[$ll]['line_number'];
        echo "total_distributed = "       . $people[$ll]['total_distributed'] . " " ;
        $total_distributed                = $people[$ll]['total_distributed'];
        echo "return2 = "                 . $people[$ll]['return']            . " " ;
        $return2                          = $people[$ll]['return'];
        echo "sales = "                   . $people[$ll]['sales']             . " " ; 
        $sales                            = $people[$ll]['sales'];
        echo "Send  = "                   . $people[$ll]['Send']              . " " ;
        $send2                            = $people[$ll]['Send'];
        echo "discount = "                . $people[$ll]['discount']          . " " ; 
        $discount                         = $people[$ll]['discount'];
        echo "nota_ponto = "              . $people[$ll]['nota_ponto']        . " " ;
        $nota_ponto                       = $people[$ll]['nota_ponto'];
        echo "invoice_number = "          . $people[$ll]['invoice_number']
                . "</br>" ; 
        $invoice_number                   = $people[$ll]['invoice_number'];
        date_default_timezone_set('America/Sao_Paulo');
        $date = date('Y-m-d H:i:s');
        
         // incluir dados no  Banco de dados
         $query = "INSERT INTO artigos (titulo_artigo, descricao_artigo,
             conteudo_artigo,foto,categorias_artigo_id,slug_artigo,
             created,customercode,cnpj,cde_number,id_edition,id_product,edition_number,
             Id_prod_Planeta,recall_type,recall_date,line_number,total_distributed,return2,
             sales,discount,nota_ponto,invoice_number,status)
             VALUES ('x','x','x','cinco.png','1','1','$date','$customercode','$cnpj'"
                 . ",'$cde_number','$id_edition', '$id_product','$edition_number'"
                 . ",'$Id_prod_Planeta','$recall_type','$recall_date','$line_number'"
                 . ",'$total_distributed','$return2','$sales','$discount', '$nota_ponto'"
                 . ",'$invoice_number','A'    )";
          
         
         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);

}
}


?>
