PABX da Simples IP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

606 lines
26 KiB

<?php
include_once 'util/relatoriosBase.php';
require("pesquisaFuncoes.php");
class RelPesquisa extends Relatorios {
private $___lstPesquisa = '';
private $___plId;
private $___plDescricao = '';
private $___ppId = 0;
private $___ppNome = '';
private $___datas = array();
private $___servico;
private $___tipoRelatorio;
public function __construct($idProg, $dbcon, $incDac) {
parent::__construct($idProg, $dbcon, $incDac);
$this->___dac = isset($_POST["listaDacs"]) ? $_POST["listaDacs"] : 0;
$this->___lstPesquisa = isset($_POST["lstPesquisa"]) ? $_POST["lstPesquisa"] : '0| | | |0| ';
$this->___servico = isset($_POST["listaServico"]) ? $_POST["listaServico"] : 0;
}
function ___ValidaForm() {
parent::___ValidaForm();
if (!IsPostBack()) {
return;
}
if (!$this->___lstPesquisa) {
GeraExcept('Informe uma Pesquisa!');
}
}
function ___FiltrosRelatorio() {
global $dbcon;
if($this->___lstPesquisa){
$this->___plId = explode('|', $this->___lstPesquisa)[0];
}
$lstPesquisa = GetPesquisaRel($dbcon, $this->___plId);
if ($this->___media != MEDIA_PRINT_GRID) {
/*
* Recupera os filtros da sessao do usuario.
*/
list($this->___dac, $this->___dataIni, $this->___dataFim) = $_SESSION["SSstorageFiltros"];
if ($this->___media == MEDIA_PRINT_HTML) {
$fila = $this->GetIncDac() ? 'Fila: ' . GetDacDesc($this->GetDbCon(), $this->GetIdProg() . ' ') : '';
return sprintf("%sData Inicial: %s Data Final: %s Tipo: %s", $fila, $this->___dataIni, $this->___dataFim, ($this->___tipoRelatorio ? 'Analítico' : 'Sintético'));
}
$fltCsv = array();
if ($this->GetIncDac()) {
$fltCsv[] = 'Fila:' . GetDacDesc($this->GetDbCon(), $this->___dac);
}
$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;
}
if ($this->GetIncDac()) {
$dacs = GetDac($this->GetDbCon(), $this->___dac, "", 0, 1, 0, 0, 1);
}
$lkPrint = "";
if ($this->___dataCount) {
$prt = '<a href="index.php?idProg=%s&media=%s&tipoRelatorio=%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&tipoRelatorio=%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, $this->___tipoRelatorio, substr(LimpaString(GetDispProgSel($this->GetIdProg())), 0, 20), 'print.gif');
$lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_EXCEL, $this->___tipoRelatorio, 'prt-xls.png');
$lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_PDF, $this->___tipoRelatorio, 'prt-pdf.png');
$lkPrint .= sprintf($prt, $this->GetidProg(), MEDIA_PRINT_CSV, $this->___tipoRelatorio, 'prt-csv.png');
}
//$tipoRel = !$this->___tipoRelatorio ? '' : 'checked="checked"';
/*
* Personalise esta funcao se necessitar de outras opnioes de filtro.
*/
$filtro = '<table border="0" cellspacing="1" cellpadding="1">';
/*
* Rotulos.
*/
$filtro .= '<tr>';
$filtro .= '<td>Pesquisa</td>';
if ($this->GetIncDac())
$filtro .= '<td>Fila</td>';
$filtro .= GetUsoServico() ? '<td>Serviço</td>' : '';
$filtro .= '<td>Data Inicial</td>';
$filtro .= '<td>Data Final </td>';
$filtro .= '<td align="center" valign="middle">&nbsp;</td>';
$filtro .= '</tr>';
/*
* Valores.
*/
$filtro .= '<tr>';
$filtro .= "<td><select name=\"lstPesquisa\">$lstPesquisa</select></td>";
if ($this->GetIncDac())
$filtro .= sprintf('<td>%s</td>', $dacs);
if (GetUsoServico()) {
$filtro .= sprintf('<td>%s</td>', GetIdentRota(false, $this->___servico));
}
$filtro .= '<td><input name="dataIni" id="dataIni" type="text" size="15" maxlength="10" value="%s" onkeyup="formataData(this)" title="dd/mm/aaaa"></td>';
$filtro .= '<td><input name="dataFim" id="dataFim" type="text" size="15" maxlength="10" value="%s" onkeyup="formataData(this)" title="dd/mm/aaaa"></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 emissao dos relatoios para impressao.
*/
$_SESSION["SSstorageFiltros"] = array($this->___dac, $this->___dataIni, $this->___dataFim);
return sprintf($filtro, $this->___dataIni, $this->___dataFim, $lkPrint);
}
protected function ___GetDadosDb($pqId = null) {
global $dbcon;
$dataIni = FormatDtMssql($this->___dataIni);
$dataFim = FormatDtMssql($this->___dataFim);
$plId = $this->___plId;
$ppId = $this->___ppId;
$dac = $this->___dac;
$ppNome = $this->___ppNome;
$plDescricao = $this->___plDescricao;
$datas = $this->___datas;
$srv = $this->___servico;
$mostraColunaServicos = GetUsoServico() == RELATORIO_SERVICOS_HABILITA;
GetDadosPesq($dbcon, $this->___lstPesquisa, $plId, $plDescricao, $ppId, $ppNome, $datas);
$query = "SELECT pq_display, pq_questao,
(select count(*)
from pbx_pesquisa_movimento a ";
if (GetUsoServico()) {
$query .= " left join pbx_servicos_registra c on c.uniqueid = a.uniqueid ";
}
$query .= " where pl_id = $plId
and pp_id = a.pp_id
and pq_id = a.pq_id
and id_dac = $dac ";
if (GetUsoServico() && $srv) {
$query .= " AND c.serv_id = '{$srv}'";
}
$query .= "and pm_data >= '$dataIni'
and pm_data <= '$dataFim')
as num_resp,pq_id
from pbx_pesquisa_questoes a, pbx_pesquisa_liberacao b
where b.pp_id = a.pp_id
and b.pl_id = '$plId'
and pq_status = 1 order by 1";
$result = $this->___GetQuery($query);
$row = pg_fetch_all($result);
$this->SetData($row ? $row : array());
foreach ($row as $dataPsq) {
$sql = "select a.po_id, a.po_tecla, a.po_opcao, count(b.po_id) as num_resp
from pbx_pesquisa_opcoes a
left outer join pbx_pesquisa_movimento b on b.po_id = a.po_id
and pl_id = $plId
and b.pq_id = a.pq_id
and b.id_dac = $dac
and b.pm_data >= '$dataIni'
and b.pm_data <= '$dataFim' ";
if (GetUsoServico()) {
$sql .= " left join pbx_servicos_registra c on c.uniqueid = b.uniqueid ";
}
$sql .= " where a.pq_id = {$dataPsq['pq_id']} ";
if (GetUsoServico() && $srv) {
$sql .= " AND c.serv_id = '{$srv}'";
}
$sql .= " group by a.po_id, a.po_tecla, a.po_opcao
order by 2";
$res = $this->___GetQuery($sql);
$rows = pg_fetch_all($res);
$this->SetData($rows ? $rows : array());
}
$query = "SELECT fila, d.matricula, apelido,
SUM(CASE WHEN (c.data_reg IS NOT NULL)THEN 1 ELSE 0 END) AS envio_pesquisa,
SUM(CASE WHEN (c.data_fim IS NOT NULL)THEN 1 ELSE 0 END) AS completou_pesquisa,
COUNT(*) AS chamadas_realizadas
FROM pbx_bilhetes a
INNER JOIN pbx_eventos_dacs b ON a.uniqueid = b.uid2
LEFT JOIN pbx_pesquisa_regitra c ON a.uniqueid = c.uniqueid
LEFT JOIN pbx_usuarios d ON d.matricula = SUBSTRING(b.agente, 7, 4)
LEFT JOIN pbx_queues_grupos e ON e.nome = b.fila";
if (GetUsoServico()) {
$query .= " LEFT JOIN pbx_servicos_registra s ON s.uniqueid = a.uniqueid ";
}
$query .= " WHERE calldate::DATE >= '$dataIni'
AND calldate::DATE <= '$dataFim'
AND b.evento IN('COMPLETEAGENT','COMPLETECALLER', 'TRANSFER','COMPLETEAGENTRAMAL','COMPLETECALLERRAMAL', 'COMPLETACALLER', 'COMPLETAAGENT')
AND e.id = '$dac' ";
if (GetUsoServico() && $srv) {
$query .= " AND s.serv_id = '{$srv}'";
}
$query .= " GROUP BY fila, d.matricula, apelido";
$result = $this->___GetQuery($query);
$row = pg_fetch_all($result);
$this->SetData($row ? $row : array());
}
protected function ___PreparaCsv() {
$dataRel = array();
$filtro = $this->___FiltrosRelatorio();
/*
* Inicia o relatório.
*/
$dataRel[] = GenerateCsvFromArray($filtro);
$dadosCabecalio = array('Opcao', 'Resposta', 'Quantidade', 'Porcentagem');
$dadosField = array('po_tecla', 'po_opcao', 'num_resp', 'por_resp');
/*
* Monta a linha de cabecalio.
*/
$dataRel[] = GenerateCsvFromArray($dadosCabecalio);
$x = 1;
$somaResp = 0;
/*
* Linhas de dados.
*/
foreach ($this->GetData()[0] as $row) {
$somaResp = $this->__CalculaPorcentagem($row['pq_questao'], $this->GetData()[0]);
$dataRel[] = GenerateCsvFromArray(array('', '', RemoveAcentos($row['pq_questao'])));
$linha = array();
foreach ($dadosCabecalio as $value) {
$linha[] = $value;
}
$dataRel[] = GenerateCsvFromArray($linha);
$dataRel[] = GenerateCsvFromArray($linha);
// Formata linha de dados.
foreach ($this->GetData()[$x] as $dataPsq) {
$linha = array();
foreach ($dadosField as $key) {
if ($key == 'po_opcao') {
$linha[] = RemoveAcentos($dataPsq["$key"]);
} else if ($key == 'por_resp') {
$dataPsq['por_resp'] = $dataPsq["num_resp"] ?? number_format(($dataPsq["num_resp"] / $somaResp) * 100.0, 2, ',', '.') . '%';
$linha[] = $dataPsq['por_resp'];
} else {
$linha[] = $dataPsq["$key"];
}
}
$dataRel[] = GenerateCsvFromArray($linha);
unset($linha);
}
$dataRel[] = GenerateCsvFromArray(array('Total', '', 'Resp.: '.$somaResp, $somaResp ?? number_format(($somaResp / $somaResp) * 100.0, 2, ',', '.') . '%'));
$x++;
}
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaExcel() {
$dataRel = array();
$filtro = $this->___FiltrosRelatorio();
/*
* Inicia o relatório.
*/
/*
* 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('Opcao', 'Resposta', 'Quantidade', 'Porcentagem');
$dadosField = array('po_tecla', 'po_opcao', 'num_resp', 'por_resp');
/*
* Monta a linha de cabecalio.
*/
$x = 1;
$somaResp = 0;
/*
* Linhas de dados.
*/
foreach ($this->GetData()[0] as $row) {
$somaResp = $this->__CalculaPorcentagem($row['pq_questao'], $this->GetData()[0]);
$dataRel[] = array('LABEL', '', '', RemoveAcentos($row['pq_questao']));
$linha = array('LABEL');
foreach ($dadosCabecalio as $value) {
$linha[] = $value;
}
$dataRel[] = $linha;
// Formata linha de dados.
foreach ($this->GetData()[$x] as $dataPsq) {
$linha = array('DADOS');
foreach ($dadosField as $key) {
if ($key == 'po_opcao') {
$linha[] = RemoveAcentos($dataPsq["$key"]);
} else if ($key == 'por_resp') {
$dataPsq['por_resp'] = $dataPsq["num_resp"] ?? number_format(($dataPsq["num_resp"] / $somaResp) * 100.0, 2, ',', '.') . '%';
$linha[] = $dataPsq['por_resp'];
} else {
$linha[] = $dataPsq["$key"];
}
}
$dataRel[] = $linha;
unset($linha);
}
$dataRel[] = array('LABEL', 'Total', '', 'Resp.: '.$somaResp, $somaResp ?? number_format(($somaResp / $somaResp) * 100.0, 2, ',', '.') . '%');
$x++;
}
$dataRel[] = array('LABEL', '');
$dadosCabecalio = array('Fila', 'Agente', 'Direcionamento Pesquisa', 'Conclusao Pesquisa', 'Chamadas');
$linha = array('LABEL');
foreach ($dadosCabecalio as $value) {
$linha[] = $value;
}
$dataRel[] = $linha;
$dadosField = array('fila', 'apelido', 'envio_pesquisa', 'completou_pesquisa', 'chamadas_realizadas');
$linha = array('DADOS');
foreach ($this->GetData()[$x] as $stat) {
$linha = array('DADOS');
foreach ($dadosField as $key) {
$linha[] = $stat[$key];
}
}
$dataRel[] = $linha;
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaGrid() {
$dataRel = array();
$dadosCabecalio = array(
'Opção' => 'align="center"',
'Resposta' => 'align="center"',
'Quantidade' => 'align="center"',
'Porcentagem [ % ]' => 'align="center"'
);
$dadosField = array(
'po_tecla' => 'align="center"',
'po_opcao' => 'align="center"',
'num_resp' => 'align="center"',
'por_resp' => 'align="center"'
);
/*
* Inicia o relatório.
*/
$dataRel[] = '<table width="50%" cellpadding="2" class="grid">';
/*
* Monta a linha de cabecalio.
*/
$dataRel[] = "<tr><th colspan='4'>Relatório Pesquisa</th></tr>";
/*
* 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;
}
//print_r($this->GetData());
$x = 1;
$somaResp = 0;
$linhas = '';
/*
* Linhas de dados.
*/
foreach ($this->GetData()[0] as $row) {
//Monta a Questão da Pergunta da pesquisa.
$linhas .= sprintf("<tr><th colspan=\"4\"><span style='color:#404040'>%s</span></th></tr>", $row['pq_questao']);
$linhas .= "<tr>";
foreach ($dadosCabecalio as $key => $value) {
$linhas .= sprintf("<th %s>%s</th>", $value, $key);
}
$linhas .= "</tr>";
$dataRel[] = $linhas;
unset($linhas);
//Cria as linha dos resultado de acordo com as respostas
$somaResp = $this->__CalculaPorcentagem($row['pq_questao'], $this->GetData()[0]);
foreach ($this->GetData()[$x] as $dataPesquisa) {
$linhas .= "<tr>";
foreach ($dadosField as $k => $value) {
if ($k == 'por_resp') {
$linhas .= sprintf("<td %s>%s%%</td>", $value, $dataPesquisa["num_resp"] ?? number_format(($dataPesquisa["num_resp"] / $somaResp) * 100.0, 2, ',', '.'));
} else {
$linhas .= sprintf("<td %s>%s</td>", $value, $dataPesquisa["$k"]);
}
}
$linhas .= "</tr>";
}
$linhas .= sprintf("<tr><td colspan='2' align='center'><b>Total</b></td><td align='center'><b>$somaResp</b></td><td align='center'><b>%s</b></td></tr>", $somaResp ?? number_format(($somaResp / $somaResp) * 100.0, 2, ',', '.').'%');
$x++;
$dataRel[] = $linhas;
unset($linhas);
}
/*
* Fecha relatorio.
*/
$dataRel[] = '</table>';
$dataRel[] = "<br>";
$dataRel[] = '<table width="50%" cellpadding="2" class="grid">';
$dataRel[] = "<tr><th colspan='5'>Estátistica Agente</th></tr>";
$dataRel[] = "<tr>"
. "<th align='center'>Fila</th>"
. "<th align='center'>Agente</th>"
. "<th align='center'>Direcionamento Pesquisa</th>"
. "<th align='center'>Conclusao Pesquisa</th>"
. "<th align='center'>Chamadas</th>"
. "</tr>";
$ttDirect = 0;
$ttCC = 0;
$ttCall = 0;
foreach ($this->GetData()[$x] as $est) {
$dataRel[] = "<tr>"
. "<td align='center'>{$est['fila']}</td>"
. "<td align='center'>{$est['apelido']}</td>"
. "<td align='center'>{$est['envio_pesquisa']}</td>"
. "<td align='center'>{$est['completou_pesquisa']}</td>"
. "<td align='center'>{$est['chamadas_realizadas']}</td>"
. "</tr>";
$ttDirect += $est['envio_pesquisa'];
$ttCC += $est['completou_pesquisa'];
$ttCall += $est['chamadas_realizadas'];
}
$dataRel[] = "<tr><th colspan='2'>Total</th> <th>$ttDirect</th> <th>$ttCC</th> <th>$ttCall</th></tr>";
$dataRel[] = '</table>';
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaHtml() {
}
protected function ___PreparaPdf() {
list($this->___dac, $this->___dataIni, $this->___dataFim) = $_SESSION["SSstorageFiltros"];
/**
* TABELA DADOS PRINCIPAIS
*/
$dadosCabecalio = array(
'Opção' => 'C',
'Resposta' => 'C',
'Quantidade' => 'C',
'Porcentagem [ % ]' => 'C'
);
$dadosField = array(
'po_tecla' => 'C',
'po_opcao' => 'C',
'num_resp' => 'C',
'por_resp' => 'C'
);
$somaTotalQtd = 0;
$dados = $this->GetData()[0];
$x = 1;
$z = 0;
$dataQst = array();
$tables = array();
$widthHeader = array(37, 77, 37, 37);
foreach ($dados as $row) {
$somaTotalQtd = $this->__CalculaPorcentagem($row['pq_questao'], $this->GetData()[0]);
foreach ($this->GetData()[$x] as $data) {
$dataQst[$z]['pq_questao'] = $data['pq_questao'];
$dataQst[$z]['po_opcao'] = $data['po_opcao'];
$dataQst[$z]['num_resp'] = $data['num_resp'];
$dataQst[$z]['po_tecla'] = $data['po_tecla'];
$dataQst[$z]['por_resp'] = $data["num_resp"] ?? number_format(($data["num_resp"] / $somaTotalQtd) * 100.0, 2, ',', '.') . '%';
$z++;
}
$totalizador = array('Total: ', $row['pq_questao'], 'Resp.: '.$somaTotalQtd , $somaTotalQtd ?? number_format(($somaTotalQtd / $somaTotalQtd) * 100.0, 2, ',', '.') . '%');
$tables[] = array($dadosCabecalio, $dadosField, $dataQst, $widthHeader, $totalizador, 0);
$x++;
unset($dataQst);
}
$dadosCabecalio = array(
'Fila' => 'C',
'Agente' => 'C',
'Direcionamento Pesquisa' => 'C',
'Conclusao Pesquisa' => 'C',
'Chamadas' => 'C'
);
$dadosField = array(
'fila' => 'C',
'apelido' => 'C',
'envio_pesquisa' => 'C',
'completou_pesquisa' => 'C',
'chamadas_realizadas' => 'C'
);
$widthHeader = array(20, 20, 40, 40, 20);
$tables[] = array($dadosCabecalio, $dadosField, $this->GetData()[$x], $widthHeader, 0, 0);
$this->___dataRel = $tables;
}
protected function __CalculaPorcentagem($coluna, $array) {
$somaQtd = 0;
foreach ($array as $value) {
if ($value['pq_questao'] == $coluna) {
$somaQtd += $value['num_resp'];
}
}
return $somaQtd;
}
public function ExecutaRelatorio() {
global $jsStartup, $jsJQuery, $smarty;
try {
/*
* Use esta função para validar dados do formulário.
*/
$this->___ValidaForm();
/*
* Recupera os dados do banco.
*/
$this->___ConteudoRelatorio();
/*
* Imprime relató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á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');
}
}
$relPesquisa = new RelPesquisa($idProg, $dbcon, 1);
$relPesquisa->ExecutaRelatorio();