CREATE OR REPLACE PROCEDURE APURA_CSLL(
   P_DTINICIAL      IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_DTFINAL        IN MAPAAPURAC.DTINIPERIODO%TYPE,
   P_CDIMPOSTO      IN IMPOSTO.CDIMPOSTONFE%TYPE,
   P_CDEMPRESA      IN EMPRESA.CDEMPRESA%TYPE,
   P_NRSEQUMAPAPU   IN MAPAAPURAC.NRSEQUMAPAPU%TYPE,
   P_GERBLCT        IN VARCHAR2,
   P_NRDRE          IN NUMBER )
AS
   V_VRALIQPRES           NUMBER;
   V_VRLIMITEADIC         NUMBER;
   V_VRALIQADICIR         NUMBER;
   V_VRALIQIR             NUMBER;
   V_VRALIQIMPCSLL        NUMBER;
   V_VRIMPOSACRLP         NUMBER;
   V_VRIMPOSACRLR         NUMBER;
   V_VRDEBITOTPRESU       NUMBER;
   V_VRDEBITOTPRESUPAG    NUMBER;
   V_VRTOTIMPOSACRLR      NUMBER;
   V_VRLUCROOPERAC        NUMBER;
   V_VRLUCROOPERAC_CTB    NUMBER;
   V_VRLUCROOPERAC_TOT    NUMBER;
   V_VRLUCRONAOOPERAC     NUMBER;
   V_VRLUCRONAOOPERAC_CTB NUMBER;
   V_VRLUCRONAOOPERAC_TOT NUMBER;
   V_VRFATNFLP            NUMBER;
   V_VRFATNFLPAG          NUMBER;
   V_VRFATNFLR            NUMBER;
   V_VRBASECALCLR         NUMBER;
   V_VRDEDUNFLP           NUMBER;
   V_VRDEDUNFLR           NUMBER;
   V_VRLUCROLIQLP         NUMBER;
   V_VRIMPOSAPURPRESU     NUMBER;
   V_VRIMPOSAPURPRESUPAG  NUMBER;
   V_VRPARCEXCEDLP        NUMBER;
   V_VRPARCEXCEDLPAG      NUMBER;
   V_VRADICIONALLP        NUMBER;
   V_VRADICIONALLPAG      NUMBER;
   V_VRCOMPENSACOESLP     NUMBER;
   V_VRDEBLUCPRESU        NUMBER;
   V_VRLUCROLIQLR         NUMBER;
   V_VRDEDUCAOREAL        NUMBER;
   V_VRADICIONALLR        NUMBER;
   V_VRPARCEXCEDLR        NUMBER;
   V_VRCOMPENSACOESLR     NUMBER;
   V_VRDEBLUCREAL         NUMBER;
   V_VRLUCROLIQREAL       NUMBER;
   V_IDFORMAPUR           VARCHAR2(3);
   V_CDIMPOSTONFE         VARCHAR2(2);
   V_VRRECEBRUTAJUS       FLOAT;
   V_VRRECEBRUTAJUSPAG    FLOAT;
   V_VRADICAO             FLOAT;
   V_VRADICAOTOT          FLOAT;
   V_VRADICAOTOTPAG       FLOAT;
   V_VRDEDUCAO            FLOAT;
   V_VRDEDUCAOTOT         FLOAT;
   V_VRDEDUCAOTOTPAG      FLOAT;
   V_VREXCLUSAO           FLOAT;
   V_VREXCLUSAOTOT        FLOAT;
   V_VREXCLUSAOTOTPAG     FLOAT;
   V_VRDEBLUCREALTOT      FLOAT;
   V_NRSEQITDETAPU        NUMBER;
   V_NRSEQITDETABLCT      VARCHAR2(20);
   V_IDFORMAPUREMPR       VARCHAR2(1);
BEGIN
   V_VRIMPOSACRLP         := 0;
   V_VRIMPOSACRLR         := 0;
   V_VRDEBITOTPRESU       := 0;
   V_VRDEBITOTPRESUPAG    := 0;
   V_VRTOTIMPOSACRLR      := 0;
   V_VRLUCROOPERAC        := 0;
   V_VRLUCROOPERAC_CTB    := 0;
   V_VRLUCROOPERAC_TOT    := 0;
   V_VRLUCRONAOOPERAC     := 0;
   V_VRLUCRONAOOPERAC_CTB := 0;
   V_VRLUCRONAOOPERAC_TOT := 0;
   V_VRFATNFLP            := 0;
   V_VRFATNFLPAG          := 0;
   V_VRFATNFLR            := 0;
   V_VRBASECALCLR         := 0;
   V_VRDEDUNFLP           := 0;
   V_VRDEDUNFLR           := 0;
   V_VRLUCROLIQLP         := 0;
   V_VRIMPOSAPURPRESU     := 0;
   V_VRIMPOSAPURPRESUPAG  := 0;
   V_VRPARCEXCEDLP        := 0;
   V_VRPARCEXCEDLPAG      := 0;
   V_VRCOMPENSACOESLP     := 0;
   V_VRDEBLUCPRESU        := 0;
   V_VRLUCROLIQLR         := 0;
   V_VRDEDUCAOREAL        := 0;
   V_VRPARCEXCEDLR        := 0;
   V_VRCOMPENSACOESLR     := 0;
   V_VRDEBLUCREAL         := 0;
   V_VRADICIONALLP        := 0;
   V_VRADICIONALLPAG      := 0;
   V_VRADICIONALLR        := 0;

   /*Busca a forma de apuração da Empresa (Mensal/Trimestral)*/
   SELECT IDFORMAPUR, IDFORMAPUREMPR
     INTO V_IDFORMAPUR, V_IDFORMAPUREMPR
     FROM EMPRESA
    WHERE CDEMPRESA = P_CDEMPRESA;

   /*Grava os lançamentos que vão compor o mapa*/
   GRAVA_CONTCTBL_MAPA(P_CDEMPRESA, P_DTINICIAL, P_DTFINAL, P_CDIMPOSTO, P_NRSEQUMAPAPU, NVL(V_IDFORMAPUREMPR,'C'));

   /******** Verifica a parametrização das alíquotas de Presunção do CSLL ********/
   BEGIN
      SELECT MAX(NVL(VRALIQPRESCSLL,0))
        INTO V_VRALIQPRES
        FROM ALIQIRPJCSLL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
          V_VRALIQPRES    :=0;
   END;

   /******** Verifica as alíquotas parametrizadas ********/
   BEGIN
      SELECT MAX(VRALIQCSLL), MAX(VRLIMADICIRPJ), MAX(VRPERCADICIR), MAX(VRALIQIR)
        INTO V_VRALIQIMPCSLL, V_VRLIMITEADIC, V_VRALIQADICIR, V_VRALIQIR
        FROM PARAMIMPOSTO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRALIQIMPCSLL := 0;
         V_VRLIMITEADIC  := 0;
         V_VRALIQADICIR  := 0;
         V_VRALIQIR      := 0;
   END;

   /*INSERE O VALOR DA RECEITA BRUTA AGRUPADO PELAS ALIQUOTAS DE PRESUNÇÃO*/
   FOR C_RECBRUTALIQ IN (SELECT VRALIQPRESCSLL,
                                SUM(VRRECEBRUTALIQ) AS VRRECEBRUTALIQ,
                                SUM(VRRECEBRUTAJUS) AS VRRECEBRUTAJUS,
                                IDFORMAPURITEM
                           FROM (SELECT A.VRALIQPRESCSLL,
                                        SUM(D.VRITEMSTRU) AS VRRECEBRUTALIQ,
                                        SUM(ROUND(D.VRITEMSTRU*A.VRALIQPRESCSLL/100,2)) AS VRRECEBRUTAJUS,
                                        IDFORMAPURITEM
                                   FROM RECDESPIRPJ R, ALIQIRPJCSLL A, INFCTBIRPJ I, DEMOCTBTEMP D
                                  WHERE R.NRSEQALIQIRPJ    = A.NRSEQALIQIRPJ
                                    AND R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
                                    AND I.CDTIPODEMO       = D.CDTIPODEMO
                                    AND I.CDINFCTBIRPJ     = D.CDITEMSTRU
                                    AND R.CDEMPRESA        = P_CDEMPRESA
                                    AND R.IDRECDESPIRPJ    = 'R'
                                    AND P_NRDRE            = D.NRDEMOCTBTEMP
                                  GROUP BY A.VRALIQPRESCSLL, IDFORMAPURITEM
                                  UNION ALL
                                 SELECT A.VRALIQPRESCSLL,
                                        SUM(D.VRITEMSTRU) AS VRRECEBRUTALIQ,
                                        SUM(ROUND(D.VRITEMSTRU*A.VRALIQPRESCSLL/100,2)) AS VRRECEBRUTAJUS ,
                                        IDFORMAPURITEM
                                   FROM RECDESPIRPJ R, ALIQIRPJCSLL A, INFCTBIRPJ I, DEMOCTBTEMP D
                                  WHERE R.NRSEQALIQIRPJ    = A.NRSEQALIQIRPJ
                                    AND R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
                                    AND I.CDTIPODEMO       = D.CDTIPODEMO
                                    AND I.CDINFCTBIRPJ     = D.CDITEMSTRU
                                    AND R.CDEMPRESA        = P_CDEMPRESA
                                    AND R.IDRECDESPIRPJ    = 'D'
                                    AND P_NRDRE            = D.NRDEMOCTBTEMP
                                  GROUP BY A.VRALIQPRESCSLL, IDFORMAPURITEM
                                  UNION ALL
                                 SELECT L.VRALIQPRESCSLL,
                                        SUM(L.VRLANCAMENT+DECODE(V_IDFORMAPUREMPR,'P',NVL(VALOR_IMPOSTO_RETIDO_REGCAIXA(L.CDEMPRESA, L.NRLANCCONT, L.CDCONTCTBL),0),0)) AS VRRECEBRUTALIQ,
                                        SUM(ROUND((L.VRLANCAMENT+DECODE(V_IDFORMAPUREMPR,'P',NVL(VALOR_IMPOSTO_RETIDO_REGCAIXA(L.CDEMPRESA, L.NRLANCCONT, L.CDCONTCTBL),0),0))*L.VRALIQPRESCSLL/100,2)) AS VRRECEBRUTAJUS,
                                            IDFORMAPURITEM
                                   FROM (SELECT A.VRALIQPRESCSLL, LC.CDEMPRESA, LC.NRLANCCONT, LC.CDCONTCTBL, SUM(LC.VRLANCAMENT) AS VRLANCAMENT, I.IDFORMAPURITEM
                                           FROM RECDESPIRPJ R, ALIQIRPJCSLL A, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
                                          WHERE R.NRSEQALIQIRPJ    = A.NRSEQALIQIRPJ
                                            AND R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
                                            AND I.IDINFCTBIRPJ     = 'C'
                                            AND LC.DTLANCCONT      BETWEEN P_DTINICIAL AND P_DTFINAL
                                            AND LC.CDCONTCTBL     LIKE I.CDINFCTBIRPJ||'%'
                                            AND LC.CDEMPRESA       = P_CDEMPRESA
                                            AND LC.VRLANCAMENT     > 0
                                            AND R.CDEMPRESA        = P_CDEMPRESA
                                            AND R.IDRECDESPIRPJ    = 'R'
                                            AND R.IDCONSVRDEB      = 'N'
                                            AND LC.IDLANCINTE      <> 'F'
                                            AND LC.CDEMPRESA       = CM.CDEMPRESA
                                            AND LC.NRLANCCONT      = CM.NRLANCCONT
                                            AND LC.NRSEQLANC       = CM.NRSEQLANC
                                            AND I.IDFORMAPURITEM   = CM.IDFORMAPURITEM
                                            AND CM.NRSEQUMAPAPU    = P_NRSEQUMAPAPU
                                          GROUP BY A.VRALIQPRESCSLL, LC.CDEMPRESA, LC.NRLANCCONT, LC.CDCONTCTBL, I.IDFORMAPURITEM
                                          UNION
                                         SELECT A.VRALIQPRESCSLL, LC.CDEMPRESA, LC.NRLANCCONT, LC.CDCONTCTBL, SUM(LC.VRLANCAMENT) AS VRLANCAMENT, I.IDFORMAPURITEM
                                           FROM RECDESPIRPJ R, ALIQIRPJCSLL A, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
                                          WHERE R.NRSEQALIQIRPJ    = A.NRSEQALIQIRPJ
                                            AND R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
                                            AND I.IDINFCTBIRPJ     = 'C'
                                            AND LC.DTLANCCONT      BETWEEN P_DTINICIAL AND P_DTFINAL
                                            AND LC.CDCONTCTBL     LIKE I.CDINFCTBIRPJ||'%'
                                            AND LC.CDEMPRESA       = P_CDEMPRESA
                                            AND R.CDEMPRESA        = P_CDEMPRESA
                                            AND R.IDRECDESPIRPJ    = 'R'
                                            AND R.IDCONSVRDEB      = 'S'
											           AND LC.IDLANCINTE      <> 'F'
                                            AND LC.CDEMPRESA       = CM.CDEMPRESA
                                            AND LC.NRLANCCONT      = CM.NRLANCCONT
                                            AND LC.NRSEQLANC       = CM.NRSEQLANC
                                            AND I.IDFORMAPURITEM   = CM.IDFORMAPURITEM
                                            AND CM.NRSEQUMAPAPU    = P_NRSEQUMAPAPU
                                          GROUP BY A.VRALIQPRESCSLL, LC.CDEMPRESA, LC.NRLANCCONT, LC.CDCONTCTBL, I.IDFORMAPURITEM) L
                                  GROUP BY L.VRALIQPRESCSLL, IDFORMAPURITEM
                                  UNION ALL
                                 SELECT A.VRALIQPRESCSLL,
                                        SUM(LC.VRLANCAMENT) AS VRRECEBRUTALIQ,
                                        SUM(ROUND(LC.VRLANCAMENT*A.VRALIQPRESCSLL/100,2)) AS VRRECEBRUTAJUS ,
                                        I.IDFORMAPURITEM
                                   FROM RECDESPIRPJ R, ALIQIRPJCSLL A, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
                                  WHERE R.NRSEQALIQIRPJ    = A.NRSEQALIQIRPJ
                                    AND R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
                                    AND I.IDINFCTBIRPJ     = 'C'
                                    AND LC.DTLANCCONT      BETWEEN P_DTINICIAL AND P_DTFINAL
                                    AND LC.CDCONTCTBL     LIKE I.CDINFCTBIRPJ||'%'
                                    AND LC.CDEMPRESA       = P_CDEMPRESA
                                    AND R.CDEMPRESA        = P_CDEMPRESA
                                    AND R.IDRECDESPIRPJ    = 'D'
                                    AND LC.IDLANCINTE      <> 'F'
                                    AND LC.CDEMPRESA       = CM.CDEMPRESA
                                    AND LC.NRLANCCONT      = CM.NRLANCCONT
                                    AND LC.NRSEQLANC       = CM.NRSEQLANC
                                    AND I.IDFORMAPURITEM   = CM.IDFORMAPURITEM
                                    AND CM.NRSEQUMAPAPU    = P_NRSEQUMAPAPU
                                  GROUP BY A.VRALIQPRESCSLL, I.IDFORMAPURITEM)
                          GROUP BY VRALIQPRESCSLL, IDFORMAPURITEM)LOOP
      IF P_GERBLCT = 'N' THEN
         IF C_RECBRUTALIQ.VRRECEBRUTALIQ > 0 THEN
            INSERT INTO RECEBRUTALIQMAPA
               (NRSEQUMAPAPU, VRRECEBRUTALIQ, VRALIQPRES, VRRECEBRUTAJUS, IDFORMAPURITEM)
            VALUES
               (P_NRSEQUMAPAPU, C_RECBRUTALIQ.VRRECEBRUTALIQ, C_RECBRUTALIQ.VRALIQPRESCSLL, C_RECBRUTALIQ.VRRECEBRUTAJUS, C_RECBRUTALIQ.IDFORMAPURITEM);
            COMMIT;
         END IF;
      END IF;
   END LOOP;

   /*SOMA O TOTAL DA RECEITA BRUTA COMPETENCIA*/
   BEGIN
      SELECT SUM(VRRECEBRUTAJUS)
        INTO V_VRRECEBRUTAJUS
        FROM RECEBRUTALIQMAPA
       WHERE NRSEQUMAPAPU   = P_NRSEQUMAPAPU
         AND IDFORMAPURITEM = 'C';
   EXCEPTION
        WHEN NO_DATA_FOUND THEN
          V_VRRECEBRUTAJUS :=0;
   END;

   /*SOMA O TOTAL DA RECEITA BRUTA PAGAMENTO*/
   BEGIN
      SELECT SUM(VRRECEBRUTAJUS)
        INTO V_VRRECEBRUTAJUSPAG
        FROM RECEBRUTALIQMAPA
       WHERE NRSEQUMAPAPU   = P_NRSEQUMAPAPU
         AND IDFORMAPURITEM = 'P';
   EXCEPTION
        WHEN NO_DATA_FOUND THEN
          V_VRRECEBRUTAJUSPAG :=0;
   END;

   /*ADIÇÕES*/
   BEGIN
      SELECT MAX(NRSEQITDETAPU)
        INTO V_NRSEQITDETAPU
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU  = P_NRSEQUMAPAPU
         AND IDTIPODETAPU  = 'AD';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQITDETAPU := 0;
   END;
   V_NRSEQITDETAPU := NVL(V_NRSEQITDETAPU,0) + 1;
   FOR C_ADIC IN (SELECT ABS(SUM(D.VRITEMSTRU)) AS VRADICAO, P.VRPORCENTAGEM, P.NMPARAMAPU
                    FROM PARAMAPUIRPJ P, INFCTBIRPJ I, DEMOCTBTEMP D
                   WHERE P.NRSEQPARAMAPU = I.NRSEQPARAMAPU
                     AND P.CDEMPRESA     = P_CDEMPRESA
                     AND P.IDIMPOSTO     = 'C'
                     AND P.IDPARAMAPU    = 'A'
                     AND I.CDTIPODEMO    = D.CDTIPODEMO
                     AND D.CDITEMSTRU    = I.CDINFCTBIRPJ
                     AND P_NRDRE         = D.NRDEMOCTBTEMP
                   GROUP BY P.VRPORCENTAGEM, P.NMPARAMAPU)LOOP

      /*Calcula o valor da Adição com base no percentual informado*/
      V_VRADICAO := C_ADIC.VRADICAO * C_ADIC.VRPORCENTAGEM/100;
      IF P_GERBLCT = 'N' THEN
         IF V_VRADICAO > 0 THEN
            INSERT INTO ITDETAPUCONTR
               (NRSEQUMAPAPU, IDTIPODETAPU, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
            VALUES
               (P_NRSEQUMAPAPU, 'AD', LPAD(V_NRSEQITDETAPU,4,0), C_ADIC.NMPARAMAPU, V_VRADICAO);
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;

            IF V_IDFORMAPUREMPR = 'P' THEN
               INSERT INTO ITDETAPUCONTR
                  (NRSEQUMAPAPU, IDTIPODETAPU, IDFORMAPUR, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
               VALUES
                  (P_NRSEQUMAPAPU, 'AD', 'P', LPAD(V_NRSEQITDETAPU,4,0), C_ADIC.NMPARAMAPU, V_VRADICAO);
               V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
            END IF;
         END IF;
      ELSE
         IF V_VRADICAO > 0 THEN
            NOVO_CODIGO('ITDETAIRPJ', V_NRSEQITDETABLCT );
            INSERT INTO ITDETAPUBALCTIRPJ
               (NRSEQUMAPAPU, IDTIPODETAPUIRPJ, NRSEQITDETAPUIRPJ, DSITEMDETAPUIRPJ, VRITEMDETAPUIRPJ, NRSEQITDETAPUBALCT)
            VALUES
               (P_NRSEQUMAPAPU, 'AD', LPAD(V_NRSEQITDETAPU,4,0), C_ADIC.NMPARAMAPU, V_VRADICAO, TO_NUMBER(V_NRSEQITDETABLCT));
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      END IF;
   END LOOP;

   /*ADIÇÕES  COM AS INFORMAÇÕES REFERENTES A CONTA CONTABIL*/
   FOR C_ADIC IN (SELECT ABS(SUM(LC.VRLANCAMENT)) AS VRADICAO, P.VRPORCENTAGEM, P.NMPARAMAPU
                    FROM PARAMAPUIRPJ P, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
                   WHERE P.NRSEQPARAMAPU = I.NRSEQPARAMAPU
                     AND P.CDEMPRESA     = P_CDEMPRESA
                     AND P.IDIMPOSTO     = 'C'
                     AND P.IDPARAMAPU    = 'A'
                     AND I.IDINFCTBIRPJ  = 'C'
                     AND LC.DTLANCCONT   BETWEEN P_DTINICIAL AND P_DTFINAL
                     AND LC.CDCONTCTBL   LIKE I.CDINFCTBIRPJ||'%'
                     AND LC.CDEMPRESA    = P_CDEMPRESA
					      AND LC.IDLANCINTE   <> 'F'
                     AND LC.CDEMPRESA    = CM.CDEMPRESA
                     AND LC.NRLANCCONT   = CM.NRLANCCONT
                     AND LC.NRSEQLANC    = CM.NRSEQLANC
                     AND CM.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                   GROUP BY P.VRPORCENTAGEM, P.NMPARAMAPU)LOOP

      /*Calcula o valor da Adição com base no percentual informado*/
      V_VRADICAO := C_ADIC.VRADICAO * C_ADIC.VRPORCENTAGEM/100;
      IF P_GERBLCT = 'N' THEN
         IF V_VRADICAO > 0 THEN
            INSERT INTO ITDETAPUCONTR
               (NRSEQUMAPAPU, IDTIPODETAPU, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
            VALUES
               (P_NRSEQUMAPAPU, 'AD', LPAD(V_NRSEQITDETAPU,4,0), C_ADIC.NMPARAMAPU, V_VRADICAO);
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;

            IF V_IDFORMAPUREMPR = 'P' THEN
               INSERT INTO ITDETAPUCONTR
                  (NRSEQUMAPAPU, IDTIPODETAPU, IDFORMAPUR, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
               VALUES
                  (P_NRSEQUMAPAPU, 'AD', 'P', LPAD(V_NRSEQITDETAPU,4,0), C_ADIC.NMPARAMAPU, V_VRADICAO);
               V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
            END IF;
         END IF;
      ELSE
         IF V_VRADICAO > 0 THEN
            NOVO_CODIGO('ITDETAIRPJ', V_NRSEQITDETABLCT );
            INSERT INTO ITDETAPUBALCTIRPJ
               (NRSEQUMAPAPU, IDTIPODETAPUIRPJ, NRSEQITDETAPUIRPJ, DSITEMDETAPUIRPJ, VRITEMDETAPUIRPJ, NRSEQITDETAPUBALCT)
            VALUES
               (P_NRSEQUMAPAPU, 'AD', LPAD(V_NRSEQITDETAPU,4,0), C_ADIC.NMPARAMAPU, V_VRADICAO, TO_NUMBER(V_NRSEQITDETABLCT));
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      END IF;
   END LOOP;

   /*CALCULA VALOR TOTAL DA ADIÇÃO POR COMPETÊNCIA*/
   BEGIN
      SELECT SUM(VRITEMDETAPU)
        INTO V_VRADICAOTOT
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
         AND IDTIPODETAPU = 'AD'
         AND IDFORMAPUR   = 'C';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRADICAOTOT :=0;
   END;

   IF V_VRADICAOTOT < 0 THEN
      V_VRADICAOTOT := 0;
   END IF;

   /*CALCULA VALOR TOTAL DA ADIÇÃO POR PAGAMENTO*/
   BEGIN
      SELECT SUM(VRITEMDETAPU)
        INTO V_VRADICAOTOTPAG
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
         AND IDTIPODETAPU = 'AD'
         AND IDFORMAPUR   = 'P';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRADICAOTOTPAG :=0;
   END;

   IF V_VRADICAOTOTPAG < 0 THEN
      V_VRADICAOTOTPAG := 0;
   END IF;

   /*DEDUÇÕES*/
   BEGIN
      SELECT MAX(NRSEQITDETAPU)
        INTO V_NRSEQITDETAPU
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU  = P_NRSEQUMAPAPU
         AND IDTIPODETAPU  = 'DE';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQITDETAPU := 0;
   END;
   V_NRSEQITDETAPU := NVL(V_NRSEQITDETAPU,0)+1;

   FOR C_DEDU IN (SELECT ABS(SUM(D.VRITEMSTRU)) AS VRDEDUCAO, P.VRPORCENTAGEM, P.NMPARAMAPU
                    FROM PARAMAPUIRPJ P, INFCTBIRPJ I, DEMOCTBTEMP D
                   WHERE P.NRSEQPARAMAPU = I.NRSEQPARAMAPU
                     AND P.CDEMPRESA     = P_CDEMPRESA
                     AND P.IDIMPOSTO     = 'C'
                     AND P.IDPARAMAPU    = 'C'
                     AND I.CDTIPODEMO    = D.CDTIPODEMO
                     AND D.CDITEMSTRU    = I.CDINFCTBIRPJ
                     AND P_NRDRE         = D.NRDEMOCTBTEMP
                   GROUP BY P.VRPORCENTAGEM, P.NMPARAMAPU)LOOP

      /*Calcula o valor da Dedução com base no percentual informado*/
      V_VRDEDUCAO := C_DEDU.VRDEDUCAO * C_DEDU.VRPORCENTAGEM/100;
      IF P_GERBLCT = 'N' THEN
         IF V_VRDEDUCAO > 0 THEN
            INSERT INTO ITDETAPUCONTR
               (NRSEQUMAPAPU, IDTIPODETAPU, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
            VALUES
               (P_NRSEQUMAPAPU, 'DE', LPAD(V_NRSEQITDETAPU,4,0), C_DEDU.NMPARAMAPU, V_VRDEDUCAO);
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;

            IF V_IDFORMAPUREMPR = 'P' THEN
               INSERT INTO ITDETAPUCONTR
                  (NRSEQUMAPAPU, IDTIPODETAPU, IDFORMAPUR, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
               VALUES
                  (P_NRSEQUMAPAPU, 'DE', 'P', LPAD(V_NRSEQITDETAPU,4,0), C_DEDU.NMPARAMAPU, V_VRDEDUCAO);
               V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
            END IF;
         END IF;
      ELSE
         IF V_VRDEDUCAO > 0 THEN
            NOVO_CODIGO('ITDETAIRPJ', V_NRSEQITDETABLCT );
            INSERT INTO ITDETAPUBALCTIRPJ
               (NRSEQUMAPAPU, IDTIPODETAPUIRPJ, NRSEQITDETAPUIRPJ, DSITEMDETAPUIRPJ, VRITEMDETAPUIRPJ, NRSEQITDETAPUBALCT )
            VALUES
               (P_NRSEQUMAPAPU, 'DE', LPAD(V_NRSEQITDETAPU,4,0), C_DEDU.NMPARAMAPU, V_VRDEDUCAO, TO_NUMBER(V_NRSEQITDETABLCT));
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      END IF;
   END LOOP;

   /*DEDUÇÕES CONTA CONTABIL*/
   FOR C_DEDU IN (SELECT ABS(SUM(LC.VRLANCAMENT)) AS VRDEDUCAO, P.VRPORCENTAGEM, P.NMPARAMAPU
                    FROM PARAMAPUIRPJ P, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
                   WHERE P.NRSEQPARAMAPU = I.NRSEQPARAMAPU
                     AND P.CDEMPRESA     = P_CDEMPRESA
                     AND P.IDIMPOSTO     = 'C'
                     AND P.IDPARAMAPU    = 'C'
                     AND I.IDINFCTBIRPJ  = 'C'
                     AND LC.DTLANCCONT   BETWEEN P_DTINICIAL AND P_DTFINAL
                     AND LC.CDCONTCTBL   LIKE I.CDINFCTBIRPJ||'%'
                     AND LC.CDEMPRESA    = P_CDEMPRESA
					      AND LC.IDLANCINTE   <> 'F'
                     AND LC.CDEMPRESA    = CM.CDEMPRESA
                     AND LC.NRLANCCONT   = CM.NRLANCCONT
                     AND LC.NRSEQLANC    = CM.NRSEQLANC
                     AND CM.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                   GROUP BY P.VRPORCENTAGEM, P.NMPARAMAPU)LOOP

      /*Calcula o valor da Dedução com base no percentual informado*/
      V_VRDEDUCAO := C_DEDU.VRDEDUCAO * C_DEDU.VRPORCENTAGEM/100;
      IF P_GERBLCT = 'N' THEN
         IF V_VRDEDUCAO > 0 THEN
            INSERT INTO ITDETAPUCONTR
               (NRSEQUMAPAPU, IDTIPODETAPU, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
            VALUES
               (P_NRSEQUMAPAPU, 'DE', LPAD(V_NRSEQITDETAPU,4,0), C_DEDU.NMPARAMAPU, V_VRDEDUCAO);
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
            
            IF V_IDFORMAPUREMPR = 'P' THEN
               INSERT INTO ITDETAPUCONTR
                  (NRSEQUMAPAPU, IDTIPODETAPU, IDFORMAPUR, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
               VALUES
                  (P_NRSEQUMAPAPU, 'DE', 'P', LPAD(V_NRSEQITDETAPU,4,0), C_DEDU.NMPARAMAPU, V_VRDEDUCAO);
               V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
            END IF;
         END IF;
      ELSE
         IF V_VRDEDUCAO > 0 THEN
            NOVO_CODIGO('ITDETAIRPJ', V_NRSEQITDETABLCT );
            INSERT INTO ITDETAPUBALCTIRPJ
               (NRSEQUMAPAPU, IDTIPODETAPUIRPJ, NRSEQITDETAPUIRPJ, DSITEMDETAPUIRPJ, VRITEMDETAPUIRPJ, NRSEQITDETAPUBALCT)
            VALUES
               (P_NRSEQUMAPAPU, 'DE', LPAD(V_NRSEQITDETAPU,4,0), C_DEDU.NMPARAMAPU, V_VRDEDUCAO, TO_NUMBER(V_NRSEQITDETABLCT));
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      END IF;
   END LOOP;

   /*DEDUÇÕES IMPOSTO RETIDO - APURAÇÃO POR REGIME DE CAIXA*/
   FOR C_DEDU IN (SELECT ROUND(SUM((VRPROPRECBC*VRALIQNF/100)),2) AS VRDEDUCAO
                    FROM EMPRESA E, IMPBAIXAREC IM, NOTAFISCAL N, IMPOSTO I
                   WHERE E.CDEMPRESA      = P_CDEMPRESA
                     AND E.IDFORMAPUREMPR = 'P'
                     AND IM.DTBAIXAREC    BETWEEN P_DTINICIAL AND P_DTFINAL
                     AND IM.CDIMPOSTO     = I.CDIMPOSTO
                     AND I.CDIMPOSTONFE   = '76'
                     AND IM.CDFILIAL      = N.CDFILIAL
                     AND IM.NRLANCTONF    = N.NRLANCTONF
                     AND N.CDEMPRESA      = E.CDEMPRESA) LOOP

      IF P_GERBLCT = 'N' THEN
         IF C_DEDU.VRDEDUCAO > 0 THEN
            INSERT INTO ITDETAPUCONTR
               (NRSEQUMAPAPU, IDTIPODETAPU, IDFORMAPUR, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
            VALUES
               (P_NRSEQUMAPAPU, 'DE', 'P', LPAD(V_NRSEQITDETAPU,4,0), 'CSLL RETIDO', C_DEDU.VRDEDUCAO);
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      ELSE
         IF C_DEDU.VRDEDUCAO > 0 THEN
            NOVO_CODIGO('ITDETAIRPJ', V_NRSEQITDETABLCT );
            INSERT INTO ITDETAPUBALCTIRPJ
               (NRSEQUMAPAPU, IDTIPODETAPUIRPJ, NRSEQITDETAPUIRPJ, DSITEMDETAPUIRPJ, VRITEMDETAPUIRPJ, NRSEQITDETAPUBALCT)
            VALUES
               (P_NRSEQUMAPAPU, 'DE', LPAD(V_NRSEQITDETAPU,4,0), 'CSLL RETIDO', C_DEDU.VRDEDUCAO, TO_NUMBER(V_NRSEQITDETABLCT));
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      END IF;
   END LOOP;

   /*CALCULA VALOR TOTAL DA DEDUÇÃO POR COMPETÊNCIA*/
   BEGIN
      SELECT SUM(VRITEMDETAPU)
        INTO V_VRDEDUCAOTOT
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
         AND IDTIPODETAPU = 'DE'
         AND IDFORMAPUR   = 'C';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRDEDUCAOTOT :=0;
   END;

   IF V_VRDEDUCAOTOT < 0 THEN
      V_VRDEDUCAOTOT := 0;
   END IF;

   /*CALCULA VALOR TOTAL DA DEDUÇÃO POR PAGAMENTO*/
   BEGIN
      SELECT SUM(VRITEMDETAPU)
        INTO V_VRDEDUCAOTOTPAG
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
         AND IDTIPODETAPU = 'DE'
         AND IDFORMAPUR   = 'P';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRDEDUCAOTOTPAG :=0;
   END;

   IF V_VRDEDUCAOTOTPAG < 0 THEN
      V_VRDEDUCAOTOTPAG := 0;
   END IF;

   /*EXCLUSÕES*/
   BEGIN
      SELECT MAX(NRSEQITDETAPU)
        INTO V_NRSEQITDETAPU
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU  = P_NRSEQUMAPAPU
         AND IDTIPODETAPU  = 'EX';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_NRSEQITDETAPU := 0;
   END;
   V_NRSEQITDETAPU := NVL(V_NRSEQITDETAPU,0)+1;
   FOR C_EXCL IN (SELECT ABS(SUM(D.VRITEMSTRU)) AS VREXCLUSAO, P.VRPORCENTAGEM, P.NMPARAMAPU
                    FROM PARAMAPUIRPJ P, INFCTBIRPJ I, DEMOCTBTEMP D
                   WHERE P.NRSEQPARAMAPU = I.NRSEQPARAMAPU
                     AND P.CDEMPRESA     = P_CDEMPRESA
                     AND P.IDIMPOSTO     = 'C'
                     AND P.IDPARAMAPU    = 'E'
                     AND I.CDTIPODEMO    = D.CDTIPODEMO
                     AND D.CDITEMSTRU    = I.CDINFCTBIRPJ
                     AND P_NRDRE         = D.NRDEMOCTBTEMP
                   GROUP BY P.VRPORCENTAGEM, P.NMPARAMAPU)LOOP

      /*Calcula o valor da Exclusão com base no percentual informado*/
      V_VREXCLUSAO := C_EXCL.VREXCLUSAO * C_EXCL.VRPORCENTAGEM/100;
      IF P_GERBLCT = 'N' THEN
         IF V_VREXCLUSAO > 0 THEN
            INSERT INTO ITDETAPUCONTR
               (NRSEQUMAPAPU, IDTIPODETAPU, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
            VALUES
               (P_NRSEQUMAPAPU, 'EX', LPAD(V_NRSEQITDETAPU,4,0), C_EXCL.NMPARAMAPU, V_VREXCLUSAO);
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;

            IF V_IDFORMAPUREMPR = 'P' THEN
               INSERT INTO ITDETAPUCONTR
                  (NRSEQUMAPAPU, IDTIPODETAPU, IDFORMAPUR, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
               VALUES
                  (P_NRSEQUMAPAPU, 'EX', 'P', LPAD(V_NRSEQITDETAPU,4,0), C_EXCL.NMPARAMAPU, V_VREXCLUSAO);
               V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
            END IF;
         END IF;
      ELSE
         IF V_VREXCLUSAO > 0 THEN
            NOVO_CODIGO('ITDETAIRPJ', V_NRSEQITDETABLCT );
            INSERT INTO ITDETAPUBALCTIRPJ
               (NRSEQUMAPAPU, IDTIPODETAPUIRPJ, NRSEQITDETAPUIRPJ, DSITEMDETAPUIRPJ, VRITEMDETAPUIRPJ, NRSEQITDETAPUBALCT)
            VALUES
               (P_NRSEQUMAPAPU, 'EX', LPAD(V_NRSEQITDETAPU,4,0), C_EXCL.NMPARAMAPU, V_VREXCLUSAO, TO_NUMBER(V_NRSEQITDETABLCT));
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      END IF;
   END LOOP;

   /*EXCLUSÕES INFORMAÇOES DE CONTA CONTABIL*/
   FOR C_EXCL IN (SELECT ABS(SUM(LC.VRLANCAMENT)) AS VREXCLUSAO, P.VRPORCENTAGEM, P.NMPARAMAPU
                    FROM PARAMAPUIRPJ P, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
                   WHERE P.NRSEQPARAMAPU = I.NRSEQPARAMAPU
                     AND P.CDEMPRESA     = P_CDEMPRESA
                     AND P.IDIMPOSTO     = 'C'
                     AND P.IDPARAMAPU    = 'E'
                     AND I.IDINFCTBIRPJ  = 'C'
                     AND LC.DTLANCCONT   BETWEEN P_DTINICIAL AND P_DTFINAL
                     AND LC.CDCONTCTBL   LIKE I.CDINFCTBIRPJ||'%'
                     AND LC.CDEMPRESA    = P_CDEMPRESA
					      AND LC.IDLANCINTE   <> 'F'
                     AND LC.CDEMPRESA    = CM.CDEMPRESA
                     AND LC.NRLANCCONT   = CM.NRLANCCONT
                     AND LC.NRSEQLANC    = CM.NRSEQLANC
                     AND CM.NRSEQUMAPAPU = P_NRSEQUMAPAPU
                   GROUP BY P.VRPORCENTAGEM, P.NMPARAMAPU)LOOP

      /*Calcula o valor da Exclusão com base no percentual informado*/
      V_VREXCLUSAO := C_EXCL.VREXCLUSAO * C_EXCL.VRPORCENTAGEM/100;
      IF P_GERBLCT = 'N' THEN
         IF V_VREXCLUSAO > 0 THEN
            INSERT INTO ITDETAPUCONTR
               (NRSEQUMAPAPU, IDTIPODETAPU, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
            VALUES
               (P_NRSEQUMAPAPU, 'EX', LPAD(V_NRSEQITDETAPU,4,0), C_EXCL.NMPARAMAPU, V_VREXCLUSAO);
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;

            IF V_IDFORMAPUREMPR = 'P' THEN
               INSERT INTO ITDETAPUCONTR
                  (NRSEQUMAPAPU, IDTIPODETAPU, IDFORMAPUR, NRSEQITDETAPU, DSITEMDETAPU, VRITEMDETAPU)
               VALUES
                  (P_NRSEQUMAPAPU, 'EX', 'P', LPAD(V_NRSEQITDETAPU,4,0), C_EXCL.NMPARAMAPU, V_VREXCLUSAO);
               V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
            END IF;
         END IF;
      ELSE
         IF V_VREXCLUSAO > 0 THEN
            NOVO_CODIGO('ITDETAIRPJ', V_NRSEQITDETABLCT );
            INSERT INTO ITDETAPUBALCTIRPJ
               (NRSEQUMAPAPU, IDTIPODETAPUIRPJ, NRSEQITDETAPUIRPJ, DSITEMDETAPUIRPJ, VRITEMDETAPUIRPJ, NRSEQITDETAPUBALCT)
            VALUES
               (P_NRSEQUMAPAPU, 'EX', LPAD(V_NRSEQITDETAPU,4,0), C_EXCL.NMPARAMAPU, V_VREXCLUSAO, TO_NUMBER(V_NRSEQITDETABLCT));
            V_NRSEQITDETAPU := V_NRSEQITDETAPU + 1;
         END IF;
      END IF;
   END LOOP;

   /*CALCULA VALOR TOTAL DA EXCLUSÃO POR COMPETÊNCIA*/
   BEGIN
      SELECT SUM(VRITEMDETAPU)
        INTO V_VREXCLUSAOTOT
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
         AND IDTIPODETAPU = 'EX'
         AND IDFORMAPUR   = 'C';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VREXCLUSAOTOT :=0;
   END;

   IF V_VREXCLUSAOTOT < 0 THEN
      V_VREXCLUSAOTOT := 0;
   END IF;

   /*CALCULA VALOR TOTAL DA EXCLUSÃO POR PAGAMENTO*/
   BEGIN
      SELECT SUM(VRITEMDETAPU)
        INTO V_VREXCLUSAOTOTPAG
        FROM ITDETAPUCONTR
       WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU
         AND IDTIPODETAPU = 'EX'
         AND IDFORMAPUR   = 'P';
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VREXCLUSAOTOTPAG :=0;
   END;

   IF V_VREXCLUSAOTOTPAG < 0 THEN
      V_VREXCLUSAOTOTPAG := 0;
   END IF;

   /*LUCRO OPERACIONAL*/
   BEGIN
      SELECT SUM(D.VRITEMSTRU) AS VRLUCROOPERAC
        INTO V_VRLUCROOPERAC
        FROM RECDESPIRPJ R, INFCTBIRPJ I, DEMOCTBTEMP D
       WHERE R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
         AND I.CDTIPODEMO       = D.CDTIPODEMO
         AND I.CDINFCTBIRPJ     = D.CDITEMSTRU
         AND R.CDEMPRESA        = P_CDEMPRESA
         AND R.IDRECDESPIRPJ    = 'R'
         AND R.IDRECDESPIRPJOP  = 'S'
         AND P_NRDRE            = D.NRDEMOCTBTEMP;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRLUCROOPERAC :=0;
   END;

   /*LUCRO OPERACIONAL CONTA CONTABIL*/
   BEGIN
      SELECT SUM(LC.VRLANCAMENT) AS VRLUCROOPERAC
        INTO V_VRLUCROOPERAC_CTB
        FROM RECDESPIRPJ R, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
       WHERE R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
         AND R.CDEMPRESA        = P_CDEMPRESA
         AND R.IDRECDESPIRPJ    = 'R'
         AND I.IDINFCTBIRPJ     = 'C'
         AND LC.DTLANCCONT      BETWEEN P_DTINICIAL AND P_DTFINAL
         AND LC.CDCONTCTBL      LIKE I.CDINFCTBIRPJ||'%'
         AND LC.CDEMPRESA       = P_CDEMPRESA
         AND LC.VRLANCAMENT     > 0
         AND R.IDRECDESPIRPJOP  = 'S'
         AND LC.IDLANCINTE      <> 'F'
         AND LC.CDEMPRESA       = CM.CDEMPRESA
         AND LC.NRLANCCONT      = CM.NRLANCCONT
         AND LC.NRSEQLANC       = CM.NRSEQLANC
         AND CM.NRSEQUMAPAPU    = P_NRSEQUMAPAPU;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRLUCROOPERAC_CTB :=0;
   END;

   /*LUCRO OPERACIONAL TOTAL*/
   V_VRLUCROOPERAC_TOT := NVL(V_VRLUCROOPERAC_CTB,0) + NVL(V_VRLUCROOPERAC,0);
   IF V_VRLUCROOPERAC_TOT < 0 THEN
      V_VRLUCROOPERAC_TOT := 0;
   END IF;

   /*LUCRO NÃO-OPERACIONAL*/
   BEGIN
      SELECT SUM(D.VRITEMSTRU) AS VRLUCRONAOOPERAC
        INTO V_VRLUCRONAOOPERAC
        FROM RECDESPIRPJ R, INFCTBIRPJ I, DEMOCTBTEMP D
       WHERE R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
         AND I.CDTIPODEMO       = D.CDTIPODEMO
         AND I.CDINFCTBIRPJ     = D.CDITEMSTRU
         AND R.CDEMPRESA        = P_CDEMPRESA
         AND R.IDRECDESPIRPJ    = 'R'
         AND R.IDRECDESPIRPJOP  = 'N'
         AND P_NRDRE            = D.NRDEMOCTBTEMP;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_VRLUCRONAOOPERAC :=0;
   END;

   /*LUCRO NÃO-OPERACIONAL CONTA CONTABIL*/
   BEGIN
      SELECT SUM(LC.VRLANCAMENT) AS VRLUCRONAOOPERAC
        INTO V_VRLUCRONAOOPERAC_CTB
        FROM RECDESPIRPJ R, INFCTBIRPJ I, LANCCONT LC, CONTCTBLMAPA CM
       WHERE R.NRSEQRECDESPIRPJ = I.NRSEQPARAMAPU
         AND I.IDINFCTBIRPJ     = 'C'
         AND LC.DTLANCCONT      BETWEEN P_DTINICIAL AND P_DTFINAL
         AND LC.CDCONTCTBL      LIKE I.CDINFCTBIRPJ||'%'
         AND LC.CDEMPRESA       = P_CDEMPRESA
         AND LC.VRLANCAMENT     > 0
         AND R.CDEMPRESA        = P_CDEMPRESA
         AND R.IDRECDESPIRPJ    = 'R'
         AND R.IDRECDESPIRPJOP  = 'N'
		   AND LC.IDLANCINTE      <> 'F'
         AND LC.CDEMPRESA       = CM.CDEMPRESA
         AND LC.NRLANCCONT      = CM.NRLANCCONT
         AND LC.NRSEQLANC       = CM.NRSEQLANC
         AND CM.NRSEQUMAPAPU    = P_NRSEQUMAPAPU;
    EXCEPTION
       WHEN NO_DATA_FOUND THEN
          V_VRLUCRONAOOPERAC_CTB :=0;
    END;

    /*LUCRO NÃO-OPERACIONAL TOTAL*/
    V_VRLUCRONAOOPERAC_TOT := NVL(V_VRLUCRONAOOPERAC,0) + NVL(V_VRLUCRONAOOPERAC_CTB,0);
    IF V_VRLUCRONAOOPERAC_TOT < 0 THEN
       V_VRLUCRONAOOPERAC_TOT := 0;
    END IF;

   /*------------- LUCRO REAL --------------*/

   /******** Cálculo do Lucro Líquido ********/
   V_VRLUCROLIQLR    :=  NVL(V_VRLUCROOPERAC_TOT,0) +  NVL(V_VRLUCRONAOOPERAC_TOT,0);
   IF V_VRLUCROLIQLR < 0 THEN
      V_VRLUCROLIQLR := 0;
   END IF;
   V_VRLUCROLIQREAL  :=  NVL(V_VRLUCROLIQLR ,0)+ NVL(V_VRADICAOTOT,0) - NVL(V_VREXCLUSAOTOT,0);
   IF V_VRLUCROLIQREAL < 0 THEN
      V_VRLUCROLIQREAL := 0;
   END IF;
   V_VRDEBLUCREAL    :=  NVL(V_VRLUCROLIQREAL,0) * NVL(V_VRALIQIMPCSLL,0)/100;
   IF V_VRDEBLUCREAL < 0 THEN
      V_VRDEBLUCREAL := 0;
   END IF;

   /******** Cálculo do Imposto Devido ********/
   V_VRDEBLUCREALTOT :=  NVL(V_VRDEBLUCREAL,0) - NVL(V_VRDEDUCAOTOT,0);
   IF V_VRDEBLUCREALTOT < 0 THEN
      V_VRDEBLUCREALTOT := 0;
   END IF;

   /*------------- LUCRO PRESUMIDO --------------*/

   /******** Cálculo do Faturamento Bruto COMPETENCIA ********/
   V_VRFATNFLP  := NVL(V_VRRECEBRUTAJUS,0) + NVL(V_VRADICAOTOT,0) - NVL(V_VREXCLUSAOTOT,0);
   IF V_VRFATNFLP < 0 THEN
      V_VRFATNFLP := 0;
   END IF;
   /******** Cálculo do Faturamento Bruto PAGAMENTO ********/
   V_VRFATNFLPAG  := NVL(V_VRRECEBRUTAJUSPAG,0) + NVL(V_VRADICAOTOTPAG,0) - NVL(V_VREXCLUSAOTOTPAG,0);
   IF V_VRFATNFLPAG < 0 THEN
      V_VRFATNFLPAG := 0;
   END IF;
   /******** Cálculo do Imposto Devido COMPETENCIA********/
   V_VRIMPOSAPURPRESU  := V_VRFATNFLP * (V_VRALIQIMPCSLL/100);
   IF V_VRIMPOSAPURPRESU < 0 THEN
      V_VRIMPOSAPURPRESU := 0;
   END IF;
   /******** Cálculo do Imposto Devido PAGAMENTO********/
   V_VRIMPOSAPURPRESUPAG  := V_VRFATNFLPAG * (V_VRALIQIMPCSLL/100);
   IF V_VRIMPOSAPURPRESUPAG < 0 THEN
      V_VRIMPOSAPURPRESUPAG := 0;
   END IF;

   /******** Cálculo do Imposto à Recolher COMPETENCIA********/
   V_VRDEBITOTPRESU   := V_VRIMPOSAPURPRESU +  V_VRADICIONALLP - NVL(V_VRDEDUCAOTOT,0);
   IF V_VRDEBITOTPRESU < 0 THEN
      V_VRDEBITOTPRESU := 0;
   END IF;

   /******** Cálculo do Imposto à Recolher PAGAMENTO********/
   V_VRDEBITOTPRESUPAG    := V_VRIMPOSAPURPRESUPAG + V_VRADICIONALLPAG - NVL(V_VRDEDUCAOTOTPAG,0);
   IF V_VRDEBITOTPRESUPAG < 0 THEN
      V_VRDEBITOTPRESUPAG := 0;
   END IF;

   IF V_IDFORMAPUREMPR = 'P' THEN
      V_VRDEBLUCPRESU := V_VRDEBITOTPRESUPAG;
   ELSE
      V_VRDEBLUCPRESU := V_VRDEBITOTPRESU;
   END IF;

   IF V_IDFORMAPUR IN ('LPM', 'LRM', 'LPT') THEN
      V_VRDEBLUCREALTOT := 0;
   ELSE
      V_VRDEBLUCPRESU := 0;
   END IF;

   /*------- Atualiza os valores cálculados nas respectivas tabelas -------*/
   IF P_GERBLCT = 'N' THEN
      UPDATE MAPAAPUIMPOFED
         SET VRFATBRUTOPRESU     = V_VRRECEBRUTAJUS,
             VRFATBRUTOPRESUPAG  = V_VRRECEBRUTAJUSPAG,
             VRADICPRESU	      = NVL(V_VRADICAOTOT,0),
             VRADICPRESUPAG      = NVL(V_VRADICAOTOTPAG,0),
             VREXCLPRESU	      = NVL(V_VREXCLUSAOTOT,0),
             VREXCLPRESUPAG      = NVL(V_VREXCLUSAOTOTPAG,0),
             VRALIQPRESUNCAO     = V_VRALIQPRES,
             VRALIQIRPJ          = V_VRALIQIR,
             VRALIQCSLL	         = V_VRALIQIMPCSLL,
             VRIMPOSAPURPRESU    = V_VRIMPOSAPURPRESU,
             VRIMPOSAPURPRESUPAG = V_VRIMPOSAPURPRESUPAG,
             VRCOMPPRESU	      = V_VRCOMPENSACOESLP,
             VRDEBLUCPRESU	      = V_VRDEBLUCPRESU,
             VRADICIONALPRES     = V_VRADICIONALLP,
             VRADICIONALPRESPAG  = V_VRADICIONALLPAG,
             VRFATBRUTOREAL      = V_VRFATNFLP,
             VRFATBRUTOREALPAG   = V_VRFATNFLPAG,
             VRADICREAL	         = NVL(V_VRADICAOTOT,0),
             VREXCLREAL	         = NVL(V_VREXCLUSAOTOT,0),
             VRLUCROLIQREAL      = V_VRLUCROLIQREAL,
             VRDEDUCAOREAL	      = NVL(V_VRDEDUCAOTOT,0),
             VRCOMPREAL	         = V_VRCOMPENSACOESLR,
             VRDEBLUCREAL	      = V_VRDEBLUCREAL,
             VRADICIONALREAL     = V_VRADICIONALLR,
             VRLUCROOPERAC       = V_VRLUCROOPERAC_TOT,
             VRLUCRONAOOPERAC    = V_VRLUCRONAOOPERAC_TOT,
             VRDEDUCAOPRESU      = NVL(V_VRDEDUCAOTOT,0),
             VRDEDUCAOPRESUPAG   = NVL(V_VRDEDUCAOTOTPAG,0),
             VRDEBITOTPRESU      = V_VRDEBITOTPRESU,
             VRDEBITOTPRESUPAG   = V_VRDEBITOTPRESUPAG,
             VRLIMITEADIC	      = V_VRLIMITEADIC,
             VRALIQADICIR	      = V_VRALIQADICIR,
             VRPARCEXCEDPRES     = V_VRPARCEXCEDLP,
             VRPARCEXCEDPRESPAG  = V_VRPARCEXCEDLPAG,
             VRPARCEXCEDREAL     = V_VRPARCEXCEDLR,
             VRDEBLUCREALTOT     = V_VRDEBLUCREALTOT,
             VRLUCROLIQTOT       = V_VRLUCROLIQLR
       WHERE NRSEQUMAPAPU        = P_NRSEQUMAPAPU;
      COMMIT;
   ELSE
      DELETE FROM BALCTREDSUSPIRPJ
            WHERE NRSEQUMAPAPU = P_NRSEQUMAPAPU;

      INSERT INTO BALCTREDSUSPIRPJ
         (NRSEQUMAPAPU, MESBALCTIRPJ, VRLUCRLIQBALCTIRPJ, VRADICBALCTIRPJ, VREXCLBALCTIRPJ, CDIMPOSTO,
          VRLUCREALBALCTIRPJ, VRALIQBALCTIRPJ, VRIMPAPURBALCTIRPJ, VRADICIONAL, VRDEDUCOES, VRDEBLUCREAL )
      VALUES
         (P_NRSEQUMAPAPU, TO_CHAR(P_DTINICIAL,'MMYYYY'), V_VRRECEBRUTAJUS, NVL(V_VRADICAOTOT,0), NVL(V_VREXCLUSAOTOT,0), P_CDIMPOSTO,
         V_VRFATNFLP, V_VRALIQIMPCSLL, V_VRIMPOSAPURPRESU, V_VRADICIONALLP, NVL(V_VRDEDUCAOTOT,0), V_VRDEBITOTPRESU);
      COMMIT;
   END IF;

   /*Atualiza o tipo de apuração do mapa para, quando de apuração trimestral, indicar que o mesmo pode ser parcelado
     na tela Preparação de Pagamento */
   IF V_IDFORMAPUR IN ('LPM', 'LRM', 'LPT') THEN
      UPDATE MAPAAPURAC
         SET IDTPMAPAAPURAC = V_IDFORMAPUR,
		     IDBLOQMAPAPU   = 'A',
             VRTOTAIMPOS    = V_VRDEBLUCPRESU,
             VRRECEGUIA     = V_VRDEBLUCPRESU,
             VRTOTAGUIA     = NVL(V_VRDEBLUCPRESU,0) + NVL(VRMULTAGUIA,0) + NVL(VRJUROGUIA,0)
       WHERE NRSEQUMAPAPU   = P_NRSEQUMAPAPU;
   ELSE
      UPDATE MAPAAPURAC
         SET IDTPMAPAAPURAC = V_IDFORMAPUR,
		     IDBLOQMAPAPU   = 'A',
             VRTOTAIMPOS    = V_VRDEBLUCREALTOT,
             VRRECEGUIA     = V_VRDEBLUCREALTOT,
             VRTOTAGUIA     = NVL(V_VRDEBLUCREALTOT,0) + NVL(VRMULTAGUIA,0) + NVL(VRJUROGUIA,0)
       WHERE NRSEQUMAPAPU   = P_NRSEQUMAPAPU;
   END IF;
   COMMIT;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('APURA_CSLL','0.00.000'); END;
/