difDataHora($horaAnterior, substr(FormataDBDataHora($dados["inicio_operacao"]), 0, 19), 's'); //Insere operacao de tempo Livre $query .= FormataInsert($insert, $dados, $dataAtu, 'livre', $tempoLivre, 1); // $resultInsert = pg_query($dbcon, $query); if (true) { //if($resultInsert) $query .= FormataInsert($insert, $dados, $dataAtu, $dados['operacao'], $tempoLivre); // $resultInsert = pg_query($dbcon, $query); } $horaAnterior = ''; } //Verifica se o insert foi bem sucedido //if(!$resultInsert) break; } pg_query($dbcon, $query); } $GLOBALS["buffLinhas"] = array(); $linhas = GetResult($dbcon); $linhas .= GetResult($dbcon, 1); $imp = $GLOBALS["imp"]; $_SESSION["SSlinhasDesempenhoDiscador"] = $GLOBALS["buffLinhas"]; } $jsPage[] = "functions.js"; //$jsJQuery[] = "\$('#listaDacs').change(function(){GetCampanhaLista();}) "; $jsJQuery[] = "\$('#dataIni').keypress(function(){formataData(this);});"; $jsJQuery[] = "\$('#dataFim').keypress(function(){formataData(this);}); "; $jsJQuery[] = "\$('#dataIni').change(function(){GetCampanha();}); "; $jsJQuery[] = "\$('#dataFim').change(function(){GetCampanha();}); "; $jsJQuery[] = "\$('#dataFim').focus(function(){this.value = '';}); "; $jsJQuery[] = "\$('#dataIni').focus(function(){this.value = '';}); "; $dacs = GetCampanhas($dbcon, $cmpId, 0, $_SESSION["dataIni"], $_SESSION["dataFim"]); $listas = GetListaCampanha($dbcon, $cmpId, $listId, FormatDtMssql($_SESSION["dataIni"]), FormatDtMssql($_SESSION["dataFim"])); //Atualiza template $smarty->assign("dacs", $dacs); $smarty->assign("listas", $listas); $smarty->assign('dataIni', $_SESSION["dataIni"]); $smarty->assign('dataFim', $_SESSION["dataFim"]); $smarty->assign("linha", $linhas); $smarty->assign("imp", $imp); //Seta o template GetTemplate($smarty, $templateName); } function GetQuery($cmpId) { $listId = $_POST["listId"]; $dtIni = is_date($_SESSION["dataIni"]) ? FormatDtMssql($_SESSION["dataIni"]) : ''; $dtFim = is_date($_SESSION["dataFim"]) ? FormatDtMssql($_SESSION["dataFim"]) : ''; $dddpadrao = GetDddPadrao(); $tipoChamada = $_SESSION["tipoChamada"]; $matricula = '0'; $ret = ""; $ret = "CREATE TEMPORARY TABLE agente_operacao (matricula varchar(4) not null, data date not null, inicio_operacao timestamp not null, final_operacao timestamp, apelido varchar(20), id_dac int not null, dac varchar(120), list_id int, list_nome varchar(100), uniqueid varchar(32), operacao varchar(20), tempo_operacao bigint); CREATE INDEX \"idxAnaliseDacOpera\" ON agente_operacao (id_dac, data, matricula ); CREATE INDEX \"idxAnaliseDacOperaOrd\" ON agente_operacao (matricula, inicio_operacao ); CREATE INDEX \"pkAnaliseDacOpera\" ON agente_operacao (id_dac, uniqueid, inicio_operacao, matricula); CREATE TEMPORARY TABLE agente_pausa (matricula varchar(4) not null, data date not null, inicio_operacao timestamp not null, final_operacao timestamp, apelido varchar(20), id_dac int not null, dac varchar(120), list_id int, list_nome varchar(100), uniqueid varchar(32), operacao varchar(20), tempo_operacao bigint); CREATE INDEX \"idxAnaliseOpera\" ON agente_pausa (id_dac, data, matricula); CREATE INDEX \"CONSTRAINT\" ON agente_pausa (id_dac, uniqueid, inicio_operacao, matricula); insert into agente_operacao select i.matricula, conf_data::date, a.conf_data as inicio_operacao, a.conf_data as final_oepracao, i.apelido, a.cmp_id as id_dac, b.cmp_descricao as dac, a.list_id, j.list_nome, c.uniqueid, cast('atendimento' as varchar(30)) as operacao, sum(case when(h.evento in('COMPLETECALLER', 'COMPLETEAGENT', 'CONNECT'))then case when(h.evento <> 'CONNECT') then h.param2::int else param1::int end else 0 end) as tempo_operacao from pbx_campanha_contato_fone a inner join pbx_campanha b on b.cmp_id = a.cmp_id inner join pbx_campanha_complemento c on c.conf_id = a.conf_id inner join pbx_bilhetes g on g.uniqueid = c.uniqueid inner join pbx_eventos_dacs h on h.uid2 = g.accountcode and h.evento in('COMPLETEAGENT', 'COMPLETECALLER', 'CONNECT') and h.fila = b.cmp_descricao inner join pbx_usuarios i on i.matricula = c.matricula inner join pbx_campanha_lista j on j.list_id = a.list_id where a.cmp_id = '$cmpId' and a.list_id = case when($listId = 0)then a.list_id else $listId end and a.conf_data::date >= '$dtIni' and a.conf_data::date <= '$dtFim' and g.lastapp <> 'Transferred Call' and i.matricula = case when('$matricula' = '0')then i.matricula else '$matricula' end group by 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; insert into agente_pausa select distinct matricula, entrada_pausa::date, entrada_pausa, coalesce(saida_pausa, entrada_pausa) as saida_pausa, cast('' as varchar(20)) as apelido, id_dac, cast('' as varchar(100)) as dac, 0 as id_lista,cast('' as varchar(100)) as list_nome, '', b.motivo, extract(epoch from ((coalesce(saida_pausa, entrada_pausa) - entrada_pausa))) as tempo_operacao from pbx_eventos_agentes a, pbx_motivos_pausas b where b.id = a.id_motivo_pausa and coalesce(login, a.entrada_pausa)::date >= (select min(login) as login from agente_operacao c, pbx_campanha_complemento d, pbx_eventos_agentes a where d.uniqueid = c.uniqueid and a.id = d.login_id)::date and coalesce(login,a.entrada_pausa)::date <= (select max(logoff) as login from agente_operacao c, pbx_campanha_complemento d, pbx_eventos_agentes a where d.uniqueid = c.uniqueid and a.id = d.login_id)::date and id_dac = '$cmpId' and a.matricula = case when('$matricula' = '0')then a.matricula else '$matricula' end; insert into agente_operacao select * from agente_pausa a where exists( select '' from agente_operacao b where b.id_dac = a.id_dac and b.data = a.data::date and b.matricula = a.matricula ); "; //if(IsAdmin ()){ echo $ret; exit;}//and motivo = 'acw' return $ret; } function FormataInsert($query, $dados, $dataAtu, $operacao, $tempoLivre, $tmpLivre = 0) { $finalOper = ($operacao == 'atendimento') ? SomaSegundos($dados["inicio_operacao"], $dados['tempo_operacao']) : $dados['final_operacao']; return sprintf($query, QuotedStr(session_id()), QuotedStr($dataAtu), QuotedStr($dados['matricula']), QuotedStr($dados['data']), QuotedStr($dados['inicio_operacao']), QuotedStr($finalOper), QuotedStr($dados['apelido']), QuotedStr($dados['id_dac']), QuotedStr($dados['dac']), QuotedStr($dados['list_id']), QuotedStr($dados['list_nome']), QuotedStr($operacao), QuotedStr(($tmpLivre ? $tempoLivre : $dados['tempo_operacao'])), QuotedStr($tempoLivre) ) . ";\n"; } function SomaSegundos($dtDb, $eg) { /* * Retorna array com inforamcoes da data */ $d = split('[-" ":.]', $dtDb); /* * Converte as informacoes do array em timestamp * -> HORA, MINUTO, SEGUNDO, MÊS, DIA, ANO */ return date('Y-m-d H:i:s', mktime($d[3], $d[4], ($d[5] + $eg), $d[1], $d[2], $d[0])); /* * Data Banco -> 2011-12-30 12:45:59.084715 * Retorno Split -> * Array * ( * [0] => 2011 * [1] => 12 * [2] => 30 * [3] => 12 * [4] => 45 * [5] => 59 * [6] => 084715 * ) */ } function GetResult($dbcon, $agrup = 0) { $idSessao = session_id(); if ($agrup) { $query = "select a.id_dac, b.cmp_descricao as dac, sum(case when(operacao = 'atendimento')then tempo_operacao else 0 end) as tempo_atendimento, sum(case when(operacao = 'atendimento')then 1 else 0 end) as qtde_atendimento, sum(case when(operacao = 'livre')then tempo_livre else 0 end) as tempo_livre, sum(case when(operacao = 'livre')then 1 else 0 end) as qtde_livre, sum(case when(operacao not in('livre', 'atendimento'))then tempo_operacao else 0 end) as tempo_pausa, sum(case when(operacao not in('livre', 'atendimento'))then 1 else 0 end) as qtde_pausa from pbx_desempenho_discador a, pbx_campanha b, pbx_usuarios c where b.cmp_id = a.id_dac and id_sessao = '$idSessao' and c.matricula = a.matricula group by a.id_dac, b.cmp_descricao "; } else { $query = "select a.id_dac, b.cmp_descricao as dac, a.matricula, c.apelido, sum(case when(operacao = 'atendimento')then tempo_operacao else 0 end) as tempo_atendimento, sum(case when(operacao = 'atendimento')then 1 else 0 end) as qtde_atendimento, sum(case when(operacao = 'livre')then tempo_livre else 0 end) as tempo_livre, sum(case when(operacao = 'livre')then 1 else 0 end) as qtde_livre, sum(case when(operacao not in('livre', 'atendimento'))then tempo_operacao else 0 end) as tempo_pausa, sum(case when(operacao not in('livre', 'atendimento'))then 1 else 0 end) as qtde_pausa from pbx_desempenho_discador a, pbx_campanha b, pbx_usuarios c where b.cmp_id = a.id_dac and id_sessao = '$idSessao' and c.matricula = a.matricula group by a.id_dac, b.cmp_descricao,a.matricula, c.apelido order by matricula"; } //if(IsAdmin ()) echo $query; $result = pg_query($dbcon, $query); $cmpId = $GLOBALS["cmpId"]; $buffLinhas = array(); $imp = 0; while ($dados = pg_fetch_array($result)) { $dataIni = $_SESSION["dataIni"]; $dataFim = $_SESSION["dataFim"]; $dacs = $dados["id_dac"] . "-" . $dados["dac"]; $listas = $listId ? ($listId . "-" . $dados["list_nome"]) : "Todas"; $agente = $dados["apelido"] . "/" . $matricula; $matricula = $dados["matricula"]; $_SESSION["dac"] = $dados["dac"]; $quantAtende = $dados["qtde_atendimento"]; $quantPausa = $dados["qtde_pausa"]; $tempoAtende = SecondToStrTime(round($dados["tempo_atendimento"] / $quantAtende)); $tempoPausa = SecondToStrTime(round($dados["tempo_pausa"] / $quantAtende)); $agt = $dados[apelido] . "/" . $dados[matricula]; $linkOper = "$agt"; $tempoLivre = $dados["tempo_livre"]; $tempoLivre = SecondToStrTime(round($tempoLivre / $quantAtende), 0); $linkPausa = !$quantPausa ? 0 : "$quantPausa"; // $tempoTotalAtendimento $tempoTotalAtendimento = 0; //substr($datasRange[$dados[matricula]][3],0, 8); if (!$agrup) { $linha = " $dados[dac] $linkOper $quantAtende $tempoAtende $linkPausa $tempoPausa $tempoLivre "; } else { $linha = " Geral $quantAtende $tempoAtende $quantPausa $tempoPausa $tempoLivre "; } $GLOBALS["buffLinhas"][] = $linha; $linhas .= $linha; $imp++; } $GLOBALS["imp"] = $imp; return $linhas; } ?>