CREATE OR REPLACE PROCEDURE REINF_R2099(
    P_CDEMPRESA      IN EMPRESA.CDEMPRESA%TYPE,
    P_NRENVIO        IN NUMBER,
    P_NREVENTO       IN NUMBER,
    P_DSVERSAO       IN VARCHAR2,
    P_IDAMBIENTE     IN VARCHAR2,
    P_DTMESCOMPETENC IN ENVIOEVENTOREINF.DTMESCOMPETENC%TYPE )
AS
   V_NRSEQREINF      NUMBER;
   V_DTCOMPSEMMOVTO  VARCHAR2(7);
   V_COUNT           NUMBER;
BEGIN
   V_COUNT:= 0;
   BEGIN
     SELECT TO_CHAR(DTCOMPSEMMOVTO,'YYYY-MM')
       INTO V_DTCOMPSEMMOVTO
       FROM ENVIOEVENTOREINF 
      WHERE NRENVIO = (SELECT MAX(NRENVIO)
                         FROM ENVIOEVENTOREINF
                        WHERE CDEMPRESA      = P_CDEMPRESA
                          AND IDSTATUS       <> 'P'-- Nao considera eventos que ainda nao foram enviados
                          AND NREVENTO       = P_NREVENTO
                          AND DTMESCOMPETENC = ADD_MONTHS(P_DTMESCOMPETENC,-1) );
   EXCEPTION
   WHEN NO_DATA_FOUND THEN
      V_DTCOMPSEMMOVTO := NULL;
   END;
   BEGIN
      SELECT COUNT(*)
        INTO V_COUNT
        FROM EVENTOREINF EVT,
             ENVIOEVENTOREINF ENV
       WHERE EVT.DSVERSAO       = P_DSVERSAO
         AND EVT.NMEVENTO       IN ('R-2010','R-2020','R-2040','R-2060', 'R-2070' )
         AND ENV.NREVENTO       = EVT.NREVENTO
         AND ENV.CDEMPRESA      = P_CDEMPRESA
         AND ENV.DTMESCOMPETENC = P_DTMESCOMPETENC
         AND IDSTATUS           = 'C'             --- STATUS CONCLUIDO
         AND ENV.DSVERSAO       = EVT.DSVERSAO;
      EXCEPTION
      WHEN NO_DATA_FOUND THEN
      V_COUNT := NULL;
   END;
   /* BUSCA  AS INFORMACOES PARA O FECHAMENTO DE EVENTOS*/
   FOR C_2099 IN (SELECT TO_CHAR(TO_DATE(P_DTMESCOMPETENC),'YYYY-MM')                               AS PERAPUR,
                         P_IDAMBIENTE                                                               AS TMPAMB,
                         '1'                                                                        AS PROCEMI ,
                         '1.5.1'                                                                    AS VERPROC,
                         DECODE(E.IDTPIJUREMPR,'J','1','F','2',' ')                                 AS TPINSC,
                         DECODE(E.IDTPIJUREMPR,'J',SUBSTR(E.NRINSJUREMPR,1,8), 'F', E.NRINSJUREMPR) AS NRINSC,
                         R.NMREPRES                                                                 AS NMRESP,
                         R.NRCPFREPRES                                                              AS CPFRESP,
                         R.NRDDDTELEREPRES1||R.NRTELEREPRES1                                        AS FONEFIXO,
                         R.DSEMAILREPRES                                                            AS EMAIL,
                         EX.EVTSERVTM                                                               AS EVTSERVTM,     --R-2010
                         EX.EVTSERVPR                                                               AS EVTSERVPR,     --R-2020
                         'N'                                                                        AS EVTASSDESPREC, --R-2030
                         EX.EVTASSDESPREP                                                           AS EVTASSDESPREP, --R-2040
                         'N'                                                                        AS EVTCOMPROD,    --R-2050
                         EX.EVTCPRB                                                                 AS EVTCPRB,       --R-2060
                         EX.EVTPGTOS                                                                AS EVTPGTOS,      --R-2070
                         EX.EVTAQUIS                                                                AS EVTAQUIS,      --R-2055
                         CASE WHEN (V_COUNT > 0) THEN NULL
                              ELSE NVL(V_DTCOMPSEMMOVTO,TO_CHAR(P_DTMESCOMPETENC,'YYYY-MM'))
                         END                                                                        AS COMPSEMMOVTO
                    FROM PARAMEMPREINF P,
                         EMPRESA E,
                         REPRESLEGAL R ,
                        --VERIFICAR SE O EVENTO JA FOI ENVIADO NO PERIODO INFORMADO
                        (SELECT *
                           FROM
                               (SELECT EVT.NMEVENTO,
                                       'N' AS EXISTE
                                  FROM EVENTOREINF EVT
                                 WHERE NOT EXISTS
                                       (SELECT 1
                                          FROM ENVIOEVENTOREINF ENV
                                         WHERE ENV.NREVENTO       = EVT.NREVENTO
                                           AND ENV.CDEMPRESA      = P_CDEMPRESA
                                           AND ENV.DTMESCOMPETENC = P_DTMESCOMPETENC
                                           AND IDSTATUS           = 'C'    --- STATUS CONCLUIDO
                                           AND ENV.DSVERSAO       = EVT.DSVERSAO )
                                   AND EVT.DSVERSAO  = P_DSVERSAO
                                   AND EVT.NMEVENTO IN ('R-2010','R-2020','R-2040','R-2060', 'R-2070', 'R-2055')
                                 UNION
                                SELECT EVT.NMEVENTO,
                                       'S' AS EXISTE
                                  FROM EVENTOREINF EVT,
                                       ENVIOEVENTOREINF ENV
                                 WHERE EVT.DSVERSAO       = P_DSVERSAO
                                   AND EVT.NMEVENTO       IN ('R-2010','R-2020','R-2040','R-2060', 'R-2070', 'R-2055' )
                                   AND ENV.NREVENTO       = EVT.NREVENTO
                                   AND ENV.CDEMPRESA      = P_CDEMPRESA
                                   AND ENV.DTMESCOMPETENC = P_DTMESCOMPETENC
                                   AND IDSTATUS           = 'C'            --- STATUS CONCLUIDO
                                   AND ENV.DSVERSAO       = EVT.DSVERSAO ) E
                          PIVOT (MAX(EXISTE) FOR NMEVENTO IN ('R-2010' AS EVTSERVTM ,'R-2020' AS EVTSERVPR,'R-2040' AS EVTASSDESPREP,'R-2060' AS EVTCPRB, 'R-2070'AS EVTPGTOS, 'R-2055' AS EVTAQUIS) ))EX
                   WHERE E.CDEMPRESA = P.CDEMPRESA
                     AND P.CDEMPRESA = P_CDEMPRESA
                     AND P.CDREPRES  = R.CDREPRES ) LOOP
                  NOVO_CODIGO_NT('NRSEQREINF', V_NRSEQREINF);
                  INSERT INTO ARQEFD_REINF
                         ( CDEMPRESA, NREVENTO, NRENVIO, CDCAMPO1, CDCAMPO2,
                           CDCAMPO3, CDCAMPO4, CDCAMPO5, CDCAMPO6, CDCAMPO7,
                           CDCAMPO8, CDCAMPO9, CDCAMPO10, CDCAMPO11, CDCAMPO12,
                           CDCAMPO13, CDCAMPO14, CDCAMPO15, CDCAMPO16, CDCAMPO17,
                           CDCAMPO18, CDCAMPO19, NRSEQREINF)
                  VALUES ( P_CDEMPRESA, P_NREVENTO, P_NRENVIO, C_2099.PERAPUR, C_2099.TMPAMB,
                           C_2099.PROCEMI , C_2099.VERPROC, C_2099.TPINSC, C_2099.NRINSC, C_2099.NMRESP,
                           C_2099.CPFRESP, C_2099.FONEFIXO, C_2099.EMAIL, C_2099.EVTSERVTM, C_2099.EVTSERVPR,
                           C_2099.EVTASSDESPREC, C_2099.EVTASSDESPREP, C_2099.EVTCOMPROD, C_2099.EVTCPRB, C_2099.EVTPGTOS,
                           C_2099.COMPSEMMOVTO, C_2099.EVTAQUIS, V_NRSEQREINF );
   END LOOP;
END;