$query = "-- Procura todas os transbordos que foram gerados pelo asterisk. EXITWITHTIMEOUT
drop table if exists transbordo_saida;
create temporary table transbordo_saida as
select distinct a.fila, a.uniqueid, a.trbd_origem_destino as fila_destino
from pbx_eventos_transbordo a, ast_eventos_dacs b
where b.fila = a.trbd_origem_destino
and b.uid2 = a.uniqueid
and a.data_reg::date = now()::date
and a.trbd_aplicacao = 'EXITWITHTIMEOUT'
and a.trbd_origem_destino_tipo = 'filas'
and trbd_tipo = 'S';
-- Insere o evento TRANSBORDANDO na fila que a chamdada saiu.
insert into ast_eventos_dacs(uid1,uid2,fila,evento)
select trunc(uniqueid::numeric),uniqueid,fila, 'TRANSBORDANDO' as evento from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila and uid2 = a.uniqueid and evento = 'TRANSBORDANDO');
-- Insere o evento TRANSBORDADO na fila que a chamdada entrou(Destino do Transbordo). EXITWITHTIMEOUT faz transbordo mas não insere o evento na fila de destino.
insert into ast_eventos_dacs(uid1,uid2,fila,evento)
select trunc(uniqueid::numeric),uniqueid,fila_destino, 'TRANSBORDADO' as evento from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila_destino and uid2 = a.uniqueid and evento = 'TRANSBORDADO');
-- Procura todas os transbordos que foram gerados de forma assistida pela aplicacao.
drop table if exists transbordo_saida;
create temporary table transbordo_saida as
select distinct a.fila, a.uniqueid, a.trbd_origem_destino as fila_destino
from pbx_eventos_transbordo a, ast_eventos_dacs b
where b.fila = a.trbd_origem_destino
and b.uid2 = a.uniqueid
and a.data_reg::date = now()::date
and a.trbd_aplicacao <> 'EXITWITHTIMEOUT'
and a.trbd_origem_destino_tipo = 'filas'
and trbd_tipo = 'S';
-- Todas as chamadas transbordadas pelo transbordo assistido gera um evento abandon que precisa ser removido.
delete from ast_eventos_dacs a
where exists(select '' from transbordo_saida where uniqueid = a.uid2 and fila = a.fila)
and a.evento = 'ABANDON';
-- Insere o evento TRANSBORDANDO na fila que a chamdada saiu caso nao exista.
insert into ast_eventos_dacs(uid1,uid2,fila,evento)
select trunc(uniqueid::numeric),uniqueid,fila, 'TRANSBORDANDO' as evento from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila and uid2 = a.uniqueid and evento = 'TRANSBORDANDO');
-- Insere o evento TRANSBORDADO na fila que a chamdada entrou(Destino do Transbordo) caso nao exista. EXITWITHTIMEOUT faz transbordo mas não insere o evento na fila de destino.
insert into ast_eventos_dacs(uid1,uid2,fila,evento)
select trunc(uniqueid::numeric),uniqueid,fila_destino, 'TRANSBORDADO' as evento from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila_destino and uid2 = a.uniqueid and evento = 'TRANSBORDADO');
--Procura todos os transbordo que foram gerados para um dispositivo diferente de uma fila.
drop table if exists transbordo_saida;
create temporary table transbordo_saida as
select distinct a.fila, a.uniqueid
from pbx_eventos_transbordo a
where a.data_reg::date = now()::date
and a.trbd_origem_destino_tipo <> 'filas'
and trbd_tipo = 'S';
-- Todas as chamadas transbordadas pelo transbordo assistido gera um evento abandon que precisa ser removido.
delete from ast_eventos_dacs a
where exists(select '' from transbordo_saida where uniqueid = a.uid2 and fila = a.fila)
and a.evento = 'ABANDON';
-- Insere o evento transbordando caso o mesmo nao exista na fila de origem.
insert into ast_eventos_dacs(uid1,uid2,fila,evento)
select trunc(uniqueid::numeric),uniqueid,fila, 'TRANSBORDANDO' as evento from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila and uid2 = a.uniqueid and evento = 'TRANSBORDANDO');
-- Apaga o evento \"EXITWITHTIMEOUT\" gerado pelo transbordo nativo.
delete from ast_eventos_dacs b using ast_bilhetes a where b.uid2 = a.uniqueid and a.calldate::date = now()::date and b.evento = 'EXITWITHTIMEOUT';";
drop table if exists transbordo_saida;
create temporary table transbordo_saida as
select distinct a.fila, a.uniqueid, a.trbd_origem_destino as fila_destino, a.org_id
from pbx_eventos_transbordo a, ast_eventos_dacs b
where b.fila = a.trbd_origem_destino
and b.uid2 = a.uniqueid
and a.data_reg::date = now()::date
and a.trbd_aplicacao = 'EXITWITHTIMEOUT'
and a.trbd_origem_destino_tipo = 'filas'
and trbd_tipo = 'S'
and a.org_id = b.org_id
;
-- Insere o evento TRANSBORDANDO na fila que a chamdada saiu.
insert into ast_eventos_dacs(uid1,uid2,fila,evento, org_id)
select trunc(uniqueid::numeric),uniqueid,fila, 'TRANSBORDANDO' as evento, org_id from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila and uid2 = a.uniqueid and evento = 'TRANSBORDANDO');
-- Insere o evento TRANSBORDADO na fila que a chamdada entrou(Destino do Transbordo). EXITWITHTIMEOUT faz transbordo mas não insere o evento na fila de destino.
insert into ast_eventos_dacs(uid1,uid2,fila,evento, org_id)
select trunc(uniqueid::numeric),uniqueid,fila_destino, 'TRANSBORDADO' as evento, org_id from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila_destino and uid2 = a.uniqueid and evento = 'TRANSBORDADO');
-- Procura todas os transbordos que foram gerados de forma assistida pela aplicacao.
drop table if exists transbordo_saida;
create temporary table transbordo_saida as
select distinct a.fila, a.uniqueid, a.trbd_origem_destino as fila_destino, a.org_id
from pbx_eventos_transbordo a, ast_eventos_dacs b
where b.fila = a.trbd_origem_destino
and b.uid2 = a.uniqueid
and a.data_reg::date = now()::date
and a.trbd_aplicacao <> 'EXITWITHTIMEOUT'
and a.trbd_origem_destino_tipo = 'filas'
and trbd_tipo = 'S'
and a.org_id = b.org_id
;
-- Todas as chamadas transbordadas pelo transbordo assistido gera um evento abandon que precisa ser removido.
delete from ast_eventos_dacs a
where exists(select '' from transbordo_saida where uniqueid = a.uid2 and fila = a.fila and org_id = a.org_id)
and a.evento = 'ABANDON'
;
-- Insere o evento TRANSBORDANDO na fila que a chamdada saiu caso nao exista.
insert into ast_eventos_dacs(uid1,uid2,fila,evento, org_id)
select trunc(uniqueid::numeric),uniqueid,fila, 'TRANSBORDANDO' as evento, org_id from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila and uid2 = a.uniqueid and evento = 'TRANSBORDANDO' and org_id = a.org_id);
-- Insere o evento TRANSBORDADO na fila que a chamdada entrou(Destino do Transbordo) caso nao exista. EXITWITHTIMEOUT faz transbordo mas não insere o evento na fila de destino.
insert into ast_eventos_dacs(uid1,uid2,fila,evento, org_id)
select trunc(uniqueid::numeric),uniqueid,fila_destino, 'TRANSBORDADO' as evento, org_id from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila_destino and uid2 = a.uniqueid and evento = 'TRANSBORDADO' and org_id = a.org_id);
--Procura todos os transbordo que foram gerados para um dispositivo diferente de uma fila.
drop table if exists transbordo_saida;
create temporary table transbordo_saida as
select distinct a.fila, a.uniqueid, a.org_id
from pbx_eventos_transbordo a
where a.data_reg::date = now()::date
and a.trbd_origem_destino_tipo <> 'filas'
and trbd_tipo = 'S'
;
-- Todas as chamadas transbordadas pelo transbordo assistido gera um evento abandon que precisa ser removido.
delete from ast_eventos_dacs a
where exists(select '' from transbordo_saida where uniqueid = a.uid2 and fila = a.fila and org_id = a.org_id)
and a.evento = 'ABANDON';
-- Insere o evento transbordando caso o mesmo nao exista na fila de origem.
insert into ast_eventos_dacs(uid1,uid2,fila,evento, org_id)
select trunc(uniqueid::numeric),uniqueid,fila, 'TRANSBORDANDO' as evento, org_id from transbordo_saida a where not exists(select '' from ast_eventos_dacs where fila = a.fila and uid2 = a.uniqueid and evento = 'TRANSBORDANDO' and org_id = a.org_id);
-- Apaga o evento \"EXITWITHTIMEOUT\" gerado pelo transbordo nativo.
delete from ast_eventos_dacs b using ast_bilhetes a where b.uid2 = a.uniqueid and a.calldate::date = now()::date and b.evento = 'EXITWITHTIMEOUT' and a.org_id = b.org_id;";