Skip to main content
se añadieron 10 caracteres en el cuerpo
Origen Enlace
Noel L
  • 235
  • 2
  • 7
  • 35
$archivo = 'archivo.txt';
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$archivo);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archivo));
 
include($archivo);




class database {

private $conn;
private $serverName;
private $connectionInfo;

public function __construct()
{
$serverName = "000.000.00.00"; // Ip de conexion
$connectionInfo = array( "Database"=>"bdd", "blabla"=>"user", "PWD"=>"contraseña"); // Cadena de conexión

$this->conn = sqlsrv_connect( $serverName, $connectionInfo );

if( $this->conn === false ) {

die( print_r( sqlsrv_errors(), true));  //  muestra mensaje si falla
}

}




public function consulta queryMonth  ($fecha1,$fecha2)
{


$res = array();
$rowres = "";


echo $fecha1;

$stmt = sqlsrv_query( $this->conn,"with cte AS ( 
                                    select DISTINCT c.CodeCompany as CompanyCode,
                                    CONVERT(char(6), GETDATE(), 112) as Period,
                                                        a.idClave as EmployeID,
                                     c.nombre + ' ' + c.apellidos as EmployeName,
                                                        d.idDepto as Depto,
                                                     t.HorasAlMes as PlanWorkHoursMonth,
               SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth
    
    
        from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
                             JOIN tblDepto    d ON c.fkDepto=d.idDepto
                             JOIN tblTurno    t ON c.fkTurno=t.idTurno
                             
             where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and 
                CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1 
            
            group by
            c.CodeCompany, 
            a.idClave, 
            d.idDepto,
            t.HorasAlMes,
            c.nombre + ' ' + c.apellidos)

select  CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,


OvertimeHoursMonth=
    case 
        when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
        
    else 0
            
    end,

LeaveHoursMonth=

    case 
        when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
    else 0
    
    end

    
   
 from cte");
                                      

if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}




$archivo= 'archivo.txt'; 


$handler = fopen($archivo,'w');
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    fwrite($handler,$row["CompanyCode"]);
    fwrite($handler,$row["Period"]);
}
fclose($handler);





while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {

$rowres .= '<tr class="success">
    <td>'.$row["CompanyCode"].' </td>
    <td>'.$row["Period"].' </td>
    <td>'.$row["EmployeID"]. '</td>
    <td>'.utf8_encode($row["EmployeName"]). '</td>
    <td>'.$row["Depto"]. '</td>
    <td>'.$row["PlanWorkHoursMonth"].'</td>
    <td>'.$row["ActualWorkHoursMonth"].'</td>
    <td>'.$row["OvertimeHoursMonth"].'</td>
    <td>'.$row["LeaveHoursMonth"].'</td>
    
  </tr>';


}


sqlsrv_free_stmt( $stmt);   
return $rowres;





}   
<div class="main">

<div class="main-inner">

    <div class="container">

      <div class="row">
        
        <div class="span12">            
            
            <div class="widget ">
                <a name="head"></a>     
                <div class="widget-header">
                    <i class="icon-user"></i>
                    <h3> R.1 </h3>
                </div> <!-- /widget-header -->
                
                <div class="widget-content">                    
                    <div class="tabbable">
                    <br>        
                            <form id="edit-profile"  method="POST"  action ="document.php" class="form-horizontal">
                                <fieldset>                                      
                                                
                                    <div class="control-group">                                         
                                    
                                        <label class="control-label" for="date1">Fecha Inicio:</label>
                                        <div class="controls">
                                        <input type="text"  id="date1" name="date1" value="<?php if(isset($_POST['date1'])) echo $_POST['date1']; else 
                                            echo date('d-m-Y'); ?>">    
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                                    
                                            <div class="control-group">                                         
                                        <label class="control-label" for="date2">Fecha Final:</label>
                                        <div class="controls">
                                            <input type="text"  id="date2"  name="date2"  value="<?php if(isset($_POST['date2'])) echo $_POST['date2']; else 
                                            echo date('d-m-Y'); ?>" >
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                    
                                     <br />
                                            
                                    <div class="form-actions">
                                        <button type="submit" class="btn btn-primary">Buscar </button> 
                            
                                    </div> <!-- /form-actions -->
                                </fieldset>
                            </form>
                    

 <table id="example" class="table table-striped table-bordered" >


<thead>
  <tr>
    <th>CompanyCode</th>
    <th>Period</th>
    <th>EmployeID</th>
    <th>EmployeName</th>
    <th>Depto</th>
    <th>PlanWorkHoursMonth</th>
    <th>ActualWorkHoursMonth</th>
    <th>OvertimeHoursMonth</th>
    <th>LeaveHoursMonth</th>
  </tr>
</thead>
<tbody> 

<?php 




if (isset($_POST['date1']) && isset($_POST['date2']))
{


echo $db->consulta>   queryMonth($_POST['date1'],$_POST['date2']); 






}else
{
    echo "<p> Coloque fecha de inicio y fecha de fin </p>";
}


?>

  </tbody>
</table>
    
                            
                            </div>
                            
                    </div>
                    
                    
                </div>              
            </div>
        </div>
        
        
        
        
      </div> <!-- /row -->
    </div> <!-- /container -->      
</div> <!-- /main-inner -->
$archivo = 'archivo.txt';
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$archivo);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archivo));
 
include($archivo);




class database {

private $conn;
private $serverName;
private $connectionInfo;

public function __construct()
{
$serverName = "000.000.00.00"; // Ip de conexion
$connectionInfo = array( "Database"=>"bdd", "blabla"=>"user", "PWD"=>"contraseña"); // Cadena de conexión

$this->conn = sqlsrv_connect( $serverName, $connectionInfo );

if( $this->conn === false ) {

die( print_r( sqlsrv_errors(), true));  //  muestra mensaje si falla
}

}




public function consulta($fecha1,$fecha2)
{


$res = array();
$rowres = "";


echo $fecha1;

$stmt = sqlsrv_query( $this->conn,"with cte AS ( 
                                    select DISTINCT c.CodeCompany as CompanyCode,
                                    CONVERT(char(6), GETDATE(), 112) as Period,
                                                        a.idClave as EmployeID,
                                     c.nombre + ' ' + c.apellidos as EmployeName,
                                                        d.idDepto as Depto,
                                                     t.HorasAlMes as PlanWorkHoursMonth,
               SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth
    
    
        from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
                             JOIN tblDepto    d ON c.fkDepto=d.idDepto
                             JOIN tblTurno    t ON c.fkTurno=t.idTurno
                             
             where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and 
                CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1 
            
            group by
            c.CodeCompany, 
            a.idClave, 
            d.idDepto,
            t.HorasAlMes,
            c.nombre + ' ' + c.apellidos)

select  CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,


OvertimeHoursMonth=
    case 
        when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
        
    else 0
            
    end,

LeaveHoursMonth=

    case 
        when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
    else 0
    
    end

    
   
 from cte");
                                      

if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}




$archivo= 'archivo.txt'; 


$handler = fopen($archivo,'w');
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    fwrite($handler,$row["CompanyCode"]);
    fwrite($handler,$row["Period"]);
}
fclose($handler);





while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {

$rowres .= '<tr class="success">
    <td>'.$row["CompanyCode"].' </td>
    <td>'.$row["Period"].' </td>
    <td>'.$row["EmployeID"]. '</td>
    <td>'.utf8_encode($row["EmployeName"]). '</td>
    <td>'.$row["Depto"]. '</td>
    <td>'.$row["PlanWorkHoursMonth"].'</td>
    <td>'.$row["ActualWorkHoursMonth"].'</td>
    <td>'.$row["OvertimeHoursMonth"].'</td>
    <td>'.$row["LeaveHoursMonth"].'</td>
    
  </tr>';


}


sqlsrv_free_stmt( $stmt);   
return $rowres;





}   
<div class="main">

<div class="main-inner">

    <div class="container">

      <div class="row">
        
        <div class="span12">            
            
            <div class="widget ">
                <a name="head"></a>     
                <div class="widget-header">
                    <i class="icon-user"></i>
                    <h3> R.1 </h3>
                </div> <!-- /widget-header -->
                
                <div class="widget-content">                    
                    <div class="tabbable">
                    <br>        
                            <form id="edit-profile"  method="POST"  action ="document.php" class="form-horizontal">
                                <fieldset>                                      
                                                
                                    <div class="control-group">                                         
                                    
                                        <label class="control-label" for="date1">Fecha Inicio:</label>
                                        <div class="controls">
                                        <input type="text"  id="date1" name="date1" value="<?php if(isset($_POST['date1'])) echo $_POST['date1']; else 
                                            echo date('d-m-Y'); ?>">    
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                                    
                                            <div class="control-group">                                         
                                        <label class="control-label" for="date2">Fecha Final:</label>
                                        <div class="controls">
                                            <input type="text"  id="date2"  name="date2"  value="<?php if(isset($_POST['date2'])) echo $_POST['date2']; else 
                                            echo date('d-m-Y'); ?>" >
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                    
                                     <br />
                                            
                                    <div class="form-actions">
                                        <button type="submit" class="btn btn-primary">Buscar </button> 
                            
                                    </div> <!-- /form-actions -->
                                </fieldset>
                            </form>
                    

 <table id="example" class="table table-striped table-bordered" >


<thead>
  <tr>
    <th>CompanyCode</th>
    <th>Period</th>
    <th>EmployeID</th>
    <th>EmployeName</th>
    <th>Depto</th>
    <th>PlanWorkHoursMonth</th>
    <th>ActualWorkHoursMonth</th>
    <th>OvertimeHoursMonth</th>
    <th>LeaveHoursMonth</th>
  </tr>
</thead>
<tbody> 

<?php 




if (isset($_POST['date1']) && isset($_POST['date2']))
{


echo $db->consulta($_POST['date1'],$_POST['date2']); 






}else
{
    echo "<p> Coloque fecha de inicio y fecha de fin </p>";
}


?>

  </tbody>
</table>
    
                            
                            </div>
                            
                    </div>
                    
                    
                </div>              
            </div>
        </div>
        
        
        
        
      </div> <!-- /row -->
    </div> <!-- /container -->      
</div> <!-- /main-inner -->
$archivo = 'archivo.txt';
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$archivo);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archivo));
 
include($archivo);




class database {

private $conn;
private $serverName;
private $connectionInfo;

public function __construct()
{
$serverName = "000.000.00.00"; // Ip de conexion
$connectionInfo = array( "Database"=>"bdd", "blabla"=>"user", "PWD"=>"contraseña"); // Cadena de conexión

$this->conn = sqlsrv_connect( $serverName, $connectionInfo );

if( $this->conn === false ) {

die( print_r( sqlsrv_errors(), true));  //  muestra mensaje si falla
}

}




public function  queryMonth  ($fecha1,$fecha2)
{


$res = array();
$rowres = "";


echo $fecha1;

$stmt = sqlsrv_query( $this->conn,"with cte AS ( 
                                    select DISTINCT c.CodeCompany as CompanyCode,
                                    CONVERT(char(6), GETDATE(), 112) as Period,
                                                        a.idClave as EmployeID,
                                     c.nombre + ' ' + c.apellidos as EmployeName,
                                                        d.idDepto as Depto,
                                                     t.HorasAlMes as PlanWorkHoursMonth,
               SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth
    
    
        from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
                             JOIN tblDepto    d ON c.fkDepto=d.idDepto
                             JOIN tblTurno    t ON c.fkTurno=t.idTurno
                             
             where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and 
                CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1 
            
            group by
            c.CodeCompany, 
            a.idClave, 
            d.idDepto,
            t.HorasAlMes,
            c.nombre + ' ' + c.apellidos)

select  CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,


OvertimeHoursMonth=
    case 
        when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
        
    else 0
            
    end,

LeaveHoursMonth=

    case 
        when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
    else 0
    
    end

    
   
 from cte");
                                      

if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}




$archivo= 'archivo.txt'; 


$handler = fopen($archivo,'w');
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    fwrite($handler,$row["CompanyCode"]);
    fwrite($handler,$row["Period"]);
}
fclose($handler);





while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {

$rowres .= '<tr class="success">
    <td>'.$row["CompanyCode"].' </td>
    <td>'.$row["Period"].' </td>
    <td>'.$row["EmployeID"]. '</td>
    <td>'.utf8_encode($row["EmployeName"]). '</td>
    <td>'.$row["Depto"]. '</td>
    <td>'.$row["PlanWorkHoursMonth"].'</td>
    <td>'.$row["ActualWorkHoursMonth"].'</td>
    <td>'.$row["OvertimeHoursMonth"].'</td>
    <td>'.$row["LeaveHoursMonth"].'</td>
    
  </tr>';


}


sqlsrv_free_stmt( $stmt);   
return $rowres;





}   
<div class="main">

<div class="main-inner">

    <div class="container">

      <div class="row">
        
        <div class="span12">            
            
            <div class="widget ">
                <a name="head"></a>     
                <div class="widget-header">
                    <i class="icon-user"></i>
                    <h3> R.1 </h3>
                </div> <!-- /widget-header -->
                
                <div class="widget-content">                    
                    <div class="tabbable">
                    <br>        
                            <form id="edit-profile"  method="POST"  action ="document.php" class="form-horizontal">
                                <fieldset>                                      
                                                
                                    <div class="control-group">                                         
                                    
                                        <label class="control-label" for="date1">Fecha Inicio:</label>
                                        <div class="controls">
                                        <input type="text"  id="date1" name="date1" value="<?php if(isset($_POST['date1'])) echo $_POST['date1']; else 
                                            echo date('d-m-Y'); ?>">    
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                                    
                                            <div class="control-group">                                         
                                        <label class="control-label" for="date2">Fecha Final:</label>
                                        <div class="controls">
                                            <input type="text"  id="date2"  name="date2"  value="<?php if(isset($_POST['date2'])) echo $_POST['date2']; else 
                                            echo date('d-m-Y'); ?>" >
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                    
                                     <br />
                                            
                                    <div class="form-actions">
                                        <button type="submit" class="btn btn-primary">Buscar </button> 
                            
                                    </div> <!-- /form-actions -->
                                </fieldset>
                            </form>
                    

 <table id="example" class="table table-striped table-bordered" >


<thead>
  <tr>
    <th>CompanyCode</th>
    <th>Period</th>
    <th>EmployeID</th>
    <th>EmployeName</th>
    <th>Depto</th>
    <th>PlanWorkHoursMonth</th>
    <th>ActualWorkHoursMonth</th>
    <th>OvertimeHoursMonth</th>
    <th>LeaveHoursMonth</th>
  </tr>
</thead>
<tbody> 

<?php 




if (isset($_POST['date1']) && isset($_POST['date2']))
{


echo $db->   queryMonth($_POST['date1'],$_POST['date2']); 






}else
{
    echo "<p> Coloque fecha de inicio y fecha de fin </p>";
}


?>

  </tbody>
</table>
    
                            
                            </div>
                            
                    </div>
                    
                    
                </div>              
            </div>
        </div>
        
        
        
        
      </div> <!-- /row -->
    </div> <!-- /container -->      
</div> <!-- /main-inner -->
se añadieron 6189 caracteres en el cuerpo
Origen Enlace
Noel L
  • 235
  • 2
  • 7
  • 35

Código de la conexion a la BDD y de la funcion queryMonth <?php

$archivo = 'archivo.txt';
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$archivo);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archivo));
 
include($archivo);




class database {

private $conn;
private $serverName;
private $connectionInfo;

public function __construct()
{
$serverName = "000.000.00.00"; // Ip de conexion
$connectionInfo = array( "Database"=>"bdd", "blabla"=>"user", "PWD"=>"contraseña"); // Cadena de conexión

$this->conn = sqlsrv_connect( $serverName, $connectionInfo );

if( $this->conn === false ) {

die( print_r( sqlsrv_errors(), true));  //  muestra mensaje si falla
}

}




public function consulta($fecha1,$fecha2)
{


$res = array();
$rowres = "";


echo $fecha1;

$stmt = sqlsrv_query( $this->conn,"with cte AS ( 
                                    select DISTINCT c.CodeCompany as CompanyCode,
                                    CONVERT(char(6), GETDATE(), 112) as Period,
                                                        a.idClave as EmployeID,
                                     c.nombre + ' ' + c.apellidos as EmployeName,
                                                        d.idDepto as Depto,
                                                     t.HorasAlMes as PlanWorkHoursMonth,
               SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth
    
    
        from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
                             JOIN tblDepto    d ON c.fkDepto=d.idDepto
                             JOIN tblTurno    t ON c.fkTurno=t.idTurno
                             
             where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and 
                CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1 
            
            group by
            c.CodeCompany, 
            a.idClave, 
            d.idDepto,
            t.HorasAlMes,
            c.nombre + ' ' + c.apellidos)

select  CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,


OvertimeHoursMonth=
    case 
        when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
        
    else 0
            
    end,

LeaveHoursMonth=

    case 
        when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
    else 0
    
    end

    
   
 from cte");
                                      

if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}




$archivo= 'archivo.txt'; 


$handler = fopen($archivo,'w');
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    fwrite($handler,$row["CompanyCode"]);
    fwrite($handler,$row["Period"]);
}
fclose($handler);





while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {

$rowres .= '<tr class="success">
    <td>'.$row["CompanyCode"].' </td>
    <td>'.$row["Period"].' </td>
    <td>'.$row["EmployeID"]. '</td>
    <td>'.utf8_encode($row["EmployeName"]). '</td>
    <td>'.$row["Depto"]. '</td>
    <td>'.$row["PlanWorkHoursMonth"].'</td>
    <td>'.$row["ActualWorkHoursMonth"].'</td>
    <td>'.$row["OvertimeHoursMonth"].'</td>
    <td>'.$row["LeaveHoursMonth"].'</td>
    
  </tr>';


}


sqlsrv_free_stmt( $stmt);   
return $rowres;





}   

}

?>

Código del HTML de donde se envia las fechas, fecha 1 y fecha2

<div class="main">

<div class="main-inner">

    <div class="container">

      <div class="row">
        
        <div class="span12">            
            
            <div class="widget ">
                <a name="head"></a>     
                <div class="widget-header">
                    <i class="icon-user"></i>
                    <h3> R.1 </h3>
                </div> <!-- /widget-header -->
                
                <div class="widget-content">                    
                    <div class="tabbable">
                    <br>        
                            <form id="edit-profile"  method="POST"  action ="document.php" class="form-horizontal">
                                <fieldset>                                      
                                                
                                    <div class="control-group">                                         
                                    
                                        <label class="control-label" for="date1">Fecha Inicio:</label>
                                        <div class="controls">
                                        <input type="text"  id="date1" name="date1" value="<?php if(isset($_POST['date1'])) echo $_POST['date1']; else 
                                            echo date('d-m-Y'); ?>">    
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                                    
                                            <div class="control-group">                                         
                                        <label class="control-label" for="date2">Fecha Final:</label>
                                        <div class="controls">
                                            <input type="text"  id="date2"  name="date2"  value="<?php if(isset($_POST['date2'])) echo $_POST['date2']; else 
                                            echo date('d-m-Y'); ?>" >
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                    
                                     <br />
                                            
                                    <div class="form-actions">
                                        <button type="submit" class="btn btn-primary">Buscar </button> 
                            
                                    </div> <!-- /form-actions -->
                                </fieldset>
                            </form>
                    

 <table id="example" class="table table-striped table-bordered" >


<thead>
  <tr>
    <th>CompanyCode</th>
    <th>Period</th>
    <th>EmployeID</th>
    <th>EmployeName</th>
    <th>Depto</th>
    <th>PlanWorkHoursMonth</th>
    <th>ActualWorkHoursMonth</th>
    <th>OvertimeHoursMonth</th>
    <th>LeaveHoursMonth</th>
  </tr>
</thead>
<tbody> 

<?php 




if (isset($_POST['date1']) && isset($_POST['date2']))
{


echo $db->consulta($_POST['date1'],$_POST['date2']); 






}else
{
    echo "<p> Coloque fecha de inicio y fecha de fin </p>";
}


?>

  </tbody>
</table>
    
                            
                            </div>
                            
                    </div>
                    
                    
                </div>              
            </div>
        </div>
        
        
        
        
      </div> <!-- /row -->
    </div> <!-- /container -->      
</div> <!-- /main-inner -->

Código de la conexion a la BDD y de la funcion queryMonth <?php

$archivo = 'archivo.txt';
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$archivo);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archivo));
 
include($archivo);




class database {

private $conn;
private $serverName;
private $connectionInfo;

public function __construct()
{
$serverName = "000.000.00.00"; // Ip de conexion
$connectionInfo = array( "Database"=>"bdd", "blabla"=>"user", "PWD"=>"contraseña"); // Cadena de conexión

$this->conn = sqlsrv_connect( $serverName, $connectionInfo );

if( $this->conn === false ) {

die( print_r( sqlsrv_errors(), true));  //  muestra mensaje si falla
}

}




public function consulta($fecha1,$fecha2)
{


$res = array();
$rowres = "";


echo $fecha1;

$stmt = sqlsrv_query( $this->conn,"with cte AS ( 
                                    select DISTINCT c.CodeCompany as CompanyCode,
                                    CONVERT(char(6), GETDATE(), 112) as Period,
                                                        a.idClave as EmployeID,
                                     c.nombre + ' ' + c.apellidos as EmployeName,
                                                        d.idDepto as Depto,
                                                     t.HorasAlMes as PlanWorkHoursMonth,
               SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth
    
    
        from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
                             JOIN tblDepto    d ON c.fkDepto=d.idDepto
                             JOIN tblTurno    t ON c.fkTurno=t.idTurno
                             
             where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and 
                CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1 
            
            group by
            c.CodeCompany, 
            a.idClave, 
            d.idDepto,
            t.HorasAlMes,
            c.nombre + ' ' + c.apellidos)

select  CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,


OvertimeHoursMonth=
    case 
        when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
        
    else 0
            
    end,

LeaveHoursMonth=

    case 
        when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
    else 0
    
    end

    
   
 from cte");
                                      

if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}




$archivo= 'archivo.txt'; 


$handler = fopen($archivo,'w');
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    fwrite($handler,$row["CompanyCode"]);
    fwrite($handler,$row["Period"]);
}
fclose($handler);





while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {

$rowres .= '<tr class="success">
    <td>'.$row["CompanyCode"].' </td>
    <td>'.$row["Period"].' </td>
    <td>'.$row["EmployeID"]. '</td>
    <td>'.utf8_encode($row["EmployeName"]). '</td>
    <td>'.$row["Depto"]. '</td>
    <td>'.$row["PlanWorkHoursMonth"].'</td>
    <td>'.$row["ActualWorkHoursMonth"].'</td>
    <td>'.$row["OvertimeHoursMonth"].'</td>
    <td>'.$row["LeaveHoursMonth"].'</td>
    
  </tr>';


}


sqlsrv_free_stmt( $stmt);   
return $rowres;





}   

}

?>

Código del HTML de donde se envia las fechas, fecha 1 y fecha2

<div class="main">

<div class="main-inner">

    <div class="container">

      <div class="row">
        
        <div class="span12">            
            
            <div class="widget ">
                <a name="head"></a>     
                <div class="widget-header">
                    <i class="icon-user"></i>
                    <h3> R.1 </h3>
                </div> <!-- /widget-header -->
                
                <div class="widget-content">                    
                    <div class="tabbable">
                    <br>        
                            <form id="edit-profile"  method="POST"  action ="document.php" class="form-horizontal">
                                <fieldset>                                      
                                                
                                    <div class="control-group">                                         
                                    
                                        <label class="control-label" for="date1">Fecha Inicio:</label>
                                        <div class="controls">
                                        <input type="text"  id="date1" name="date1" value="<?php if(isset($_POST['date1'])) echo $_POST['date1']; else 
                                            echo date('d-m-Y'); ?>">    
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                                    
                                            <div class="control-group">                                         
                                        <label class="control-label" for="date2">Fecha Final:</label>
                                        <div class="controls">
                                            <input type="text"  id="date2"  name="date2"  value="<?php if(isset($_POST['date2'])) echo $_POST['date2']; else 
                                            echo date('d-m-Y'); ?>" >
                                        </div> <!-- /controls -->               
                                    </div> <!-- /control-group -->
                    
                                     <br />
                                            
                                    <div class="form-actions">
                                        <button type="submit" class="btn btn-primary">Buscar </button> 
                            
                                    </div> <!-- /form-actions -->
                                </fieldset>
                            </form>
                    

 <table id="example" class="table table-striped table-bordered" >


<thead>
  <tr>
    <th>CompanyCode</th>
    <th>Period</th>
    <th>EmployeID</th>
    <th>EmployeName</th>
    <th>Depto</th>
    <th>PlanWorkHoursMonth</th>
    <th>ActualWorkHoursMonth</th>
    <th>OvertimeHoursMonth</th>
    <th>LeaveHoursMonth</th>
  </tr>
</thead>
<tbody> 

<?php 




if (isset($_POST['date1']) && isset($_POST['date2']))
{


echo $db->consulta($_POST['date1'],$_POST['date2']); 






}else
{
    echo "<p> Coloque fecha de inicio y fecha de fin </p>";
}


?>

  </tbody>
</table>
    
                            
                            </div>
                            
                    </div>
                    
                    
                </div>              
            </div>
        </div>
        
        
        
        
      </div> <!-- /row -->
    </div> <!-- /container -->      
</div> <!-- /main-inner -->
Origen Enlace
Noel L
  • 235
  • 2
  • 7
  • 35

Generar reporte en txt con consulta en SQL Server desde PHP

De la función siguiente queryMonth deseo generar los resultados de la consulta en un TXT.

Función queryMonth:

public function queryMonth($fecha1,$fecha2)
 {


$res = array();
$rowres = "";


$stmt = sqlsrv_query( $this->conn,"with cte AS ( 
                                select DISTINCT c.CodeCompany as CompanyCode,
                                CONVERT(char(6), GETDATE(), 112) as Period,
                                                    a.idClave as EmployeID,
                                 c.nombre + ' ' + c.apellidos as EmployeName,
                                                    d.idDepto as Depto,
                                                 t.HorasAlMes as PlanWorkHoursMonth,
           SUM(DATEDIFF(HOUR, fechaEntrada, fechaSalida) %24) as ActualWorkHoursMonth


    from tblasistencia a JOIN tblpersonal c ON a.idClave=c.idClave
                         JOIN tblDepto    d ON c.fkDepto=d.idDepto
                         JOIN tblTurno    t ON c.fkTurno=t.idTurno

         where fechaEntrada BETWEEN CONVERT(DATETIME, '".$fecha1." 00:00:00',103) and 
            CONVERT(DATETIME,'".$fecha2." 23:59:59',103)and c.fkStatus !=1 

        group by
        c.CodeCompany, 
        a.idClave, 
        d.idDepto,
        t.HorasAlMes,
        c.nombre + ' ' + c.apellidos)

select  CompanyCode,Period, EmployeID, EmployeName,Depto,PlanWorkHoursMonth,ActualWorkHoursMonth,


 OvertimeHoursMonth=
  case 
    when ActualWorkHoursMonth>PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)

  else 0

  end,

LeaveHoursMonth=

  case 
    when ActualWorkHoursMonth<PlanWorkHoursMonth then abs(PlanWorkHoursMonth-ActualWorkHoursMonth)
  else 0

  end



from cte");



if( $stmt === false) {
   die( print_r( sqlsrv_errors(), true) );
}

Aquí se supone que se debería generar el TXT $stmt trae el resultado de la consulta

$archivo = 'archivo.txt';

$handler = fopen($archivo,'a');
while($row = sqlsrv_fetch_array($stmt)) {
   fwrite($handler,$row['CompanyCode']);
   fwrite($handler,$row['Period']);
}
fclose($handler);

Con esto se visualizan los datos en una tabla en HTML

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {

$rowres .= '<tr class="success">
    <td>'.$row["CompanyCode"].' </td>
    <td>'.$row["Period"].' </td>
    <td>'.$row["EmployeID"]. '</td>
    <td>'.utf8_encode($row["EmployeName"]). '</td>
    <td>'.$row["Depto"]. '</td>
    <td>'.$row["PlanWorkHoursMonth"].'</td>
    <td>'.$row["ActualWorkHoursMonth"].'</td>
    <td>'.$row["OvertimeHoursMonth"].'</td>
    <td>'.$row["LeaveHoursMonth"].'</td>

   </tr>';
}


sqlsrv_free_stmt( $stmt);  
return $rowres;


}

Form con los POST de donde se envian las fechas: fecha1 y Fecha2 <?php

  if (isset($_POST['date1']) && isset($_POST['date2']))
  {


   echo $db->queryMonth($_POST['date1'],$_POST['date2']); 


  }else
  {
    echo "<p> Coloque fecha de inicio y fecha de fin </p>";
  }


  ?>

La sintaxis para generar el TXT:

  $archivo = 'archivo.txt';

  $handler = fopen($archivo,'a');
  while($row = sqlsrv_fetch_array($stmt)) {
    fwrite($handler,$row['CompanyCode']);
    fwrite($handler,$row['Period']);
 }
 fclose($handler);

Sin embargo no se esta generando el TXT.

Sin embargo si pongo las siguientes lineas de código en la parte superior de donde hago la conexión a la BDD genera un TXT guardando el código fuente de la página html, como se muestra en la imagen a continuación: introducir la descripción de la imagen aquí

Código que descarga un TXT con el contenido de la imagen anterior

$archivo = 'archivo.txt';
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$archivo);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($archivo));
 
include($archivo);