$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 -->