CREATE OR REPLACE PROCEDURE APURAC_DESPESAROP(
   P_CDFILIAL       FILIAL.CDFILIAL%TYPE,
   P_FILIALPAR      FILIAL.CDFILIAL%TYPE,
   P_DTINICIAL      IN DATE,
   P_DTFINAL        IN DATE,
   P_NRROPFILIAL    IN VARCHAR,
   P_NUMEXEC        IN NUMBER)
IS
   V_IDDESPDIVROP      CHAR;
   V_IDDESPMOBROP      CHAR;
   V_IDDESPMOBROPCTB   CHAR;
   V_IDAPUMOCTPGROP    CHAR;
   V_IDAPUCTPGROP      CHAR;
   V_IDAPURDEPRECROP   CHAR;
   V_IDDESPENTROP      CHAR;
   V_CDGRUPDESP        VARCHAR2(10);
   V_NRSEQDESPROD      NUMBER;
   V_NRSEQDEDUROP      NUMBER;
   V_NRSEQFATUROP      NUMBER;
   V_CDFILCENTPME      VARCHAR2(04);
   V_CDEMPRESA         VARCHAR2(02);
   V_IDDTAPUCTPAGAR    VARCHAR2(30);
   V_IDCONSPERRATFIL   CHAR;
   V_IDTIPODEPRECROP   CHAR;
   V_CDTPCTPAGAR       VARCHAR2(10);
   V_HA_CONTAPAGAR     VARCHAR2(10);
   V_VRRATENF          FLOAT;
   V_EXCECAO           VARCHAR2(20);
   V_IDCONSLANCAJUSPAG VARCHAR2(01);
   V_IDCONSLANCAJUSREC VARCHAR2(01);
   V_IDCONSDEBBCO      VARCHAR2(01);
   V_IDCONSCREDBCO     VARCHAR2(01);
   V_VRPEROTHERSRAT    FLOAT;
   V_IDAPURDESPENTRMO  VARCHAR2(01);
   V_IDCONSRATENTRMO   VARCHAR2(01);
   V_IDLANCIMPPAG      VARCHAR2(01);
   V_TIPOCONTAPAGAR    VARCHAR2(10);
   V_VRTAXAADMIN       FLOAT;
   V_VRPERCTAXAADMIN   FLOAT;
   V_IDTIPOCALCTAXAADM VARCHAR2(30);
   V_MODMESTAXA        FLOAT;
   V_NRDIAINIORCA      FLOAT;
   V_FATEFETIVO        FLOAT;
   V_IDFATUROP         VARCHAR2(02);
   V_FATNOTAFISC       FLOAT;
   V_IDVOLUNEGOCIO     VARCHAR2(30);
   V_IDCONSFATUDIV     VARCHAR2(01);
   V_IDGERROPFOS       VARCHAR2(01);
   V_DEDUIMPO          FLOAT;
   V_IDPERCFIXIMPREC   VARCHAR2(01);
   V_FATDEVCANC        FLOAT;
   V_FATRECEXTRA       FLOAT;
   V_FATDIVERSO        FLOAT;
   V_FATFORSALE        FLOAT;
   V_CREDIMPO          FLOAT;
   V_DESCSOBVENDA      FLOAT;
   V_ENCARGOS          FLOAT;
   V_BRUTO             FLOAT;
   V_LIQUIDO           FLOAT;
   V_IDCALCIMPFAT      VARCHAR2(30);
   V_IDTPVRCTPAGAR     VARCHAR2(10);
   V_IDTPAPURACAO      VARCHAR2(01);
   V_IDAPUCTRECROP     VARCHAR2(01);
   V_IDDTAPUCTRECROP   VARCHAR2(30);
   V_IDDTAPURFAT       VARCHAR2(01);
   V_DEDUIMPOFOS       FLOAT(126);
   V_IDEXIBEIMPOFOS    VARCHAR2(01);
   V_IDPERFIXIMPDRF    VARCHAR2(01);
   V_NRROPFILIAL_ADM   NUMBER;
   V_NRSEQFATUROP_ADM  NUMBER;
BEGIN
   BEGIN
      SELECT IDDESPDIVROP, IDDESPMOBROP, IDDESPMOBROPCTB, IDAPUMOCTPGROP, IDAPUCTPGROP,
             IDAPURDEPRECROP, IDDESPENTROP, CDGRUPDESP, IDDTAPUCTPAGAR, IDCONSPERRATFIL,
             IDTIPODEPRECROP, IDCONSLANCAJUSPAG, IDCONSLANCAJUSREC, IDCONSDEBBCO,
             IDCONSCREDBCO, IDAPURDESPENTRMO, IDCONSRATENTRMO, IDLANCIMPPAG,
             IDTIPOCALCTAXAADM, IDFATUROP, IDVOLUNEGOCIO, IDCONSFATUDIV,
             IDGERROPFOS, IDPERCFIXIMPREC, IDCALCIMPFAT, IDTPVRCTPAGAR, IDTPAPURACAO,
             NVL(IDAPUCTRECROP,'N'), NVL(IDDTAPUCTRECROP,'BAIXA'), NVL(IDDTAPURFAT,'E'),
             NVL(P.IDEXIBEIMPOFOS,'N'), NVL(P.IDPERFIXIMPDRF, 'N')
        INTO V_IDDESPDIVROP, V_IDDESPMOBROP, V_IDDESPMOBROPCTB, V_IDAPUMOCTPGROP, V_IDAPUCTPGROP,
             V_IDAPURDEPRECROP, V_IDDESPENTROP, V_CDGRUPDESP, V_IDDTAPUCTPAGAR, V_IDCONSPERRATFIL,
             V_IDTIPODEPRECROP, V_IDCONSLANCAJUSPAG, V_IDCONSLANCAJUSREC, V_IDCONSDEBBCO,
             V_IDCONSCREDBCO, V_IDAPURDESPENTRMO, V_IDCONSRATENTRMO, V_IDLANCIMPPAG,
             V_IDTIPOCALCTAXAADM, V_IDFATUROP, V_IDVOLUNEGOCIO, V_IDCONSFATUDIV,
             V_IDGERROPFOS, V_IDPERCFIXIMPREC, V_IDCALCIMPFAT, V_IDTPVRCTPAGAR, V_IDTPAPURACAO,
             V_IDAPUCTRECROP, V_IDDTAPUCTRECROP, V_IDDTAPURFAT,
             V_IDEXIBEIMPOFOS, V_IDPERFIXIMPDRF
        FROM PARFILIALROP P
       WHERE P.CDFILIAL = P_FILIALPAR;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN BEGIN
        V_IDDESPDIVROP := NULL;    V_IDDESPMOBROPCTB := NULL;   V_IDAPUCTPGROP := 'N';
        V_IDDESPMOBROP := NULL;    V_IDAPUMOCTPGROP  := 'N';    V_IDAPURDEPRECROP := 'N';
        V_IDDESPENTROP := NULL;    V_CDGRUPDESP := NULL;        V_IDDTAPUCTPAGAR := 'BAIXA';
        V_IDCONSPERRATFIL := 'N';  V_IDTIPODEPRECROP := 'C';    V_IDAPURDESPENTRMO := 'N';
        V_IDCONSRATENTRMO := 'N';  V_IDLANCIMPPAG := 'N';       V_IDTIPOCALCTAXAADM := NULL;
        V_IDFATUROP := NULL;       V_IDVOLUNEGOCIO := NULL;     V_IDCONSFATUDIV := NULL;
        V_IDGERROPFOS := NULL;     V_IDPERCFIXIMPREC := NULL;   V_IDCALCIMPFAT := NULL;
        V_IDTPVRCTPAGAR := 'LIQUIDO'; V_IDTPAPURACAO := NULL;   V_IDAPUCTRECROP := 'N';
        V_IDDTAPUCTRECROP := 'BAIXA'; V_IDDTAPURFAT := 'E';  V_IDEXIBEIMPOFOS := 'N';
        V_IDPERFIXIMPDRF := 'N';
     END;
   END;

   BEGIN
     SELECT CASE WHEN IDFILCENTPME = 'S'
                 THEN CDFILIAL
                 ELSE CDFILCENTPME
            END
       INTO V_CDFILCENTPME
       FROM FILIAL
      WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       V_CDFILCENTPME := NULL;
   END;

   BEGIN
      SELECT CDEMPRESA
        INTO V_CDEMPRESA
        FROM FILIAL F LEFT JOIN PARFILIALROP P ON F.CDFILIAL = P.CDFILIAL
       WHERE F.CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_CDEMPRESA := NULL;
      END;
   END;

   IF V_IDDESPDIVROP = 'S' THEN
      FOR C_DESPESASDIV IN (SELECT D.CDFILIAL, D.CDDESPPREV, D.DTPRDESPFILI,
                                   D.VRPRDESPFILI, D.VRREDESPFILI, IDDESTACAROP
                              FROM DESPPREVFILI D LEFT JOIN DPSUBGRDESP DP ON D.CDDESPPREV = DP.CDDESPPREV
                                                  LEFT JOIN DESPPREV E ON D.CDDESPPREV = E.CDDESPPREV
                             WHERE D.CDFILIAL = P_CDFILIAL
                               AND TRUNC(D.DTPRDESPFILI) = P_DTINICIAL
                               AND ((DP.CDGRUPDESP  = V_CDGRUPDESP) OR (V_CDGRUPDESP IS NULL))) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'DESPESA', NULL, V_CDFILCENTPME,
                      C_DESPESASDIV.DTPRDESPFILI, SYSDATE, C_DESPESASDIV.CDDESPPREV, C_DESPESASDIV.VRREDESPFILI, 1,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL); /* DD CD */
      END LOOP;
   END IF;

   IF V_IDDESPMOBROP = 'S' THEN
      FOR C_DESPESASMO IN (SELECT L.CDFILIAL, L.DTLANCDESFAT, L.CDDESPADMFAT,
                                  L.VRLANCDESFAT, L.VRTXLANCDESP, L.VRPREVDESFAT
                             FROM LANCDESPFATU L
                            WHERE L.CDFILIAL = P_CDFILIAL
                              AND TRUNC(L.DTLANCDESFAT) = P_DTINICIAL) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'MAOOBRA', 'MANUAL', V_CDFILCENTPME,
                      C_DESPESASMO.DTLANCDESFAT, SYSDATE, C_DESPESASMO.CDDESPADMFAT, C_DESPESASMO.VRLANCDESFAT*(-1), 1,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL); /* DM */
      END LOOP;
   END IF;

   IF (V_IDDESPMOBROPCTB = 'S') THEN
      FOR C_FOLHA IN (SELECT CDCONTCTBL, CDDESPADMFAT
                        FROM DESPORCMOCCONT
                       WHERE ((CDFILIAL = P_CDFILIAL) OR (CDFILIAL = 'T'))
                         AND CDDESPADMFAT = '06'
                       GROUP BY CDCONTCTBL, CDDESPADMFAT) LOOP

         FOR C_LANCCUST IN (SELECT ROUND(SUM(VRLANCCUST),2) VRLANC
                              FROM LANCCCUSTO
                             WHERE CDEMPRESA   = V_CDEMPRESA
                               AND CDFILIAL    = P_CDFILIAL
                               AND CDCONTCTBL  = C_FOLHA.CDCONTCTBL
                               AND TRUNC(DTCOMPCUSTO) = P_DTINICIAL
                            HAVING SUM(VRLANCCUST) <> 0) LOOP

            NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

            INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                     DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                     CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                     CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                     DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
                 VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'MAOOBRA', 'CONTABIL', V_CDFILCENTPME,
                         P_DTINICIAL, SYSDATE, C_FOLHA.CDDESPADMFAT, /*CASE WHEN C_LANCCUST.VRLANC > 0
                                                                        THEN -C_LANCCUST.VRLANC
                                                                        ELSE ABS( */ C_LANCCUST.VRLANC /*)
                                                                   END */, 1,
                         P_CDFILIAL, NULL, NULL, NULL, NULL,
                         NULL, NULL, NULL, NULL, NULL,
                         NULL, NULL, NULL); /* DM */
         END LOOP;
      END LOOP;
      FOR C_FOLHA IN (SELECT CDCONTCTBL, CDDESPADMFAT
                        FROM DESPORCMOCCONT
                       WHERE ((CDFILIAL = P_CDFILIAL) OR (CDFILIAL = 'T'))
                         AND CDDESPADMFAT <> '06'
                       GROUP BY CDCONTCTBL, CDDESPADMFAT) LOOP
         FOR C_LANCCUST IN (SELECT ROUND(SUM(VRLANCCUST),2) VRLANC
                              FROM LANCCCUSTO
                             WHERE CDFILIAL = P_CDFILIAL
                               AND CDCONTCTBL = C_FOLHA.CDCONTCTBL
                               AND TRUNC(DTCOMPCUSTO) = P_DTINICIAL
                            HAVING SUM(VRLANCCUST) <> 0) LOOP

            NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

            INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                     DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                     CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                     CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                     DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
                 VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'MAOOBRA', 'CONTABIL', V_CDFILCENTPME,
                         P_DTINICIAL, SYSDATE, C_FOLHA.CDDESPADMFAT, C_LANCCUST.VRLANC, 1,
                         P_CDFILIAL, NULL, NULL, NULL, NULL,
                         NULL, NULL, NULL, NULL, NULL,
                         NULL, NULL, NULL); /* DM */
         END LOOP;
      END LOOP;
   END IF;

   IF V_IDAPUMOCTPGROP = 'S' THEN
      FOR C_CTPAGMOBRA IN (SELECT CP.IDSTATPAG, CP.CDFILIBPAG, CP.CDFILIAL,CP.NRLANCTONF,CP.NRSEQVENPAG, CP.NRPARCELAPAG, TC.CDTPCTPAGAR,
                                  CASE WHEN NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR) = 'ENTRADA' THEN NF.DTENTRSAID
                                       WHEN NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR) = 'VENCIMENTO' THEN CP.DTATUAVENPAG
                                       ELSE DTBAIXAPAG
                                  END DTBAIXAPAG,
                                  --NVL(VRRATCCCTPAG,CP.VRATUAPAG)
                                  CASE WHEN NVL(CP.VRATUAPAG,0) = 0
                                       THEN 0
                                       ELSE (NVL(VRRATCCCTPAG,CP.VRATUAPAG)/CP.VRATUAPAG)*CP.VRORIGPAG
                                  END AS VRCONTARATEADA, NF.NRNOTAFISC, NF.CDFORNECED,
                                  CP.DSCONTAPAG
                             FROM CONTAPAGAR CP INNER JOIN NOTAFISCAL NF ON NF.CDFILIAL = CP.CDFILIAL
                                                                        AND NF.NRLANCTONF = CP.NRLANCTONF
                                                INNER JOIN BOLETPAGAR BO ON CP.CDFILIBPAG = BO.CDFILIAL
                                                                        AND CP.NRLANBOLPAG = BO.NRLANBOLPAG
                                                LEFT  JOIN BAIXAPAGA BP ON BO.CDFILIBAIPAG = BP.CDFILIAL
                                                                       AND BO.NRBAIXAPAG = BP.NRBAIXAPAG
                                                INNER JOIN TIPOCTPAGROP TC ON TC.CDTPCTPAGAR = CP.CDTPCTPAGAR
                                                LEFT  JOIN RATCTPAGAR R ON R.CDFILIAL = CP.CDFILIAL
                                                                       AND R.NRLANCTONF = CP.NRLANCTONF
                                                                       AND R.NRSEQVENPAG = CP.NRSEQVENPAG
                                                                       AND R.NRPARCELAPAG = CP.NRPARCELAPAG
                            WHERE ((('ENTRADA' = NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR)) AND (TRUNC(NF.DTENTRSAID) = P_DTINICIAL)) OR
                               (('VENCIMENTO' = NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR)) AND (TRUNC(CP.DTATUAVENPAG) = P_DTINICIAL)) OR
                               (('BAIXA' = NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR)) AND (TRUNC(BP.DTBAIXAPAG) = P_DTINICIAL)))
                              AND TC.CDFILIAL = P_FILIALPAR
                              AND TC.IDTPCTPAGROP = 'MAO'
                              AND ((CDFIRATCTPAG = P_CDFILIAL) OR (CDFIRATCTPAG IS NULL) AND (NF.CDFILIRECE = P_CDFILIAL))) LOOP

            NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

            INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                     DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                     CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                     CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                     DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
                 VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'MAOOBRA', NULL, V_CDFILCENTPME,
                         C_CTPAGMOBRA.DTBAIXAPAG, SYSDATE, C_CTPAGMOBRA.CDTPCTPAGAR, C_CTPAGMOBRA.VRCONTARATEADA*(-1), 1,
                         C_CTPAGMOBRA.CDFILIAL, C_CTPAGMOBRA.NRLANCTONF, C_CTPAGMOBRA.NRSEQVENPAG, C_CTPAGMOBRA.NRPARCELAPAG, C_CTPAGMOBRA.CDFORNECED,
                         NULL, C_CTPAGMOBRA.NRNOTAFISC, NULL, NULL, NULL,
                         NULL, NULL, NULL); /* DM */
      END LOOP;
   END IF;

   IF V_IDAPUCTPGROP = 'S' THEN
      FOR C_CONTAPAGAR IN (SELECT CP.IDSTATPAG, CP.CDFILIBPAG, CP.CDFILIAL,CP.NRLANCTONF,CP.NRSEQVENPAG, CP.NRPARCELAPAG, TC.CDTPCTPAGAR,
                                  DECODE(NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR),'ENTRADA',NF.DTENTRSAID,DECODE
                                  (NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR),'VENCIMENTO',CP.DTATUAVENPAG,DTBAIXAPAG))DTBAIXAPAG,
                                  CASE WHEN NVL(TC.IDTPVRCTPAGAR,V_IDTPVRCTPAGAR) = 'LIQUIDO'
                                       THEN NVL(VRRATCCCTPAG,CP.VRATUAPAG)
                                       ELSE CASE WHEN NVL(CP.VRATUAPAG,0) <> 0
                                                 THEN (NVL(VRRATCCCTPAG,CP.VRATUAPAG)/CP.VRATUAPAG)*CP.VRORIGPAG
                                                 ELSE CP.VRORIGPAG
                                            END
                                  END AS VRCONTARATEADA,
                                  NF.NRNOTAFISC, NF.CDFORNECED
                             FROM CONTAPAGAR CP, NOTAFISCAL NF, BOLETPAGAR BO, BAIXAPAGA BP, TIPOCTPAGROP TC, RATCTPAGAR R
                            WHERE CP.CDFILIBPAG     = BO.CDFILIAL
                              AND CP.NRLANBOLPAG    = BO.NRLANBOLPAG
                              AND BO.CDFILIBAIPAG   = BP.CDFILIAL(+)
                              AND BO.NRBAIXAPAG     = BP.NRBAIXAPAG(+)
                              AND ((('ENTRADA'      = NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR)) AND (TRUNC(NF.DTENTRSAID)    = P_DTINICIAL)) OR
                                  (('VENCIMENTO'    = NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR)) AND (TRUNC(CP.DTATUAVENPAG)  = P_DTINICIAL)) OR
                                  (('BAIXA'         = NVL(TC.IDDTAPUCTPAGAR,V_IDDTAPUCTPAGAR)) AND (TRUNC(BP.DTBAIXAPAG)    = P_DTINICIAL)))
                              AND TC.CDFILIAL       = P_FILIALPAR
                              AND TC.CDTPCTPAGAR    = CP.CDTPCTPAGAR
                              AND TC.IDTPCTPAGROP   = 'DSP'
                              AND NF.CDFILIAL       = CP.CDFILIAL
                              AND NF.NRLANCTONF     = CP.NRLANCTONF
                              AND R.CDFILIAL(+)     = CP.CDFILIAL
                              AND R.NRLANCTONF(+)   = CP.NRLANCTONF
                              AND R.NRSEQVENPAG(+)  = CP.NRSEQVENPAG
                              AND R.NRPARCELAPAG(+) = CP.NRPARCELAPAG
                              AND ((CDFIRATCTPAG    = P_CDFILIAL) OR (CDFIRATCTPAG IS NULL) AND (NF.CDFILIRECE = P_CDFILIAL))
                              AND NOT EXISTS (SELECT 1
                                                FROM TIPOPAGIMPROP TI
                                               WHERE TI.CDFILIAL = TC.CDFILIAL
                                                 AND TI.CDTPCTPAGAR = TC.CDTPCTPAGAR)) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'DESPESA', 'CONTA_PAGAR', V_CDFILCENTPME,
                      C_CONTAPAGAR.DTBAIXAPAG, SYSDATE, C_CONTAPAGAR.CDTPCTPAGAR, C_CONTAPAGAR.VRCONTARATEADA, 1,
                      C_CONTAPAGAR.CDFILIAL, C_CONTAPAGAR.NRLANCTONF, C_CONTAPAGAR.NRSEQVENPAG, C_CONTAPAGAR.NRPARCELAPAG, C_CONTAPAGAR.CDFORNECED,
                      NULL, C_CONTAPAGAR.NRNOTAFISC, NULL, NULL, NULL,
                      NULL, NULL, NULL); /* DC */
      END LOOP;
   END IF;

   IF V_IDAPURDESPENTRMO = 'S' THEN
      FOR C_DESPCOMPRA IN (SELECT DISTINCT NF.CDTIPOOPER,NF.NRNOTAFISC,NF.CDFILIAL,NF.CDFILIRECE,NF.NRLANCTONF,NF.DTENTRSAID, NF.CDSERIE,
                                  NF.CDFORNECED,NF.IDSTATUS,NF.DTINIREFEFAT,NF.DTFINREFEFAT,NF.CDFILICCUST,A.CDTPCTPAGAR
                             FROM (SELECT TP.CDFILIAL,TP.CDTIPOOPER,T.IDENTRSAIDOP,TP.IDTPOPERACROP,T.CDTPCTPAGAR
                                     FROM TIPOOPERACROP TP,TIPOOPERAC T
                                    WHERE TP.CDTIPOOPER = T.CDTIPOOPER
                                      AND TP.IDENTRSAIDOP = T.IDENTRSAIDOP
                                      AND TP.IDTPOPERACROP = 'MO'
                                      AND TP.CDFILIAL = P_FILIALPAR) A,
                                  (SELECT DISTINCT N.CDTIPOOPER,N.NRNOTAFISC,N.CDFILIAL,N.CDFILIRECE,N.NRLANCTONF,N.DTENTRSAID,N.CDSERIE,
                                          N.CDFORNECED,N.IDSTATUS,N.DTINIREFEFAT,N.DTFINREFEFAT,R.CDFILICCUST,N.IDENTRSAIDOP
                                     FROM NOTAFISCAL N,RATEIONF R,ITEMPRODUTO I
                                    WHERE N.IDENTRSAIDOP = 'E'
                                      AND TRUNC(N.DTENTRSAID) = P_DTINICIAL
                                      AND N.IDSISTEMA = 'DFE'
                                      AND N.CDFILIAL = I.CDFILIAL
                                      AND N.NRLANCTONF = I.NRLANCTONF
                                      AND R.CDFILIAL = I.CDFILIAL
                                      AND R.NRLANCTONF = I.NRLANCTONF
                                      AND R.NRSEQITPROD = I.NRSEQITPROD
                                      AND R.CDFILICCUST = P_CDFILIAL
                                      AND V_IDCONSRATENTRMO = 'S'
                                    UNION ALL
                                   SELECT DISTINCT N.CDTIPOOPER,N.NRNOTAFISC,N.CDFILIAL,N.CDFILIRECE,N.NRLANCTONF,N.DTENTRSAID,N.CDSERIE,
                                          N.CDFORNECED,N.IDSTATUS,N.DTINIREFEFAT,N.DTFINREFEFAT,P_CDFILIAL AS CDFILICCUST,N.IDENTRSAIDOP
                                     FROM NOTAFISCAL N,ITEMPRODUTO I
                                    WHERE N.CDFILIRECE = P_CDFILIAL
                                      AND N.IDENTRSAIDOP = 'E'
                                      AND TRUNC(N.DTENTRSAID) = P_DTINICIAL
                                      AND N.IDSTATUS <> 'C'
                                      AND N.IDSISTEMA = 'DFE'
                                      AND N.CDFILIAL = I.CDFILIAL
                                      AND N.NRLANCTONF = I.NRLANCTONF) NF
                            WHERE NF.CDTIPOOPER = A.CDTIPOOPER
                              AND NF.IDENTRSAIDOP = A.IDENTRSAIDOP
                              AND A.IDTPOPERACROP = 'MO'
                              AND P_FILIALPAR = A.CDFILIAL) LOOP

         IF (V_IDAPUCTPGROP = 'S') AND (V_IDAPUMOCTPGROP = 'S') THEN
            BEGIN
               SELECT DISTINCT C.CDTPCTPAGAR
                 INTO V_CDTPCTPAGAR
                 FROM CONTAPAGAR C INNER JOIN TIPOCTPAGAR T ON C.CDTPCTPAGAR = T.CDTPCTPAGAR
                                   INNER JOIN TIPOCTPAGROP TP ON C.CDTPCTPAGAR = TP.CDTPCTPAGAR
                                                             AND TIPOPARAM_DRO(NVL(C.CDFIMOVFLPAG,C.CDFILIAL)) = TP.CDFILIAL
                WHERE C.NRLANCTONF = C_DESPCOMPRA.NRLANCTONF
                  AND C.CDFILIAL = C_DESPCOMPRA.CDFILIAL
                  AND TP.CDTPCTPAGAR = C_DESPCOMPRA.CDTPCTPAGAR;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDTPCTPAGAR := 'NAO';
            END;
         ELSE
            V_CDTPCTPAGAR := 'NAO';
         END IF;

         IF V_CDTPCTPAGAR = 'NAO' THEN
            FOR C_ITDESPCOMPRA IN (SELECT DISTINCT I.CDPRODUTO, I.NRSEQITPROD, I.VRBRUTITEM,I.QTITEMPROD AS QUANTIDADE,
                                          SUBSTR(P.CDARVPROD,1,1) NIVEL_INICIAL, P.CDARVPROD
                                     FROM  ITEMPRODUTO I, PRODUTO P, PRODFILI F,
                                          (SELECT DISTINCT A.CDPRODUTO AS CDPRODUTO
                                             FROM PRODUTOROP R, PRODUTO A, PRODUTO B
                                            WHERE (INSTR(A.CDARVPROD,B.CDARVPROD) = 1)
                                              AND R.CDPRODUTO = B.CDPRODUTO
                                              AND R.CDFILIAL = P_FILIALPAR
                                              AND IDTPPRODUTOROP = 'MO') A
                                    WHERE I.CDFILIAL = C_DESPCOMPRA.CDFILIAL
                                      AND I.NRLANCTONF = C_DESPCOMPRA.NRLANCTONF
                                      AND P.CDPRODUTO = I.CDPRODUTO
                                      AND F.CDFILIAL = P_CDFILIAL
                                      AND F.CDPRODUTO = P.CDPRODESTO
                                      AND (NOT EXISTS (SELECT PPR.CDFILIAL
                                                         FROM PRODUTOROP PPR, PRODUTO PP
                                                        WHERE PPR.IDTPPRODUTOROP = 'CO'
                                                          AND PPR.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
                                                          AND PPR.CDPRODUTO = PP.CDPRODUTO
                                                          AND P.CDARVPROD LIKE PP.CDARVPROD||'%')
                                            OR EXISTS (SELECT PPR.CDFILIAL
                                                         FROM PRODUTOROP PPR, PRODUTO PP
                                                        WHERE PPR.IDTPPRODUTOROP = 'OX'
                                                          AND PPR.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
                                                          AND PPR.CDPRODUTO = PP.CDPRODUTO
                                                          AND P.CDARVPROD LIKE PP.CDARVPROD||'%'))
                                      AND I.CDPRODUTO = A.CDPRODUTO) LOOP

               IF (V_IDAPUCTPGROP = 'S') AND (V_IDAPUMOCTPGROP = 'S') THEN
                  BEGIN
                     SELECT NVL(CDTPCTPAGAR, 'NAO')
                       INTO V_CDTPCTPAGAR
                       FROM (SELECT P.CDTPCTPAGAR, P.CDNVPRODUTO
                               FROM PRODUTO P, TIPOCTPAGROP TP
                              WHERE P.CDTPCTPAGAR = TP.CDTPCTPAGAR
                                AND P.CDARVPROD IN (SELECT MAX(T.CDARVPROD)
                                                      FROM PRODUTO T
                                                     WHERE T.CDARVPROD BETWEEN C_ITDESPCOMPRA.NIVEL_INICIAL AND C_ITDESPCOMPRA.CDARVPROD
                                                     GROUP BY T.CDNVPRODUTO)
                                AND TP.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
                              ORDER BY P.CDNVPRODUTO DESC)
                      WHERE ROWNUM = 1;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_CDTPCTPAGAR := 'NAO';
                  END;
               END IF;

               IF V_CDTPCTPAGAR = 'NAO' THEN
                  V_VRRATENF := 0;
                  IF(V_IDCONSRATENTRMO = 'S') THEN
                     BEGIN
                        SELECT TRUNC(ROUND(NVL(SUM(R.VRPERRAT),0),6),2)
                          INTO V_VRPEROTHERSRAT
                          FROM RATEIONF R, FILIAL F, CENTCUST C
                         WHERE (R.CDFILIAL    = C_DESPCOMPRA.CDFILIAL) AND
                               (R.NRLANCTONF  = C_DESPCOMPRA.NRLANCTONF)  AND
                               (R.NRSEQITPROD = C_ITDESPCOMPRA.NRSEQITPROD) AND
                               (R.CDFILICCUST = F.CDFILIAL)   AND
                               (R.CDCENTCUSNF = C.CDCENTCUST) AND
                               (R.CDFILICCUST  <> P_CDFILIAL)
                         ORDER BY R.CDFILICCUST, R.CDCENTCUSNF;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_VRPEROTHERSRAT :=  0;
                     END;

                     IF(V_VRPEROTHERSRAT < 100 )THEN
                        BEGIN
                           --SELECT NVL(SUM(DECODE(R.VRPERRAT,100,C_ITDESPCOMPRA.VRBRUTITEM,NVL(R.VRRATENF,C_ITDESPCOMPRA.VRBRUTITEM))),C_ITDESPCOMPRA.VRBRUTITEM) AS VRRATENF
                           SELECT NVL(SUM(DECODE(R.VRPERRAT,100,C_ITDESPCOMPRA.VRBRUTITEM,NVL(C_ITDESPCOMPRA.VRBRUTITEM*R.VRPERRAT/100,C_ITDESPCOMPRA.VRBRUTITEM))),C_ITDESPCOMPRA.VRBRUTITEM) AS VRRATENF
                             INTO V_VRRATENF
                             FROM RATEIONF R
                            WHERE R.CDFILICCUST = P_CDFILIAL
                              AND R.CDFILIAL    = C_DESPCOMPRA.CDFILIAL
                              AND R.NRLANCTONF  = C_DESPCOMPRA.NRLANCTONF
                              AND R.NRSEQITPROD = C_ITDESPCOMPRA.NRSEQITPROD
                              AND R.VRRATENF > 0;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRRATENF := 0;
                        END;
                     ELSE
                        V_VRRATENF := 0;
                     END IF;
                  ELSE
                     V_VRRATENF := C_ITDESPCOMPRA.VRBRUTITEM;
                  END IF;

                  BEGIN
                     SELECT CASE WHEN COUNT(DISTINCT R.CDPRODUTO) = 0
                                 THEN 'NAO'
                                 ELSE TO_CHAR(COUNT(DISTINCT R.CDPRODUTO))
                            END AS EXCECAO
                       INTO V_EXCECAO
                       FROM PRODUTOROP R INNER JOIN PRODUTO B ON R.CDPRODUTO = B.CDPRODUTO
                                         INNER JOIN PRODUTO A ON A.CDPRODUTO = C_ITDESPCOMPRA.CDPRODUTO
                                                             AND A.CDPRODUTO LIKE B.CDPRODUTO||'%'
                      WHERE R.CDFILIAL = P_FILIALPAR
                        AND R.IDTPPRODUTOROP = 'MX';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_EXCECAO := 'NAO';
                     END;
                  END;

                  V_TIPOCONTAPAGAR := NULL;
                  BEGIN
                     SELECT NVL(CDTPCTPAGAR,C_DESPCOMPRA.CDTPCTPAGAR)
                       INTO V_TIPOCONTAPAGAR
                       FROM (SELECT B.CDARVPROD, B.CDTPCTPAGAR
                               FROM PRODUTO P, PRODUTO B
                              WHERE P.CDPRODUTO = C_ITDESPCOMPRA.CDPRODUTO
                                AND P.CDARVPROD LIKE B.CDARVPROD||'%'
                                AND B.CDTPCTPAGAR IS NOT NULL
                              ORDER BY B.CDARVPROD DESC)
                      WHERE ROWNUM = 1;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_TIPOCONTAPAGAR := NULL;
                     END;
                  END;

                  IF V_EXCECAO = 'NAO' THEN
                     IF V_VRRATENF > 0 THEN
                        NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

                        INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                                 DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                                 CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                                 CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                                 DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
                             VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'MAOOBRA', 'LANC_ENTRADA', V_CDFILCENTPME,
                                     C_DESPCOMPRA.DTENTRSAID, SYSDATE, V_TIPOCONTAPAGAR, V_VRRATENF*(-1), C_ITDESPCOMPRA.QUANTIDADE,
                                     C_DESPCOMPRA.CDFILIAL, C_DESPCOMPRA.NRLANCTONF, NULL, NULL, C_DESPCOMPRA.CDFORNECED,
                                     C_DESPCOMPRA.CDTIPOOPER, C_DESPCOMPRA.NRNOTAFISC, C_DESPCOMPRA.CDSERIE, C_DESPCOMPRA.IDSTATUS, C_DESPCOMPRA.DTINIREFEFAT,
                                     C_DESPCOMPRA.DTFINREFEFAT, C_ITDESPCOMPRA.NRSEQITPROD, C_ITDESPCOMPRA.CDPRODUTO); /* DE */
                     END IF;
                  END IF;
               END IF;
            END LOOP;
         END IF;
      END LOOP;
   END IF;

   IF (V_IDAPURDEPRECROP = 'S') AND (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      FOR C_DEPREC IN (SELECT SUM(VRRATOPERABE) AS VRRATOPERABE, F.DTFECHABEM
                         FROM CAPAOPERABEM C INNER JOIN FECHAMENTOBEM F ON C.CDEMPRESA = F.CDEMPRESA
                                                                       AND C.IDSIMULFECHABEM = F.IDSIMULFECHABEM
                                                                       AND C.NRSEQFECHABEM = F.NRSEQFECHABEM
                                             INNER JOIN TIPOOPERBEM T ON C.CDEMPRESA = T.CDEMPRESA
                                                                     AND C.CDTIPOOPERBEM = T.CDTIPOOPERBEM
                                             INNER JOIN ITCAPAOPERABEM IT ON C.CDEMPRESA = IT.CDEMPRESA
                                                                         AND C.IDSIMULOPERABEM = IT.IDSIMULOPERABEM
                                                                         AND C.NRSEQCAPAOPEBEM = IT.NRSEQCAPAOPEBEM
                                             INNER JOIN RATOPERABEM RT ON IT.CDEMPRESA = RT.CDEMPRESA
                                                                      AND IT.IDSIMULOPERABEM = RT.IDSIMULOPERABEM
                                                                      AND IT.NRSEQCAPAOPEBEM = RT.NRSEQCAPAOPEBEM
                                                                      AND IT.NRSEQITCAPAOPE = RT.NRSEQITCAPAOPE
                        WHERE TRUNC(F.DTFECHABEM) = P_DTINICIAL
                          AND RT.CDFILIAL = P_CDFILIAL
                          AND T.IDTIPOOPERBEM = '02'
                          AND T.CDEMPRESA = V_CDEMPRESA
                          AND C.IDTIPOFECHAOPER = V_IDTIPODEPRECROP
                        GROUP BY F.DTFECHABEM) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'DEPRECIACAO', NULL, V_CDFILCENTPME,
                      C_DEPREC.DTFECHABEM, SYSDATE, NULL, C_DEPREC.VRRATOPERABE, 1,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL); /* DB */
      END LOOP;
   END IF;

   IF V_IDDESPENTROP = 'S' THEN
      FOR C_DESPCOMPRA IN (SELECT NF.CDTIPOOPER,NF.NRNOTAFISC,NF.CDFILIAL,NF.CDFILIRECE,NF.NRLANCTONF,NF.DTENTRSAID, NF.CDSERIE,
                                  NF.CDFORNECED,NF.IDSTATUS,NF.DTINIREFEFAT,NF.DTFINREFEFAT,NF.CDFILICCUST,MAX(NVL(NF.CDTPCTPAGAR,A.CDTPCTPAGAR)) CDTPCTPAGAR
                             FROM (SELECT TP.CDFILIAL,TP.CDTIPOOPER,T.IDENTRSAIDOP,TP.IDTPOPERACROP,T.CDTPCTPAGAR
                                     FROM TIPOOPERACROP TP,TIPOOPERAC T
                                    WHERE TP.CDTIPOOPER = T.CDTIPOOPER
                                      AND TP.IDENTRSAIDOP = T.IDENTRSAIDOP
                                      AND TP.IDTPOPERACROP = 'DE'
                                      AND TP.CDFILIAL = P_FILIALPAR) A,
                                  (SELECT DISTINCT N.CDTIPOOPER,N.NRNOTAFISC,N.CDFILIAL,N.CDFILIRECE,N.NRLANCTONF,N.DTENTRSAID,N.CDSERIE,
                                          N.CDFORNECED,N.IDSTATUS,N.DTINIREFEFAT,N.DTFINREFEFAT,R.CDFILICCUST,N.IDENTRSAIDOP, NULL/*R.*/CDTPCTPAGAR
                                     FROM NOTAFISCAL N,RATEIONF R,ITEMPRODUTO I
                                    WHERE N.IDENTRSAIDOP = 'E'
                                      AND TRUNC(N.DTENTRSAID) = P_DTINICIAL
                                      AND N.IDSISTEMA = 'DFE'
                                      AND N.CDFILIAL = I.CDFILIAL
                                      AND N.NRLANCTONF = I.NRLANCTONF
                                      AND R.CDFILIAL = I.CDFILIAL
                                      AND R.NRLANCTONF = I.NRLANCTONF
                                      AND R.NRSEQITPROD = I.NRSEQITPROD
                                      AND R.CDFILICCUST = P_CDFILIAL
                                      AND V_IDCONSPERRATFIL = 'S'
                                    UNION ALL
                                   SELECT DISTINCT N.CDTIPOOPER,N.NRNOTAFISC,N.CDFILIAL,N.CDFILIRECE,N.NRLANCTONF,N.DTENTRSAID,N.CDSERIE,
                                          N.CDFORNECED,N.IDSTATUS,N.DTINIREFEFAT,N.DTFINREFEFAT,P_CDFILIAL AS CDFILICCUST,N.IDENTRSAIDOP,NULL CDTPCTPAGAR
                                     FROM NOTAFISCAL N,ITEMPRODUTO I
                                    WHERE N.CDFILIRECE = P_CDFILIAL
                                      AND N.IDENTRSAIDOP = 'E'
                                      AND TRUNC(N.DTENTRSAID) = P_DTINICIAL
                                      AND N.IDSTATUS <> 'C'
                                      AND N.IDSISTEMA = 'DFE'
                                      AND N.CDFILIAL = I.CDFILIAL
                                      AND N.NRLANCTONF = I.NRLANCTONF) NF
                            WHERE NF.CDTIPOOPER = A.CDTIPOOPER
                              AND NF.IDENTRSAIDOP = A.IDENTRSAIDOP
                              AND A.IDTPOPERACROP = 'DE'
                              AND P_FILIALPAR = A.CDFILIAL
                            GROUP BY NF.CDTIPOOPER,NF.NRNOTAFISC,NF.CDFILIAL,NF.CDFILIRECE,NF.NRLANCTONF,NF.DTENTRSAID, NF.CDSERIE,
                                  NF.CDFORNECED,NF.IDSTATUS,NF.DTINIREFEFAT,NF.DTFINREFEFAT,NF.CDFILICCUST) LOOP

         IF (V_IDAPUCTPGROP = 'S') AND (V_IDAPUMOCTPGROP = 'S') THEN
            BEGIN
               SELECT DISTINCT C.CDTPCTPAGAR
                 INTO V_CDTPCTPAGAR
                 FROM CONTAPAGAR C INNER JOIN TIPOCTPAGAR T ON C.CDTPCTPAGAR = T.CDTPCTPAGAR
                                   INNER JOIN TIPOCTPAGROP TP ON C.CDTPCTPAGAR = TP.CDTPCTPAGAR
                                                             AND TIPOPARAM_DRO(NVL(C.CDFIMOVFLPAG,C.CDFILIAL)) = TP.CDFILIAL
                WHERE C.NRLANCTONF = C_DESPCOMPRA.NRLANCTONF
                  AND C.CDFILIAL = C_DESPCOMPRA.CDFILIAL
                  AND TP.CDTPCTPAGAR = C_DESPCOMPRA.CDTPCTPAGAR;
            EXCEPTION
               WHEN NO_DATA_FOUND THEN
                  V_CDTPCTPAGAR := 'NAO';
            END;
         ELSE
            V_CDTPCTPAGAR := 'NAO';
         END IF;

         IF V_CDTPCTPAGAR = 'NAO' THEN
            FOR C_ITDESPCOMPRA IN (SELECT DISTINCT I.CDPRODUTO, I.NRSEQITPROD, I.VRBRUTITEM,I.QTITEMPROD AS QUANTIDADE,
                                          SUBSTR(P.CDARVPROD,1,1) NIVEL_INICIAL, P.CDARVPROD
                                     FROM  ITEMPRODUTO I, PRODUTO P, PRODFILI F,
                                          (SELECT DISTINCT A.CDPRODUTO AS CDPRODUTO
                                             FROM PRODUTOROP R, PRODUTO A, PRODUTO B
                                            WHERE (INSTR(A.CDARVPROD,B.CDARVPROD) = 1)
                                              AND R.CDPRODUTO = B.CDPRODUTO
                                              AND R.CDFILIAL = P_FILIALPAR
                                              AND IDTPPRODUTOROP = 'DE') A
                                    WHERE I.CDFILIAL = C_DESPCOMPRA.CDFILIAL
                                      AND I.NRLANCTONF = C_DESPCOMPRA.NRLANCTONF
                                      AND P.CDPRODUTO = I.CDPRODUTO
                                      AND F.CDFILIAL = P_CDFILIAL
                                      AND F.CDPRODUTO = P.CDPRODESTO
                                      AND (NOT EXISTS (SELECT PPR.CDFILIAL
                                                         FROM PRODUTOROP PPR, PRODUTO PP
                                                        WHERE PPR.IDTPPRODUTOROP = 'CO'
                                                          AND PPR.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
                                                          AND PPR.CDPRODUTO = PP.CDPRODUTO
                                                          AND P.CDARVPROD LIKE PP.CDARVPROD||'%')
                                            OR EXISTS (SELECT PPR.CDFILIAL
                                                         FROM PRODUTOROP PPR, PRODUTO PP
                                                        WHERE PPR.IDTPPRODUTOROP = 'OX'
                                                          AND PPR.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
                                                          AND PPR.CDPRODUTO = PP.CDPRODUTO
                                                          AND P.CDARVPROD LIKE PP.CDARVPROD||'%'))
                                      AND I.CDPRODUTO = A.CDPRODUTO) LOOP

               IF (V_IDAPUCTPGROP = 'S') AND (V_IDAPUMOCTPGROP = 'S') THEN
                  BEGIN
                     SELECT NVL(CDTPCTPAGAR, 'NAO')
                       INTO V_CDTPCTPAGAR
                       FROM (SELECT P.CDTPCTPAGAR, P.CDNVPRODUTO
                               FROM PRODUTO P, TIPOCTPAGROP TP
                              WHERE P.CDTPCTPAGAR = TP.CDTPCTPAGAR
                                AND P.CDARVPROD IN (SELECT MAX(T.CDARVPROD)
                                                      FROM PRODUTO T
                                                     WHERE T.CDARVPROD BETWEEN C_ITDESPCOMPRA.NIVEL_INICIAL AND C_ITDESPCOMPRA.CDARVPROD
                                                     GROUP BY T.CDNVPRODUTO)
                                AND TP.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
                              ORDER BY P.CDNVPRODUTO DESC)
                      WHERE ROWNUM = 1;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN
                        V_CDTPCTPAGAR := 'NAO';
                  END;
               END IF;

               IF V_CDTPCTPAGAR = 'NAO' THEN
                  V_VRRATENF := 0;
                  IF(V_IDCONSPERRATFIL = 'S') THEN
                     BEGIN
                        SELECT TRUNC(ROUND(NVL(SUM(R.VRPERRAT),0),6),2)
                          INTO V_VRPEROTHERSRAT
                          FROM RATEIONF R, FILIAL F, CENTCUST C
                         WHERE (R.CDFILIAL    = C_DESPCOMPRA.CDFILIAL) AND
                               (R.NRLANCTONF  = C_DESPCOMPRA.NRLANCTONF)  AND
                               (R.NRSEQITPROD = C_ITDESPCOMPRA.NRSEQITPROD) AND
                               (R.CDFILICCUST = F.CDFILIAL)   AND
                               (R.CDCENTCUSNF = C.CDCENTCUST) AND
                               (R.CDFILICCUST  <> P_CDFILIAL)
                         ORDER BY R.CDFILICCUST, R.CDCENTCUSNF;
                     EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_VRPEROTHERSRAT :=  0;
                     END;

                     IF(V_VRPEROTHERSRAT < 100 )THEN
                        BEGIN
                           --SELECT NVL(SUM(DECODE(R.VRPERRAT,100,C_ITDESPCOMPRA.VRBRUTITEM,NVL(R.VRRATENF,C_ITDESPCOMPRA.VRBRUTITEM))),C_ITDESPCOMPRA.VRBRUTITEM) AS VRRATENF
                           SELECT NVL(SUM(DECODE(R.VRPERRAT,100,C_ITDESPCOMPRA.VRBRUTITEM,NVL(C_ITDESPCOMPRA.VRBRUTITEM*R.VRPERRAT/100,C_ITDESPCOMPRA.VRBRUTITEM))),0) AS VRRATENF
                             INTO V_VRRATENF
                             FROM RATEIONF R
                            WHERE R.CDFILICCUST = P_CDFILIAL
                              AND R.CDFILIAL    = C_DESPCOMPRA.CDFILIAL
                              AND R.NRLANCTONF  = C_DESPCOMPRA.NRLANCTONF
                              AND R.NRSEQITPROD = C_ITDESPCOMPRA.NRSEQITPROD
                              AND R.VRRATENF > 0;
                        EXCEPTION
                           WHEN NO_DATA_FOUND THEN
                              V_VRRATENF := 0 ;
                        END;
                     ELSE
                        V_VRRATENF := 0;
                     END IF;
                  ELSE
                     V_VRRATENF := C_ITDESPCOMPRA.VRBRUTITEM;
                  END IF;

                  BEGIN
                     SELECT CASE WHEN COUNT(DISTINCT R.CDPRODUTO) = 0
                                 THEN 'NAO'
                                 ELSE TO_CHAR(COUNT(DISTINCT R.CDPRODUTO))
                            END AS EXCECAO
                       INTO V_EXCECAO
                       FROM PRODUTOROP R INNER JOIN PRODUTO B ON R.CDPRODUTO = B.CDPRODUTO
                                         INNER JOIN PRODUTO A ON A.CDPRODUTO = C_ITDESPCOMPRA.CDPRODUTO
                                                             AND A.CDPRODUTO LIKE B.CDPRODUTO||'%'
                      WHERE R.CDFILIAL = P_FILIALPAR
                        AND R.IDTPPRODUTOROP = 'EX';
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_EXCECAO := 'NAO';
                     END;
                  END;

                  V_TIPOCONTAPAGAR := NULL;
                  BEGIN
                     SELECT NVL(CDTPCTPAGAR,C_DESPCOMPRA.CDTPCTPAGAR)
                       INTO V_TIPOCONTAPAGAR
                       FROM (SELECT B.CDARVPROD, B.CDTPCTPAGAR
                               FROM PRODUTO P, PRODUTO B
                              WHERE P.CDPRODUTO = C_ITDESPCOMPRA.CDPRODUTO
                                AND P.CDARVPROD LIKE B.CDARVPROD||'%'
                                AND B.CDTPCTPAGAR IS NOT NULL
                              ORDER BY B.CDARVPROD DESC)
                      WHERE ROWNUM = 1;
                  EXCEPTION
                     WHEN NO_DATA_FOUND THEN BEGIN
                        V_TIPOCONTAPAGAR := NULL;
                     END;
                  END;

                  IF V_EXCECAO = 'NAO' THEN
                     SELECT NVL(MAX(C.CDTPCTPAGAR),'NAO')
                       INTO V_EXCECAO
                       FROM CONTAPAGAR C INNER JOIN TIPOCTPAGROP TP ON C.CDTPCTPAGAR = TP.CDTPCTPAGAR
                                                                   AND TIPOPARAM_DRO(NVL(C.CDFIMOVFLPAG,C.CDFILIAL)) = TP.CDFILIAL
                      WHERE C.CDFILIAL = C_DESPCOMPRA.CDFILIAL
                        AND C.NRLANCTONF = C_DESPCOMPRA.NRLANCTONF;

                     IF V_EXCECAO = 'NAO' THEN
                        IF V_VRRATENF > 0 THEN
                           NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

                           INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                                    DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                                    CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                                    CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                                    DTFINREFEFAT, NRSEQITPROD, CDPRODUTO)
                                VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'DESPESA', 'LANC_ENTRADA', V_CDFILCENTPME,
                                        C_DESPCOMPRA.DTENTRSAID, SYSDATE, V_TIPOCONTAPAGAR, V_VRRATENF, C_ITDESPCOMPRA.QUANTIDADE,
                                        C_DESPCOMPRA.CDFILIAL, C_DESPCOMPRA.NRLANCTONF, NULL, NULL, C_DESPCOMPRA.CDFORNECED,
                                        C_DESPCOMPRA.CDTIPOOPER, C_DESPCOMPRA.NRNOTAFISC, C_DESPCOMPRA.CDSERIE, C_DESPCOMPRA.IDSTATUS, C_DESPCOMPRA.DTINIREFEFAT,
                                        C_DESPCOMPRA.DTFINREFEFAT, C_ITDESPCOMPRA.NRSEQITPROD, C_ITDESPCOMPRA.CDPRODUTO); /* DE */
                        END IF;
                     END IF;
                  END IF;
               END IF;
            END LOOP;
         END IF;
      END LOOP;
   END IF;

   IF (V_IDCONSLANCAJUSPAG = 'S') AND  (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      FOR C_AJUSPAG IN (SELECT CASE WHEN NVL(CP.VRATUAPAG,0) = 0
                                    THEN 0
                                    ELSE (R.VRRATCCCTPAG/CP.VRATUAPAG)
                               END * CASE WHEN T.IDAJUCTPAG = 'D' THEN AJ.VRAJUSTEPAG*(-1)
                                                                  ELSE AJ.VRAJUSTEPAG
                                     END VRAJUSTEPAG, AJ.DTAJUSTEPAG, TP.CDFILIAL, NF.NRNOTAFISC, NF.CDFORNECED, NF.NRLANCTONF,
                               NF.CDSERIE, NF.IDSTATUS, T.CDAJUCTPAG, CP.CDTPCTPAGAR
                          FROM TIPOAJUSPAGROP TP INNER JOIN TIPOAJPAGAR T ON TP.CDAJUCTPAG = T.CDAJUCTPAG
                                                 INNER JOIN AJCTPAGAR AJ ON --AJ.CDFIMFAJCNTP = P_CDFILIAL
                                                                        --AND
                                                                        AJ.CDAJUCTPAG = TP.CDAJUCTPAG
                                                 INNER JOIN CONTAPAGAR CP ON CP.CDFILIAL = AJ.CDFILIAL
                                                                         AND CP.NRLANCTONF = AJ.NRLANCTONF
                                                                         AND CP.NRSEQVENPAG = AJ.NRSEQVENPAG
                                                                         AND CP.NRPARCELAPAG = AJ.NRPARCELAPAG
                                                 INNER JOIN NOTAFISCAL NF ON NF.CDFILIAL = AJ.CDFILIAL
                                                                         AND NF.NRLANCTONF = AJ.NRLANCTONF
                                                 LEFT  JOIN RATCTPAGAR R ON R.CDFILIAL = CP.CDFILIAL
                                                                        AND R.NRLANCTONF = CP.NRLANCTONF
                                                                        AND R.NRSEQVENPAG = CP.NRSEQVENPAG
                                                                        AND R.NRPARCELAPAG = CP.NRPARCELAPAG
                                                                       --AND R.CDFIRATCTPAG = AJ.CDFIMFAJCNTP
                         WHERE TP.CDFILIAL = P_FILIALPAR
                           AND TRUNC(AJ.DTAJUSTEPAG) = P_DTINICIAL
                           AND NVL(R.CDFIRATCTPAG, AJ.CDFIMFAJCNTP) = P_CDFILIAL) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO, CDAJUSTECT)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'AJUSTECT', 'CONTA_PAGAR', V_CDFILCENTPME,
                      C_AJUSPAG.DTAJUSTEPAG, SYSDATE, C_AJUSPAG.CDTPCTPAGAR, C_AJUSPAG.VRAJUSTEPAG, 1,
                      C_AJUSPAG.CDFILIAL, C_AJUSPAG.NRLANCTONF, NULL, NULL, C_AJUSPAG.CDFORNECED,
                      NULL, C_AJUSPAG.NRNOTAFISC, C_AJUSPAG.CDSERIE, C_AJUSPAG.IDSTATUS, NULL,
                      NULL, NULL, NULL, C_AJUSPAG.CDAJUCTPAG);

      END LOOP;

      FOR C_AJUSPAGBOL IN (SELECT AJ.CDFILIAL, AJ.NRLANBOLPAG, AJ.CDAJUCTPAG,
                                  CASE WHEN NVL(B.VRBOLPAG,0) = 0
                                       THEN 0
                                       ELSE (SUM(R.VRRATCCCTPAG)/B.VRBOLPAG) 
                                  END * CASE WHEN T.IDAJUCTPAG = 'D' 
                                             THEN AJ.VRAJBOLPAG*(-1)
                                             ELSE AJ.VRAJBOLPAG
                                        END VRAJUSTEPAG,
                                  BX.DTBAIXAPAG DTAJUSTEPAG, B.VRBOLPAG, SUM(R.VRRATCCCTPAG) VRRATCCCTPAG, 
                                  CASE WHEN NVL(B.VRBOLPAG,0) = 0
                                       THEN 0
                                       ELSE (SUM(R.VRRATCCCTPAG)/B.VRBOLPAG)
                                  END PERC,
                                  B.CDFORNECED, B.NRPARCBOLPAG
                             FROM TIPOAJUSPAGROP TP INNER JOIN TIPOAJPAGAR T ON TP.CDAJUCTPAG = T.CDAJUCTPAG
                                                    INNER JOIN AJBOLPAG AJ ON AJ.CDAJUCTPAG = TP.CDAJUCTPAG
                                                    INNER JOIN BOLETPAGAR B ON AJ.CDFILIAL = B.CDFILIAL
                                                                           AND AJ.NRLANBOLPAG = B.NRLANBOLPAG
                                                    INNER JOIN BAIXAPAGA BX ON B.CDFILIBAIPAG = BX.CDFILIAL
                                                                           AND B.NRBAIXAPAG = BX.NRBAIXAPAG
                                                    INNER JOIN CONTAPAGAR C ON C.CDFILIBPAG = B.CDFILIAL
                                                                           AND C.NRLANBOLPAG = B.NRLANBOLPAG
                                                    LEFT  JOIN RATCTPAGAR R ON R.CDFILIAL = C.CDFILIAL
                                                                           AND R.NRLANCTONF = C.NRLANCTONF
                                                                           AND R.NRSEQVENPAG = C.NRSEQVENPAG
                                                                           AND R.NRPARCELAPAG = C.NRPARCELAPAG
                            WHERE TP.CDFILIAL = P_FILIALPAR
                              AND TRUNC(BX.DTBAIXAPAG) = P_DTINICIAL
                              AND B.IDSTATBPAG = 'B'
                              AND R.CDFIRATCTPAG = P_CDFILIAL
                         GROUP BY AJ.CDFILIAL, AJ.NRLANBOLPAG, AJ.CDAJUCTPAG, T.IDAJUCTPAG, BX.DTBAIXAPAG, T.CDAJUCTPAG, AJ.VRAJBOLPAG, B.VRBOLPAG,
                                  B.CDFORNECED, B.NRPARCBOLPAG) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO, CDAJUSTECT)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'AJUSTECT', 'CONTA_PAGAR', V_CDFILCENTPME,
                      C_AJUSPAGBOL.DTAJUSTEPAG, SYSDATE, NULL, C_AJUSPAGBOL.VRAJUSTEPAG, 1,
                      C_AJUSPAGBOL.CDFILIAL, NULL, NULL, NULL, C_AJUSPAGBOL.CDFORNECED,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, C_AJUSPAGBOL.CDAJUCTPAG);
      END LOOP;
   END IF;

   IF (V_IDCONSLANCAJUSREC = 'S') AND (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      FOR C_AJUSREC IN (SELECT CASE WHEN NVL(C.VRATUAREC,0) = 0
                                    THEN 0
                                    ELSE (R.VRRATCCCTREC/C.VRATUAREC)
                               END * CASE WHEN T.IDAJUCTREC = 'D' THEN AJ.VRAJUSTEREC*(-1)
                                                                   ELSE AJ.VRAJUSTEREC
                               END * (-1) VRAJUSTEREC, AJ.DTAJUSTEREC, TP.CDFILIAL, NF.NRNOTAFISC, NF.CDCLIENTE, NF.NRLANCTONF,
                               NF.CDSERIE, NF.IDSTATUS, T.CDAJUCTREC, C.CDTPCTRECEB
                          FROM TIPOAJUSRECROP TP INNER JOIN TIPOAJRECEBER T ON TP.CDAJUCTREC = T.CDAJUCTREC
                                                 INNER JOIN AJCTRECEBER AJ ON --AJ.CDFIMFAJCNTR = P_CDFILIAL
                                                                          --AND
                                                                          AJ.CDAJUCTREC = TP.CDAJUCTREC
                                                 INNER JOIN CONTARECEBER C ON C.CDFILIAL = AJ.CDFILIAL
                                                                          AND C.NRLANCTONF = AJ.NRLANCTONF
                                                                          AND C.NRSEQVENREC = AJ.NRSEQVENREC
                                                                          AND C.NRPARCELAREC = AJ.NRPARCELAREC
                                                 INNER JOIN NOTAFISCAL NF ON NF.CDFILIAL = AJ.CDFILIAL
                                                                         AND NF.NRLANCTONF = AJ.NRLANCTONF
                                                 LEFT  JOIN RATCTRECEBER R ON R.CDFILIAL = C.CDFILIAL
                                                                          AND R.NRLANCTONF = C.NRLANCTONF
                                                                          AND R.NRSEQVENREC = C.NRSEQVENREC
                                                                          AND R.NRPARCELAREC = C.NRPARCELAREC
                                                                          --AND R.CDFIRATCTREC = AJ.CDFIMFAJCNTR
                         WHERE TP.CDFILIAL = P_FILIALPAR
                           AND TRUNC(AJ.DTAJUSTEREC) = P_DTINICIAL
                           AND NVL(R.CDFIRATCTREC, AJ.CDFIMFAJCNTR) = P_CDFILIAL) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDCLIENTE,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO, CDAJUSTECT)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'AJUSTECT', 'CONTA_RECEBER', V_CDFILCENTPME,
                      C_AJUSREC.DTAJUSTEREC, SYSDATE, C_AJUSREC.CDTPCTRECEB, C_AJUSREC.VRAJUSTEREC, 1,
                      C_AJUSREC.CDFILIAL, C_AJUSREC.NRLANCTONF, NULL, NULL, C_AJUSREC.CDCLIENTE,
                      NULL, C_AJUSREC.NRNOTAFISC, C_AJUSREC.CDSERIE, C_AJUSREC.IDSTATUS, NULL,
                      NULL, NULL, NULL, C_AJUSREC.CDAJUCTREC);

      END LOOP;

      FOR C_AJUSRECBOL IN (SELECT AJ.CDFILIAL, AJ.NRLANBOLREC, AJ.CDAJUCTREC,
                                  CASE WHEN NVL(B.VRBOLREC,0) = 0
                                       THEN 0
                                       ELSE (SUM(R.VRRATCCCTREC)/B.VRBOLREC)
                                  END * CASE WHEN T.IDAJUCTREC = 'D' THEN AJ.VRAJBOLREC*(-1)
                                                                     ELSE AJ.VRAJBOLREC
                                        END * (-1) VRAJUSTEREC,
                                  BX.DTBAIXAREC DTAJUSTEREC, B.VRBOLREC, SUM(R.VRRATCCCTREC) VRRATCCCTREC, 
                                  CASE WHEN NVL(B.VRBOLREC,0) = 0
                                       THEN 0
                                       ELSE (SUM(R.VRRATCCCTREC)/B.VRBOLREC)
                                  END PERC,
                                  B.CDCLIENTE, B.NRPARCBOLREC
                             FROM TIPOAJUSRECROP TP INNER JOIN TIPOAJRECEBER T ON TP.CDAJUCTREC = T.CDAJUCTREC
                                                    INNER JOIN AJBOLREC AJ ON AJ.CDAJUCTREC = TP.CDAJUCTREC
                                                    INNER JOIN BOLETRECEBER B ON AJ.CDFILIAL = B.CDFILIAL
                                                                             AND AJ.NRLANBOLREC = B.NRLANBOLREC
                                                    INNER JOIN BAIXARECEBIDA BX ON B.CDFILIBAIREC = BX.CDFILIAL
                                                                               AND B.NRBAIXAREC = BX.NRBAIXAREC
                                                    INNER JOIN CONTARECEBER C ON C.CDFILIBOLREC = B.CDFILIAL
                                                                             AND C.NRLANBOLREC = B.NRLANBOLREC
                                                    INNER JOIN NOTAFISCAL NF ON NF.CDFILIAL = C.CDFILIAL
                                                                            AND NF.NRLANCTONF = C.NRLANCTONF
                                                    LEFT  JOIN RATCTRECEBER R ON R.CDFILIAL = C.CDFILIAL
                                                                             AND R.NRLANCTONF = C.NRLANCTONF
                                                                             AND R.NRSEQVENREC = C.NRSEQVENREC
                                                                             AND R.NRPARCELAREC = C.NRPARCELAREC
                            WHERE TP.CDFILIAL = P_FILIALPAR
                              AND TRUNC(BX.DTBAIXAREC) = P_DTINICIAL
                              AND B.IDSTATBREC = 'B'
                              AND R.CDFIRATCTREC = P_CDFILIAL
                            GROUP BY AJ.CDFILIAL, AJ.NRLANBOLREC, AJ.CDAJUCTREC, T.IDAJUCTREC, BX.DTBAIXAREC, T.CDAJUCTREC, AJ.VRAJBOLREC, B.VRBOLREC,
                                  B.CDCLIENTE, B.NRPARCBOLREC) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDCLIENTE,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO, CDAJUSTECT)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'AJUSTECT', 'CONTA_RECEBER', V_CDFILCENTPME,
                      C_AJUSRECBOL.DTAJUSTEREC, SYSDATE, NULL, C_AJUSRECBOL.VRAJUSTEREC, 1,
                      C_AJUSRECBOL.CDFILIAL, NULL, NULL, NULL, C_AJUSRECBOL.CDCLIENTE,
                      NULL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, C_AJUSRECBOL.CDAJUCTREC);
      END LOOP;
   END IF;

   IF (V_IDCONSDEBBCO = 'S') AND (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      FOR C_AJUSDEBBCO IN (SELECT M.DTMOVIDEBI, M.VRMOVIDEBI VRMOVIDEBI, M.CDFORNECED, L.CDTPDEBIBANC, M.CDFILIAL,
                                  M.CDCLIENTE, MB.NRDOCMBANC
                             FROM LANCDEBBCOROP L INNER JOIN MOVDEBBANC M ON L.CDTPDEBIBANC = M.CDTPDEBIBANC
                                                  INNER JOIN MOVBANCARIO MB ON MB.CDREDUCONT = M.CDREDUCONT
                                                                           AND MB.NRMOVBANC = M.NRMOVBANC
                            WHERE L.CDFILIAL = P_FILIALPAR
                              AND M.CDFILIAL = P_CDFILIAL
                              AND TRUNC(M.DTMOVIDEBI) = P_DTINICIAL
                              AND MB.IDORIGMBANC = 'D') LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO, CDTPCREDDEBBCO, CDCLIENTE)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'AJUSTECT', 'DEBITO', V_CDFILCENTPME,
                      C_AJUSDEBBCO.DTMOVIDEBI, SYSDATE, NULL, C_AJUSDEBBCO.VRMOVIDEBI, 1,
                      C_AJUSDEBBCO.CDFILIAL, NULL, NULL, NULL, C_AJUSDEBBCO.CDFORNECED,
                      NULL, C_AJUSDEBBCO.NRDOCMBANC, NULL, NULL, NULL,
                      NULL, NULL, NULL, C_AJUSDEBBCO.CDTPDEBIBANC, C_AJUSDEBBCO.CDCLIENTE);
      END LOOP;
   END IF;

   IF (V_IDCONSCREDBCO = 'S') AND (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      FOR C_AJUSCREBCO IN (SELECT M.DTMOVICRED, M.VRMOVICRED*(-1) VRMOVICRED, M.CDFORNECED, L.CDTPCREDBANC, M.CDFILIAL,
                                  M.CDCLIENTE, MB.NRDOCMBANC
                             FROM LANCCREDBCOROP L INNER JOIN MOVCREDBANC M ON L.CDTPCREDBANC = M.CDTPCREDBANC
                                                   INNER JOIN MOVBANCARIO MB ON MB.CDREDUCONT = M.CDREDUCONT
                                                                            AND MB.NRMOVBANC = M.NRMOVBANC
                            WHERE L.CDFILIAL = P_FILIALPAR
                              AND M.CDFILIAL = P_CDFILIAL
                              AND TRUNC(M.DTMOVICRED) = P_DTINICIAL
                              AND MB.IDORIGMBANC = 'C') LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO, CDTPCREDDEBBCO, CDCLIENTE)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'AJUSTECT', 'CREDITO', V_CDFILCENTPME,
                      C_AJUSCREBCO.DTMOVICRED, SYSDATE, NULL, C_AJUSCREBCO.VRMOVICRED, 1,
                      C_AJUSCREBCO.CDFILIAL, NULL, NULL, NULL, C_AJUSCREBCO.CDFORNECED,
                      NULL, C_AJUSCREBCO.NRDOCMBANC, NULL, NULL, NULL,
                      NULL, NULL, NULL, C_AJUSCREBCO.CDTPCREDBANC, C_AJUSCREBCO.CDCLIENTE);
      END LOOP;
   END IF;

   IF (V_IDLANCIMPPAG = 'S') AND (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      FOR C_IMPOSTO IN (SELECT CP.IDSTATPAG, CP.CDFILIBPAG, CP.CDFILIAL,CP.NRLANCTONF,CP.NRSEQVENPAG, CP.NRPARCELAPAG, TC.CDTPCTPAGAR,
                               DECODE(V_IDDTAPUCTPAGAR,'ENTRADA',NF.DTENTRSAID, DECODE(V_IDDTAPUCTPAGAR,'VENCIMENTO',CP.DTATUAVENPAG,DTBAIXAPAG))DTBAIXAPAG,
                               --NVL(VRRATCCCTPAG,CP.VRATUAPAG)
                               CASE WHEN CP.VRATUAPAG = 0 THEN 0 ELSE (NVL(VRRATCCCTPAG,CP.VRATUAPAG)/CP.VRATUAPAG)*CP.VRORIGPAG END AS VRCONTARATEADA, NF.NRNOTAFISC, NF.CDFORNECED,
                               T.DSTPCTPAGAR
                          FROM CONTAPAGAR CP, NOTAFISCAL NF, BOLETPAGAR BO, BAIXAPAGA BP, TIPOPAGIMPROP TC, RATCTPAGAR R, TIPOCTPAGAR T
                         WHERE CP.CDFILIBPAG     = BO.CDFILIAL
                           AND CP.NRLANBOLPAG    = BO.NRLANBOLPAG
                           AND BO.CDFILIBAIPAG   = BP.CDFILIAL(+)
                           AND BO.NRBAIXAPAG     = BP.NRBAIXAPAG(+)
                           AND ((('ENTRADA'      = V_IDDTAPUCTPAGAR) AND (TRUNC(NF.DTENTRSAID)    = P_DTINICIAL)) OR
                               (('VENCIMENTO'    = V_IDDTAPUCTPAGAR) AND (TRUNC(CP.DTATUAVENPAG)  = P_DTINICIAL)) OR
                               (('BAIXA'         = V_IDDTAPUCTPAGAR) AND (TRUNC(BP.DTBAIXAPAG)    = P_DTINICIAL)))
                           AND TC.CDFILIAL       = P_FILIALPAR
                           AND TC.CDTPCTPAGAR    = CP.CDTPCTPAGAR
                           AND T.CDTPCTPAGAR     = CP.CDTPCTPAGAR
                           AND NF.CDFILIAL       = CP.CDFILIAL
                           AND NF.NRLANCTONF     = CP.NRLANCTONF
                           AND R.CDFILIAL(+)     = CP.CDFILIAL
                           AND R.NRLANCTONF(+)   = CP.NRLANCTONF
                           AND R.NRSEQVENPAG(+)  = CP.NRSEQVENPAG
                           AND R.NRPARCELAPAG(+) = CP.NRPARCELAPAG
                           AND ((CDFIRATCTPAG    = P_CDFILIAL) OR (CDFIRATCTPAG IS NULL) AND (NF.CDFILIRECE = P_CDFILIAL))) LOOP

               NOVO_CODIGO('GESDEDUCAOROP', V_NRSEQDEDUROP);

               INSERT INTO DEDUCAOROP
                         (NRFILIALROP, NRDEDUCAOROP, CDFILIAL, DTREFERENC, NRNOTAFISC,
                          CDTPCTPAGAR, CDENCARGO, DSDEDUCAO, CDCLIENTE, CDFILIALBENE,
                          CDFORNECED, DSBENEFICIARIO, CDFILIALLANC, NRLANCTONF, NRRECEXTRAFILI,
                          IDTPLANCROP, CDFILIALCENT, VRDEDUCAO, VRPERFIXCALCIMP)
                    VALUES
                         (P_NRROPFILIAL, V_NRSEQDEDUROP, P_CDFILIAL, P_DTINICIAL, C_IMPOSTO.NRNOTAFISC,
                          C_IMPOSTO.CDTPCTPAGAR, NULL, C_IMPOSTO.DSTPCTPAGAR, NULL, NULL,
                          NULL, NULL, C_IMPOSTO.CDFILIAL, C_IMPOSTO.NRLANCTONF, NULL,
                          'CONTA_PAGAR', V_CDFILCENTPME, C_IMPOSTO.VRCONTARATEADA, NULL);
      END LOOP;
   END IF;

   IF (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      SELECT NVL(MAX(NVL(F.NRDIAINIORCAGOP,P.NRDIAINIORCA)),1)
        INTO V_NRDIAINIORCA
        FROM PARAMORCAOPERAC P LEFT JOIN FILIAL F ON F.CDFILIAL = P_CDFILIAL
       WHERE P.CDPARORCAOPERAC = '00001';

      V_MODMESTAXA := 0;
      IF V_NRDIAINIORCA > TO_NUMBER(TO_CHAR(P_DTINICIAL,'DD')) THEN
         V_MODMESTAXA := -1;
      END IF;

      SELECT SUM(CASE TO_CHAR(ADD_MONTHS(P_DTINICIAL,V_MODMESTAXA),'MM') WHEN '01' THEN TF.VRMES01
                                                                         WHEN '02' THEN TF.VRMES02
                                                                         WHEN '03' THEN TF.VRMES03
                                                                         WHEN '04' THEN TF.VRMES04
                                                                         WHEN '05' THEN TF.VRMES05
                                                                         WHEN '06' THEN TF.VRMES06
                                                                         WHEN '07' THEN TF.VRMES07
                                                                         WHEN '08' THEN TF.VRMES08
                                                                         WHEN '09' THEN TF.VRMES09
                                                                         WHEN '10' THEN TF.VRMES10
                                                                         WHEN '11' THEN TF.VRMES11
                                                                         WHEN '12' THEN TF.VRMES12
                                                                         ELSE 0
                 END)
        INTO V_VRPERCTAXAADMIN
        FROM TAXAADMFILGES TF
       WHERE TF.CDFILIAL = P_CDFILIAL
         AND TF.CDANO = TO_NUMBER(TO_CHAR(ADD_MONTHS(P_DTINICIAL,V_MODMESTAXA),'YYYY'));

      IF V_VRPERCTAXAADMIN IS NULL THEN
         SELECT SUM(CASE TO_CHAR(ADD_MONTHS(P_DTINICIAL,V_MODMESTAXA),'MM') WHEN '01' THEN TF.VRMES01
                                                                            WHEN '02' THEN TF.VRMES02
                                                                            WHEN '03' THEN TF.VRMES03
                                                                            WHEN '04' THEN TF.VRMES04
                                                                            WHEN '05' THEN TF.VRMES05
                                                                            WHEN '06' THEN TF.VRMES06
                                                                            WHEN '07' THEN TF.VRMES07
                                                                            WHEN '08' THEN TF.VRMES08
                                                                            WHEN '09' THEN TF.VRMES09
                                                                            WHEN '10' THEN TF.VRMES10
                                                                            WHEN '11' THEN TF.VRMES11
                                                                            WHEN '12' THEN TF.VRMES12
                                                                            ELSE 0
                    END)
           INTO V_VRPERCTAXAADMIN
           FROM TAXAADMREGGES TF INNER JOIN FILIAL F ON TF.CDAREAGERE = F.CDAREAGERE
          WHERE F.CDFILIAL = P_CDFILIAL
            AND TF.CDANO = TO_NUMBER(TO_CHAR(ADD_MONTHS(P_DTINICIAL,V_MODMESTAXA),'YYYY'));
      END IF;

      IF V_VRPERCTAXAADMIN IS NULL THEN
         SELECT NVL(SUM(TAXAADMINMATRIZ),0)
           INTO V_VRPERCTAXAADMIN
           FROM PARAMGESTAO
          WHERE CDPARAMGESTAO = '0001';
      END IF;

      /* Faturamento */
      V_FATEFETIVO := 0;
      IF (V_IDFATUROP = 'FE') OR (V_IDFATUROP = 'AB') THEN
         SELECT NVL(SUM(F.VRFATURAMENTO),0)
           INTO V_FATEFETIVO
           FROM FATURAMENTOROP F INNER JOIN SERVICO S ON F.CDFILIAL = S.CDFILIAL
                                                     AND F.CDSERVFILI = S.CDSERVFILI
          WHERE F.IDTPLANCROP = 'FAT_EFETIVO'
            AND F.CDFILIAL = P_CDFILIAL
            AND TRUNC(F.DTENTRSAID) = P_DTINICIAL;
      END IF;

      V_FATNOTAFISC := 0;
      IF V_IDFATUROP = 'FS' THEN
         SELECT NVL(SUM(F.VRFATURAMENTO),0)
           INTO V_FATNOTAFISC
           FROM FATURAMENTOROP F
          WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
            AND F.CDFILIAL = P_CDFILIAL
            AND CASE WHEN V_IDDTAPURFAT = 'R'
                     THEN TRUNC(F.DTREFERENC)
                     ELSE TRUNC(F.DTENTRSAID)
                END = P_DTINICIAL;
      END IF;

      IF V_IDFATUROP = 'AB' THEN
         SELECT NVL(SUM(F.VRFATURAMENTO),0)
           INTO V_FATNOTAFISC
           FROM FATURAMENTOROP F
          WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
            AND F.CDFILIAL = P_CDFILIAL
            AND CASE WHEN V_IDDTAPURFAT = 'R'
                     THEN TRUNC(F.DTREFERENC)
                     ELSE TRUNC(F.DTENTRSAID)
                END = P_DTINICIAL
            AND F.NRAPURACFAT IS NULL;
      END IF;

      IF V_IDVOLUNEGOCIO = 'S' THEN
         SELECT NVL(SUM(F.VRFATURAMENTO),0)
           INTO V_FATRECEXTRA
           FROM FATURAMENTOROP F
          WHERE F.IDTPLANCROP = 'RECEITA_EXTRA'
            AND F.CDFILIAL = P_CDFILIAL
            AND TRUNC(F.DTENTRSAID) = P_DTINICIAL;
      ELSE
         V_FATRECEXTRA := 0;
      END IF;

      IF V_IDCONSFATUDIV = 'S' THEN
         SELECT NVL(SUM(F.VRFATURAMENTO),0)
           INTO V_FATDIVERSO
           FROM FATURAMENTOROP F
          WHERE F.IDTPLANCROP = 'FAT_DIVERSOS'
            AND F.CDFILIAL = P_CDFILIAL
            AND TRUNC(F.DTENTRSAID) = P_DTINICIAL;
      ELSE
         V_FATDIVERSO := 0;
      END IF;

      IF V_IDGERROPFOS = 'S' THEN
         SELECT NVL(SUM(F.VRFATURAMENTO),0) - NVL(SUM(FD.VRFATURAMENTO),0)
           INTO V_FATFORSALE
           FROM FATURAMENTOROP F LEFT JOIN FATURAMENTOROP FD ON F.IDTPLANCROP = FD.IDTPLANCROP
                                                            AND F.CDFILIAL = FD.CDFILIAL
                                                            AND F.DTENTRSAID = FD.DTENTRSAID
                                                            AND FD.IDSTATUS = 'V'
          WHERE F.IDTPLANCROP = 'FAT_FORSALE'
            AND F.IDSTATUS = 'B'
            AND F.CDFILIAL = P_CDFILIAL
            AND TRUNC(F.DTENTRSAID) = P_DTINICIAL;
      ELSE
         V_FATFORSALE := 0;
      END IF;

      SELECT NVL(SUM(CASE R.IDTRATIMPROP WHEN 'D' THEN NVL(D.VRDEDUCAO,0)
                                                  WHEN 'V' THEN 0
                                                  ELSE D.VRDEDUCAO*(-1)
                              END),0)
        INTO V_DEDUIMPO
        FROM DEDUCAOROP D INNER JOIN IMPOSTOROP R ON D.CDIMPOSTO = R.CDIMPOSTO
                                                 AND R.CDFILIAL = P_FILIALPAR
                          INNER JOIN PARFILIALROP P ON P.CDFILIAL = P_FILIALPAR
       WHERE D.CDFILIAL = P_CDFILIAL
         AND TRUNC(D.DTREFERENC) = P_DTINICIAL
         AND ((D.IDTPLANCROP = 'FAT_NOTAFISCAL' AND R.IDTPIMPOSTOROP IN ('FS')) OR
              (D.IDTPLANCROP = 'FAT_EFETIVO' AND R.IDTPIMPOSTOROP IN ('FE')))
         AND (R.IDTPIMPOSTOROP = P.IDFATUROP OR P.IDFATUROP = 'AB');

      IF V_IDLANCIMPPAG = 'S' THEN
         SELECT NVL(SUM(D.VRDEDUCAO),0)+V_DEDUIMPO
           INTO V_DEDUIMPO
           FROM DEDUCAOROP D
          WHERE D.CDFILIAL = P_CDFILIAL
            AND TRUNC(D.DTREFERENC) = P_DTINICIAL
            AND D.IDTPLANCROP = 'CONTA_PAGAR';
      END IF;
      IF NVL(V_IDEXIBEIMPOFOS,'N') = 'S' THEN
         IF V_IDPERFIXIMPDRF = 'S' THEN
            SELECT CASE R.IDTRATIMPROP WHEN 'A' THEN -1
                                       WHEN 'D' THEN 1
                                       ELSE 0
                    END * D.VRDEDUCAO
              INTO V_DEDUIMPOFOS
              FROM DEDUCAOROP D, IMPOSTOROP R
             WHERE D.CDFILIAL = P_CDFILIAL
               AND D.IDTPLANCROP = 'FAT_FORSALE'
               AND D.DTREFERENC = P_DTINICIAL
               AND R.CDFILIAL = TIPOPARAM_DRO(P_CDFILIAL)
               AND R.CDIMPOSTO = D.CDIMPOSTO
               AND R.IDTPIMPOSTOROP = 'FN';
         ELSE
            SELECT SUM(D.VRDEDUCAO)
              INTO V_DEDUIMPOFOS
              FROM DEDUCAOROP D
             WHERE D.CDFILIAL = P_CDFILIAL
               AND D.IDTPLANCROP = 'FAT_FORSALE'
               AND D.DTREFERENC = P_DTINICIAL;
         END IF;
      ELSE
         V_DEDUIMPOFOS := 0;
      END IF;
      IF V_IDPERCFIXIMPREC <> 'S' THEN
         SELECT NVL(SUM(CASE R.IDTRATIMPROP WHEN 'D' THEN NVL(D.VRDEDUCAO,0)*(-1)
                                                     ELSE D.VRDEDUCAO
                        END),0)
           INTO V_CREDIMPO
           FROM DEDUCAOROP D INNER JOIN IMPOSTOROP R ON D.CDIMPOSTO = R.CDIMPOSTO
                                                    AND R.CDFILIAL = P_FILIALPAR
                             INNER JOIN IMPOSTO I ON D.CDIMPOSTO = I.CDIMPOSTO,
                PARFILIALROP P
          WHERE D.CDFILIAL = P_CDFILIAL
            AND TRUNC(D.DTREFERENC) = P_DTINICIAL
            AND D.IDTPLANCROP = 'CONSUMO_ENTRADA'
            AND R.IDTPIMPOSTOROP IN ('CC')
            AND P.CDFILIAL = P_FILIALPAR
            AND P.IDPERCFIXIMPREC <> 'S';
      ELSE
         SELECT (NVL(SUM(C.VRITEMLANCROP),0)*NVL(MAX(P.VRPERIMPRECUROP),0))/100
           INTO V_CREDIMPO
           FROM CONSUMOROP C INNER JOIN FILIALROP F ON C.NRFILIALROP = F.NRFILIALROP
                             INNER JOIN FORNECEDOR FO ON FO.CDFORNECED = C.CDCLIENTEROP,
                PARFILIALROP P
          WHERE C.IDTPLANCROP = 'CONSUMO_ENTRADA'
            AND F.CDFILIAL = P_CDFILIAL
            AND F.DTFILIALROP = C.DTCONSUMROP
            AND TRUNC(F.DTFILIALROP) = P_DTINICIAL
            AND C.CDTIPOOPERROP <> '06'
            AND P.CDFILIAL = P_FILIALPAR
            AND P.IDPERCFIXIMPREC = 'S';
      END IF;

      IF V_IDGERROPFOS = 'S' THEN
         SELECT NVL(SUM(F.VRFATURAMENTO),0)
           INTO V_DESCSOBVENDA
           FROM FATURAMENTOROP F
          WHERE F.IDTPLANCROP = 'FAT_FORSALE'
            AND F.IDSTATUS = 'D'
            AND F.CDFILIAL = P_CDFILIAL
            AND TRUNC(F.DTENTRSAID) = P_DTINICIAL;
      ELSE
         V_DESCSOBVENDA := 0;
      END IF;

      SELECT V_DESCSOBVENDA + NVL(SUM(I.VRITEMENCA*(CASE E.IDENCARGO WHEN 'A' THEN (-1)
                                                                     WHEN 'V' THEN 0
                                                                     ELSE 1
                                                    END)),0)
        INTO V_DESCSOBVENDA
        FROM FATURAMENTOROP F INNER JOIN ITEMENCARGO I ON F.CDFILIALLANC = I.CDFILIAL
                                                      AND F.NRLANCTONF = I.NRLANCTONF
                              INNER JOIN DEDURECEITAROP D ON D.CDFILIAL = P_FILIALPAR
                                                         AND D.CDENCARGO = I.CDENCARGO
                              INNER JOIN ENCARGO E ON I.CDENCARGO = E.CDENCARGO
       WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
         AND F.CDFILIAL = P_CDFILIAL
         AND (((TRUNC(F.DTENTRSAID) = P_DTINICIAL) AND (V_IDDTAPURFAT IN ('E', 'S')))
            OR (TRUNC(F.DTREFERENC) = P_DTINICIAL AND V_IDDTAPURFAT = 'R'));

      SELECT NVL(SUM(I.VRITEMENCA*(CASE E.IDENCARGO WHEN 'A' THEN (-1)
                                                    WHEN 'V' THEN 0
                                                    ELSE 1
                                   END)),0)
        INTO V_ENCARGOS
        FROM FATURAMENTOROP F INNER JOIN ITEMENCARGO I ON F.CDFILIALLANC = I.CDFILIAL
                                                      AND F.NRLANCTONF = I.NRLANCTONF
                              INNER JOIN ENCARGO E ON I.CDENCARGO = E.CDENCARGO
       WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
         AND F.CDFILIAL = P_CDFILIAL
         AND (((TRUNC(F.DTENTRSAID) = P_DTINICIAL) AND (V_IDDTAPURFAT IN ('E', 'S')))
            OR (TRUNC(F.DTREFERENC) = P_DTINICIAL AND V_IDDTAPURFAT = 'R'))
         AND NOT EXISTS (SELECT 1
                           FROM DEDURECEITAROP D
                          WHERE D.CDFILIAL = P_FILIALPAR
                            AND D.CDENCARGO = I.CDENCARGO);

      V_FATDEVCANC := 0;
      IF (V_IDFATUROP = 'FS') OR (V_IDFATUROP = 'AB') THEN
         SELECT NVL(SUM(F.VRFATDEVCANC),0)
           INTO V_FATDEVCANC
           FROM FATURAMENTOROP F
          WHERE F.IDTPLANCROP = 'FAT_NOTAFISCAL'
            AND F.CDFILIAL = P_CDFILIAL
            AND (((TRUNC(F.DTENTRSAID) = P_DTINICIAL) AND (V_IDDTAPURFAT IN ('E', 'S')))
               OR (TRUNC(F.DTREFERENC) = P_DTINICIAL AND V_IDDTAPURFAT = 'R'));
      END IF;

      V_BRUTO := V_FATEFETIVO + V_FATNOTAFISC + V_FATRECEXTRA + V_FATFORSALE + V_FATDIVERSO;
      V_LIQUIDO := CASE WHEN V_IDCALCIMPFAT = 'FATBRUTO'
                        THEN V_BRUTO + NVL(V_CREDIMPO,0) - (NVL(V_DEDUIMPO,0) + NVL(V_DEDUIMPOFOS,0) + NVL(V_DESCSOBVENDA,0) + NVL(V_ENCARGOS,0) + NVL(V_FATDEVCANC,0))
                        ELSE V_BRUTO + NVL(V_CREDIMPO,0) - (NVL(V_DESCSOBVENDA,0) + NVL(V_ENCARGOS,0) + NVL(V_FATDEVCANC,0))
                   END;

      V_VRTAXAADMIN := ROUND(CASE WHEN (V_VRPERCTAXAADMIN > 0)
                                  THEN CASE WHEN V_IDTIPOCALCTAXAADM = 'FATURAMENTO_BRUTO'
                                            THEN V_BRUTO
                                            ELSE V_LIQUIDO
                                       END * (V_VRPERCTAXAADMIN / 100)
                                  ELSE 0
                             END,2);

      NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

      INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                               DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                               CDFILPARDESP)
           VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'TAXAADM', NULL, V_CDFILCENTPME,
                   P_DTINICIAL, SYSDATE, NULL, V_VRTAXAADMIN, V_VRPERCTAXAADMIN,
                   P_CDFILIAL);

      FOR C_TAXASADM IN (SELECT A.CDFILIALADM, AU.CDFILIAL,
                                CASE TO_CHAR(P_DTINICIAL, 'MM')
                                   WHEN '01' THEN NVL(AU.VRMES01, AG.VRMES01)
                                   WHEN '02' THEN NVL(AU.VRMES02, AG.VRMES02)
                                   WHEN '03' THEN NVL(AU.VRMES03, AG.VRMES03)
                                   WHEN '04' THEN NVL(AU.VRMES04, AG.VRMES04)
                                   WHEN '05' THEN NVL(AU.VRMES05, AG.VRMES05)
                                   WHEN '06' THEN NVL(AU.VRMES06, AG.VRMES06)
                                   WHEN '07' THEN NVL(AU.VRMES07, AG.VRMES07)
                                   WHEN '08' THEN NVL(AU.VRMES08, AG.VRMES08)
                                   WHEN '09' THEN NVL(AU.VRMES09, AG.VRMES09)
                                   WHEN '10' THEN NVL(AU.VRMES10, AG.VRMES10)
                                   WHEN '11' THEN NVL(AU.VRMES11, AG.VRMES11)
                                   WHEN '12' THEN NVL(AU.VRMES12, AG.VRMES12)
                                END/100 VRPERCENTUAL, A.NRORG
                           FROM (SELECT DISTINCT A.CDFILIALADM, A.NRORG
                                   FROM ARRECADAADMGER A
                                  WHERE A.IDATIVO = 'S') A LEFT JOIN ARRECADAADMUNI AU 
                                                                  ON AU.CDFILIALADM = A.CDFILIALADM
                                                                 AND AU.CDANO = TO_NUMBER(TO_CHAR(P_DTINICIAL, 'YYYY'))
                                                                 AND AU.CDFILIAL = P_CDFILIAL
                                                           LEFT JOIN ARRECADAADMGER AG
                                                                  ON AG.CDANO = TO_NUMBER(TO_CHAR(P_DTINICIAL, 'YYYY'))
                                                                 AND AG.CDFILIALADM = A.CDFILIALADM
                                                                 AND AG.IDATIVO = 'S') LOOP

         SELECT MAX(F.NRFILIALROP), MAX(FAT.NRFATURAMENTOROP)
           INTO V_NRROPFILIAL_ADM, V_NRSEQFATUROP_ADM
           FROM FILIALROP F LEFT JOIN FATURAMENTOROP FAT ON FAT.CDFILIAL = F.CDFILIAL
                                                        AND FAT.NRFILIALROP = F.NRFILIALROP
                                                        AND FAT.DTENTRSAID = F.DTFILIALROP
                                                        AND FAT.IDTPLANCROP = 'FAT_ARRECADAADM'
          WHERE F.CDFILIAL    = C_TAXASADM.CDFILIALADM
            AND F.DTFILIALROP = P_DTINICIAL;
         
         IF V_NRROPFILIAL_ADM IS NOT NULL THEN
            IF V_NRSEQFATUROP_ADM IS NULL THEN
               NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP_ADM);

               INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                           CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                           CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                           CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                           IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO,
                                           CDLOJA)
               VALUES (V_NRROPFILIAL_ADM, V_NRSEQFATUROP_ADM, C_TAXASADM.CDFILIALADM, P_DTINICIAL, P_DTINICIAL,
                       NULL, NULL, NULL, NULL, NULL,
                       P_CDFILIAL, NULL, NULL, NULL, NULL,
                       NULL, NULL, NULL, 'FAT_ARRECADAADM', V_CDFILCENTPME,
                       NULL, NULL, NULL, V_VRTAXAADMIN * C_TAXASADM.VRPERCENTUAL, 1,
                       NULL);
            ELSE            
               UPDATE FATURAMENTOROP
                  SET VRFATURAMENTO = V_VRTAXAADMIN * C_TAXASADM.VRPERCENTUAL
                WHERE CDFILIAL = C_TAXASADM.CDFILIALADM
                  AND CDFILIALLANC = P_CDFILIAL
                  AND DTENTRSAID = P_DTINICIAL
                  AND IDTPLANCROP = 'FAT_ARRECADAADM';
            END IF;
         END IF;
      END LOOP;
   END IF;

   IF V_IDAPUCTRECROP = 'S' THEN
      FOR C_CREDDESP IN (SELECT CP.IDSTATREC, CP.CDFILIBOLREC, CP.CDFILIAL, R.CDFIRATCTREC, CP.NRLANCTONF,
                                NF.CDFILIRECE, NF.NRNOTAFISC, CP.NRSEQVENREC, CP.NRPARCELAREC, TC.CDTPCTRECEB,
                                DECODE(V_IDDTAPUCTRECROP, 'SAIDA', NF.DTENTRSAID,
                                DECODE(V_IDDTAPUCTRECROP, 'VENCIMENTO', CP.DTATUAVENREC, DTBAIXAREC)) DTBAIXAREC,
                                (SUM(NVL(VRRATCCCTREC, CP.VRATUAREC)))VRRATCCCTREC, VRORIGREC, NF.CDFORNECED,
                                V_IDDTAPUCTRECROP AS IDAPUCTRECROP, NF.CDSERIE, NF.CDCLIENTE
                           FROM CONTARECEBER CP, NOTAFISCAL NF, BOLETRECEBER BO, BAIXARECEBIDA BP,
                                TIPOCTRECROP TC, RATCTRECEBER R
                          WHERE CP.CDFILIBOLREC     = BO.CDFILIAL
                            AND CP.NRLANBOLREC      = BO.NRLANBOLREC
                            AND BO.CDFILIBAIREC     = BP.CDFILIAL(+)
                            AND BO.NRBAIXAREC       = BP.NRBAIXAREC(+)
                            AND ((('SAIDA'             = V_IDDTAPUCTRECROP)
                             AND (TRUNC(NF.DTENTRSAID) = P_DTINICIAL))
                             OR (('VENCIMENTO'        = V_IDDTAPUCTRECROP)
                             AND (TRUNC(CP.DTATUAVENREC) = P_DTINICIAL))
                             OR (('BAIXA'             = V_IDDTAPUCTRECROP)
                             AND (TRUNC(BP.DTBAIXAREC) = P_DTINICIAL)))
                            AND TC.CDFILIAL         = P_FILIALPAR
                            AND TC.CDTPCTRECEB      = CP.CDTPCTRECEB
                            AND TC.IDTPCTRECROP     = 'CRD'
                            AND NF.CDFILIAL         = CP.CDFILIAL
                            AND NF.NRLANCTONF       = CP.NRLANCTONF
                            AND R.CDFILIAL(+)       = CP.CDFILIAL
                            AND R.NRLANCTONF(+)     = CP.NRLANCTONF
                            AND R.NRSEQVENREC(+)    = CP.NRSEQVENREC
                            AND R.NRPARCELAREC(+)   = CP.NRPARCELAREC
                            AND ((CDFIRATCTREC        = P_CDFILIAL)
                             OR (CDFIRATCTREC IS NULL)
                             AND (NF.CDFILIRECE       = P_CDFILIAL))
                          GROUP BY CP.IDSTATREC, CP.CDFILIBOLREC, CP.CDFILIAL, R.CDFIRATCTREC, CP.NRLANCTONF,
                                   NF.CDFILIRECE, NF.NRNOTAFISC, CP.NRSEQVENREC, CP.NRPARCELAREC, TC.CDTPCTRECEB,
                                   DECODE(V_IDDTAPUCTRECROP, 'SAIDA', NF.DTENTRSAID, DECODE(V_IDDTAPUCTRECROP, 'VENCIMENTO', CP.DTATUAVENREC, DTBAIXAREC)),
                                   CP.VRATUAREC, VRORIGREC, NF.CDFORNECED, V_IDDTAPUCTRECROP, NF.CDSERIE, NF.CDCLIENTE
                          ORDER BY CDTPCTRECEB, DTBAIXAREC) LOOP

         NOVO_CODIGO('GESDESPESASROP', V_NRSEQDESPROD);

         INSERT INTO DESPESASROP (NRFILIALROP, NRDESPESASROP, IDTPLANCROP, IDTIPODESP, CDFILIALCENT,
                                  DTDESPESA, DTLANCAMENTO, CDDESPESA, VRDESPESA, QTDESPESA,
                                  CDFILPARDESP, NRLANCTONFROP, NRSEQVENPAGROP, NRPARCELAPAGROP, CDFORNECED,
                                  CDTIPOOPER, NRNOTAFISC, CDSERIE, IDSTATUS, DTINIREFEFAT,
                                  DTFINREFEFAT, NRSEQITPROD, CDPRODUTO, CDTPCREDDEBBCO, CDCLIENTE)
              VALUES (P_NRROPFILIAL, V_NRSEQDESPROD, 'DESPESA', 'CONTA_RECEBER', V_CDFILCENTPME,
                      C_CREDDESP.DTBAIXAREC, SYSDATE, C_CREDDESP.CDTPCTRECEB, C_CREDDESP.VRRATCCCTREC, 1,
                      C_CREDDESP.CDFILIAL, C_CREDDESP.NRLANCTONF, C_CREDDESP.NRSEQVENREC, C_CREDDESP.NRPARCELAREC, NULL,
                      NULL, C_CREDDESP.NRNOTAFISC, C_CREDDESP.CDSERIE, NULL, NULL,
                      NULL, NULL, NULL, NULL, C_CREDDESP.CDCLIENTE);
                --'Reembolso com despesas'
      END LOOP;
   END IF;
END;
/