--DEL35_DBCFX_CRM_PRE_RDAC_ESPL
create
or replace view del35_dbcfx_crm_pre_rdac_espl
(unique_id, id_richiesta, codice_richiesta, inbound_outbound, sistema_in_out, cod_tipo_comunicazione, desc_tipo_comunicazione, delibera, esito_notifica, data_inv_not_da_dbcfx, data_ric_not_su_dbcfx, data_notifica, codice_ordine_olo, data_ordine_olo, codice_ordine_ti, data_ordine_ti, dn, id_risorsa, nome_file_inviato_a_now, nome_file_ricevuto_da_now, cod_operatore_recipient, cod_operatore_donor, cod_operatore_donating, dac, des, dac_rimodulata, bid, unique_id_now, unique_id_crm_notify, unique_id_crm, unique_id_now_notify, data_filtro)
as
select SYS_GUID()                                         as UNIQUE_ID,
       null                                               AS ID_RICHIESTA,
       CAST(gcndo.codice_richiesta AS varchar2(100))      AS CODICE_RICHIESTA,
       'OUTBOUND'                                         AS INBOUND_OUTBOUND,
       CAST(gcndo.sist_comp AS varchar2(100))             AS SISTEMA_IN_OUT,
       CAST(gfri.tipo_comunicazione AS varchar2(100))     AS TIPO_COMUNICAZIONE,
       CASE gfri.TIPO_COMUNICAZIONE
           WHEN 2 THEN CAST('Notificaito  verifiche Formali e Tecniche del Donor vs Recipient' AS varchar2
                            (100)) --Non interessato
           WHEN 3 THEN CAST('PRENOTIFICA' AS varchar2(100))
           WHEN 5 THEN CAST('ESITO VERIFICHE CS' AS varchar2(100))
           WHEN 6 THEN CAST('Comunicazione es verifiche  del Donor vs Recipient' AS varchar2 (100)) -- Non Interessato
           WHEN 7 THEN CAST('ESPLETAMENTO' AS varchar2(100))
           WHEN 10 THEN CAST('RIMODULAZIONE DAC' AS varchar2(100))
           WHEN 11 THEN CAST('ANNULLAMENTO' AS varchar2(100))
           WHEN 17 THEN CAST('RIMODULAZIONE DAC' AS varchar2(100))
           WHEN 18 THEN CAST('ANNULLAMENTO' AS varchar2(100))
           END                                            AS DESC_TIPO_COMUNICAZIONE,
       CASE
           WHEN gcndo.ambito = 'USCITANP_DEL103' THEN 'Del.103'
           WHEN gcndo.ambito = 'USCITANP_DEL35' THEN 'Del.35'
           ELSE gcndo.ambito
           END                                            AS DELIBERA,
       null                                               AS ESITO_NOTIFICA,
       CAST(gcndo.data_invio_messaggio AS varchar2(100))  AS DATA_INV_NOT_DA_DBCFX,
       null                                               AS DATA_RIC_NOT_SU_DBCFX,
       CAST(gfri.data_ricezione AS varchar2(100))         AS DATA_NOTIFICA,
       CAST(gcndo.codice_ordine_olo AS varchar2(100))     as CODICE_ORDINE_OLO,
       CAST(gcndo.data_ordine AS varchar2(100))           as DATA_ORDINE_OLO,
       null                                               as CODICE_ORDINE_TI,
       null                                               as DATA_ORDINE_TI,
       CAST(gcndo.directory_number AS varchar2(100))      as DN,
       null                                               as ID_RISORSA,
       null                                               as NOME_FILE_INVIATO_A_NOW,
       CAST(gfri.NOME_FILE AS varchar2(100))              as NOME_FILE_RICEVUTO_DA_NOW,
       CAST(gfri.COD_OP_RECIPIENT AS varchar2(100))       as COD_OPERATORE_RECIPIENT,
       CAST(gcndo.id_operatore_donor AS varchar2(100))    AS COD_OPERATORE_DONOR,
       CAST(gcndo.id_operatore_donating AS varchar2(100)) AS COD_OPERATORE_DONATING,
       CAST(gcndo.DAC AS varchar2(100))                   AS DAC,
       CAST(gcndo.DES AS varchar2(100))                   AS DES,
       CASE gcndo.tipo_evento
           WHEN 'RIMODULAZIONE_DAC' THEN CAST(gcndo.DAC AS varchar2(100))
           END                                            AS DAC_RIMODULATA,
       CAST(gcndo.BUSINESS_ID AS varchar2(100))           AS BID,
       CAST(GFRI.PROG_ID AS varchar2(100))                AS UNIQUE_ID_NOW,
       CAST(gcndo.unique_id AS varchar2(100))             AS UNIQUE_ID_CRM_NOTIFY,
       null                                                  UNIQUE_ID_CRM,
       null                                                  UNIQUE_ID_NOW_NOTIFY,
       GFRI.DATA_RICEZIONE                                AS DATA_FILTRO
from gnp_crm_notify_donor_out gcndo
         inner join gnp_fenp_richieste_in GFRI on gcndo.CODICE_RICHIESTA = GFRI.id_richiesta
where GFRI.TIPO_COMUNICAZIONE IN (3, 7, 10, 11, 17, 18)
  AND gcndo.tipo_evento = (CASE gfri.TIPO_COMUNICAZIONE
                               WHEN 3 THEN CAST('PRENOTIFICA' AS varchar2(100))
                               WHEN 7 THEN CAST('ESPLETAMENTO' AS varchar2(100))
                               WHEN 10 THEN CAST('RIMODULAZIONE_DAC' AS varchar2(100))
                               WHEN 11 THEN CAST('ANNULLAMENTO' AS varchar2(100))
                               WHEN 17 THEN CAST('RIMODULAZIONE_DAC' AS varchar2(100))
                               WHEN 18 THEN CAST('ANNULLAMENTO' AS varchar2(100))
    END)
  AND ambito IN ('USCITANP_DEL35', 'USCITANP_DEL103', 'Del.103', 'Del.35')
;