$query = sprintf("select a.uniqueid, a.calldate, a.src, a.dst, case when(d.evento = 'ABANDON')then strtoint(d.param3) else a.billsec end as tempo_atendimento, coalesce(d.fila, '-') as fila, coalesce(e.apelido, '-') as agente,
$query = sprintf(
d.evento, case when(tplg_codigo in(33,34))then 'MÓVEL' else c.nome_municipio end as nome_municipio, c.uf, b.tplg_descricao,
"SELECT
case when((d.evento in('COMPLETAAGENT','COMPLETACALLER') and (billsec > 3) ) or ((d.evento is null) and (billsec > 3)))then 'ATENDIDA' else 'N ATENDIDA' end as status
a.uniqueid,
from pbx_bilhetes a
a.calldate,
inner join pbx_tipo_ligacao b on b.tplg_codigo = a.tipo_chamada
a.src,
inner join pbx_municipios c on c.id = a.id_municipio
a.dst,
left join pbx_eventos_dacs d on d.uid2 = a.uniqueid and d.evento in('COMPLETAAGENT','COMPLETACALLER','ABANDON')
CASE
left join pbx_usuarios e on e.matricula = substring(d.agente,7,4)
WHEN (d.evento = 'ABANDON') THEN STRTOINT(d.param3)
where b.tplg_tipo = 3
ELSE a.billsec
AND a.calldate = (SELECT MAX(calldate) FROM pbx_bilhetes WHERE uniqueid = a.uniqueid)
END AS tempo_atendimento,
and a.calldate::timestamp >= '%s'
COALESCE(d.fila, '-') AS fila,
and a.calldate::timestamp <= '%s'
COALESCE(e.apelido, '-') AS agente,
and a.lastapp <> 'Transferred Call'", $dataIni, $dataFim);
d.evento,
CASE
WHEN (tplg_codigo IN (33, 34)) THEN 'MÓVEL'
ELSE c.nome_municipio
END AS nome_municipio,
c.uf,
b.tplg_descricao,
CASE
WHEN ((d.evento IN ('COMPLETAAGENT', 'COMPLETACALLER') AND (billsec > 3))
OR ((d.evento IS NULL) AND (billsec > 3))) THEN 'ATENDIDA'
ELSE 'N ATENDIDA'
END AS status
FROM pbx_bilhetes a
INNER JOIN pbx_tipo_ligacao b ON b.tplg_codigo = a.tipo_chamada
LEFT JOIN pbx_municipios c ON c.id = a.id_municipio
LEFT JOIN pbx_eventos_dacs d ON d.uid2 = a.uniqueid
AND d.evento IN ('COMPLETAAGENT', 'COMPLETACALLER', 'ABANDON')
LEFT JOIN pbx_usuarios e ON e.matricula = SUBSTRING(d.agente, 7, 4)
WHERE
b.tplg_tipo = 3
AND a.calldate = (SELECT MAX(calldate) FROM pbx_bilhetes WHERE uniqueid = a.uniqueid)
AND a.calldate::TIMESTAMP >= '%s'
AND a.calldate::TIMESTAMP <= '%s'
AND a.lastapp <> 'Transferred Call'",
$dataIni,
$dataFim
);
if (!empty($telefone) && ($fonePesquisa == 1))
if (!empty($telefone) && ($fonePesquisa == 1))
$query .= sprintf(" AND a.src LIKE '%s' ", PreparaLike($telefone));
$query .= sprintf(" AND a.src LIKE '%s' ", PreparaLike($telefone));