<?php
include_once 'util/relatoriosBase.php';
$tpLayout = -1;
class TotalizadorCentroCustoDetalhado extends Relatorios {
private $___codEmpresa;
private $___codDptos;
public function __construct($idProg, $dbcon, $incDac) {
parent::__construct($idProg, $dbcon, $incDac);
$this->___dataIni = isset($_GET["dtI"]) ? $_GET["dtI"] : date('d/m/Y');
$this->___dataFim = isset($_GET["dtF"]) ? $_GET["dtF"] : date('d/m/Y');
$this->___codEmpresa = isset($_GET["empresa"]) ? $_GET["empresa"] : '';
$this->___codDptos = isset($_GET["depto"]) ? $_GET["depto"] : 0;
}
function ___ValidaForm() {
parent::___ValidaForm();
if (!IsPostBack()) {
return;
}
if ($this->___codDptos < 0 ) {
GeraExcept('Informe o c<EFBFBD> digo departamento!');
}
if (!$this->___codEmpresa) {
GeraExcept('Informe o c<EFBFBD> digo empresa!');
}
}
function ___FiltrosRelatorio() {
global $dbcon;
if ($this->___media != MEDIA_PRINT_GRID) {
/*
* Recupera os filtros da sess<EFBFBD> o do usu<EFBFBD> rio.
*/
list($this->___dataIni, $this->___dataFim, $this->___codDptos, $this->___codEmpresa) = $_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 = "grid" border = "0" cellspacing = "1" cellpadding = "1" > ';
/*
* Rotulos.
*/
$filtro .= '< tr > ';
$filtro .= '< th align = "center" > Data Inicial< / th > ';
$filtro .= '< th align = "center" > Data Final< / th > ';
$filtro .= '< th align = "center" > Empresa< / th > ';
$filtro .= '< th align = "center" > Impress<EFBFBD> o< / th > ';
$filtro .= '< / tr > ';
/*
* Valores.
*/
$filtro .= '< tr > ';
$filtro .= '< td align = "center" > %s< / td > ';
$filtro .= '< td align = "center" > %s< / td > ';
$filtro .= sprintf('< td align = "center" > %s< / td > ', GetEmpresas($dbcon, $this->___codEmpresa, 'select', 0, 0, 0));
$filtro .= '< td align = "center" > %s< / td > ';
$filtro .= '< / tr > ';
$filtro .= '< / table > ';
/*
* Guarda os filtros passados para recuperar na emiss<EFBFBD> o dos relat?ios para impress<EFBFBD> o.
*/
$_SESSION["SSstorageFiltros"] = array($this->___dataIni, $this->___dataFim);
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);
$emp = $this->___codEmpresa;
// QUERY MASTER
$query = "select d.id as codigo_empresa, d.nome as nome_empresa, e.id as codigo_departamento, e.nome_depto, count(*) as quantidade, sum(case when a.lastdata like ('CONTASENHA%') then 0 else a.billsec end) as duracao
from pbx_bilhetes a, pbx_bilhetes_complemento b, pbx_usuarios_cs c, pbx_empresa d, pbx_departamentos e
where b.uniqueid2 = a.uniqueid
and c.id = b.id_usuario
and e.id = c.departamento
and e.empresa = c.empresa
and d.id = e.empresa
and a.id_bilhetes = (select max (l.id_bilhetes) from pbx_bilhetes l where l.uniqueid = a.uniqueid and l.lastapp < > 'Transferred Call' )
and coalesce(trim(b.dac), '') = ''
and a.calldate::timestamp >= '$dataIni'
and a.calldate::timestamp < = '$dataFim' ";
if ($emp) {
$query .= " and d.id = $emp ";
}
$query .= "group by 1,2,3,4 ";
//if(IsAdmin ()) echo $query;
$result = $this->___GetQuery($query);
$row = pg_fetch_all($result);
$this->SetData($row ? $row : array());
//QUERY DETALHADO
foreach ($row as $queryRow) {
$codEmp = $queryRow['codigo_empresa'];
$codDpt = $queryRow['codigo_departamento'];
$sql = "select c.matricula, c.nome, c.apelido, d.id as codigo_empresa, d.nome as nome_empresa,
e.id as codigo_departamento, e.nome_depto, sum(case when a.lastdata like ('CONTASENHA%') then 0 else a.billsec end) as duracao, count(*) as qtde
from pbx_bilhetes a, pbx_bilhetes_complemento b, pbx_usuarios_cs c, pbx_empresa d, pbx_departamentos e
where b.uniqueid2 = a.uniqueid
and c.id = b.id_usuario
and e.id = c.departamento
and e.empresa = c.empresa
and d.id = e.empresa
and a.id_bilhetes = (select max (l.id_bilhetes) from pbx_bilhetes l where l.uniqueid = a.uniqueid and l.lastapp < > 'Transferred Call' )
and a.calldate::timestamp >= '$dataIni'
and a.calldate::timestamp < = '$dataFim'
and d.id = $codEmp
and e.id = $codDpt
group by c.matricula, c.nome, c.apelido, d.id, d.nome,
e.id, e.nome_depto";
//if(IsAdmin ()) echo $sql;
$resultD = $this->___GetQuery($sql);
$rowD = pg_fetch_all($resultD);
$this->SetData($rowD ? $rowD : array());
}
}
protected function ___PreparaCsv() {
$dataRel = array();
$filtro = $this->___FiltrosRelatorio();
$dataRel[] = GenerateCsvFromArray($filtro);
for ($z = 1; $z < = count($this->GetData()[0]); $z++) {
$dadosCabecalio = array(
'Conta' => 'align="center"',
'Usuario' => 'align="center"',
'Quantidade' => 'align="center"',
'Duracao' => 'align="left"'
);
$dadosField = array(
'matricula' => 'align="center"',
'apelido' => 'align="center"',
'qtde' => 'align="center"',
'duracao' => 'align="left"'
);
$dataRel[] = GenerateCsvFromArray(array_keys($dadosCabecalio));
$somaTempo = 0;
$somaReg = 0;
/*
* Inicia o relat<EFBFBD> rio.
*/
foreach ($this->GetData()[$z] as $row) {
$somaTempo += $row["duracao"];
$somaReg += $row['qtde'];
$row["data"] = date('d/m/Y H:i:s', strtotime($row["data"]));
$row["duracao"] = SecondToStrTime($row["duracao"]);
$linha = array();
foreach ($dadosField as $key => $value) {
$linha[] = $row["$key"];
}
$dataRel[] = GenerateCsvFromArray($linha);
}
$linha = array('Total', '', 'Registros: ' . $somaReg, 'Duracao: ' . SecondToStrTime($somaTempo));
$dataRel[] = GenerateCsvFromArray($linha);
}
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaExcel() {
$dataRel = array();
$filtro = $this->___FiltrosRelatorio();
/*
* Inicia o relat<EFBFBD> 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;
for ($z = 1; $z < = count($this->GetData()[0]); $z++) {
$dadosCabecalio = array('Conta', 'Usuario', 'Quantidade', 'Duracao');
$dadosField = array('matricula', 'apelido', 'qtde', 'duracao');
/*
* Monta a linha de cabecalio.
*/
$linha1 = array('LABEL');
foreach ($dadosCabecalio as $value) {
$linha1[] = $value;
}
$dataRel[] = $linha1;
$somaTempo = 0;
$somaReg = 0;
/*
* Linhas de dados.
*/
foreach ($this->GetData()[$z] as $row) {
$somaTempo += $row["duracao"];
$somaReg += $row['qtde'];
$row["duracao"] = SecondToStrTime($row["duracao"]);
// Formata linha de dados.
$linha2 = array('DADOS');
foreach ($dadosField as $key) {
$linha2[] = $row["$key"];
}
$dataRel[] = $linha2;
}
$linha3 = array('LABEL', 'Total', '', 'Registros: ' . $somaReg, 'Duracao: ' . SecondToStrTime($somaTempo));
$dataRel[] = $linha3;
}
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaGrid() {
$z = 1;
foreach ($this->GetData()[0] as $dadosAr) {
$dadosCabecalio = array(
'Conta' => 'align="left"',
'Usu<EFBFBD> rio' => 'align="left"',
'Quantidade' => 'align="left"',
'Dura<EFBFBD> <EFBFBD> o' => 'align="left"'
);
$dadosField = array(
'matricula' => 'align="left"',
'apelido' => 'align="left"',
'qtde' => 'align="left"',
'duracao' => 'align="left"'
);
/*
* Inicia o relat<EFBFBD> rio.
*/
$dataRel[] = sprintf('< h3 > %s< / h3 > ', $dadosAr['nome_depto']);
$dataRel[] = '< table width = "90%" cellpadding = "2" class = "grid" > ';
/*
* Monta a linha de cabecalio.
*/
//$linha = '< tr > < th colspan = "4" > < h3 > Relat<EFBFBD> rio Totalizador por Centro de Custo Detalhado< / h3 > < / th > < / tr > ';
$linha = '< tr > ';
foreach ($dadosCabecalio as $key => $value) {
$linha .= sprintf("< th % s > %s< / th > ", $value, $key);
}
$linha .= "< / tr > ";
/*
* Linhas de dados.
*/
$somaTempo = 0;
$somaReg = 0;
foreach ($this->GetData()[$z] as $row) {
$somaTempo += $row['duracao'];
$somaReg += $row['qtde'];
$row["duracao"] = SecondToStrTime($row["duracao"]);
// 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.
*/
$linha1 = '< tr > < th colspan = "2" align = "left" > Total: < / th > < th align = "left" > Registros: %s< / th > < th align = "left" > Dura<EFBFBD> <EFBFBD> o: %s< / th > < / tr > ';
$dataRel[] = sprintf($linha1, $somaReg, SecondToStrTime($somaTempo));
$dataRel[] = '< / table > ';
$z++;
}
/*
* Fecha relatorio.
*/
$this->___dataRel[] = $dataRel;
}
protected function ___PreparaHtml() {
}
protected function ___PreparaPdf() {
list($this->___dataIni, $this->___dataFim) = $_SESSION["SSstorageFiltros"];
for ($z = 1; $z < = count($this->GetData()[0]); $z++) {
$somaReg = 0;
$somaTempo = 0;
$x = 0;
$dados = array();
foreach ($this->GetData()[$z] as $row) {
$somaReg += $row['qtde'];
$somaTempo += $row['duracao'];
$dados[$x]["duracao"] = SecondToStrTime($row["duracao"]);
$dados[$x]["apelido"] = $row["apelido"];
$dados[$x]["matricula"] = $row["matricula"];
$dados[$x]["qtde"] = $row["qtde"];
$x++;
}
//HEADER DA TABELA DO PDF -> DADOS EM ARRAY
$header = array(
'Conta' => 'L',
'Usu<EFBFBD> rio' => 'L',
'Quantidade' => 'L',
'Dura<EFBFBD> <EFBFBD> o' => 'L'
);
$dataHeader = array(
'matricula' => 'L',
'apelido' => 'L',
'qtde' => 'L',
'duracao' => 'L'
);
$widthHeader = array(45, 45, 45, 45);
$totaliza = array('Total', '', "Registros: " . $somaReg, "Dura<EFBFBD> <EFBFBD> o: " . SecondToStrTime($somaTempo));
//Monta o array para passar para o prepara; Necess<EFBFBD> rio passar 6 parametros
$table1 = array($header, $dataHeader, $dados, $widthHeader, $totaliza, 0);
$dataRel[] = $table1;
}
$this->___dataRel = $dataRel;
}
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');
}
}
$relSaintes = new TotalizadorCentroCustoDetalhado($idProg, $dbcon, null);
$relSaintes->ExecutaRelatorio();
?>