Prueba Reporteria #2- Solucion

parent fd5d74d0
function generateFileDownload(filename, data, xhr, replaceFilename){
console.log(filename);
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1 && replaceFilename) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1])
filename = matches[1].replace(/['"]/g, '');
}
var type = xhr.getResponseHeader('Content-Type');
var blob = new Blob([data], {type: type});
console.log(filename);
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a = document.createElement("a");
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location = downloadUrl;
}
setTimeout(function () {
URL.revokeObjectURL(downloadUrl);
}, 100); // cleanup
}
}
......@@ -4,7 +4,10 @@ namespace App\BackendBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\File;
use App\Utilities\CsvResponse;
use App\Utilities\CsvReporteGeneral;
use App\Entity\PluTag;
......@@ -29,8 +32,8 @@ class ReportesController extends AbstractController {
$this->manager = $this->getDoctrine()->getManager('default');
$compania = $request->get("compania", null);
$tipoReporte = $request->get("tipoReporte", null);
$fInicio = $request->get("fInicio", null);
$fFin = $request->get("fFin", null);
$fInicio = $request->get("fechaInicio", null);
$fFin = $request->get("fechaFin", null);
$log->debug("compania $compania |tipoReporte $tipoReporte | fInicio $fInicio | fFin $fFin");
......@@ -57,7 +60,7 @@ class ReportesController extends AbstractController {
$lineaCsv->setFecha($registro->getCreatedAt()->format("Y-m-d H:i:s"));
$conjuntoDatosCsv[] = $lineaCsv;
} else {
if ($compania == $registro->getLisId()->getLisTag()->getTagId()) {
if ($compania == $registro->getLisId()->getTag()->getTagId()) {
$lineaCsv->setId($registro->getSusId());
$lineaCsv->setUsuario($registro->getSusMsisdn());
......@@ -76,8 +79,28 @@ class ReportesController extends AbstractController {
endforeach;
$dataCSV = Utiles::generarReporteGeneralCSV($conjuntoDatosCsv);
$response = new CsvResponse($dataCSV, 200);
$file = fopen("php://output", 'w');
foreach ($dataCSV as $fields) {
fputcsv($file, $fields);
}
$data = '';
while ($line = fgets($file)) {
$data .= $line;
}
$data .= fgets($file);
$response = new Response($data, 200);
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_INLINE,
'test.csv'
);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', 'text/csv');
//echo $response;
return $response;
}
......
......@@ -62,7 +62,6 @@ class PluSuscripcionRepository extends ServiceEntityRepository {
->andWhere("p.susVigente = 1")
->andWhere("p.susUltimoTipoXml = 1")
->setParameters(array("fechaInicio" => $fechaInicio." 00:00:00", "fechaFin" => $fechaFin." 23:59:59"))
->orderBy('p.susId', 'ASC')
->getQuery()
->getResult();
return $result;
......
......@@ -15,12 +15,15 @@ class CsvReporteGeneral {
private $tipoSuscripcion;
private $fecha;
public function getId() {
return $this->id;
}
public function getLista() {
public function setId($id) {
$this->id = $id;
}
public function getLista() {
return $this->lista;
}
......
<?php
namespace App\Utilities;
use Symfony\Component\HttpFoundation\Response;
......
......@@ -58,6 +58,7 @@
{% block javascripts %}
<script src="{{asset('backend/bootstrap-daterangepicker/moment.js')}}"></script>
<script src="{{asset('backend/bootstrap-daterangepicker/daterangepicker.js')}}"></script>
<script src="{{asset('utils/utils.js')}}"></script>
<script>
var reporteDateRangePicker = function () {
......@@ -120,7 +121,9 @@
beforeSend: function () {
},
success: function () {
success: function (data, status, xhr) {
console.log(status);
generateFileDownload(moment(new Date()).format("YYYY_MM_DD_HHmmss") + "_Reporte_general.csv",data, xhr, false);
},
complete: function () {
$("#btnCrearReporte").button('reset');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment