DROP VIEW IF exists pbx_ramais;
CREATE OR REPLACE VIEW pbx_ramais
AS SELECT a.id,
    'SIP'::character varying(16) AS tipo_ramal,
    a.nome,
    a.dispositivo,
    a.context AS contexto,
    a.habilita_prepago,
    a.saldo_prepago,
    a.ramalsecretaria,
    a.sigame_interno,
    a.sigame_externo,
    a.sigame_naoatende,
    a.sigame_ocupado,
    a.extranet,
    a.org_id
   FROM pbx_sip_ramais a
UNION ALL
 SELECT b.id,
    'IAX2'::character varying(16) AS tipo_ramal,
    b.nome,
    b.dispositivo,
    b.context AS contexto,
    b.habilita_prepago,
    b.saldo_prepago,
    b.ramalsecretaria,
    b.sigame_interno,
    b.sigame_externo,
    b.sigame_naoatende,
    b.sigame_ocupado,
    ''::character varying(256) AS extranet,
    b.org_id
   FROM pbx_iax_pbx b
UNION ALL
 SELECT c.id,
    'PBX'::text AS tipo_ramal,
    c.numero AS nome,
    (c.dispositivo::text || '/'::text) || c.numero::text AS dispositivo,
    c.contexto,
    c.habilita_prepago,
    c.saldo_prepago,
    c.ramalsecretaria,
    c.sigame_interno,
    c.sigame_externo,
    c.sigame_naoatende,
    c.sigame_ocupado,
    ''::character varying(256) AS extranet,
    c.org_id
   FROM pbx_ramais_pbx c
  ORDER BY 1;
--------------------------------------------------------
DROP VIEW IF exists pbx_ramais_mesa;
CREATE OR REPLACE VIEW pbx_ramais_mesa
AS SELECT 0 AS tipo,
   pbx_sip_ramais.id,
   pbx_sip_ramais.nome,
   pbx_sip_ramais.dispositivo,
   pbx_sip_ramais.callerid,
   'SIP'::text AS tipo_table,
   pbx_sip_ramais.conta_senha,
   ''::character varying AS ddd,
   pbx_sip_ramais.context AS contexto,
   pbx_sip_ramais.org_id as org_id
   FROM pbx_sip_ramais
UNION ALL
 SELECT 1 AS tipo,
   0 AS id,
   pbx_ramais_agenda.rma_telefone AS nome,
   pbx_ramais_agenda.rma_telefone AS dispositivo,
   pbx_ramais_agenda.rma_nome AS callerid,
   'AGD'::text AS tipo_table,
   'F'::character varying AS conta_senha,
   pbx_ramais_agenda.ddd,
   ' '::character varying AS contexto,
   pbx_ramais_agenda.org_id AS org_id
   FROM pbx_ramais_agenda
UNION ALL
 SELECT 0 AS tipo,
   pbx_iax_pbx.id,
   pbx_iax_pbx.nome,
   pbx_iax_pbx.dispositivo,
   pbx_iax_pbx.nome AS callerid,
   'IAX'::text AS tipo_table,
   pbx_iax_pbx.conta_senha,
   ''::character varying AS ddd,
   pbx_iax_pbx.context AS contexto,
   pbx_iax_pbx.org_id AS org_id
    FROM pbx_iax_pbx
  ORDER BY 5;
--------------------------------------------------------
DROP VIEW IF exists pbx_troncos;
CREATE OR REPLACE VIEW pbx_troncos
AS SELECT 'SIP'::text AS tipo,
    pbx_troncos_sip.id,
    pbx_troncos_sip.nome,
    pbx_troncos_sip.max_ligacoes,
    pbx_troncos_sip.nome AS username,
    pbx_troncos_sip.contrato,
    pbx_troncos_sip.callerid,
    pbx_troncos_sip.context,
    pbx_troncos_sip.callerid AS callerid_tronco,
    pbx_troncos_sip.portabilidade,
    pbx_troncos_sip.org_id 
   FROM pbx_troncos_sip
UNION ALL
 SELECT 'IAX2'::text AS tipo,
    pbx_troncos_iax.id,
    pbx_troncos_iax.nome,
    pbx_troncos_iax.max_ligacoes,
    pbx_troncos_iax.nome AS username,
    pbx_troncos_iax.contrato,
    pbx_troncos_iax.callerid,
    pbx_troncos_iax.context,
    pbx_troncos_iax.callerid AS callerid_tronco,
    pbx_troncos_iax.portabilidade,
    pbx_troncos_iax.org_id 
   FROM pbx_troncos_iax
UNION ALL
 SELECT 'KHOMP'::text AS tipo,
    pbx_troncos_khomp.id,
    pbx_troncos_khomp.descricao AS nome,
    pbx_troncos_khomp.max_ligacoes,
    pbx_troncos_khomp.grupo AS username,
    pbx_troncos_khomp.contrato,
    pbx_troncos_khomp.callerid,
    pbx_troncos_khomp.contexto AS context,
    pbx_troncos_khomp.callerid AS callerid_tronco,
    pbx_troncos_khomp.portabilidade,
    pbx_troncos_khomp.org_id 
   FROM pbx_troncos_khomp;

   ---------------------------

DROP VIEW IF exists pbx_lista_arquivo_som;
CREATE OR REPLACE VIEW pbx_lista_arquivo_som AS
SELECT a.tipo,
    a.arq_som,
    org_id
   FROM ( SELECT 'ura'::text AS tipo,
            pbx_ura.som_ura AS arq_som,
            org_id
           FROM pbx_ura
        UNION
         SELECT 'anuncio'::text AS tipo,
            pbx_anuncios.musica AS arq_som,
            org_id
           FROM pbx_anuncios
        UNION
         SELECT 'conferencia'::text AS tipo,
            pbx_conferencia.anuncio AS arq_som,
            org_id
           FROM pbx_conferencia) a
  WHERE btrim(a.arq_som::text) <> ''::text;