﻿--case ORD_0800:
            select cast('S' as char(1)) as tipo, case when(exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid))then 'S' else 'N' end as flag_fila, a.id_bilhetes, a.dst, a.uniqueid
                   from pbx_bilhetes a
                  where tipo_chamada = 99
                    and substring(dst,1,4) = '0800';
        --case ORD_EMERGENCIA:
            select cast('S' as char(1)) as tipo, case when(exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid))then 'S' else 'N' end as flag_fila, a.id_bilhetes, a.dst, a.uniqueid
                   from pbx_bilhetes a
                  where tipo_chamada = 99
                    and dst in('100', '128', '190', '191', '192', '193', '194', '197', '198', '199');
        --case ORD_INTERNA:
            select distinct cast('I' as char(1)) as tipo, case when(exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid))then 'S' else 'N' end as flag_fila, a.id_bilhetes, a.dst, a.uniqueid
                   from pbx_bilhetes a
                  where a.tipo_chamada = 99
                    and exists(select '' from pbx_ramais where nome = a.src)
                    and exists(select '' from pbx_ramais where nome = a.dst);
         --case ORD_INTERNA_SC: //ligacao interna nao inserida na pbx_bilhetes_complemento.
            select distinct cast('I' as char(1)) as tipo,case when(exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid))then 'S' else 'N' end as flag_fila, a.id_bilhetes, a.dst, a.uniqueid
                   from pbx_bilhetes a 
                  where a.tipo_chamada = 99  
                    and dst not in('100', '128', '190', '191', '192', '193', '194', '197', '198', '199')
                    and length(sonumero(src)) < 8 and length(sonumero(dst)) < 8;
        --case ORD_FILA_E:
            select cast('E' as char(1)) as tipo, 'S' as flag_fila, a.id_bilhetes, a.src as dst, a.uniqueid
                    from pbx_bilhetes a, pbx_eventos_dacs b 
                   where b.uid2 = a.uniqueid 
                     and a.tipo_chamada = 99
                     and length(a.src) >= 8
                     and exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid);
        --case ORD_FIlA_S:
            select cast('S' as char(1)) as tipo, 'S' as flag_fila, a.id_bilhetes, a.src as dst, a.uniqueid
                    from pbx_bilhetes a, pbx_eventos_dacs b 
                   where b.uid2 = a.uniqueid 
                     and a.tipo_chamada = 99
                     and length(a.dst) >= 8
                     and exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid);     
        --case ORD_ENTRADA:
            select  distinct cast('E' as char(1)) as tipo, 'N' as flag_fila, a.id_bilhetes, a.src as dst, a.uniqueid
                   from pbx_bilhetes a, pbx_bilhetes_complemento b
                  where b.uniqueid2 = a.uniqueid
                     and length(a.src) >= 8
                    and not exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid)
                    and a.tipo_chamada = 99;
        --case ORD_SAIDA:
            select  distinct cast('S' as char(1)) as tipo, 'N' as flag_fila, a.id_bilhetes, a.dst, a.uniqueid
                   from pbx_bilhetes a, pbx_bilhetes_complemento b
                  where b.uniqueid2 = a.uniqueid
                     and length(a.dst) >= 8
                    and not exists(select '' from pbx_eventos_dacs where uid2 = a.uniqueid)
                    and a.tipo_chamada = 99;    