<?php
include_once 'util/relatoriosBase.php';
class RelRamalRamal extends Relatorios {
private $___tempoDuracao;
private $___ramal;
private $___destino;
public function __construct($idProg, $dbcon, $incDac) {
parent::__construct($idProg, $dbcon, $incDac);
$this->___tempoDuracao = isset($_POST['tempoDuracao']) ? $_POST['tempoDuracao'] : 1;
$this->___ramal = isset($_POST['ramal']) ? $_POST['ramal'] : '';
$this->___destino = isset($_POST['destino']) ? $_POST['destino'] : '';
$this->___dataIni = IsPostBack() ? $_POST['dataIni'] : date('d/m/Y')." 00:00";
$this->___dataFim = IsPostBack() ? $_POST['dataFim'] : date('d/m/Y')." 23:59";
}
function ___ValidaForm() {
parent::___ValidaForm();
if (empty($this->___tempoDuracao)) {
GeraExcept('Informe um tempo de dura<EFBFBD> <EFBFBD> o!');
}
if ($this->___ramal) {
if (!soNumero($this->___ramal)) {
GeraExcept('Informe apenas n<EFBFBD> meros no campo de Ramal!');
}
}
if ($this->___destino) {
if (!soNumero($this->___destino)) {
GeraExcept('Informe apenas n<EFBFBD> meros no campo de Destino!');
}
}
}
function ___FiltrosRelatorio() {
if ($this->___media != MEDIA_PRINT_GRID) {
/*
* Recupera os filtros da sess<EFBFBD> o do usu<EFBFBD> rio.
*/
list($this->___dataIni, $this->___dataFim, $this->___ramal, $this->___destino) = $_SESSION["SSstorageFiltros"];
if ($this->___media == MEDIA_PRINT_HTML) {
return sprintf("Data Inicial: %s Data Final: %s ", $this->___dataIni, $this->___dataFim);
}
$fltCsv = array();
$fltCsv[] = "Data Inicial: " . $this->___dataIni;
$fltCsv[] = "Data Final: " . $this->___dataFim;
$fltCsv[] = "Data Emissao: " . date('d/m/Y H:i:s');
$fltCsv[] = "Emitido Por: " . GetLogin();
return $fltCsv;
}
$lkPrint = "";
if ($this->___dataCount) {
$prt = '< a href = "index.php?idProg=%s&media=%s" > < img src = "imgSite/%s" alt = "Clique aqui para imprimir!" width = "24" height = "24" border = "0" > < / a > ';
$prtHtml = "< a href = \"javaScript:NovaJanela('index.php?idProg=%s&media=%s', ' % s ' , ' 720 ' , ' 700 ' , ' scrollbars = YES');\" > < img src = \"imgSite/%s\" alt = \"Clique aqui para imprimir ! \ " width = \"24\" height = \"24\" border = \"0\" > < / a > ";
$lkPrint .= sprintf($prtHtml, $this->GetidProg(), MEDIA_PRINT_HTML, substr(LimpaString(GetDispProgSel($this->GetIdProg())), 0, 20), 'print.gif');
$lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_EXCEL, 'prt-xls.png');
$lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_PDF, 'prt-pdf.png');
$lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_CSV, 'prt-csv.png');
}
/*
* Personalise esta fun<EFBFBD> <EFBFBD> o se necessitar de outras op<EFBFBD> <EFBFBD> es de filtro.
*/
$filtro = '< table class = "filtro" border = "0" cellspacing = "1" cellpadding = "1" > ';
/*
* Rotulos.
*/
$filtro .= '< tr > ';
$filtro .= '< td >  </td>';
$filtro .= '< td > Data Inicial < / td > ';
$filtro .= '< td > Data Final < / td > ';
$filtro .= '< td > Ramal < / td > ';
$filtro .= '< td > Destino < / td > ';
$filtro .= '< td > Dura<EFBFBD> <EFBFBD> o< / td > ';
$filtro .= '< / tr > ';
/*
* Valores.
*/
$filtro .= '< tr > '
. '< td > < / td > ';
$filtro .= '< td > < input name = "dataIni" id = "dataIni" type = "text" size = "15" maxlength = "16" value = "%s" onkeyup = "formataDataHora(this)" title = "dd/mm/aaaa HH:mm" > < / td > ';
$filtro .= '< td > < input name = "dataFim" id = "dataFim" type = "text" size = "15" maxlength = "16" value = "%s" onkeyup = "formataDataHora(this)" title = "dd/mm/aaaa HH:mm" > < / td > ';
$filtro .= sprintf('< td > < input name = "ramal" type = "text" value = "%s" id = "ramal" / > < / td > ',$this->___ramal ? $this->___ramal : '');
$filtro .= sprintf('< td > < input name = "destino" type = "text" value = "%s" id = "destino" / > < / td > ',$this->___destino ? $this->___destino : '');
$filtro .= '< td > < div class = "geral" > ';
$filtro .= sprintf('< input name = "tempoDuracao" class = "clean" type = "radio" id = "tempoDuracao" value = "1" % s > Todos'
. '< input name = "tempoDuracao" class = "clean" type = "radio" id = "tempoDuracao" value = "2" % s > maior que(> 0)'
. '< input name = "tempoDuracao" class = "clean" type = "radio" id = "tempoDuracao" value = "3" % s > Igual a 0 (= 0)', $this->___tempoDuracao == 1 ? 'checked' : '', $this->___tempoDuracao == 2 ? 'checked' : '', $this->___tempoDuracao == 3 ? 'checked' : '');
$filtro .= '< / div > < / td > ';
$filtro .= '< td > < input type = "submit" name = "btConsulta" id = "btConsulta" value = "Consultar" > < / td > ';
$filtro .= '< td > %s< / td > ';
$filtro .= '< / tr > ';
$filtro .= '< / table > ';
/*
* Guarda os filtros passados para recuperar na emiss<EFBFBD> o dos relat<EFBFBD> ios para impress<EFBFBD> o.
*/
$_SESSION["SSstorageFiltros"] = array($this->___dataIni, $this->___dataFim, $this->___ramal, $this->___destino);
return sprintf($filtro, $this->___dataIni, $this->___dataFim, $lkPrint);
}
protected function ___GetDadosDb() {
$dataIni = strlen($this->___dataIni) == 10 ? FormatDtMssql($this->___dataIni) . " 00:00": FormatDtMssql(substr($this->___dataIni, 0, 10)). substr($this->___dataIni, 10, 16);
$dataFim = strlen($this->___dataFim) == 10 ? FormatDtMssql($this->___dataFim) . " 23:59": FormatDtMssql(substr($this->___dataFim, 0, 10)). substr($this->___dataFim, 10, 16);
$ramal = $this->___ramal;
$destino = $this->___destino;
$duracao = $this->___tempoDuracao;
$query = "SELECT a.src,
CASE WHEN (SUBSTRING(a.dst,1,1) = 'a')THEN 'anuncio'
WHEN (SUBSTRING(a.dst,1,1) = 's')THEN 'transferencia' ELSE a.dst END AS dst,
a.billsec, a.calldate, a.uniqueid, tplg_descricao
FROM pbx_bilhetes a
INNER JOIN pbx_tipo_ligacao b ON a.tipo_chamada = b.tplg_codigo
WHERE a.calldate::TIMESTAMP >= '$dataIni'
AND a.calldate::TIMESTAMP < = '$dataFim'
AND tplg_tipo = 1
AND id_bilhetes = (SELECT MAX(id_bilhetes) FROM pbx_bilhetes WHERE uniqueid = a.uniqueid) ";
$query .= $ramal ? " AND a.src = '$ramal'" : " AND a.src < > ''";
$query .= $destino ? " AND a.dst = '$destino'" : " AND a.dst < > ''";
switch ($duracao){
case 2:
$query .= " AND a.billsec > 0";
break;
case 3:
$query .= " AND a.billsec = 0";
break;
}
$query .= " AND NOT EXISTS(SELECT '' FROM pbx_eventos_dacs WHERE uid2 = a.uniqueid)
AND lastapp < > 'Transferred Call'
ORDER BY calldate, a.src";
$result = $this->___GetQuery($query);
$row = pg_fetch_all($result);
$this->SetData($row ? $row : array());
}
protected function ___PreparaCsv() {
$nomeDac = "";
$dataRel = array();
$dataAgrup = array();
$filtro = $this->___FiltrosRelatorio();
$dataRel[] = GenerateCsvFromArray($filtro);
$dadosCabecalio = array('Data/Hora','Origem','Destino','Duracao');
$dadosField = array('calldate','src','dst','billsec');
$dataRel[] = GenerateCsvFromArray($dadosCabecalio);
$somaTempo = 0;
$somaQuantidade = 0;
/*
* Inicia o relat<EFBFBD> rio.
*/
foreach ($this->GetData()[0] as $row) {
$row["src"] = ocultarTelefone($row["src"]);
$row["dst"] = ocultarTelefone($row["dst"]);
$row["calldate"] = date('d/m/Y H:i', strtotime($row["calldate"]));
$somaTempo += $row["billsec"];
$row["billsec"] = SecondToStrTime($row["billsec"]);
$somaQuantidade ++;
$linha = array();
foreach ($dadosField as $value) {
$linha[] = $row["$value"];
}
$dataRel[] = GenerateCsvFromArray($linha);
}
$linha = array('Total', '', 'Registros: '.$somaQuantidade, 'Duracao: '.SecondToStrTime($somaTempo));
$dataRel[] = GenerateCsvFromArray($linha);
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaExcel() {
$dataRel = array();
$dataAgrup = array();
$filtro = $this->___FiltrosRelatorio();
/*
* Cria os filtros
*/
$linha = array('LABEL');
foreach ($filtro as $values) {
list($label, $value) = explode(":", $values);
$linha[] = $label;
}
$dataRel[] = $linha;
$linha = array('DADOS');
foreach ($filtro as $values) {
list($label, $value) = explode(":", $values);
$linha[] = $value;
}
$dataRel[] = $linha;
$dadosCabecalio = array('Data/Hora','Origem','Destino','Duracao');
$dadosField = array('calldate','src','dst','billsec');
/*
* Monta a linha de cabecalio.
*/
$linha = array('LABEL');
foreach ($dadosCabecalio as $value) {
$linha[] = $value;
}
$dataRel[] = $linha;
$somaTempo = 0;
$somaQuantidade = 0;
/*
* Linhas de dados.
*/
foreach ($this->GetData()[0] as $row) {
$row["src"] = ocultarTelefone($row["src"]);
$row["dst"] = ocultarTelefone($row["dst"]);
$row["calldate"] = date('d/m/Y H:i', strtotime($row["calldate"]));
$somaTempo += $row["billsec"];
$row["billsec"] = SecondToStrTime($row["billsec"]);
$somaQuantidade ++;
// Formata linha de dados.
$linha = array('DADOS');
foreach ($dadosField as $key) {
$linha[] = $row["$key"];
}
$dataRel[] = $linha;
}
$linha = array('LABEL', 'Total', '', 'Registros: '.$somaQuantidade, 'Duracao: '.SecondToStrTime($somaTempo));
$dataRel[] = $linha;
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaGrid() {
$dadosCabecalio = array(
'Data/Hora' => 'align="center"',
'Origem' => 'align="left"',
'Destino' => 'align="left"',
'Dura<EFBFBD> <EFBFBD> o' => 'align="center"'
);
$dadosField = array(
'calldate' => 'align="center"',
'src' => 'align="left"',
'dst' => 'align="left"',
'billsec' => 'align="center"'
);
/*
* Inicia o relat<EFBFBD> rio.
*/
$dataRel[] = '< table width = "50%" cellpadding = "2" class = "grid" > ';
/*
* Monta a linha de cabecalio.
*/
$linha = '< tr > ';
foreach ($dadosCabecalio as $key => $value) {
$linha .= sprintf("< th % s > %s< / th > ", $value, $key);
}
$linha .= "< / tr > ";
$dataRel[] = $linha;
/*
* Verifica se retornou dados
*/
if (!IsPostBack() || !count($this->GetData()[0])) {
$dataRel[] = sprintf("< tr > < td align = \"center\" colspan = \"%s\" > %s< / td > < / tr > < / table > ", count($dadosCabecalio), (!IsPostBack() ? 'Informe os parametros e clique em consultar!' : 'Nenhum registro encontado!'));
$this->___dataRel[] = $dataRel;
return;
}
$somaTempo = 0;
$somaQuantidade = 0;
/*
* Linhas de dados.
*/
foreach ($this->GetData()[0] as $row) {
$row["calldate"] = date('d/m/Y H:i', strtotime($row["calldate"]));
$row["src"] = ocultarTelefone($row["src"]);
$row["dst"] = ocultarTelefone($row["dst"]);
$somaTempo += $row["billsec"];
$row["billsec"] = SecondToStrTime($row["billsec"]);
$somaQuantidade ++;
// Formata linha de dados.
$linha = "< tr > ";
foreach ($dadosField as $key => $value) {
$linha .= sprintf("< td % s > %s< / td > ", $value, $row["$key"]);
}
$linha .= "< / tr > ";
$dataRel[] = $linha;
}
/*
* Linha de totalizacao.
*/
$linha = '< tr > < th colspan = "2" align = "left" > Total< / th > < th align = "left" > Registros: %s< / th > < th align = "center" colspan = "6" > Dura<EFBFBD> <EFBFBD> o: %s< / th > < / tr > ';
$dataRel[] = sprintf($linha, $somaQuantidade, SecondToStrTime($somaTempo));
/*
* Fecha relatorio.
*/
$dataRel[] = '< / table > ';
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaHtml() {
}
protected function ___PreparaPdf() {
list($this->___dataIni, $this->___dataFim) = $_SESSION["SSstorageFiltros"];
$dados = $this->GetData()[0];
$x = 0;
$somaQtd = 0;
foreach ($dados as $row) {
$dados[$x]["src"] = ocultarTelefone($row["src"]);
$dados[$x]["dst"] = ocultarTelefone($row["dst"]);
$dados[$x]["calldate"] = date('d/m/Y H:i', strtotime($row["calldate"]));
$somaTempo += $row["billsec"];
$dados[$x]["billsec"] = SecondToStrTime($row["billsec"]);
$somaQtd ++;
$x++;
}
//HEADER DA TABELA DO PDF -> DADOS EM ARRAY
$dadosCabecalio = array(
'Data/Hora' => 'C',
'Origem' => 'C',
'Destino' => 'C',
'Dura<EFBFBD> <EFBFBD> o' => 'C'
);
$dadosField = array(
'calldate' => 'C',
'src' => 'C',
'dst' => 'C',
'billsec' => 'C'
);
$widthHeader = array(45, 45, 45, 45);
$totaliza = array('Total', "Registros: $somaQtd", '', 'Dura<EFBFBD> <EFBFBD> o: '. SecondToStrTime($somaTempo));
//Monta o array para passar para o prepara; Necess<EFBFBD> rio passar 6 parametros
$table1 = array($dadosCabecalio, $dadosField, $dados, $widthHeader, $totaliza, 0);
$this->___dataRel = array($table1);
}
public function ExecutaRelatorio() {
global $jsStartup, $jsJQuery, $smarty;
try {
/*
* Use esta fun<EFBFBD> <EFBFBD> o para validar dados do formul<EFBFBD> rio.
*/
$this->___ValidaForm();
/*
* Recupera os dados do banco.
*/
$this->___ConteudoRelatorio();
/*
* Imprime relat<EFBFBD> rio de acordo com a media passada.
*/
$conteudoRelatorio = $this->___ImprimeRelatorio();
} catch (Exception $exc) {
$this->SetMsg($exc->getMessage());
$jsStartup[] = sprintf("alert('%s');", $this->GetMsg());
$this->___GravaLog();
}
/*
* Inclua os scripts necess<EFBFBD> rios aqui.
*/
$jsJQuery[] = "\$('#dataIni').keypress(function(){formataDataHora(this);}) ";
$jsJQuery[] = "\$('#dataFim').keypress(function(){formataDataHora(this);}) ";
$smarty->assign('filtros', $this->___FiltrosRelatorio());
$smarty->assign('conteudo', $conteudoRelatorio);
$smarty->assign('msg', $this->GetMsg());
GetTemplate($smarty, 'relatoriosGrid.tpl');
}
}
$relRamal = new RelRamalRamal($idProg, $dbcon, null);
$relRamal->ExecutaRelatorio();
?>