<script>
     
                function myFunction(evento, evento1, evento2, evento3) {
        
                // alert(evento);
                // alert(evento1);
                // alert(evento2);
                // alert(evento3);
    
                var minha_var = evento;
                var minha_var1 = evento1;
                var minha_var2 = evento2;
                var minha_var3 = evento3;
    
                document.getElementById("myText").innerHTML = minha_var;
                
                document.getElementById("myText1").innerHTML = minha_var1;
                
                document.getElementById("myText2").innerHTML = minha_var2;
              
                document.getElementById("myText3").innerHTML = minha_var3;
                
                }
 </script>


 <script> 
function AdicionaSelect(){ 
 var i = document.form.select.selectedIndex; 
 
 var j = document.form.txtarea.value;
     j += document.form.select[i].text; 
     
  document.form.txtarea.value = j + "\n";
 
 
   alert(document.form.select[i].text);
  
  
  var retorno = document.form.select[i];
  
 return  retorno; 
  
  document.getElementById("myText5").innerHTML =  document.form.txtarea.value;
  
} 
</script> 
<?php
function Mask($mask,$str){

    $str = str_replace(" ","",$str);

    for($i=0;$i<strlen($str);$i++){
        $mask[strpos($mask,"#")] = $str[$i];
    }

    return $mask;

}

    echo "Versão 1.1";
    echo " - ";
    echo  $_SESSION['customercode'];
    echo " - ";
    echo  $_SESSION['cnpj'];
    echo " - ";
    echo  $_SESSION['praca']; 
    echo "-";
    echo  $_SESSION['ce'];  
    
?>



<div class="well well-personalizado">
    
    <div class="pull-right">
        
         <a href="<?php echo URL; ?>controle-artigo/gecepdistriba"><button type="button" class="btn btn-danger">Gerar Excel</button></a> 
        <!-- <a href="<?php echo URL; ?>/views/artigo/gerar_excel_ce_p_distrib_analitico"><button type="button" class="btn btn-danger">Gerar Excel</button></a> -->
    </div>
  
    <div class="page-header">
        <h1>RCE por Distribuidor Analítico</h1>
        
        
    </div>
    
    
    <?php
    
    
    
    
    $host1    = $_SESSION['HOST'];
    $user1    = $_SESSION['USER'];
    $pass1    = $_SESSION['PASS'];
    $dbname1  = $_SESSION['DBNAME'];
    
    $ccc=$_SESSION['customercode']; 
    $cnpj=$_SESSION['cnpj']; 
    $praca=$_SESSION['praca']; 
    
    $descricao1  = $_SESSION['ce'];  
    $descricao2 = $_SESSION['praca'];
    $selectedField = "selected='selected'";
    
    
    
    //********************************* //
     $ce = 0;
     if(isset($_REQUEST['slug_ce'])) {
        $ce             = $_REQUEST['slug_ce'];
        $_SESSION['ce'] = $ce;
        
     } else {
      // $ce = 0;
      // $_SESSION['ce'] = $ce;
     }
     
     if(isset($_REQUEST['slug_ce2'])) {
        $praca             = $_REQUEST['slug_ce2'];
        $_SESSION['praca'] = $praca;
        
        // *************************************
        // pesquisar o cnpj e o customercode
        // ********************************************************************
        
        // $_SESSION['customercode'] = "3501"; 
        // $_SESSION['cnpj'] = "08698902000155";
        
               $servername = $host1;
               $username   = $user1;
               $password   = $pass1;
               $dbname     = $dbname1;
               
            // Create connection
               $conn = new mysqli($servername, $username, $password, $dbname);
               // Check connection
               if ($conn->connect_error) {
                  die("Connection failed: " . $conn->connect_error);
               }
       
                  $sql =  "select contatos.customer_name as nome, cnpj , customercode from contatos
                           where contatos.customer_name = '" . $praca . "'";
               
               // echo $sql; 
               
               if ($result = $conn->query($sql)) {

               /* fetch associative array */
               while ($row = $result->fetch_assoc()) {
                         $descricao =  $row["nome"];
                         $_SESSION['customercode'] = $row["customercode"];
                         $_SESSION['cnpj'] = $row["cnpj"];
         
               } 
               }
               
               /* commit transaction */
               $conn->commit();
    
               $conn->close(); 
        
        
        // ********************************************************************

        
     } else {
      // $ce = 0;
      // $_SESSION['ce'] = $ce;
     }
    
     
    
    if (isset($this->Dados[0])):
    $catArtigos = $this->Dados[0];
    //var_dump($catArtigos);
    endif;
   
   
  
?>    
                  
            
             
             
    
              
             <div class="form-group">
                 
             <label class="col-sm-1 control-label">Praça:</label>  
             <div class="col-sm-4">
             <?php   
                if ($_SESSION['niveis_acesso_id'] == "1" 
                 or $_SESSION['niveis_acesso_id'] == "2"
                 or $_SESSION['niveis_acesso_id'] == "4"       
                        )   {
             ?>    
                <select class="form-control" name="slug_ce2" id="slug_ce2">
             <?php    
                        
                } else {
                
             ?>    
                  <select class="form-control" disabled name="slug_ce2" id="slug_ce2">   
             <?php
                }
             ?>
             <option value="0"> </option>
              
              <option value="<?php echo $descricao2; ?>" <?php echo $selectedField; ?>><?php echo $descricao2; ?> </option> 
              <?php          
               if ($_SESSION['niveis_acesso_id'] == "1"
                or $_SESSION['niveis_acesso_id'] == "2"
                or $_SESSION['niveis_acesso_id'] == "4"        
                       )   {
               $servername = $host1;
               $username   = $user1;
               $password   = $pass1;
               $dbname     = $dbname1;
               
            // Create connection
               $conn = new mysqli($servername, $username, $password, $dbname);
               // Check connection
               if ($conn->connect_error) {
                  die("Connection failed: " . $conn->connect_error);
               }
       
                       $sql =  "select contatos.customer_name as nome from carousels, contatos
                                where   contatos.customercode = carousels.customercode
                                group by contatos.customer_name order by contatos.customer_name" ;  
               
               // echo $sql; 
               
               if ($result = $conn->query($sql)) {

               /* fetch associative array */
               while ($row = $result->fetch_assoc()) {
                         $descricao =  $row["nome"];
                         if($descricao == $_REQUEST["slug_ce2"]) {
                             $selectedField = "selected='selected'";
                         } else {
                             $selectedField = "";
                         }
         ?>
                        <option value="<?php echo $descricao; ?>" <?php echo $selectedField; ?>><?php echo $descricao; ?> </option> 
                 
         <?php          
    //          $descricao =  $row["description"];
    //          $precocapa =  $row["price"];
    //          $packet    =  $row["packet"];
              //echo $packet; 
               } 
               }
               
               /* commit transaction */
               $conn->commit();
    
               $conn->close(); 
 
               }
    // *************************************************************         
                $descricao1  = $_SESSION['ce'];  
    $descricao2 = $_SESSION['praca'];
    $selectedField = "selected='selected'"; 
               
      ?>     

             
             
             </select>  
             </div>    
             <div class="form-group">    
             <label class="col-sm-3 control-label">Resposta da Chamada de Encalhe:</label>
             <div class="col-sm-4">
             <select class="form-control" name="slug_ce" id="slug_ce">
                 <option value="0"> </option>  
                   <option value="<?php echo $descricao1; ?>" <?php echo $selectedField; ?>><?php echo $descricao1; ?> </option>
      <?php          
               
               $servername = $host1;
               $username   = $user1;
               $password   = $pass1;
               $dbname     = $dbname1;
               
            // Create connection
               $conn = new mysqli($servername, $username, $password, $dbname);
               // Check connection
               if ($conn->connect_error) {
                  die("Connection failed: " . $conn->connect_error);
               }
       
                       $sql =  "select"
                         . " cde_number "
                         . "FROM carousels"
                         . "  group by cde_number" ;  
               
               // echo $sql; 
               
               if ($result = $conn->query($sql)) {

               /* fetch associative array */
               while ($row = $result->fetch_assoc()) {
                         $descricao =  $row["cde_number"];
                         if($descricao == $_REQUEST["slug_ce"]) {
                             $selectedField = "selected='selected'";
                         } else {
                             $selectedField = "";
                         }
         ?>
                        <option value="<?php echo $descricao; ?>" <?php echo $selectedField; ?>><?php echo $descricao; ?> </option> 
                 
         <?php          
    //          $descricao =  $row["description"];
    //          $precocapa =  $row["price"];
    //          $packet    =  $row["packet"];
              //echo $packet; 
               } 
               }
               
               /* commit transaction */
               $conn->commit();
    
               $conn->close(); 
 
    
    // *************************************************************         
                 
      ?>           
                </select>
                 
                 <label class="col-sm-10 control-label"></label>
                 
                 
                 
                 
                 
                 
                 
                 
            </div> 
    
    
<?php     
    
     $varcount = 0;
     
     $valor_venda = 0;
     $desconto    = 0;
     $valorce     = 0;
     
     // echo "Variavel de Sessão CE: " . $_SESSION['ce'];
    
     
    if (isset($_SESSION['msg'])):
        echo $_SESSION['msg'];
        unset($_SESSION['msg']);
    endif;
    if (isset($_SESSION['msgcad'])):
        echo $_SESSION['msgcad'];
        unset($_SESSION['msgcad']);
    endif;
    
    
     // echo $_SESSION['customercode'];    
    $paginacao = $this->Dados[1];
    $this->Dados = $this->Dados[0];
    
    ?>
    <!--<div class="pull-right">
        
        <a href="<?php echo URL; ?>controle-artigo/Gerar_excel_ce_p_dsitrib"><button type="button" class="btn btn-danger">Gerar Excel</button></a> 
      
    </div>-->
             
    
             
             
             


    <?php
    
    
    
    if (!empty($this->Dados)):
        
       
        
        ?>
        <table class="table table-striped table-bordered">
            <thead>
           
            
<?php
   

?>          
            
            <?php
           
           
            ?>
            
            
            <p id="myText5"></p>    
            
            <div class="form-group">
           
            
             <div class="col-sm-10" >
                  
                 <label class="col-sm-4 control-label">Quantidade de Documentos: :</label>
                              
                 <p id="myText"></p>
                 

                <!--<button onclick="myFunction()">Try it</button>-->
                
                 
                 
                 
               <?php
    
               

          
                        
    
                ?>
               
                 
                 
                 
            </div>
             
            </div>    
            
                
                <tr>
                    <th>Id</th>
                    <th>Praça</th>
                    <th>cnpj</th>
                    <th>CE</th>
                    <th>Produto</th>
                    <th>Vlr Unit</th>
                    <th>tipo</th>
                    <th>Disrib</th>
                    <th>Dev</th>
                    <th>Vendido</th>
                    <th>Valor</th>
                    <th>Desconto</th>
                    <th>Valor da CE</th>
                </tr>
            </thead>
            <tbody>
                
                <?php
                foreach ($this->Dados as $nivelAcesso) :
                    extract($nivelAcesso);
               
               // pesquisar tabela de produtos
               
               // Create connection
   //            $conn = new mysqli($servername, $username, $password, $dbname);
   //             // Check connection
   //            if ($conn->connect_error) {
   //               die("Connection failed: " . $conn->connect_error);
   //            }
   //
   //            $rce_id_prod_Planeta = $id_prod_Planeta;
               
   //            $sql     =  "SELECT"
   //                      . " id_product,"
   //                      . " id_collection ,"
   //                      . " id_edition, "
   //                      . " edition_number, "
   //                      . " id_edition, "
   //                      . " edition_number, "
   //                      . " id_prod_Planeta, "
   //                      . " description, "
   //                      . " price, "
   //                      . " reordable, "
   //                      . " returnable, "
   //                      . " packet "
   //                      . "FROM categorias_artigos"
   //                      . " where id_prod_Planeta = '" . $rce_id_prod_Planeta . "'"; 
               
   //            // echo $sql; 
               
   //            if ($result = $conn->query($sql)) {

               /* fetch associative array */
   //            while ($row = $result->fetch_assoc()) {
        
              
              // echo "<br />" ; 
              // echo $row["description"];
              // echo " ";
              // $descricao =  $row["description"];
   //           $precocapa =  $row["price"];
   //           $packet    =  $row["packet"];
              //echo $packet; 
   //            } 
   //            }
               
               
               /* commit transaction */
   //            $conn->commit();
    
   //            $conn->close(); 
     
                // ***********************************************************
                
               //if($cde_number == $ce) {
                   
                    $varcount++;
                    ?>               
                    <tr>
                        
                        <td><?php echo $customercode; ?></td>
                        <td><?php echo $customer_name; ?></td>
                        <td><?php echo $cnpj; ?></td>
                        <td><?php echo $cde_number; ?></td>
                        <td><?php echo $prod; ?></td>
                        <td><?php echo $price; ?></td>
                        <td><?php echo $recall_type; ?></td>
                        <td><?php echo $total_distributed; ?></td>
                        <td><?php echo $return_quantity; ?></td>
                        <td><?php echo $sales_quantity; ?></td>
                        <td><?php echo $vlr; ?></td>
                        <td><?php echo $desconto; ?></td>
                        <td><?php echo $vlrce; ?></td>
                        
                        <?php 
                        
                       //IF (trim($recall_type) == "NORMAL") {
                       //       $sales = $total_distributed - $return2;  
                       // } else {
                       //       $sales = $sales - $return2; 
                       // }
                        
                        //************************
                 //        $sales = $sales - $return2; 
                 //       IF (trim($recall_type) == "NORMAL") {
                 //           $sales = $total_distributed-$return2; 
                 //        } else {
     
                 //          IF (trim($recall_type) == "NORMAL                                            ") {
                 //              $sales = $total_distributed-$return2; 
                 //         } else {
                  //         $sales = $sales - $return2; 
                  //         }                   
                  //          }
                        
                                                    
                        
                        // ***********************
                     
                          //$precocapa = number_format($precocapa, 2, ',', ' ');
                         
                        ?>
                        
                        
                        <!--<td><?php echo $sales; ?></td>-->
                        
                        <!-- Preço da Capa -->
                       
                        <!--<td><?php echo $precocapa ?></td>-->
                        
                       
                        
                        <!-- Valor da Venda -->
                        
                        <!--<td><?php echo $precovenda ?></td>-->
                        
                        <?php 
                        //$devolucao = $precocapa * $return2;
                        //$precodesconto = ($precovenda * $discount) / 100;
                        //$valor_desconto= $precodesconto;
                       
                        //$desconto= $desconto+$valor_desconto;
                                               
                        ?>
                        
                        <!-- Pacote Padrão -->
                        
                        <!--<td><?php echo $packet ?></td>-->
                        
                        <?php 
                        //$valorce=$valor_venda-$desconto;
                        ?>
                        
                        
                        
   
     
                        
                       
                        <!-- <td class="hidden-xs"><?php 
                        $imagem_artigo = URL . "assets/imagens/artigo/$id/$foto";
                        echo "<img src='$imagem_artigo' height='45' width='65' alt='$titulo_artigo'>";
                        ?></td>
                        <td><?php echo $titulo_artigo; ?></td>
                        <td class="hidden-xs"><?php echo $categorias_artigos; ?></td> -->
                        
                        <!--<td>
                           <!-- <a href="<?php echo URL; ?>controle-artigo/visualizar/<?php echo $id; ?>"><button type="button" class="btn btn-primary">Visualizar</button></a> -->

                           <!-- <a href="<?php echo URL; ?>controle-artigo/editar/<?php echo $id; ?>"><button type="button" class="btn btn-warning">Alterar</button></a> -->

                             <!-- <a href="<?php echo URL; ?>controle-artigo/visualizar/<?php echo $id; ?>"><button type="button" class="btn btn-danger">Fechar</button></a> --> 
                             <!-- <a href="<?php echo URL; ?>controle-artigo/apagar/<?php echo $id; ?>"><button type="button" class="btn btn-danger">Apagar</button></a> -->
                        </td-->
                    </tr>
                    
                    <?php
              // }
                
                endforeach;
               
                
                
                ?>
                    
                
                    
                    
            </tbody>
            
        </table>
    
        
        <!-- <div class="col-sm-10">
                 
                   <label class="col-sm-2 control-label">Total:</label>
                   
                    <?php
                   $valor_venda2 = number_format($valor_venda, 2, ',', ' ');
                   echo $valor_venda2;
                    ?>
                   <!-- <p id="myText1"></p> 
        </div>-->
        <!--<div class="col-sm-10">
                
                
                   <label class="col-sm-2 control-label">Desconto:</label>
                   <?php
                   $desconto2 = number_format($desconto, 2, ',', ' ');
                   echo $desconto2;
                   ?>
                   <!--  <p id="myText2"></p> 
                    
        </div>-->
        <!-- <div class="col-sm-10">
                
               
                   <label class="col-sm-2 control-label">Valor CE:</label>
                   <?php
                   $valorce2 = number_format($valorce, 2, ',', ' ');
                    echo $valorce2;
                   ?>
                   
                    <!-- <p id="myText3"></p> 
        </div> -->
        
             <?php
                         
             // echo " ";
             // $valor_venda2 = number_format($valor_venda, 2, ',', ' ');
             // echo "Total      : " . $valor_venda2;
             // echo " ";
             // $desconto2 = number_format($desconto, 2, ',', ' ');
             // echo "Desconto      : " . $desconto2;
             // echo " ";
             //  $valorce2 = number_format($valorce, 2, ',', ' ');
             // echo "Valor da CE : " . $valorce2;
             
             ?>
             
           <script>myFunction(<?php echo $varcount;?>, <?php echo $valor_venda;?> , <?php echo $desconto;?> ,<?php echo $valorce;?>  )</script> 
           
           
           
           
           
           
        <?php
    endif;
     
    
    //echo Mask("#.###.###.##",$desconto);
    
    
     //if ( $varcount!=0){
     // echo $paginacao;
     //}
    
    
    
    
   
    // var_dump($this->Dados);
    ?>
    
    
</div>