--Eseguire come utente MNP
alter session set db_file_multiblock_read_count=64;

ALTER TABLE MNP_GESTIONE_RICHIESTA_CESS ADD(
	ESITO_CESSAZIONE VARCHAR2(50) NULL
);

ALTER TABLE MNP_GESTIONE_RICH_CESS_PORT ADD(
	TIM_DONOR	 NUMBER(1)  NULL,
	ESITO_CESSAZIONE VARCHAR2(50) NULL
);

CREATE TABLE MNP_MSS_VERIFICA_CESS_OUT
(
  ID_RICHIESTA    VARCHAR2(23 BYTE)             NOT NULL,
  MSISDN          VARCHAR2(10 BYTE)             NOT NULL,
  DA_PROCESSARE   NUMBER(1)                     DEFAULT 0                     NOT NULL,
  DATA_CREAZIONE  DATE                          DEFAULT SYSDATE,
  DA_SCODARE      NUMBER(1)                     DEFAULT 0                     NOT NULL,
  TIPO_PROCESSO   NUMBER(1)                     NULL
);

CREATE UNIQUE INDEX PK_MSS_VERIFICA_CESS_OUT ON MNP_MSS_VERIFICA_CESS_OUT
(ID_RICHIESTA);

ALTER TABLE MNP_MSS_VERIFICA_CESS_OUT
ADD CONSTRAINT  PK_MSS_VERIFICA_CESS_OUT PRIMARY KEY (ID_RICHIESTA);

ALTER TABLE MNP.MNP_GISP_CESS_OUT
 ADD (FORZATURA_GUI  VARCHAR2(1));
ALTER TABLE MNP.MNP_GISP_NOTIFICA_IN
 ADD (FORZATURA_GUI  VARCHAR2(1));
ALTER TABLE MNP.MNP_GISP_ATT_CESS_IN
 ADD (FORZATURA_GUI  VARCHAR2(1));
ALTER TABLE MNP.MNP_GISP_ATT_OUT
 ADD (FORZATURA_GUI  VARCHAR2(1));
ALTER TABLE MNP.MNP_ASP_CRM_CESSAZIONE_OUT
 ADD (FLAG_BLOCCANTE  CHAR(1));
 
CREATE TABLE MNP_GISP_CESS_OUT_DON_ABB
(
  UNIQUE_ID             NUMBER                  NOT NULL,
  DATA_CREAZIONE        DATE                    DEFAULT SYSDATE,
  DATA_RICHIESTA_INVIO  DATE,
  DATA_INVIO            DATE,
  DA_SCODARE            NUMBER(1)               NOT NULL,
  DA_PROCESSARE         NUMBER(1),
  NOME_RICHIESTA        VARCHAR2(16 BYTE)       NOT NULL,
  CAS                   VARCHAR2(3 BYTE)        NOT NULL,
  NUM_TEL               VARCHAR2(13 BYTE)       NOT NULL,
  NUM_TEL_SERV_B        VARCHAR2(13 BYTE),
  NUM_ADDIZ1            VARCHAR2(13 BYTE), 
  NUM_ADDIZ2            VARCHAR2(13 BYTE),
  IMSI                  VARCHAR2(15 BYTE),
  ESPORTA_MAIN          VARCHAR2(1 BYTE),
  OLOCODE               VARCHAR2(4 BYTE)        NOT NULL,
  DATA_ESPL             DATE,	   
  TID                   VARCHAR2(23 BYTE)
)
TABLESPACE TAB_HUGE_2
PCTUSED    40
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          512M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
LOGGING 
NOCACHE
NOPARALLEL
NOMONITORING;


CREATE UNIQUE INDEX PK_MNP_GISP_CESS_OUT_DON_ABB ON MNP_GISP_CESS_OUT_DON_ABB
(UNIQUE_ID)
LOGGING
TABLESPACE TAB_IDX_HUGE_2_2
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          512M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE UNIQUE INDEX IDX_GISP_CESS_OUT_DON_ABB1 ON MNP_GISP_CESS_OUT_DON_ABB
(TID)
LOGGING
TABLESPACE TAB_IDX_HUGE_2_2
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          512M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


ALTER TABLE MNP_GISP_CESS_OUT_DON_ABB ADD (
  CONSTRAINT PK_MNP_GISP_CESS_OUT_DON_ABB
 PRIMARY KEY
 (UNIQUE_ID)
    USING INDEX 
    TABLESPACE TAB_IDX_HUGE_2_2
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          512M
                NEXT             10M
                MINEXTENTS       1
                MAXEXTENTS       2147483645
                PCTINCREASE      0
                FREELISTS        1
                FREELIST GROUPS  1
               ));

ALTER TABLE MNP_GISP_CESS_OUT_DON_ABB ADD (
  CONSTRAINT IDX_MNP_GISP_CESS_OUT_DON_ABB
 UNIQUE (TID));

 -- per donor std PP
 CREATE TABLE MNP_GISP_CESS_OUT_DON_PP
(
  UNIQUE_ID             NUMBER                  NOT NULL,
  DATA_CREAZIONE        DATE                    DEFAULT SYSDATE,
  DATA_RICHIESTA_INVIO  DATE,
  DATA_INVIO            DATE,
  DA_SCODARE            NUMBER(1)               NOT NULL,
  DA_PROCESSARE         NUMBER(1),
  NOME_RICHIESTA        VARCHAR2(16 BYTE)       NOT NULL,
  CAS                   VARCHAR2(3 BYTE)        NOT NULL,
  NUM_TEL               VARCHAR2(13 BYTE)       NOT NULL,
  NUM_TEL_SERV_B        VARCHAR2(13 BYTE),
  IMSI                  VARCHAR2(15 BYTE),
  ESPORTA_MAIN          VARCHAR2(1 BYTE),
  OLOCODE               VARCHAR2(4 BYTE)        NOT NULL,
  DATA_ESPL             DATE,	   
  TID                   VARCHAR2(23 BYTE)
)
TABLESPACE TAB_HUGE_2
PCTUSED    40
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          512M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
LOGGING 
NOCACHE
NOPARALLEL
NOMONITORING;


CREATE UNIQUE INDEX PK_MNP_GISP_CESS_OUT_DON_PP ON MNP_GISP_CESS_OUT_DON_PP
(UNIQUE_ID)
LOGGING
TABLESPACE TAB_IDX_HUGE_2_2
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          512M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE UNIQUE INDEX IDX_GISP_CESS_OUT_DON_PP1 ON MNP_GISP_CESS_OUT_DON_PP
(TID)
LOGGING
TABLESPACE TAB_IDX_HUGE_2_2
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          512M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


ALTER TABLE MNP_GISP_CESS_OUT_DON_PP ADD (
  CONSTRAINT PK_MNP_GISP_CESS_OUT_DON_PP
 PRIMARY KEY
 (UNIQUE_ID)
    USING INDEX 
    TABLESPACE TAB_IDX_HUGE_2_2
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          512M
                NEXT             10M
                MINEXTENTS       1
                MAXEXTENTS       2147483645
                PCTINCREASE      0
                FREELISTS        1
                FREELIST GROUPS  1
               ));

ALTER TABLE MNP_GISP_CESS_OUT_DON_PP ADD (
  CONSTRAINT IDX_MNP_GISP_CESS_OUT_DON_PP
 UNIQUE (TID));

 
 
CREATE TABLE MNP_ASP_CRM_CESS_OUT_GISP
(
  MSISDN                     VARCHAR2(15 BYTE)  NOT NULL,
  BUSINESS_CONSUMER          VARCHAR2(2 BYTE)   NOT NULL,
  ID_PROG                    NUMBER(18)         NOT NULL,
  DA_INVIARE                 NUMBER(1)          NOT NULL,
  NOME_FILE                  VARCHAR2(40 BYTE),
  ROUTING_NUMBER             VARCHAR2(12 BYTE)  NOT NULL,
  ID_RICHIESTA               VARCHAR2(23 BYTE)  NOT NULL,
  ABBONATO_PREPAGATO         VARCHAR2(15 BYTE),
  COD_OLO                    VARCHAR2(4 BYTE),
  CODICE_ANALOGICO_DIGITALE  VARCHAR2(1 BYTE),
  SIST_COMP                  VARCHAR2(10 BYTE),
  NOME_OT                    VARCHAR2(5 BYTE),
  MSISDN_ADD1                VARCHAR2(15 BYTE),
  MSISDN_ADD2                VARCHAR2(15 BYTE),
  MSISDN_BASE                VARCHAR2(12 BYTE),
  DATA_INVIO                 DATE,
  DATA_CREAZIONE             DATE               DEFAULT SYSDATE,
  DA_BONIFICARE              NUMBER(1)          DEFAULT 0                     NOT NULL,
  DA_SCODARE                 NUMBER(1)          DEFAULT 0                     NOT NULL,
  UNIQUE_ID                  VARCHAR2(50 BYTE),
  FLAG_TC                    VARCHAR2(1 BYTE),
  SERVIZIO                   VARCHAR2(20 BYTE),
  MSISDN_AGGIUNTIVO          VARCHAR2(12 BYTE),
  MSISDN_DA_PORTARE          VARCHAR2(1 BYTE),
  CESSATO                    VARCHAR2(1 BYTE),
  TAF                        VARCHAR2(1 BYTE),
  PREVALIDAZIONE             VARCHAR2(100 BYTE),
  FLAG_BLOCCANTE             CHAR(1) 
)
TABLESPACE TAB_HUGE
PCTUSED    40
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          256M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
LOGGING 
NOCACHE
NOPARALLEL
NOMONITORING;


CREATE UNIQUE INDEX PK_ASP_CRM_CESS_OUT ON MNP_ASP_CRM_CESS_OUT_GISP
(ID_PROG)
LOGGING
TABLESPACE TAB_IDX_MEDIUM
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          64M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE INDEX IDX_ASP_CRM_CESS_OUT_1 ON MNP_ASP_CRM_CESS_OUT_GISP
(NOME_FILE)
LOGGING
TABLESPACE TAB_IDX_BIG
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE INDEX IDX_ASP_CRM_CESS_OUT_2 ON MNP_ASP_CRM_CESS_OUT_GISP
(ID_RICHIESTA)
LOGGING
TABLESPACE TAB_IDX_MEDIUM
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          64M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


CREATE INDEX IDX_ASP_CRM_CESS_OUT_3 ON MNP_ASP_CRM_CESS_OUT_GISP
(UNIQUE_ID)
LOGGING
TABLESPACE TAB_IDX_BIG
PCTFREE    10
INITRANS   2
MAXTRANS   255
STORAGE    (
            INITIAL          128M
            NEXT             10M
            MINEXTENTS       1
            MAXEXTENTS       2147483645
            PCTINCREASE      0
            FREELISTS        1
            FREELIST GROUPS  1
            BUFFER_POOL      DEFAULT
           )
NOPARALLEL;


ALTER TABLE MNP_ASP_CRM_CESS_OUT_GISP ADD (
  CONSTRAINT PK_ASP_CRM_CESS_OUT
 PRIMARY KEY
 (ID_PROG)
    USING INDEX 
    TABLESPACE TAB_IDX_MEDIUM
    PCTFREE    10
    INITRANS   2
    MAXTRANS   255
    STORAGE    (
                INITIAL          64M
                NEXT             10M
                MINEXTENTS       1
                MAXEXTENTS       2147483645
                PCTINCREASE      0
                FREELISTS        1
                FREELIST GROUPS  1
               ));


CREATE OR REPLACE PROCEDURE MNP.RECALL_INCESS_TO_ACQ (utl_path  IN   VARCHAR2)
/******************************************************************************
   NAME:       MNP.RECALL_INCESS_TO_ACQ
   PURPOSE:    

   REVISIONS:
   Ver        Date        Author           Description
   ---------  ----------  ---------------  ------------------------------------
   1.0        12/10/2010          1. Created this procedure.

   NOTES:

   la procedura recupera i dati "INCESSAZIONE" e li riporta in stato "ACQUISITA"
   per i soli tim_donor=1 e storicizza il flusso di record regrediti 
   
      Object Name:     test
      Sysdate:         12/10/2010
      Date and Time:   12/10/2010, 13.45.18, and 12/10/2010 13.45.18
      Username:         mnp
      Table Name:       (set in the "New PL/SQL Object" dialog)

******************************************************************************/
IS
   
   out_file      UTL_FILE.FILE_TYPE;


   CURSOR cur_valore_incess IS 
   select *
   from mnp_gestione_richiesta_cess
   where stato=2
         and tim_donor=1 ;

   val_incessazione cur_valore_incess%ROWTYPE;
   sql_stmt     VARCHAR2 (2000);
   
   
BEGIN
   
  DBMS_OUTPUT.put_line (   'apro il file '
                             || 'MNP'
                             || '.'
                             || TO_CHAR (SYSDATE, 'yyyyMMdd')
                          );
                          
   out_file := UTL_FILE.FOPEN (utl_path, 'log_recall_incessazione_to_acquisita'|| '.' || TO_CHAR (SYSDATE, 'yyyyMMdd'),
                      'a'
                     );


   OPEN cur_valore_incess;
   
   UTL_FILE.PUT_LINE (out_file,'Nuovo Inserimento dati del: '||SYSDATE);
   
   LOOP
      FETCH cur_valore_incess
       INTO val_incessazione;

      EXIT WHEN cur_valore_incess%NOTFOUND;                        -- process record
      
      UTL_FILE.PUT_LINE (out_file,
                         val_incessazione.ID_RICHIESTA
                         || ';'
                         || val_incessazione.MSISDN
                         || ';'
                         || val_incessazione.STATO
                         || ';'
                         || val_incessazione.DATARICEZIONERICHIESTA
                         || ';'
                         || val_incessazione.TIPO_MESSAGGIO
                         || ';'
                         || val_incessazione.CODICE_OPERATORE_RECIPIENT
                         || ';'
                         || val_incessazione.CODICE_RICHIESTA_RECIPIENT
                         || ';'
                         || val_incessazione.DATA_CUT_OVER
                         || ';'
                         || val_incessazione.DA_PROCESSARE
                         || ';'
                         || val_incessazione.TIM_DONOR
                         || ';'
                         || val_incessazione.CODICE_OPERATORE_REC_EFF
                         || ';'
                         || val_incessazione.ESITO_CESSAZIONE
                         || ';'
                         );
      
   END LOOP;

   CLOSE cur_valore_incess;

   EXECUTE IMMEDIATE    'INSERT INTO mnp_storico_cess
                        (ID_PROG, DATA_I_O, STATO_DA, STATO_A, I_O, DATA_LAVORAZIONE, ID_RICHIESTA)
                        SELECT SEQ_MNP_STORICO_CESS.NEXTVAL, SYSDATE, 2 , 1,-1, SYSDATE, id_richiesta
                        FROM mnp_gestione_richiesta_cess
                        WHERE stato=2 and tim_donor=1';             
   
   
   EXECUTE IMMEDIATE    'UPDATE mnp_gestione_richiesta_cess
                         SET stato=1, da_processare=1
                         WHERE stato=2 and tim_donor=1'; 
   
   COMMIT;
   
   UTL_FILE.fclose (out_file);
   
   DBMS_OUTPUT.put_line (   'chiudo il file '
                         || 'MNP'
                         || '.'
                         || TO_CHAR (SYSDATE, 'yyyyMMdd')
                        );
                       
EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.put_line (SQLERRM);
END;
/