<?php 
        header('Content-Type: text/html; charset=UTF-8');
            require_once("funciones.php");
            $Empresas = array();

         ///---------------------------------------
         //
         //
         //         Pendientes Proceso Sistema General
         //
         //
         //----------------------------------------        
        $sql ="
            select  
			count(f.rowid) as pendientes
        ,	nombre_empresa 
        ,   empresa.rowid 

        from fi_facturas  f 
        
		left join    fi_configuracion_empresa  empresa  on empresa.rowid  =  f.entidad 
		
        where 
    		   f.version = '4.3'
	    and  ( f.electronica_enviada = 0) 
        and  ( f.estado > 0            )
        and  ( character_length(consecutivo) = 20 and character_length(clave) = 50   )
        
        group by 
        
        f.entidad";
        
         $dbSUPERIOR = $BDARREGLO['general']->prepare($sql);
         $dbSUPERIOR->execute();
         while ($factura= $dbSUPERIOR->FETCH(PDO::FETCH_OBJ)) {
              $Empresas[$factura->rowid]['por_enviar']   = $factura->pendientes;
              $Empresas[$factura->rowid]['nombre']       = $factura->nombre_empresa;

         }
                  $dbSUPERIOR->closeCursor(); 

         ///---------------------------------------
         //
         //
         //         Pendientes Email  4.2 
         //
         //
         //----------------------------------------        
         $sql="
           SELECT 
           count(factura.rowid) as correosPendientes
         , empresa.nombre_empresa 
         , empresa.rowid 
         
         FROM `fi_facturas` as factura
         left join fi_configuracion_empresa as empresa on empresa.entidad = factura.entidad 
         where 
         factura.version = '4.2' and factura.fecha >= '2018-04-16' and factura.estado = 1 and factura.electronica_resultado = 1 AND factura.electronica_error = 0 AND factura.envio_correo_factura = 0 
         group by empresa.rowid";
         $dbSUPERIOR = $BDARREGLO['general']->prepare($sql);
         $dbSUPERIOR->execute();
         while ($factura= $dbSUPERIOR->FETCH(PDO::FETCH_OBJ)) {
              $Empresas[$factura->rowid]['correosPendientes'] = $factura->correosPendientes;
              $Empresas[$factura->rowid]['nombre']       = $factura->nombre_empresa;
         }


                  $dbSUPERIOR->closeCursor(); 

         ///---------------------------------------
         //
         //
         //         Pendientes Email  4.3
         //
         //
         //----------------------------------------        
         $sql="
           SELECT 
           count(factura.rowid) as correosPendientes
         , empresa.nombre_empresa 
         , empresa.rowid 
         
         FROM `fi_facturas` as factura
         left join fi_configuracion_empresa as empresa on empresa.entidad = factura.entidad 
         where 
         factura.version = '4.3' and factura.fecha >= '2019-07-01' and factura.estado = 1 and factura.electronica_resultado = 1 AND factura.electronica_error = 0 AND factura.envio_correo_factura = 0 
         group by empresa.rowid ";
         $dbSUPERIOR = $BDARREGLO['general']->prepare($sql);
         $dbSUPERIOR->execute();
         while ($factura= $dbSUPERIOR->FETCH(PDO::FETCH_OBJ)) {
              $Empresas[$factura->rowid]['correosPendientes3'] = $factura->correosPendientes;
              $Empresas[$factura->rowid]['nombre']       = $factura->nombre_empresa;
         }



         
 
                  $dbSUPERIOR->closeCursor(); 



         ///---------------------------------------
         //
         //
         //         Pendientes Revision Sistema General 
         //
         //
         //----------------------------------------        
        $sql ="
            select  
			count(f.rowid) as revisar
        ,	nombre_empresa 
        ,   empresa.rowid 
        ,   f.entidad 
        
        from fi_facturas  f 
        
		left join    fi_configuracion_empresa  empresa  on empresa.rowid  =  f.entidad 
		
        where 
    		   f.version = '4.3'
	    and ( fecha_creacion_server > '2019-07-01' )  
        and   electronica_resultado	 = 0  and electronica_enviada = 1  
        
        group by 
        
        empresa.rowid ";
        
        
         $dbSUPERIOR = $BDARREGLO['general']->prepare($sql);
         $dbSUPERIOR->execute();
         while ($factura= $dbSUPERIOR->FETCH(PDO::FETCH_OBJ)) {
              $Empresas[$factura->rowid]['por_revisar'] = $factura->revisar;
              $Empresas[$factura->rowid]['nombre']       = $factura->nombre_empresa;
              $Empresas[$factura->rowid]['entidad']       = $factura->entidad;
              

         }
         
 
                           $dbSUPERIOR->closeCursor(); 

         
         
          echo "<table border='1' >";
          echo "<tr>
                   
                    <td>Empresa</td>
                    <td>Por Enviar</td>
                    <td>Por Revisar</td>
                    <td>Email 4.2</td>
                    <td>Email 4.3</td>

                </tr>";
                
          foreach ($Empresas as $id => $detalle ){
                 
                    echo "<tr>
                                
                                <td ><strong>".$detalle['nombre']."</strong></td>
                                <td  align='center'>".$detalle['por_enviar']."</td>
                                <td  align='center' >".$detalle['por_revisar']."</td>                                
                                <td  align='center' >".$detalle['correosPendientes']."</td>                                
                                <td  align='center' >".$detalle['correosPendientes3']."</td>                                


                          </tr>        ";    
                          
                    $total['por_enviar']         += $detalle['por_enviar' ]            ;
                    $total['por_revisar']        += $detalle['por_revisar']            ;
                    $total['correosPendientes']  += $detalle['correosPendientes']      ;
                    $total['correosPendientes3'] += $detalle['correosPendientes3']     ;


                    
          }
          
          echo "<tr>";
            echo "<td style='background-color:grey;' > - </td>";
            echo "<td align='center' > ".$total['por_enviar'] ." </td>";
            echo "<td align='center' > ".$total['por_revisar']." </td>";
            echo "<td align='center' > ".$total['correosPendientes']." </td>";
            echo "<td align='center' > ".$total['correosPendientes3']." </td>";
            
            
            
          echo "</tr>";
          echo "</table>";
          