CREATE OR REPLACE PROCEDURE APURAC_RESOPERFILI(
    P_CDFILIAL    FILIAL.CDFILIAL%TYPE,
    P_DTINICIAL   IN   DATE,
    P_DTFINAL     IN   DATE, 
    P_IDCONSANT   IN   VARCHAR
)IS

    V_CDGRUPPROD                     VARCHAR2(10);
    V_CDGRUPDESP                     VARCHAR2(10);
    V_IDEFETIVOROP                   VARCHAR2(1);
    V_IDCOMPRAROP                    VARCHAR2(1);
    V_IDAJUSTEROP                    VARCHAR2(1);
    V_IDIMPLANTROP                   VARCHAR2(1);
    V_IDINVFINAROP                   VARCHAR2(1);
    V_IDINVINICROP                   VARCHAR2(1);
    V_IDRETIRADAROP                  VARCHAR2(1);
    V_IDDESPDIVROP                   VARCHAR2(1);
    V_IDDESPENTROP                   VARCHAR2(1);
    V_IDDESPMOBROP                   VARCHAR2(1);
    V_IDFATUROP                      VARCHAR2(2);
    V_IDNFSAIDAROP                   VARCHAR2(1);
    V_FILIALPAR                      VARCHAR2(4);
    V_IDUTPADRROPFIL                 VARCHAR2(1);
    V_IDUTPADRROP                    VARCHAR2(1);
    V_NRAPURREL                      VARCHAR2(20);
    V_CDPRODFIN                      VARCHAR2(20);
    V_CDPRODINI                      VARCHAR2(20);
    V_NRMOVIESTQ                     VARCHAR2(20);
    V_DTLANCMOVI                     DATE;
    V_DTLANCMOVIF                    DATE;
    V_NRROPFILIAL                    VARCHAR2(20);
    V_NRLANCROP                      VARCHAR2(20);
    V_NRITLANCROP                    VARCHAR2(20);
    V_NRSEQRELACIT                   VARCHAR2(10);
    V_NRTIPOCUSTO                    NUMBER;
    V_PRODUTO                        VARCHAR2(20);
    V_NRSEQIT                        VARCHAR2(10);
    V_CDFILISUBGR                    VARCHAR2(04);
    V_IDSERVRETROP                   CHAR;
    V_IDSERVFATROP                   CHAR;
    V_CONSUMO                        FLOAT;
    V_NRCOUNT                        FLOAT;
    V_NRCONS                         FLOAT;
    V_VALOR                          FLOAT;
    V_DTINICONS                      DATE;
    V_NRMESCONSROP                   NUMBER;
    V_IDPERCONSROP                   CHAR;
    V_DTINICIO                       DATE;
    V_DTINICIOST                     VARCHAR2(30);
    V_DTFINAL                        DATE;
    V_DTFILIALROP                    DATE;
    V_NRITEM                         NUMBER;
    V_CDFILCENTPME                   VARCHAR2(4);
    V_CDFILCENT_EFETPME              VARCHAR2(4);
    V_IDFILIALAGRROP                 CHAR;
    V_IDFECHDIAROP                   CHAR;
    V_APURCONS                       VARCHAR2(10);
    V_DTLANCROP                      DATE;
    V_NRROPFILINI                    VARCHAR2(20);
    V_CDFILIALIT                     VARCHAR2(04);
    V_NRLANCTONFIT                   VARCHAR2(10);
    V_VRNOTAFISCIT                   FLOAT;
    V_VRNOTAFISC                     FLOAT;
    V_IDNOTADEVO                     VARCHAR2(1);
    V_CDFISCALIT                     CHAR;
    V_ULTLANCMOVIEST                 VARCHAR2(20);
    V_VRCUSTLOGLAN                   FLOAT;
    V_VRSEGULOGLAN                   FLOAT;
    V_DTINIC                         DATE;
    V_FILIAPURA                      VARCHAR2(4);
    V_NRDIAS                         FLOAT;
    V_DTPROCESSA                     DATE;
    V_IDLANCDTMENFIL                 VARCHAR2(2);
    V_PRIIMP                         NUMBER;
    V_NRROPDIF                       BOOLEAN;
    V_CDFILIALBO                     VARCHAR2(04);
    V_IDDTAPURFAT                    VARCHAR2(1);
    V_IDAPUINVFILROP                 VARCHAR2(1);
    V_IDAPUCTPGROP                   VARCHAR2(1);
    V_IDDESPMOBROPCTB                VARCHAR2(1);
    V_IDAPUMOCTPGROP                 VARCHAR2(1);
    V_IDGERROPFOS                    VARCHAR2(1);
    V_IDCONSFATUDIV                  VARCHAR2(1);
    V_COUNT                          FLOAT;
    V_IDCONSPERRATFIL                VARCHAR2(1);
    V_CDEMPRESA                      VARCHAR2(2);
    V_IDAPURDEPRECROP                VARCHAR2(1);
    V_IDTIPODEPRECROP                VARCHAR2(1);
    V_EXCECAO                        VARCHAR2(20);
    V_IDDTAPUCTPAGAR                 VARCHAR2(30);
    V_IDAPURAINVCTPAGROP             VARCHAR(1);
    V_DESPESA_PAG                    VARCHAR(20);
    V_DTFOLHA                        DATE;
    V_IDPERFIXCALCIMP                VARCHAR2(1);
    V_IDTPVRCTPAGAR                  VARCHAR2(10);
    V_CMV                            FLOAT;
    V_DIASESTOQUE                    FLOAT;
    V_IDFILCENTPME                   VARCHAR2(1);
    V_VALORTOTAL                     FLOAT;
    V_QTD_RATEIO                     INTEGER;
    V_VR_RATEADO                     FLOAT;
    V_CDTPCTPAGAR                    VARCHAR2(10);
    V_CONSULTA_PAG                   VARCHAR2(10);
    V_VRRATENF                       FLOAT;
    V_VRPEROTHERSRAT                 FLOAT;
    V_HA_CONTAPAGAR                  VARCHAR2(10);
    V_DTINITEMP                      DATE;
    V_VRIMPOSTOIT                    FLOAT;
    V_VRIMPOSTO                      FLOAT;
    V_IDNFESTCANC                    VARCHAR2(01);
    V_POSGRUPPROD                    VARCHAR2(10);
    V_POSUBGRUPPROD                  VARCHAR2(10);
    V_IDDTAPUDESPENTR_GERAL          VARCHAR2(10);
    V_VALORCONTAPAGAR                FLOAT;
    V_VALORBRUTO                     FLOAT;
    V_VALORLIQUIDO                   FLOAT;
    V_VALOR_DESPESA                  FLOAT;
    V_VRTOTAL_NF_PARCELADA           FLOAT;
    V_PERCENT_PROD_NF                FLOAT;
    V_VRPARCELA_PERIODO              FLOAT;
    V_VALORPRODUTO_PARCELA           FLOAT;
    V_PERCENT_RATEIO                 FLOAT;
    V_VALOR_RATEIO                   FLOAT;
    V_VALOR_RATEIO_PRODUTO_PARCELA   FLOAT;
    V_NRPARCELA                      VARCHAR2(400);
    V_NRMAXPARCELA                   VARCHAR2(4);
    V_DESCITEM                       VARCHAR2(400);
    V_DESPESAEXISTE                  VARCHAR2(20);
    V_DTDESPINI                      DATE;
    V_DTDESPESA                      DATE;
    V_DTDESPFIM                      DATE;
    V_STRINGPARCELA                  VARCHAR2(200);
    V_IDDTAPUCTRECROP                VARCHAR2(30);
    V_IDAPUCTRECROP                  VARCHAR2(1);
    V_IDPERFIXIMPDRF                 VARCHAR2(1);
    V_ITVENDACAN                     FLOAT;
    V_VRVENDACANCEL                  FLOAT;
    V_VENDABRUTA                     FLOAT;
    V_VRCANCELAMENTO                 FLOAT;
    V_VRCONTABIL                     FLOAT;
    V_VRPISECF                       FLOAT;
    V_VRCOFINSECF                    FLOAT;
    V_VRTOTIMPOSTO                   FLOAT;
    V_VRPIS                          FLOAT;
    V_VRCOFINS                       FLOAT;
    V_MOVIMCONSOL                    VARCHAR2(01);
BEGIN
    V_CONSUMO       := 0;
    V_NRLANCROP     := 0;
    V_NRDIAS        := 0;
    BEGIN
        SELECT IDUTPADRROP
          INTO V_IDUTPADRROP
          FROM PARAMGERAL G;

    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            BEGIN
                V_IDUTPADRROP := NULL;
            END;
    END;

    BEGIN
        SELECT IDUTPADRROPFIL,
               CDFILCENTPME,
               CDEMPRESA
          INTO
            V_IDUTPADRROPFIL,
            V_CDFILCENTPME,
            V_CDEMPRESA
          FROM PARFILIALROP   P,
               FILIAL         F
         WHERE F.CDFILIAL      = P_CDFILIAL AND
               P.CDFILIAL(+)   = F.CDFILIAL;

    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            BEGIN
                V_IDUTPADRROPFIL   := NULL;
                V_CDFILCENTPME     := NULL;
                V_CDEMPRESA        := NULL;
            END;
    END;

    IF(V_IDUTPADRROP = 'S' OR V_IDUTPADRROPFIL = 'S')THEN
        V_FILIALPAR := 'P';
    ELSE
        IF V_CDFILCENTPME IS NULL THEN
            V_FILIALPAR := P_CDFILIAL;
        ELSE
            BEGIN
                SELECT IDFILIALAGRROP
                  INTO V_IDFILIALAGRROP
                  FROM PARFILIALROP
                 WHERE CDFILIAL = V_CDFILCENTPME;

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_IDFILIALAGRROP := 'N';
            END;

            IF V_IDFILIALAGRROP = 'S' THEN
                V_FILIALPAR := V_CDFILCENTPME;
            ELSE
                V_FILIALPAR := P_CDFILIAL;
            END IF;

        END IF;
    END IF;

    BEGIN
        SELECT CDGRUPPROD,
               CDGRUPDESP,
               IDFATUROP,
               IDEFETIVOROP,
               IDCOMPRAROP,
               IDNFSAIDAROP,
               IDINVINICROP,
               IDIMPLANTROP,
               IDINVFINAROP,
               IDAJUSTEROP,
               IDRETIRADAROP,
               IDDESPDIVROP,
               IDDESPMOBROP,
               IDDESPMOBROPCTB,
               IDDESPENTROP,
               IDSERVRETROP,
               IDSERVFATROP,
               NVL(NRMESCONSROP, 1),
               NVL(IDPERCONSROP, 'N'),
               IDFECHDIAROP,
               IDDTAPURFAT,
               IDAPUINVFILROP,
               NVL(IDAPUCTPGROP, 'N'),
               NVL(IDAPUMOCTPGROP, 'N'),
               NVL(IDCONSPERRATFIL, 'N'),
               NVL(IDAPURDEPRECROP, 'N'),
               NVL(IDTIPODEPRECROP, 'C'),
               NVL(IDGERROPFOS, 'N'),
               NVL(IDPERFIXCALCIMP, 'N'),
               NVL(IDDTAPUCTPAGAR, 'BAIXA'),
               NVL(IDDTAPUCTRECROP, 'BAIXA'),
               NVL(IDAPURAINVCTPAGROP, 'N'),
               IDTPVRCTPAGAR,
               NVL(IDCONSFATUDIV, 'N'),
               NVL(IDDTAPUDESPENTR, 'ENTRADA'),
               NVL(IDAPUCTRECROP, 'S')
          INTO
            V_CDGRUPPROD,
            V_CDGRUPDESP,
            V_IDFATUROP,
            V_IDEFETIVOROP,
            V_IDCOMPRAROP,
            V_IDNFSAIDAROP,
            V_IDINVINICROP,
            V_IDIMPLANTROP,
            V_IDINVFINAROP,
            V_IDAJUSTEROP,
            V_IDRETIRADAROP,
            V_IDDESPDIVROP,
            V_IDDESPMOBROP,
            V_IDDESPMOBROPCTB,
            V_IDDESPENTROP,
            V_IDSERVRETROP,
            V_IDSERVFATROP,
            V_NRMESCONSROP,
            V_IDPERCONSROP,
            V_IDFECHDIAROP,
            V_IDDTAPURFAT,
            V_IDAPUINVFILROP,
            V_IDAPUCTPGROP,
            V_IDAPUMOCTPGROP,
            V_IDCONSPERRATFIL,
            V_IDAPURDEPRECROP,
            V_IDTIPODEPRECROP,
            V_IDGERROPFOS,
            V_IDPERFIXCALCIMP,
            V_IDDTAPUCTPAGAR,
            V_IDDTAPUCTRECROP,
            V_IDAPURAINVCTPAGROP,
            V_IDTPVRCTPAGAR,
            V_IDCONSFATUDIV,
            V_IDDTAPUDESPENTR_GERAL,
            V_IDAPUCTRECROP
          FROM PARFILIALROP P
         WHERE P.CDFILIAL = V_FILIALPAR;

    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            BEGIN
                V_CDGRUPPROD              := NULL;
                V_IDFATUROP               := NULL;
                V_IDEFETIVOROP            := NULL;
                V_IDCOMPRAROP             := NULL;
                V_IDNFSAIDAROP            := NULL;
                V_IDINVINICROP            := NULL;
                V_IDIMPLANTROP            := NULL;
                V_IDINVFINAROP            := NULL;
                V_IDAJUSTEROP             := NULL;
                V_IDRETIRADAROP           := NULL;
                V_IDDESPDIVROP            := NULL;
                V_IDDESPMOBROP            := NULL;
                V_IDDESPMOBROPCTB         := NULL;
                V_IDDESPENTROP            := NULL;
                V_IDSERVRETROP            := NULL;
                V_IDSERVFATROP            := NULL;
                V_NRMESCONSROP            := 1;
                V_IDPERCONSROP            := 'N';
                V_IDFECHDIAROP            := 'N';
                V_IDDTAPURFAT             := 'S';
                V_IDAPUINVFILROP          := NULL;
                V_IDAPUCTPGROP            := 'N';
                V_IDAPUMOCTPGROP          := 'N';
                V_IDCONSPERRATFIL         := 'N';
                V_IDAPURDEPRECROP         := 'N';
                V_IDTIPODEPRECROP         := 'C';
                V_IDGERROPFOS             := 'N';
                V_IDPERFIXCALCIMP         := 'N';
                V_IDDTAPUCTPAGAR          := 'BAIXA';
                V_IDAPURAINVCTPAGROP      := 'N';
                V_IDTPVRCTPAGAR           := 'LIQUIDO';
                V_IDCONSFATUDIV           := 'N';
                V_IDDTAPUDESPENTR_GERAL   := 'ENTRADA';
                V_IDAPUCTRECROP           := 'S';
            END;
    END;

    BEGIN
        SELECT TO_NUMBER(NVL(CDTIPCUSTEST, '3')),
               DECODE(IDUTILDTPROC, 'S', NVL(TRUNC(DTPROCESSA), TRUNC(SYSDATE)), TRUNC(SYSDATE))
          INTO
            V_NRTIPOCUSTO,
            V_DTPROCESSA
          FROM PARAMFILIAL P,
               PARAMGERAL
         WHERE P.CDFILIAL = P_CDFILIAL;

    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            V_NRTIPOCUSTO   := '3';
            V_DTPROCESSA    := TRUNC(SYSDATE);
    END;

    SELECT NVL(MAX(CDFILIAL), MAX(CDFILIPADRAO))CDFILISUBGR
      INTO V_CDFILISUBGR
      FROM(
        SELECT DISTINCT CDFILISUBGR AS CDFILIAL,
                        '' AS CDFILIPADRAO
          FROM PRSUBGRPROD
         WHERE CDGRUPPROD    = V_CDGRUPPROD AND
               CDFILISUBGR   = P_CDFILIAL
        UNION ALL
        SELECT DISTINCT '' AS CDFILIAL,
                        CDFILISUBGR AS CDFILIPADRAO
          FROM PRSUBGRPROD
         WHERE CDGRUPPROD    = V_CDGRUPPROD AND
               CDFILISUBGR   = 'P'
    );

    IF P_IDCONSANT = 'N' THEN
        V_FILIAPURA := V_FILIALPAR;
        IF V_FILIAPURA = 'P' THEN
            V_FILIAPURA := P_CDFILIAL;
        END IF;
        BEGIN
            SELECT DTINIAPURACROP
              INTO V_DTINIC
              FROM APURACAOROP
             WHERE CDFILIAL = V_FILIAPURA;

        EXCEPTION
            WHEN NO_DATA_FOUND THEN
                V_DTINIC := P_DTINICIAL;
        END;

        IF NOT(V_DTINIC < P_DTINICIAL AND V_DTINIC IS NOT NULL)THEN
            V_DTINIC := P_DTINICIAL;
        END IF;

        BEGIN
            SELECT NVL(IDLANCDTMENFIL, 'N')
              INTO V_IDLANCDTMENFIL
              FROM PARAMFILIAL
             WHERE CDFILIAL = P_CDFILIAL;

        EXCEPTION
            WHEN NO_DATA_FOUND THEN
                V_IDLANCDTMENFIL := 'N';
        END;

        IF NVL(V_IDLANCDTMENFIL, 'N')= 'S' THEN
            BEGIN
                SELECT MAX(NRDIAS)
                  INTO V_NRDIAS
                  FROM(
                    SELECT NRDIASENTRFIL NRDIAS
                      FROM PARAMFILIAL
                     WHERE CDFILIAL                  = P_CDFILIAL AND
                           NVL(IDLANCDTMENFIL, 'N')  = 'S'
                    UNION ALL
                    SELECT NRDIASSAIDFIL NRDIAS
                      FROM PARAMFILIAL
                     WHERE CDFILIAL                  = P_CDFILIAL AND
                           NVL(IDLANCDTMENFIL, 'N')  = 'S'
                    UNION ALL
                    SELECT NRDIASAJUSFIL NRDIAS
                      FROM PARAMFILIAL
                     WHERE CDFILIAL                  = P_CDFILIAL AND
                           NVL(IDLANCDTMENFIL, 'N')  = 'S'
                )A;

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_NRDIAS := 0;
            END;
        ELSE
            BEGIN
                SELECT MAX(NRDIAS)
                  INTO V_NRDIAS
                  FROM(
                    SELECT NRDIASENTR NRDIAS
                      FROM PARAMESTOQUE
                     WHERE NVL(IDLANCDTMEN, 'N')= 'S'
                    UNION ALL
                    SELECT NRDIASSAID NRDIAS
                      FROM PARAMESTOQUE
                     WHERE NVL(IDLANCDTMEN, 'N')= 'S'
                    UNION ALL
                    SELECT NRDIASAJUS NRDIAS
                      FROM PARAMESTOQUE
                     WHERE NVL(IDLANCDTMEN, 'N')= 'S'
                )A;

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_NRDIAS := 0;
            END;
        END IF;

        IF(V_DTINIC >= V_DTPROCESSA - V_NRDIAS)THEN
            V_DTINIC := V_DTPROCESSA - V_NRDIAS;
        END IF;

        FOR C_LANCROPFILI IN(
            SELECT CDFILIAL,
                   NRROPFILIAL,
                   NRLANCROP
              FROM LANCROPFILIAL
             WHERE CDFILIAL = P_CDFILIAL AND
                   DTLANCROP >= V_DTINIC
            UNION
            SELECT CDFILIAL,
                   NRROPFILIAL,
                   NRLANCROP
              FROM LANCROPFILIAL
             WHERE CDFILIAL = P_CDFILIAL AND
                   DTINIREFEROP >= V_DTINIC
        )LOOP
            DELETE ITRELACITROP
             WHERE CDFILIAL      = P_CDFILIAL AND
                   NRLANCROP     = C_LANCROPFILI.NRLANCROP AND
                   NRROPFILIAL   = C_LANCROPFILI.NRROPFILIAL;

            DELETE ITLANCROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   NRLANCROP     = C_LANCROPFILI.NRLANCROP AND
                   NRROPFILIAL   = C_LANCROPFILI.NRROPFILIAL;

            DELETE LANCROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   NRLANCROP     = C_LANCROPFILI.NRLANCROP AND
                   NRROPFILIAL   = C_LANCROPFILI.NRROPFILIAL;

        END LOOP;

        FOR C_ROPFILI IN(
            SELECT NRROPFILIAL
              FROM ROPFILIAL
             WHERE CDFILIAL = P_CDFILIAL AND
                   DTFILIALROP >= V_DTINIC
        )LOOP DELETE ROPFILIAL
         WHERE CDFILIAL      = P_CDFILIAL AND
               NRROPFILIAL   = C_ROPFILI.NRROPFILIAL;

        END LOOP;

    ELSE
        V_DTINIC := P_DTINICIAL;
    END IF;

    V_DTFILIALROP   := V_DTINIC;
    NOVO_CODIGO('RESULTOPER' || P_CDFILIAL, V_NRROPFILIAL, ROUND(TO_NUMBER(P_DTFINAL - V_DTINIC)+ 2, 0));

    V_NRROPFILIAL   := SUBSTR(V_NRROPFILIAL, 11, 10);
    V_NRROPFILINI   := V_NRROPFILIAL;
    WHILE(V_DTFILIALROP <= P_DTFINAL)LOOP
        INSERT INTO ROPFILIAL(
            CDFILIAL,
            NRROPFILIAL,
            DTINICROP,
            DTFINALROP,
            DTGERAROP,
            DTFILIALROP
        )VALUES(
            P_CDFILIAL,
            V_NRROPFILIAL,
            V_DTINIC,
            P_DTFINAL,
            SYSDATE,
            V_DTFILIALROP
        );

        IF V_IDINVINICROP = 'S' THEN
            V_POSGRUPPROD     := NULL;
            V_POSUBGRUPPROD   := NULL;
            V_NRITLANCROP     := '00000';
            IF V_NRTIPOCUSTO IN(
                40,
                41,
                42,
                43,
                44,
                45,
                46,
                47,
                48,
                49,
                50,
                51,
                52,
                53,
                54,
                55,
                57,
                58,
                59,
                60,
                61,
                62
            )THEN
                FOR C_INVINICIAL IN(
                    SELECT P.CDPRODUTO,
                           P.QTESTDIACON,
                           PR.CDARVPROD,
                           NVL(CUSTO_PRODUTO(P.CDFILIAL, P.CDPRODUTO, V_NRTIPOCUSTO, V_DTINIC - 1, SYSDATE), 0)* P.QTESTDIACON AS
                           VRESTDIACON
                      FROM POSESTCONS   P
                     INNER JOIN PRODUTO      PR ON P.CDPRODUTO = PR.CDPRODUTO
                     WHERE P.CDFILIAL      = P_CDFILIAL AND
                           (QTESTDIACON <> 0 OR
                            VRESTDIACON <> 0)AND
                           P.DTPOSESTCON   =(
                               SELECT MAX(DTPOSESTCON)
                                 FROM POSESTCONS C
                                WHERE C.CDFILIAL    = P_CDFILIAL AND
                                      C.CDPRODUTO   = P.CDPRODUTO AND
                                      C.DTPOSESTCON <= V_DTFILIALROP
                           )
                )LOOP
                    BEGIN
                        SELECT N.CDGRUPPROD,
                               N.CDSUBGRPROD
                          INTO
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                          FROM PRODUTO       P,
                               NVSUBGRPROD   N
                         WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                               N.CDFILISUBGR   = V_CDFILISUBGR AND
                               N.CDGRUPPROD    = V_CDGRUPPROD AND
                               C_INVINICIAL.CDARVPROD LIKE P.CDARVPROD || '%';

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_POSGRUPPROD     := NULL;
                            V_POSUBGRUPPROD   := NULL;
                    END;

                    IF V_POSGRUPPROD IS NOT NULL THEN
                        V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                        INSERT INTO LANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            DTLANCROP,
                            IDTPLANCROP,
                            CDCLIENTEROP,
                            CDTIPOOPERROP,
                            NRNOTAFISCROP,
                            CDSERIENFROP,
                            CDFILIALNFROP,
                            NRLANCTONFROP,
                            CDFILCENTROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_DTFILIALROP,
                            'II',
                            '',
                            '',
                            '',
                            '',
                            P_CDFILIAL,
                            '',
                            V_CDFILCENTPME
                        );

                        V_NRITLANCROP   := '00001';
                        INSERT INTO ITLANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            NRITLANCROP,
                            CDFILIALNFITROP,
                            NRLANCTONFITROP,
                            NRSEQITPRODROP,
                            CDITEMLANCROP,
                            QTITEMLANCROP,
                            VRITEMLANCROP,
                            CDGRUPPRODROP,
                            CDSUBGRPRODROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_NRITLANCROP,
                            P_CDFILIAL,
                            '',
                            '',
                            V_POSUBGRUPPROD,
                            C_INVINICIAL.QTESTDIACON,
                            C_INVINICIAL.VRESTDIACON,
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                        );

                        V_CONSUMO       := V_CONSUMO + C_INVINICIAL.VRESTDIACON;
                    END IF;

                END LOOP;
            ELSE
                FOR C_INVINICIAL IN(
                    SELECT B.CDPRODUTO,
                           PR.CDARVPROD,
                           B.QTESTOQDIA AS QTESTDIACON,
                           NVL(CUSTO_PRODUTO_LCEST(B.CDFILIAL, B.CDPRODUTO, V_NRTIPOCUSTO, V_DTFILIALROP - 1, SYSDATE,
                                                   B.CDALMOXARIFE, B.CDLOCALESTOQ, B.NRLOTEESTQ, B.NRSUBLOTE), 0)* B.QTESTOQDIA AS
                                                   VRESTDIACON
                      FROM POSESTDIA   B
                     INNER JOIN PRODUTO     PR ON B.CDPRODUTO = PR.CDPRODUTO
                     WHERE B.CDFILIAL     = P_CDFILIAL AND
                           B.QTESTOQDIA <> 0 AND
                           B.DTPOSIESTQ   =(
                               SELECT MAX(P.DTPOSIESTQ)
                                 FROM POSESTDIA P
                                WHERE P.CDFILIAL       = P_CDFILIAL AND
                                      P.CDPRODUTO      = B.CDPRODUTO AND
                                      P.CDALMOXARIFE   = B.CDALMOXARIFE AND
                                      P.CDLOCALESTOQ   = B.CDLOCALESTOQ AND
                                      P.NRLOTEESTQ     = B.NRLOTEESTQ AND
                                      P.NRSUBLOTE      = B.NRSUBLOTE AND
                                      P.DTPOSIESTQ <= V_DTFILIALROP
                           )
                )LOOP
                    BEGIN
                        SELECT N.CDGRUPPROD,
                               N.CDSUBGRPROD
                          INTO
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                          FROM PRODUTO       P,
                               NVSUBGRPROD   N
                         WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                               N.CDFILISUBGR   = V_CDFILISUBGR AND
                               N.CDGRUPPROD    = V_CDGRUPPROD AND
                               C_INVINICIAL.CDARVPROD LIKE P.CDARVPROD || '%';

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_POSGRUPPROD     := NULL;
                            V_POSUBGRUPPROD   := NULL;
                    END;

                    IF V_POSGRUPPROD IS NOT NULL THEN
                        V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                        INSERT INTO LANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            DTLANCROP,
                            IDTPLANCROP,
                            CDCLIENTEROP,
                            CDTIPOOPERROP,
                            NRNOTAFISCROP,
                            CDSERIENFROP,
                            CDFILIALNFROP,
                            NRLANCTONFROP,
                            CDFILCENTROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_DTFILIALROP,
                            'II',
                            '',
                            '',
                            '',
                            '',
                            P_CDFILIAL,
                            '',
                            V_CDFILCENTPME
                        );

                        V_NRITLANCROP   := '00001';
                        INSERT INTO ITLANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            NRITLANCROP,
                            CDFILIALNFITROP,
                            NRLANCTONFITROP,
                            NRSEQITPRODROP,
                            CDITEMLANCROP,
                            QTITEMLANCROP,
                            VRITEMLANCROP,
                            CDGRUPPRODROP,
                            CDSUBGRPRODROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_NRITLANCROP,
                            P_CDFILIAL,
                            '',
                            '',
                            V_POSUBGRUPPROD,
                            C_INVINICIAL.QTESTDIACON,
                            C_INVINICIAL.VRESTDIACON,
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                        );

                        V_CONSUMO       := V_CONSUMO + C_INVINICIAL.VRESTDIACON;
                    END IF;

                END LOOP;
            END IF;

        END IF;

        IF V_IDINVFINAROP = 'S' THEN
            V_POSGRUPPROD     := NULL;
            V_POSUBGRUPPROD   := NULL;
            V_NRITLANCROP     := '00000';
            IF V_NRTIPOCUSTO IN(
                40,
                41,
                42,
                43,
                44,
                45,
                46,
                47,
                48,
                49,
                50,
                51,
                52,
                53,
                54,
                55,
                57,
                58,
                59,
                60,
                61,
                62
            )THEN
                FOR C_INVFINAL IN(
                    SELECT P.CDPRODUTO,
                           P.QTESTDIACON,
                           PR.CDARVPROD,
                           NVL(CUSTO_PRODUTO(P.CDFILIAL, P.CDPRODUTO, V_NRTIPOCUSTO, P_DTFINAL, SYSDATE), 0)* P.QTESTDIACON AS VRESTDIACON
                      FROM POSESTCONS   P
                     INNER JOIN PRODUTO      PR ON P.CDPRODUTO = PR.CDPRODUTO
                     WHERE P.CDFILIAL      = P_CDFILIAL AND
                           (P.QTESTDIACON <> 0 OR
                            P.VRESTDIACON <> 0)AND
                           P.DTPOSESTCON   =(
                               SELECT MAX(DTPOSESTCON)
                                 FROM POSESTCONS C
                                WHERE C.CDFILIAL    = P_CDFILIAL AND
                                      C.CDPRODUTO   = P.CDPRODUTO AND
                                      C.DTPOSESTCON <= V_DTFILIALROP + 1
                           )
                )LOOP
                    BEGIN
                        SELECT N.CDGRUPPROD,
                               N.CDSUBGRPROD
                          INTO
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                          FROM PRODUTO       P,
                               NVSUBGRPROD   N
                         WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                               N.CDFILISUBGR   = V_CDFILISUBGR AND
                               N.CDGRUPPROD    = V_CDGRUPPROD AND
                               C_INVFINAL.CDARVPROD LIKE P.CDARVPROD || '%';

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            BEGIN
                                V_POSGRUPPROD     := NULL;
                                V_POSUBGRUPPROD   := NULL;
                            END;
                    END;

                    IF V_POSGRUPPROD IS NOT NULL THEN
                        V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                        INSERT INTO LANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            DTLANCROP,
                            IDTPLANCROP,
                            CDCLIENTEROP,
                            CDTIPOOPERROP,
                            NRNOTAFISCROP,
                            CDSERIENFROP,
                            CDFILIALNFROP,
                            NRLANCTONFROP,
                            CDFILCENTROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_DTFILIALROP,
                            'IF',
                            '',
                            '',
                            '',
                            '',
                            P_CDFILIAL,
                            '',
                            V_CDFILCENTPME
                        );

                        V_NRITLANCROP   := '00001';
                        INSERT INTO ITLANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            NRITLANCROP,
                            CDFILIALNFITROP,
                            NRLANCTONFITROP,
                            NRSEQITPRODROP,
                            CDITEMLANCROP,
                            QTITEMLANCROP,
                            VRITEMLANCROP,
                            CDGRUPPRODROP,
                            CDSUBGRPRODROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_NRITLANCROP,
                            P_CDFILIAL,
                            '',
                            '',
                            V_POSUBGRUPPROD,
                            C_INVFINAL.QTESTDIACON,
                            C_INVFINAL.VRESTDIACON,
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                        );

                        V_CONSUMO       := V_CONSUMO - C_INVFINAL.VRESTDIACON;
                    END IF;

                END LOOP;
            ELSE
                FOR C_INVFINAL IN(
                    SELECT B.CDPRODUTO,
                           PR.CDARVPROD,
                           B.QTESTOQDIA AS QTESTDIACON,
                           NVL(CUSTO_PRODUTO_LCEST(B.CDFILIAL, B.CDPRODUTO, V_NRTIPOCUSTO, V_DTFILIALROP, SYSDATE,
                                                   B.CDALMOXARIFE, B.CDLOCALESTOQ, B.NRLOTEESTQ, B.NRSUBLOTE), 0)* B.QTESTOQDIA AS
                                                   VRESTDIACON
                      FROM POSESTDIA   B
                     INNER JOIN PRODUTO     PR ON B.CDPRODUTO = PR.CDPRODUTO
                     WHERE B.CDFILIAL     = P_CDFILIAL AND
                           B.QTESTOQDIA <> 0 AND
                           B.DTPOSIESTQ   =(
                               SELECT MAX(P.DTPOSIESTQ)
                                 FROM POSESTDIA P
                                WHERE P.CDFILIAL       = P_CDFILIAL AND
                                      P.CDPRODUTO      = B.CDPRODUTO AND
                                      P.CDALMOXARIFE   = B.CDALMOXARIFE AND
                                      P.CDLOCALESTOQ   = B.CDLOCALESTOQ AND
                                      P.NRLOTEESTQ     = B.NRLOTEESTQ AND
                                      P.NRSUBLOTE      = B.NRSUBLOTE AND
                                      P.DTPOSIESTQ <= V_DTFILIALROP + 1
                           )
                )LOOP
                    BEGIN
                        SELECT N.CDGRUPPROD,
                               N.CDSUBGRPROD
                          INTO
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                          FROM PRODUTO       P,
                               NVSUBGRPROD   N
                         WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                               N.CDFILISUBGR   = V_CDFILISUBGR AND
                               N.CDGRUPPROD    = V_CDGRUPPROD AND
                               C_INVFINAL.CDARVPROD LIKE P.CDARVPROD || '%';

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_POSGRUPPROD     := NULL;
                            V_POSUBGRUPPROD   := NULL;
                    END;

                    IF V_POSGRUPPROD IS NOT NULL THEN
                        V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                        INSERT INTO LANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            DTLANCROP,
                            IDTPLANCROP,
                            CDCLIENTEROP,
                            CDTIPOOPERROP,
                            NRNOTAFISCROP,
                            CDSERIENFROP,
                            CDFILIALNFROP,
                            NRLANCTONFROP,
                            CDFILCENTROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_DTFILIALROP,
                            'IF',
                            '',
                            '',
                            '',
                            '',
                            P_CDFILIAL,
                            '',
                            V_CDFILCENTPME
                        );

                        V_NRITLANCROP   := '00001';
                        INSERT INTO ITLANCROPFILIAL(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            NRITLANCROP,
                            CDFILIALNFITROP,
                            NRLANCTONFITROP,
                            NRSEQITPRODROP,
                            CDITEMLANCROP,
                            QTITEMLANCROP,
                            VRITEMLANCROP,
                            CDGRUPPRODROP,
                            CDSUBGRPRODROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_NRITLANCROP,
                            P_CDFILIAL,
                            '',
                            '',
                            V_POSUBGRUPPROD,
                            C_INVFINAL.QTESTDIACON,
                            C_INVFINAL.VRESTDIACON,
                            V_POSGRUPPROD,
                            V_POSUBGRUPPROD
                        );

                        V_CONSUMO       := V_CONSUMO - C_INVFINAL.VRESTDIACON;
                    END IF;

                END LOOP;
            END IF;

        END IF;

        V_DTFILIALROP   := TO_DATE(V_DTFILIALROP + 1);
        V_NRROPFILIAL   := LPAD(V_NRROPFILIAL + 1, 10, '0');
    END LOOP;

    FOR C_VOLUME IN(
        SELECT RF.CDFILIAL,
               R.CDRECEXTRA,
               R.DESCRECEXTRA,
               RF.DTRECEXTRAFILI,
               RF.VRRECEXTRAFILI
          FROM RECEXTRAFILI   RF,
               RECEXTRA       R
         WHERE RF.CDFILIAL     = P_CDFILIAL AND
               RF.CDRECEXTRA   = R.CDRECEXTRA AND
               RF.DTRECEXTRAFILI BETWEEN V_DTINIC AND P_DTFINAL
    )LOOP
        V_DTLANCROP     := '01/01/1990';
        V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
        IF V_DTLANCROP <> C_VOLUME.DTRECEXTRAFILI THEN
            V_NRROPFILIAL   := NULL;
            SELECT MAX(NRROPFILIAL)
              INTO V_NRROPFILIAL
              FROM ROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   DTFILIALROP   = C_VOLUME.DTRECEXTRAFILI;

            V_DTLANCROP     := C_VOLUME.DTRECEXTRAFILI;
            IF V_NRROPFILIAL IS NULL THEN
                V_NRROPFILIAL := V_NRROPFILINI;
            END IF;
            SELECT MAX(NRLANCROP)
              INTO V_NRLANCROP
              FROM LANCROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   NRROPFILIAL   = V_NRROPFILIAL;

            IF V_NRLANCROP IS NULL THEN
                V_NRLANCROP   := 0;
                V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
            ELSE
                V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
            END IF;

        END IF;

        V_NRITLANCROP   := '00000';
        INSERT INTO LANCROPFILIAL(
            CDFILIAL,
            NRROPFILIAL,
            NRLANCROP,
            DTLANCROP,
            IDTPLANCROP,
            CDCLIENTEROP,
            CDTIPOOPERROP,
            NRNOTAFISCROP,
            CDSERIENFROP,
            CDFILIALNFROP,
            NRLANCTONFROP,
            CDFILCENTROP
        )VALUES(
            P_CDFILIAL,
            V_NRROPFILIAL,
            V_NRLANCROP,
            C_VOLUME.DTRECEXTRAFILI,
            'VN',
            '',
            '',
            '',
            '',
            C_VOLUME.CDFILIAL,
            '',
            V_CDFILCENTPME
        );

        V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
        INSERT INTO ITLANCROPFILIAL(
            CDFILIAL,
            NRROPFILIAL,
            NRLANCROP,
            NRITLANCROP,
            CDFILIALNFITROP,
            NRLANCTONFITROP,
            NRSEQITPRODROP,
            CDITEMLANCROP,
            QTITEMLANCROP,
            VRITEMLANCROP
        )VALUES(
            P_CDFILIAL,
            V_NRROPFILIAL,
            V_NRLANCROP,
            V_NRITLANCROP,
            C_VOLUME.CDFILIAL,
            '',
            '',
            C_VOLUME.CDRECEXTRA,
            1,
            C_VOLUME.VRRECEXTRAFILI
        );

    END LOOP;

    IF V_IDFATUROP = 'FS' OR V_IDFATUROP = 'AB' THEN
        V_DTLANCROP := '01/01/1990';
        FOR C_FATURANF IN(
            SELECT NVL(N.CDCLIENTE, N.CDFILIRECE) CDCLIENTE,
                   N.CDTIPOOPER,
                   N.NRNOTAFISC,
                   N.CDFILIAL,
                   N.NRLANCTONF,
                   N.DTENTRSAID,
                   N.CDSERIE,
                   N.IDSTATUS,
                   N.DTINIREFEFAT,
                   N.DTFINREFEFAT,
                   DECODE(IDSISTEMA, 'PDV', N.VRPRODUTOS, N.VRNOTAFISC)AS VALORTOTAL
              FROM NOTAFISCAL      N,
                   TIPOOPERACROP   P
             WHERE(N.CDFILIEMIT      = P_CDFILIAL)AND
                  (N.IDENTRSAIDOP    = 'S')AND
                  ((('S'               = V_IDDTAPURFAT)AND
                    (N.DTENTRSAID BETWEEN V_DTINIC AND P_DTFINAL))OR
                   (('R'               = V_IDDTAPURFAT)AND
                    (N.DTFINREFEFAT BETWEEN V_DTINIC AND P_DTFINAL)))AND
                  (V_FILIALPAR       = P.CDFILIAL)AND
                  (N.CDTIPOOPER      = P.CDTIPOOPER)AND
                  (N.IDENTRSAIDOP    = P.IDENTRSAIDOP)AND
                  (P.IDTPOPERACROP   = 'FS')AND
                  ((N.IDSTATUS IN(
                      'A',
                      'E',
                      'P'
                  ))OR
                   (N.IDSTATUSNFE     = 'A')OR
                   (N.IDSISTEMA       = 'PDV'))AND
                  (N.IDSISTEMA NOT IN(
                      'REC',
                      'FIS',
                      'ORE'
                  )OR
                   N.IDSISTEMA IS NULL)AND
                  (N.IDSTATUSNFE <> 'D')AND
                  NOT EXISTS(
                      SELECT CDTIPOOPER
                        FROM TIPOOPERACROP
                       WHERE CDFILIAL        = TIPOPARAM_DRO(P_CDFILIAL)AND
                             IDENTRSAIDOP    = 'S' AND
                             IDTPOPERACROP   = 'CS' AND
                             CDTIPOOPER      = N.CDTIPOOPER
                  )
        )LOOP
            V_NRLANCROP      := LPAD(V_NRLANCROP + 1, 10, '0');
            BEGIN
                SELECT NVL(SUM(N.VRNOTAFISC), 0),
                       NVL(MAX(TP.IDNOTADEVO), 'N'),
                       NVL(MAX(TP.IDNFESTCANC), 'N')
                  INTO
                    V_VRNOTAFISCIT,
                    V_IDNOTADEVO,
                    V_IDNFESTCANC
                  FROM NOTAFISCAL     N,
                       TIPOOPERAC     TP,
                       ITEMNOTAFISC   I
                 WHERE I.CDFILIALIT      = C_FATURANF.CDFILIAL AND
                       I.NRLANCTONFIT    = C_FATURANF.NRLANCTONF AND
                       N.CDFILIAL        = I.CDFILIAL AND
                       N.NRLANCTONF      = I.NRLANCTONF AND
                       N.CDTIPOOPER      = TP.CDTIPOOPER AND
                       TP.IDENTRSAIDOP   = 'E';

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    BEGIN
                        V_VRNOTAFISCIT   := 0;
                        V_IDNOTADEVO     := 'N';
                    END;
            END;

            IF(V_IDNOTADEVO = 'S')OR(V_IDNFESTCANC = 'S')THEN
                V_VRNOTAFISC := C_FATURANF.VALORTOTAL - V_VRNOTAFISCIT;
            ELSE
                V_VRNOTAFISC := C_FATURANF.VALORTOTAL;
            END IF;

            IF V_DTLANCROP <> C_FATURANF.DTENTRSAID THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_FATURANF.DTENTRSAID;

                V_DTLANCROP     := C_FATURANF.DTENTRSAID;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP,
                IDSTATUSROP,
                DTINIREFEROP,
                DTFINREFEROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                CASE WHEN V_IDDTAPURFAT = 'R'
                     THEN C_FATURANF.DTFINREFEFAT
                     ELSE C_FATURANF.DTENTRSAID
                END,
                'FS',
                C_FATURANF.CDCLIENTE,
                C_FATURANF.CDTIPOOPER,
                C_FATURANF.NRNOTAFISC,
                C_FATURANF.CDSERIE,
                C_FATURANF.CDFILIAL,
                C_FATURANF.NRLANCTONF,
                V_CDFILCENTPME,
                C_FATURANF.IDSTATUS,
                C_FATURANF.DTINIREFEFAT,
                C_FATURANF.DTFINREFEFAT
            );

            V_NRITLANCROP    := LPAD('00000' + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_FATURANF.CDFILIAL,
                C_FATURANF.NRLANCTONF,
                '',
                '',
                0,
                V_VRNOTAFISC
            );

            V_NRSEQRELACIT   := 0;
            IF V_IDPERFIXCALCIMP = 'N' THEN
                FOR C_IMPOSTO IN(
                    SELECT P.CDIMPOSTO,
                           SUM(VALOR_IMPOSTO_NF(C_FATURANF.CDFILIAL, C_FATURANF.NRLANCTONF, CDIMPOSTO, 'I'))AS VRIMPOSTO
                      FROM IMPOSTOROP P
                     WHERE P.CDFILIAL       = V_FILIALPAR AND
                           IDTPIMPOSTOROP   = 'FS'
                     GROUP BY P.CDIMPOSTO
                     ORDER BY P.CDIMPOSTO
                )LOOP
                    IF V_IDNOTADEVO = 'S' THEN
                        FOR C_DEVNF IN(
                            SELECT I.CDFILIAL,
                                   I.NRLANCTONF
                              INTO
                                V_VRNOTAFISCIT,
                                V_IDNOTADEVO
                              FROM NOTAFISCAL     N,
                                   TIPOOPERAC     TP,
                                   ITEMNOTAFISC   I
                             WHERE I.CDFILIALIT      = C_FATURANF.CDFILIAL AND
                                   I.NRLANCTONFIT    = C_FATURANF.NRLANCTONF AND
                                   N.CDFILIAL        = I.CDFILIAL AND
                                   N.NRLANCTONF      = I.NRLANCTONF AND
                                   N.CDTIPOOPER      = TP.CDTIPOOPER AND
                                   TP.IDENTRSAIDOP   = 'E' AND
                                   TP.IDNOTADEVO     = 'S'
                        )LOOP
                            SELECT NVL(SUM(VALOR_IMPOSTO_NF(C_DEVNF.CDFILIAL, C_DEVNF.NRLANCTONF, CDIMPOSTO, 'I')), 0)AS VRIMPOSTO
                              INTO V_VRIMPOSTOIT
                              FROM IMPOSTOROP P
                             WHERE P.CDFILIAL       = V_FILIALPAR AND
                                   IDTPIMPOSTOROP   = 'FS' AND
                                   P.CDIMPOSTO      = C_IMPOSTO.CDIMPOSTO;

                            V_VRIMPOSTO := C_IMPOSTO.VRIMPOSTO - V_VRIMPOSTOIT;
                        END LOOP;
                    ELSE
                        V_VRIMPOSTO := C_IMPOSTO.VRIMPOSTO;
                    END IF;

                    V_NRSEQRELACIT := V_NRSEQRELACIT + 1;
                    INSERT INTO ITRELACITROP(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        NRSEQRELACITROP,
                        CDITRELITROP,
                        QTITRELITROP,
                        VRITRELITROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        V_NRSEQRELACIT,
                        C_IMPOSTO.CDIMPOSTO,
                        0,
                        ROUND(V_VRIMPOSTO, 2)
                    );

                END LOOP;
            ELSE
                FOR C_IMPOSTO IN(
                    SELECT P.CDIMPOSTO,
                           (V_VRNOTAFISC * NVL(VRPERFIXCALCIMP, 0))/ 100 AS VRIMPOSTO,
                           VRPERFIXCALCIMP
                      FROM IMPOSTOROP P
                     WHERE P.CDFILIAL       = V_FILIALPAR AND
                           IDTPIMPOSTOROP   = 'FS' AND
                           ROWNUM <= 5
                     ORDER BY P.CDIMPOSTO
                )LOOP
                    V_NRSEQRELACIT := V_NRSEQRELACIT + 1;
                    INSERT INTO ITRELACITROP(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        NRSEQRELACITROP,
                        CDITRELITROP,
                        QTITRELITROP,
                        VRITRELITROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        V_NRSEQRELACIT,
                        C_IMPOSTO.CDIMPOSTO,
                        C_IMPOSTO.VRPERFIXCALCIMP,
                        ROUND(C_IMPOSTO.VRIMPOSTO, 2)
                    );

                END LOOP;
            END IF;

        END LOOP;

    END IF;

    IF V_IDFATUROP = 'FE' OR V_IDFATUROP = 'AB' THEN
        SELECT LPAD(ABS(ROUND(DBMS_RANDOM.VALUE(9999, 9999999999))), 10, '0')
          INTO V_NRAPURREL
          FROM DUAL;

        APURAC_RELGESDIRETA(P_CDFILIAL, V_DTINIC, P_DTFINAL, V_NRAPURREL);
        V_DTLANCROP := '01/01/1990';
        SELECT DECODE(IDFILCENTPME, 'S', CDFILIAL, CDFILCENTPME),
               IDFILCENTPME
          INTO
            V_CDFILCENT_EFETPME,
            V_IDFILCENTPME
          FROM FILIAL
         WHERE CDFILIAL = P_CDFILIAL;

        FOR C_EFET_PME IN(
            SELECT E.CDFILCENTPME,
                   E.CDFILIAL,
                   E.CDSERVFILI,
                   E.DTEFETCLPME,
                   E.CDCLIENTE,
                   NVL(SUM(E.QTCOMEFCLPME), 0)AS QTCOMEFCLPME,
                   NVL(SUM(VRSERVICO * E.QTCOMEFCLPME), 0)VRDIETFATU
              FROM EFETCLIEPME     E,
                   HISTVRSERVPME   C,
                   FILIAL          F,
                   (
                       SELECT CDFILIAL,
                              CDSERVFILI,
                              'E' AS IDTPSERVICOROP
                         FROM SERVICO
                        WHERE CDFILIAL         = P_CDFILIAL AND
                              V_IDSERVFATROP   = 'S'
                       UNION
                       SELECT CDFILIAL,
                              CDSERVFILI,
                              IDTPSERVICOROP
                         FROM SERVICOROP
                        WHERE CDFILIAL         = TIPOPARAM_DRO(P_CDFILIAL)AND
                              V_IDSERVFATROP   = 'N'
                   )S
             WHERE E.CDFILCENTPME   = V_CDFILCENT_EFETPME AND
                   V_IDFILCENTPME   = 'S' AND
                   DTEFETCLPME BETWEEN V_DTINIC AND P_DTFINAL AND
                   IDTPSERVICOROP   = 'E' AND
                   E.CDSERVFILI     = S.CDSERVFILI AND
                   E.CDFILCENTPME   = C.CDFILCENTPME AND
                   E.CDSERVFILI     = C.CDSERVFILI AND
                   E.CDCLIENTE      = C.CDCLIENTE AND
                   E.CDFILIAL       = C.CDFILIAL AND
                   E.CDFILIAL       = F.CDFILIAL AND
                   E.QTCOMEFCLPME > 0 AND
                   F.CDFILIAL IN(
                       SELECT CDFILIAL
                         FROM FILIAL
                        WHERE CDFILCENTPME = E.CDFILCENTPME
                   )
             GROUP BY E.CDFILCENTPME,
                      E.CDFILIAL,
                      E.CDSERVFILI,
                      E.DTEFETCLPME,
                      E.CDCLIENTE
            UNION ALL
            SELECT E.CDFILCENTPME,
                   E.CDFILIAL,
                   E.CDSERVFILI,
                   E.DTEFETCLPME,
                   E.CDCLIENTE,
                   NVL(SUM(E.QTCOMEFCLPME), 0)AS QTCOMEFCLPME,
                   NVL(SUM(VRSERVICO * E.QTCOMEFCLPME), 0)VRDIETFATU
              FROM EFETCLIEPME     E,
                   HISTVRSERVPME   C,
                   FILIAL          F,
                   (
                       SELECT CDFILIAL,
                              CDSERVFILI,
                              'E' AS IDTPSERVICOROP
                         FROM SERVICO
                        WHERE CDFILIAL         = P_CDFILIAL AND
                              V_IDSERVFATROP   = 'S'
                       UNION
                       SELECT CDFILIAL,
                              CDSERVFILI,
                              IDTPSERVICOROP
                         FROM SERVICOROP
                        WHERE CDFILIAL         = TIPOPARAM_DRO(P_CDFILIAL)AND
                              V_IDSERVFATROP   = 'N'
                   )S
             WHERE E.CDFILIAL       = P_CDFILIAL AND
                   V_IDFILCENTPME   = 'N' AND
                   DTEFETCLPME BETWEEN V_DTINIC AND P_DTFINAL AND
                   IDTPSERVICOROP   = 'E' AND
                   E.CDSERVFILI     = S.CDSERVFILI AND
                   S.CDSERVFILI     = C.CDSERVFILI AND
                   E.CDFILCENTPME   = C.CDFILCENTPME AND
                   E.CDSERVFILI     = C.CDSERVFILI AND
                   E.CDCLIENTE      = C.CDCLIENTE AND
                   E.CDFILIAL       = C.CDFILIAL AND
                   E.CDFILIAL       = F.CDFILIAL AND
                   E.QTCOMEFCLPME > 0 AND
                   F.CDFILIAL IN(
                       SELECT CDFILIAL
                         FROM FILIAL
                        WHERE CDFILCENTPME = E.CDFILCENTPME
                   )
             GROUP BY E.CDFILCENTPME,
                      E.CDFILIAL,
                      E.CDSERVFILI,
                      E.DTEFETCLPME,
                      E.CDCLIENTE
        )LOOP
            V_NRLANCROP      := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_EFET_PME.DTEFETCLPME THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_EFET_PME.DTEFETCLPME;

                V_DTLANCROP     := C_EFET_PME.DTEFETCLPME;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_EFET_PME.DTEFETCLPME,
                'FE',
                C_EFET_PME.CDCLIENTE,
                '',
                '',
                '',
                C_EFET_PME.CDFILIAL,
                '',
                V_CDFILCENT_EFETPME
            );

            V_NRITLANCROP    := '00000';
            V_NRITLANCROP    := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_EFET_PME.CDFILIAL,
                '',
                '',
                C_EFET_PME.CDSERVFILI,
                C_EFET_PME.QTCOMEFCLPME,
                C_EFET_PME.VRDIETFATU
            );

            V_NRSEQRELACIT   := 0;
            IF V_IDPERFIXCALCIMP = 'N' THEN
                IMPOSTOROP_RELGESPME(P_CDFILIAL, V_DTINIC, P_DTFINAL, V_NRROPFILIAL, V_NRLANCROP,
                                     V_NRITLANCROP, C_EFET_PME.CDCLIENTE, C_EFET_PME.DTEFETCLPME, C_EFET_PME.CDSERVFILI);
            ELSE
                FOR C_IMPOSTO IN(
                    SELECT P.CDIMPOSTO,
                           (C_EFET_PME.VRDIETFATU * NVL(VRPERFIXCALCIMP, 0))/ 100 AS VRIMPOSTO,
                           VRPERFIXCALCIMP
                      FROM IMPOSTOROP P
                     WHERE P.CDFILIAL       = V_FILIALPAR AND
                           IDTPIMPOSTOROP   = 'FE'
                     ORDER BY P.CDIMPOSTO
                )LOOP
                    V_NRSEQRELACIT := V_NRSEQRELACIT + 1;
                    INSERT INTO ITRELACITROP(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        NRSEQRELACITROP,
                        CDITRELITROP,
                        QTITRELITROP,
                        VRITRELITROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        V_NRSEQRELACIT,
                        C_IMPOSTO.CDIMPOSTO,
                        C_IMPOSTO.VRPERFIXCALCIMP,
                        ROUND(C_IMPOSTO.VRIMPOSTO, 2)
                    );

                END LOOP;
            END IF;

        END LOOP;

        FOR C_EFET_HOSP IN(
            SELECT E.CDFILIAL,
                   E.DTEFETDIET,
                   E.CDSERVFILI,
                   E.CDCLIENTE,
                   NVL(SUM(UTIL_APURAC.CALCVALRSERV(E.CDFILIAL, E.CDCLIENTE, E.CDSERVFILI, DTEFETDIET, NULL)* E.QTCOMEFETDIE), 0)
                   AS VRDIETFATU,
                   NVL(SUM(E.QTCOMEFETDIE), 0)AS QTCOMEFETDIE
              FROM EFETDIET     E,
                   SERVICOROP   S
             WHERE E.CDFILIAL       = P_CDFILIAL AND
                   E.CDSERVFILI     = S.CDSERVFILI AND
                   E.DTEFETDIET BETWEEN V_DTINIC AND P_DTFINAL AND
                   V_FILIALPAR      = S.CDFILIAL AND
                   IDTPSERVICOROP   = 'E' AND
                   V_IDSERVFATROP <> 'S'
             GROUP BY E.CDFILIAL,
                      E.DTEFETDIET,
                      E.CDSERVFILI,
                      E.CDCLIENTE
            UNION ALL
            SELECT E.CDFILIAL,
                   E.DTEFETDIET,
                   E.CDSERVFILI,
                   E.CDCLIENTE,
                   NVL(SUM(UTIL_APURAC.CALCVALRSERV(E.CDFILIAL, E.CDCLIENTE, E.CDSERVFILI, DTEFETDIET, NULL)* E.QTCOMEFETDIE), 0)
                   AS VRDIETFATU,
                   NVL(SUM(E.QTCOMEFETDIE), 0)AS QTCOMEFETDIE
              FROM EFETDIET E
             WHERE E.CDFILIAL       = P_CDFILIAL AND
                   E.DTEFETDIET BETWEEN V_DTINIC AND P_DTFINAL AND
                   V_IDSERVFATROP   = 'S'
             GROUP BY E.CDFILIAL,
                      E.DTEFETDIET,
                      E.CDSERVFILI,
                      E.CDCLIENTE
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_EFET_HOSP.DTEFETDIET THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_EFET_HOSP.DTEFETDIET;

                V_DTLANCROP     := C_EFET_HOSP.DTEFETDIET;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_EFET_HOSP.DTEFETDIET,
                'FE',
                C_EFET_HOSP.CDCLIENTE,
                '',
                '',
                '',
                C_EFET_HOSP.CDFILIAL,
                '',
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := '00000';
            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_EFET_HOSP.CDFILIAL,
                '',
                '',
                C_EFET_HOSP.CDSERVFILI,
                C_EFET_HOSP.QTCOMEFETDIE,
                C_EFET_HOSP.VRDIETFATU
            );

            IF V_IDPERFIXCALCIMP = 'S' THEN
                V_NRSEQRELACIT := 0;
                FOR C_IMPOSTO IN(
                    SELECT P.CDIMPOSTO,
                           (C_EFET_HOSP.VRDIETFATU * NVL(VRPERFIXCALCIMP, 0))/ 100 AS VRIMPOSTO,
                           VRPERFIXCALCIMP
                      FROM IMPOSTOROP P
                     WHERE P.CDFILIAL       = V_FILIALPAR AND
                           IDTPIMPOSTOROP   = 'FE'
                     ORDER BY P.CDIMPOSTO
                )LOOP
                    V_NRSEQRELACIT := V_NRSEQRELACIT + 1;
                    INSERT INTO ITRELACITROP(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        NRSEQRELACITROP,
                        CDITRELITROP,
                        QTITRELITROP,
                        VRITRELITROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        V_NRSEQRELACIT,
                        C_IMPOSTO.CDIMPOSTO,
                        C_IMPOSTO.VRPERFIXCALCIMP,
                        ROUND(C_IMPOSTO.VRIMPOSTO, 2)
                    );

                END LOOP;

            ELSE
                IMPOSTOROP_RELGESHOSP(P_CDFILIAL, V_DTINIC, P_DTFINAL, V_NRROPFILIAL, V_NRLANCROP,
                                      V_NRITLANCROP, C_EFET_HOSP.CDCLIENTE, C_EFET_HOSP.DTEFETDIET, C_EFET_HOSP.CDSERVFILI);
            END IF;

        END LOOP;

        FOR C_EFETIVOS IN(
            SELECT DISTINCT CDCLIENTEAPGD,
                            DTAPURACFATGD,
                            CDSERVFILIAPGD,
                            CDFILIALAPGD,
                            NRAPURACGD,
                            NVL(SUM(T.NREFETIVOSAPGD), 0)AS QUANTIDADE,
                            NVL(SUM(T.VRPRECUNITAPGD * T.NREFETIVOSAPGD), 0)AS VALORTOTAL
              FROM SERVICOROP       S,
                   TRAB_APURAC_GD   T
             WHERE(T.CDFILIALAPGD     = P_CDFILIAL)AND
                  (T.NRAPURACGD       = V_NRAPURREL)AND
                  (T.DTAPURACFATGD BETWEEN V_DTINIC AND P_DTFINAL)AND
                  (V_FILIALPAR        = S.CDFILIAL)AND
                  (T.CDSERVFILIAPGD   = S.CDSERVFILI)AND
                  (IDTPSERVICOROP     = 'E')AND
                  (V_IDSERVFATROP <> 'S')
             GROUP BY CDFILIALAPGD,
                      NRAPURACGD,
                      T.CDSERVFILIAPGD,
                      DTAPURACFATGD,
                      CDCLIENTEAPGD
            UNION ALL
            SELECT DISTINCT CDCLIENTEAPGD,
                            DTAPURACFATGD,
                            CDSERVFILIAPGD,
                            CDFILIALAPGD,
                            NRAPURACGD,
                            NVL(SUM(T.NREFETIVOSAPGD), 0)AS QUANTIDADE,
                            NVL(SUM(T.VRPRECUNITAPGD * T.NREFETIVOSAPGD), 0)AS VALORTOTAL
              FROM TRAB_APURAC_GD T
             WHERE(T.CDFILIALAPGD   = P_CDFILIAL)AND
                  (T.NRAPURACGD     = V_NRAPURREL)AND
                  (T.DTAPURACFATGD BETWEEN V_DTINIC AND P_DTFINAL)AND
                  (V_IDSERVFATROP   = 'S')
             GROUP BY CDFILIALAPGD,
                      NRAPURACGD,
                      T.CDSERVFILIAPGD,
                      DTAPURACFATGD,
                      CDCLIENTEAPGD
            UNION ALL
            SELECT DISTINCT CDCLIENTEAPTU,
                            DTAPURACFATTU,
                            CDSERVFILIAPTU,
                            CDFILIALAPTU,
                            NRAPURACTU,
                            NVL(SUM(T.NREFETIVOSAPTU), 0)AS QUANTIDADE,
                            NVL(SUM(T.VRPRECUNITAPTU * T.NREFETIVOSAPTU), 0)AS VALORTOTAL
              FROM TRAB_APURAC_TU T
             WHERE(T.CDFILIALAPTU   = P_CDFILIAL)AND
                  (T.NRAPURACTU     = V_NRAPURREL)AND
                  (T.DTAPURACFATTU BETWEEN V_DTINIC AND P_DTFINAL)AND
                  (V_IDSERVFATROP   = 'S')
             GROUP BY CDFILIALAPTU,
                      NRAPURACTU,
                      T.CDSERVFILIAPTU,
                      DTAPURACFATTU,
                      CDCLIENTEAPTU
            UNION ALL
            SELECT DISTINCT T.CDCLIENTEAPEQ,
                            T.DTAPURACFATEQ,
                            T.CDSERVFILIAPEQ,
                            T.CDFILIALAPEQ,
                            T.NRAPURACEQ,
                            NVL(SUM(T.NREFETIVOSAPEQ), 0)AS QUANTIDADE,
                            NVL(SUM(T.VRPRECUNITAPEQ * T.NREFETIVOSAPEQ), 0)AS VALORTOTAL
              FROM TRAB_APURAC_EQ T
             WHERE(T.CDFILIALAPEQ   = P_CDFILIAL)AND
                  (T.NRAPURACEQ     = V_NRAPURREL)AND
                  (T.DTAPURACFATEQ BETWEEN V_DTINIC AND P_DTFINAL)AND
                  (V_IDSERVFATROP   = 'S')
             GROUP BY CDFILIALAPEQ,
                      NRAPURACEQ,
                      T.CDSERVFILIAPEQ,
                      DTAPURACFATEQ,
                      CDCLIENTEAPEQ
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_EFETIVOS.DTAPURACFATGD THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_EFETIVOS.DTAPURACFATGD;

                V_DTLANCROP     := C_EFETIVOS.DTAPURACFATGD;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_EFETIVOS.DTAPURACFATGD,
                'FE',
                C_EFETIVOS.CDCLIENTEAPGD,
                '',
                '',
                '',
                C_EFETIVOS.CDFILIALAPGD,
                C_EFETIVOS.NRAPURACGD,
                V_CDFILCENT_EFETPME
            );

            V_NRITLANCROP   := '00000';
            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_EFETIVOS.CDFILIALAPGD,
                C_EFETIVOS.NRAPURACGD,
                '',
                C_EFETIVOS.CDSERVFILIAPGD,
                C_EFETIVOS.QUANTIDADE,
                C_EFETIVOS.VALORTOTAL
            );

            IF V_IDPERFIXCALCIMP = 'S' THEN
                V_NRSEQRELACIT := 0;
                FOR C_IMPOSTO IN(
                    SELECT P.CDIMPOSTO,
                           (C_EFETIVOS.VALORTOTAL * NVL(VRPERFIXCALCIMP, 0))/ 100 AS VRIMPOSTO,
                           VRPERFIXCALCIMP
                      FROM IMPOSTOROP P
                     WHERE P.CDFILIAL       = V_FILIALPAR AND
                           IDTPIMPOSTOROP   = 'FE'
                     ORDER BY P.CDIMPOSTO
                )LOOP
                    V_NRSEQRELACIT := V_NRSEQRELACIT + 1;
                    INSERT INTO ITRELACITROP(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        NRSEQRELACITROP,
                        CDITRELITROP,
                        QTITRELITROP,
                        VRITRELITROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        V_NRSEQRELACIT,
                        C_IMPOSTO.CDIMPOSTO,
                        C_IMPOSTO.VRPERFIXCALCIMP,
                        ROUND(C_IMPOSTO.VRIMPOSTO, 2)
                    );

                END LOOP;

            ELSE
                IMPOSTOROP_RELGESDIRETA(P_CDFILIAL, V_DTINIC, P_DTFINAL, V_NRAPURREL, V_NRROPFILIAL,
                                        V_NRLANCROP, V_NRITLANCROP, C_EFETIVOS.CDCLIENTEAPGD, C_EFETIVOS.DTAPURACFATGD, C_EFETIVOS
                                        .CDSERVFILIAPGD);
            END IF;

        END LOOP;

        DELETE FROM TRAB_APURAC_GD
         WHERE(CDFILIALAPGD   = P_CDFILIAL)AND
              (NRAPURACGD     = V_NRAPURREL)AND
              (DTAPURACFATGD BETWEEN V_DTINIC AND P_DTFINAL);

        DELETE FROM TRAB_APURAC_EQ
         WHERE(CDFILIALAPEQ   = P_CDFILIAL)AND
              (NRAPURACEQ     = V_NRAPURREL)AND
              (DTAPURACFATEQ BETWEEN V_DTINIC AND P_DTFINAL);

        DELETE FROM TRAB_APURAC_TU
         WHERE(CDFILIALAPTU   = P_CDFILIAL)AND
              (NRAPURACTU     = V_NRAPURREL)AND
              (DTAPURACFATTU BETWEEN V_DTINIC AND P_DTFINAL);

    END IF;

    IF V_IDGERROPFOS = 'S' THEN
        V_NRITLANCROP   := '00000';
        V_DTLANCROP     := '01/01/1990';
        FOR C_REG IN(
            SELECT CDFILIAL,
                   DTENTRVENDA,
                   NRNUMERNF,
                   SUM(VRVENDBRUT)VRVENDAS,
                   SUM(VRACRESCIMO)VRACRESCIMO,
                   SUM(VRDESCONTO)VRDESCONTOS,
                   SUM(VRIMPOSTO)VRIMPOSTO,
                   CDLOJA
              FROM(
                SELECT V.CDFILIAL,
                       V.DTENTRVENDA,
                       V.CDLOJA,
                       V.CDCAIXA,
                       SUM(TRUNC(I.QTPRODVEND *(NVL(I.VRUNITVEND, 0)+ NVL(I.VRUNITVENDCL, 0)), 2))AS VRVENDBRUT,
                       DECODE(V.IDSITUVENDA, 'O', SUM(NVL(I.VRACRITVEND, 0)), 0)AS VRACRESCIMO,
                       DECODE(V.IDSITUVENDA, 'O', SUM(NVL(I.VRDESITVEND, 0)), 0)AS VRDESCONTO,
                       SUM(I.QTPRODVEND)AS QTVENDIDA,
                       0 AS NRNUMERNF,
                       SUM((TRUNC(I.QTPRODVEND *(I.VRUNITVEND + NVL(I.VRUNITVENDCL, 0)), 2)+ NVL(I.VRACRITVEND, 0)- NVL(I.VRDESITVEND
                       , 0))* P.VRPEALPRODIT / 100)AS VRIMPOSTO
                  FROM VENDA          V
                 INNER JOIN ITEMVENDA      I ON(I.CDFILIAL     = V.CDFILIAL)AND
                                          (I.CDCAIXA      = V.CDCAIXA)AND
                                          (I.NRSEQVENDA   = V.NRSEQVENDA)
                 INNER JOIN ITVENDAIMPOS   P ON(I.CDFILIAL       = P.CDFILIAL)AND
                                             (I.CDCAIXA        = P.CDCAIXA)AND
                                             (I.NRSEQVENDA     = P.NRSEQVENDA)AND
                                             (I.NRSEQUITVEND   = P.NRSEQUITVEND)
                 INNER JOIN CAIXA          C ON(V.CDFILIAL   = C.CDFILIAL)AND
                                      (V.CDCAIXA    = C.CDCAIXA)AND
                                      (V.CDLOJA     = C.CDLOJA)
                 WHERE(V.CDFILIAL = P_CDFILIAL)AND
                      (V.DTENTRVENDA BETWEEN V_DTINIC AND P_DTFINAL)
                 GROUP BY V.CDFILIAL,
                          V.DTENTRVENDA,
                          V.IDSITUVENDA,
                          V.CDLOJA,
                          V.CDCAIXA
            )
             GROUP BY CDFILIAL,
                      DTENTRVENDA,
                      NRNUMERNF,
                      CDLOJA
        )LOOP
        
        /* CALCULA O VALOR DOS IMPOSTOS PIS E COFINS PARA VENDAS ECF */
            SELECT SUM((P.VRALIQIT / 100)* P.VRPRODUTO)
              INTO V_VRPISECF
              FROM ITEMVENOUTIMPOS   P,
                   IMPOSTO           I,
                   CAIXA             C
             WHERE P.CDFILIAL     = C_REG.CDFILIAL AND
                   P.DTREDUCAOZ   = C_REG.DTENTRVENDA AND
                   I.SGIMPOSTO    = 'PIS' AND
                   I.CDIMPOSTO    = P.CDIMPOSTO AND
                   C.CDFILIAL     = P.CDFILIAL AND
                   C.CDCAIXA      = P.CDCAIXA AND
                   C.CDLOJA       = C_REG.CDLOJA;

            SELECT SUM((P.VRALIQIT / 100)* P.VRPRODUTO)
              INTO V_VRCOFINSECF
              FROM ITEMVENOUTIMPOS   P,
                   IMPOSTO           I,
                   CAIXA             C
             WHERE P.CDFILIAL     = C_REG.CDFILIAL AND
                   P.DTREDUCAOZ   = C_REG.DTENTRVENDA AND
                   I.SGIMPOSTO    = 'COFINS' AND
                   I.CDIMPOSTO    = P.CDIMPOSTO AND
                   C.CDFILIAL     = P.CDFILIAL AND
                   C.CDCAIXA      = P.CDCAIXA AND
                   C.CDLOJA       = C_REG.CDLOJA;
			
		 /* CALCULA O VALOR DOS IMPOSTOS PIS E COFINS PARA VENDAS SAT e NFC-e */

            SELECT SUM(NVL(P.VRIMPPIS, 0)),
                   SUM(NVL(P.VRIMPCOFINS, 0))
              INTO
                V_VRPIS,
                V_VRCOFINS
              FROM VENDA          V,
                   ITVENDAIMPOS   P
             WHERE V.CDFILIAL      = C_REG.CDFILIAL AND
                   V.DTENTRVENDA   = C_REG.DTENTRVENDA AND
                   V.CDLOJA        = C_REG.CDLOJA AND
                   V.IDSITUVENDA   = 'O' AND
                   V.IDTPEMISVEND IN(
                       'S',
                       'N'
                   )AND
                   V.CDFILIAL      = P.CDFILIAL AND
                   V.CDCAIXA       = P.CDCAIXA AND
                   V.NRSEQVENDA    = P.NRSEQVENDA;

            IF(V_IDPERFIXIMPDRF = 'S')THEN
                V_VRTOTIMPOSTO := 0;
            ELSE
                V_VRTOTIMPOSTO := C_REG.VRIMPOSTO + NVL(V_VRPIS, 0)+ NVL(V_VRPISECF, 0)+ NVL(V_VRCOFINS, 0)+ NVL(V_VRCOFINSECF, 0
                );
            END IF;
        
         /*Valor de itens cancelados (Pode haver vendas concluidas,porem com itens cancelados)*/

            BEGIN
                SELECT NVL(SUM(VRITCANCEL), 0)VRVENDBRUT
                  INTO V_ITVENDACAN
                  FROM(
                    SELECT V.CDFILIAL,
                           V.DTENTRVENDA,
                           V.CDLOJA,
                           V.CDCAIXA,
                           SUM(TRUNC(I.QTPRODVENDC *(NVL(I.VRUNITVENDC, 0)+ NVL(I.VRUNITVENCLC, 0)), 2))AS VRITCANCEL
                      FROM VENDA        V
                     INNER JOIN ITVENDACAN   I ON(I.CDFILIAL     = V.CDFILIAL)AND
                                               (I.CDCAIXA      = V.CDCAIXA)AND
                                               (I.NRSEQVENDA   = V.NRSEQVENDA)
                     INNER JOIN CAIXA        C ON(V.CDFILIAL   = C.CDFILIAL)AND
                                          (V.CDCAIXA    = C.CDCAIXA)AND
                                          (V.CDLOJA     = C.CDLOJA)
                     WHERE(V.CDFILIAL      = P_CDFILIAL)AND
                          (V.DTENTRVENDA   = C_REG.DTENTRVENDA)
                     GROUP BY V.CDFILIAL,
                              V.DTENTRVENDA,
                              V.IDSITUVENDA,
                              V.CDLOJA,
                              V.CDCAIXA
                );

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_ITVENDACAN := 0;
            END;

         /*Valor de vendas canceladas*/

            BEGIN
                SELECT NVL(SUM(VRITCANCEL), 0)
                  INTO V_VRVENDACANCEL
                  FROM(
                    SELECT V.CDFILIAL,
                           V.DTENTRVENDA,
                           V.CDLOJA,
                           V.CDCAIXA,
                           SUM(TRUNC(I.QTPRODVEND *(NVL(I.VRUNITVEND, 0)+ NVL(NVL(I.VRUNITVENDCL, 0), 0)), 2))AS VRITCANCEL,
                           SUM(I.QTPRODVEND)AS QTVENDIDA
                      FROM VENDA       V
                     INNER JOIN ITEMVENDA   I ON(I.CDFILIAL     = V.CDFILIAL)AND
                                              (I.CDCAIXA      = V.CDCAIXA)AND
                                              (I.NRSEQVENDA   = V.NRSEQVENDA)
                     INNER JOIN CAIXA       C ON(V.CDFILIAL   = C.CDFILIAL)AND
                                          (V.CDCAIXA    = C.CDCAIXA)AND
                                          (V.CDLOJA     = C.CDLOJA)
                     WHERE(V.CDFILIAL      = P_CDFILIAL)AND
                          (V.DTENTRVENDA   = C_REG.DTENTRVENDA)AND
                          (V.IDSITUVENDA   = 'C')
                     GROUP BY V.CDFILIAL,
                              V.DTENTRVENDA,
                              V.IDSITUVENDA,
                              V.CDLOJA,
                              V.CDCAIXA
                );

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_VRVENDACANCEL := 0;
            END;

            V_VENDABRUTA       := C_REG.VRVENDAS + V_ITVENDACAN;
            V_VRCANCELAMENTO   := V_VRVENDACANCEL + V_ITVENDACAN;
            V_VRCONTABIL       := V_VENDABRUTA - C_REG.VRDESCONTOS - V_VRCANCELAMENTO;
            V_NRLANCROP        := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_REG.DTENTRVENDA THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_REG.DTENTRVENDA;

                V_DTLANCROP     := C_REG.DTENTRVENDA;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            V_NRITLANCROP      := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                VRIMPLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_REG.DTENTRVENDA,
                'FF',
                V_VRTOTIMPOSTO
            );

            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                VRITEMLANCROP,
                VRDESCITLANCROP,
                VRBRUTITLANCROP,
                VRCANCITLANCROP,
                NRLANCTONFITROP,
                VRIMPITLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                V_VRCONTABIL,
                C_REG.VRDESCONTOS,
                V_VENDABRUTA,
                V_VRCANCELAMENTO,
                C_REG.NRNUMERNF,
                V_VRTOTIMPOSTO
            );

            IF(V_IDPERFIXIMPDRF = 'S')THEN
                FOR C_IMPOSTO IN(
                    SELECT CDIMPOSTO,
                           VRPERFIXCALCIMP,
                           (C_REG.VRVENDAS * NVL(VRPERFIXCALCIMP, 0))/ 100 AS VRIMPOSTO
                      FROM IMPOSTOROP
                     WHERE CDFILIAL         = V_FILIALPAR AND
                           IDTPIMPOSTOROP   = 'FN'
                     ORDER BY CDIMPOSTO
                )LOOP
                    V_NRSEQRELACIT := V_NRSEQRELACIT + 1;
                    INSERT INTO ITRELACITROP(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        NRSEQRELACITROP,
                        CDITRELITROP,
                        QTITRELITROP,
                        VRITRELITROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        LPAD(V_NRLANCROP, 10, '0'),
                        V_NRITLANCROP,
                        V_NRSEQRELACIT,
                        C_IMPOSTO.CDIMPOSTO,
                        C_IMPOSTO.VRPERFIXCALCIMP,
                        C_IMPOSTO.VRIMPOSTO
                    );

                END LOOP;
            END IF;

        END LOOP;

    END IF;

    IF V_IDCONSFATUDIV = 'S' THEN
        V_NRITLANCROP   := '00000';
        V_DTLANCROP     := '01/01/1990';
        FOR C_FATUDIV IN(
            SELECT L.CDFILIAL,
                   L.CDFATUDIVER,
                   L.DTLANCFATDIV,
                   L.VRLANCFATDIV
              FROM LANCFATUDIVE L
             WHERE(L.CDFILIAL = P_CDFILIAL)AND
                  (L.DTLANCFATDIV BETWEEN V_DTINIC - 32 AND P_DTFINAL)
             ORDER BY L.DTLANCFATDIV,
                      L.CDFATUDIVER
        )LOOP
            BEGIN
                SELECT COUNT(*)
                  INTO V_COUNT
                  FROM LANCROPFILIAL     L,
                       ITLANCROPFILIAL   I
                 WHERE L.CDFILIAL      = C_FATUDIV.CDFILIAL AND
                       L.DTLANCROP     = C_FATUDIV.DTLANCFATDIV AND
                       CDITEMLANCROP   = C_FATUDIV.CDFATUDIVER AND
                       L.CDFILIAL      = I.CDFILIAL AND
                       L.NRROPFILIAL   = I.NRROPFILIAL AND
                       L.NRLANCROP     = I.NRLANCROP AND
                       IDTPLANCROP     = 'FD';

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_COUNT := 0;
            END;

            IF V_COUNT = 0 THEN
                V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                IF V_DTLANCROP <> C_FATUDIV.DTLANCFATDIV THEN
                    V_NRROPFILIAL   := NULL;
                    SELECT MAX(NRROPFILIAL)
                      INTO V_NRROPFILIAL
                      FROM ROPFILIAL
                     WHERE CDFILIAL      = P_CDFILIAL AND
                           DTFILIALROP   = C_FATUDIV.DTLANCFATDIV;

                    V_DTLANCROP     := C_FATUDIV.DTLANCFATDIV;
                    IF V_NRROPFILIAL IS NULL THEN
                        V_NRROPFILIAL := V_NRROPFILINI;
                    END IF;
                    SELECT MAX(NRLANCROP)
                      INTO V_NRLANCROP
                      FROM LANCROPFILIAL
                     WHERE CDFILIAL      = P_CDFILIAL AND
                           NRROPFILIAL   = V_NRROPFILIAL;

                    IF V_NRLANCROP IS NULL THEN
                        V_NRLANCROP   := 0;
                        V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                    ELSE
                        V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                    END IF;

                END IF;

                INSERT INTO LANCROPFILIAL(
                    CDFILIAL,
                    NRROPFILIAL,
                    NRLANCROP,
                    DTLANCROP,
                    IDTPLANCROP,
                    CDCLIENTEROP,
                    CDTIPOOPERROP,
                    NRNOTAFISCROP,
                    CDSERIENFROP,
                    CDFILIALNFROP,
                    NRLANCTONFROP,
                    CDFILCENTROP
                )VALUES(
                    P_CDFILIAL,
                    V_NRROPFILIAL,
                    V_NRLANCROP,
                    C_FATUDIV.DTLANCFATDIV,
                    'FD',
                    '',
                    '',
                    '',
                    '',
                    C_FATUDIV.CDFILIAL,
                    '',
                    V_CDFILCENTPME
                );

                V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
                INSERT INTO ITLANCROPFILIAL(
                    CDFILIAL,
                    NRROPFILIAL,
                    NRLANCROP,
                    NRITLANCROP,
                    CDFILIALNFITROP,
                    NRLANCTONFITROP,
                    NRSEQITPRODROP,
                    CDITEMLANCROP,
                    QTITEMLANCROP,
                    VRITEMLANCROP,
                    VRBRUTITLANCROP
                )VALUES(
                    P_CDFILIAL,
                    V_NRROPFILIAL,
                    V_NRLANCROP,
                    V_NRITLANCROP,
                    C_FATUDIV.CDFILIAL,
                    '',
                    '',
                    C_FATUDIV.CDFATUDIVER,
                    1,
                    C_FATUDIV.VRLANCFATDIV,
                    0
                );

            END IF;

        END LOOP;

    END IF;

    IF V_IDCOMPRAROP = 'S' THEN
        V_DTLANCROP := '01/01/1990';
        FOR C_CONSCOMPRA IN(
            SELECT N.CDTIPOOPER,
                   N.NRNOTAFISC,
                   N.CDFILIAL,
                   CDFILIRECE,
                   N.NRLANCTONF,
                   DTENTRSAID,
                   N.CDSERIE,
                   N.CDFORNECED,
                   N.IDSTATUS,
                   N.DTINIREFEFAT,
                   N.DTFINREFEFAT,
                   P.NUMERORE,
                   L.NRLANCESTQ,
                   N.NRSEQCAPADOC,
                   NVL(TP.IDNFESTCANC, 'N')IDNFESTCANC,
                   NVL(TP.IDUTCUSORIGEST, 'N')IDUTCUSORIGEST,
                   NVL(TP.IDOPERDEVONF, 'N')IDOPERDEVONF
              FROM NOTAFISCAL    N,
                   LANCTOESTOQ   L,
                   TIPOOPERAC    TP,
                   (
                       SELECT NRNUMERNF   AS NUMERORE,
                              CDFILIAL,
                              DTNUMERNF   AS DATARE
                         FROM NUMERORNF
                        WHERE CDFILIAL     = P_CDFILIAL AND
                              IDENTRSAID   = 'E' AND
                              DTNUMERNF BETWEEN V_DTINIC AND P_DTFINAL
                   )P
             WHERE(N.CDFILIRECE     = P_CDFILIAL)AND
                  (N.IDENTRSAIDOP   = 'E')AND
                  (N.DTENTRSAID BETWEEN V_DTINIC AND P_DTFINAL)AND
                  (N.CDFILIAL       = L.CDFILIAL)AND
                  (N.NRLANCTONF     = L.NRLANCTONF)AND
                  (N.CDFILIAL       = P.CDFILIAL(+))AND
                  (N.DTENTRSAID     = P.DATARE(+))AND
                  (N.NRSEQCAPADOC   = P.NUMERORE(+))AND
                  (N.IDSTATUS <> 'C')AND
                  (N.IDSISTEMA NOT IN(
                      'PAG',
                      'REC',
                      'FIS',
                      'CMP'
                  )OR
                   N.IDSISTEMA IS NULL)AND
                  (N.IDENTRSAIDOP   = TP.IDENTRSAIDOP)AND
                  (N.CDTIPOOPER     = TP.CDTIPOOPER)
             ORDER BY DTENTRSAID
        )LOOP
            V_NRLANCROP      := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_CONSCOMPRA.DTENTRSAID THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_CONSCOMPRA.DTENTRSAID;

                V_DTLANCROP     := C_CONSCOMPRA.DTENTRSAID;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP,
                IDSTATUSROP,
                DTINIREFEROP,
                DTFINREFEROP,
                NUMEROREROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_DTLANCROP,
                'CC',
                C_CONSCOMPRA.CDFORNECED,
                C_CONSCOMPRA.CDTIPOOPER,
                C_CONSCOMPRA.NRNOTAFISC,
                C_CONSCOMPRA.CDSERIE,
                C_CONSCOMPRA.CDFILIAL,
                C_CONSCOMPRA.NRLANCTONF,
                V_CDFILCENTPME,
                C_CONSCOMPRA.IDSTATUS,
                C_CONSCOMPRA.DTINIREFEFAT,
                C_CONSCOMPRA.DTFINREFEFAT,
                C_CONSCOMPRA.NUMERORE
            );

            V_NRITLANCROP    := '00000';
            V_NRSEQRELACIT   := 0;
            V_PRODUTO        := '';
            V_NRSEQIT        := 'N';
            V_PRIIMP         := 0;
            FOR C_ITCONSCOMPRA IN(
                SELECT CDSUBGRPROD,
                       CDGRUPPROD,
                       SUM(VALORTOTAL)VALORTOTAL,
                       SUM(QUANTIDADE)QUANTIDADE
                  FROM(
                    SELECT AN.CDSUBGRPROD,
                           AN.CDGRUPPROD,
                           SUM(DECODE(C_CONSCOMPRA.CDTIPOOPER, '03', I.VRBRUTITEM / H.VRFATOCONV, I.VRBRUTITEM))AS VALORTOTAL,
                           SUM(I.QTITEMPROD)AS QUANTIDADE
                      FROM ITEMPRODUTO    I,
                           SUBGRPRODFIL   SG,
                           PRODFILI       PR,
                           PRODUTO        H,
                           (
                               SELECT B.CDPRODUTO,
                                      A.CDGRUPPROD,
                                      A.CDSUBGRPROD,
                                      A.CDFILISUBGR
                                 FROM PRODUTO   B,
                                      PRODUTO   C,
                                      (
                                          SELECT P.CDARVPROD AS CDPRODUTO,
                                                 N.CDGRUPPROD,
                                                 N.CDSUBGRPROD,
                                                 N.CDFILISUBGR
                                            FROM PRODUTO       P,
                                                 NVSUBGRPROD   N
                                           WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                                 N.CDFILISUBGR   = V_CDFILISUBGR AND
                                                 N.CDGRUPPROD    = V_CDGRUPPROD
                                      )A
                                WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                                      (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                                      (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                                      (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                                ORDER BY B.CDARVPROD
                           )AN
                     WHERE(I.CDFILIAL                    = C_CONSCOMPRA.CDFILIAL)AND
                          (I.NRLANCTONF                  = C_CONSCOMPRA.NRLANCTONF)AND
                          (SG.CDFILISUBGR                = V_CDFILISUBGR)AND
                          (SG.CDGRUPPROD                 = V_CDGRUPPROD)AND
                          (SG.CDFILISUBGR                = AN.CDFILISUBGR)AND
                          (SG.CDSUBGRPROD                = AN.CDSUBGRPROD)AND
                          (SG.CDGRUPPROD                 = AN.CDGRUPPROD)AND
                          (I.CDPRODUTO                   = AN.CDPRODUTO)AND
                          (H.CDPRODUTO                   = I.CDPRODUTO)AND
                          (PR.CDFILIAL                   = P_CDFILIAL)AND
                          (PR.CDPRODUTO                  = H.CDPRODESTO)AND
                          (NVL(PR.IDCNTRESTOQ, 'S')      = 'S')AND
                          NOT((C_CONSCOMPRA.IDUTCUSORIGEST   = 'S')AND
                              (C_CONSCOMPRA.IDOPERDEVONF     = 'S'))AND
                          EXISTS(
                              SELECT 1
                                FROM ITLANCTOEST
                               WHERE NRLANCESTQ             = C_CONSCOMPRA.NRLANCESTQ AND
                                     CDFILIAL               = C_CONSCOMPRA.CDFILIAL AND
                                     NVL(IDEVOLESTOQ, 'S')  = 'S'
                          )
                     GROUP BY AN.CDSUBGRPROD,
                              AN.CDGRUPPROD
                    UNION ALL
                    SELECT AN.CDSUBGRPROD,
                           AN.CDGRUPPROD,
                           SUM(I.QTLANCTOEST * I.VRLANCTOEST)AS VALORTOTAL,
                           SUM(I.QTLANCTOEST)AS QUANTIDADE
                      FROM ITLANCTOEST    I,
                           SUBGRPRODFIL   SG,
                           PRODFILI       PR,
                           PRODUTO        H,
                           (
                               SELECT B.CDPRODUTO,
                                      A.CDGRUPPROD,
                                      A.CDSUBGRPROD,
                                      A.CDFILISUBGR
                                 FROM PRODUTO   B,
                                      PRODUTO   C,
                                      (
                                          SELECT P.CDARVPROD AS CDPRODUTO,
                                                 N.CDGRUPPROD,
                                                 N.CDSUBGRPROD,
                                                 N.CDFILISUBGR
                                            FROM PRODUTO       P,
                                                 NVSUBGRPROD   N
                                           WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                                 N.CDFILISUBGR   = V_CDFILISUBGR AND
                                                 N.CDGRUPPROD    = V_CDGRUPPROD
                                      )A
                                WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                                      (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                                      (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                                      (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                                ORDER BY B.CDARVPROD
                           )AN
                     WHERE(I.CDFILIAL                    = C_CONSCOMPRA.CDFILIAL)AND
                          (I.NRLANCESTQ                  = C_CONSCOMPRA.NRLANCESTQ)AND
                          (SG.CDFILISUBGR                = V_CDFILISUBGR)AND
                          (SG.CDGRUPPROD                 = V_CDGRUPPROD)AND
                          (SG.CDFILISUBGR                = AN.CDFILISUBGR)AND
                          (SG.CDSUBGRPROD                = AN.CDSUBGRPROD)AND
                          (SG.CDGRUPPROD                 = AN.CDGRUPPROD)AND
                          (I.CDPRODUTO                   = AN.CDPRODUTO)AND
                          (H.CDPRODUTO                   = I.CDPRODUTO)AND
                          (PR.CDFILIAL                   = P_CDFILIAL)AND
                          (PR.CDPRODUTO                  = H.CDPRODESTO)AND
                          (NVL(PR.IDCNTRESTOQ, 'S')      = 'S')AND
                          (NVL(I.IDEVOLESTOQ, 'S')       = 'S')AND
                          (C_CONSCOMPRA.IDUTCUSORIGEST   = 'S')AND
                          (C_CONSCOMPRA.IDOPERDEVONF     = 'S')
                     GROUP BY AN.CDSUBGRPROD,
                              AN.CDGRUPPROD
                )X
                 GROUP BY CDSUBGRPROD,
                          CDGRUPPROD
                 ORDER BY CDSUBGRPROD,
                          CDGRUPPROD
            )LOOP
                IF(NVL(V_PRODUTO, 'N')<> C_ITCONSCOMPRA.CDSUBGRPROD)THEN
                    BEGIN
                        SELECT SUM(VRCUSTLOGLAN)VRCUSTLOGLAN,
                               SUM(VRSEGULOGLAN)VRSEGULOGLAN
                          INTO
                            V_VRCUSTLOGLAN,
                            V_VRSEGULOGLAN
                          FROM GRUPPROD       GR,
                               SUBGRUPPROD    SG,
                               PARFILIALROP   P,
                               PRSUBGRPROD    AN,
                               LANCTOESTOQ    L,
                               ITLANCTOEST    IT,
                               PRODFILI       PR
                         WHERE(IT.CDFILIAL               = C_CONSCOMPRA.CDFILIAL)AND
                              (IT.NRLANCESTQ             = C_CONSCOMPRA.NRLANCESTQ)AND
                              (P.CDFILIAL                = V_FILIALPAR)AND
                              (CDFILISUBGR               = V_CDFILISUBGR)AND
                              (AN.CDSUBGRPROD            = C_ITCONSCOMPRA.CDSUBGRPROD)AND
                              (P.CDGRUPPROD              = GR.CDGRUPPROD)AND
                              (GR.CDGRUPPROD             = SG.CDGRUPPROD)AND
                              (SG.CDSUBGRPROD            = AN.CDSUBGRPROD)AND
                              (IT.CDPRODUTO              = AN.CDPRODUTO)AND
                              (SG.CDGRUPPROD             = AN.CDGRUPPROD)AND
                              (L.NRLANCTONF(+)           = C_CONSCOMPRA.NRLANCTONF)AND
                              (L.CDFILIAL(+)             = IT.CDFILIAL)AND
                              (L.NRLANCESTQ(+)           = IT.NRLANCESTQ)AND
                              (PR.CDFILIAL               = P_CDFILIAL)AND
                              (PR.CDPRODUTO              = IT.CDPRODMOVI)AND
                              (NVL(PR.IDCNTRESTOQ, 'S')  = 'S')AND
                              (NVL(IT.IDEVOLESTOQ, 'S')  = 'S');

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            BEGIN
                                V_VRCUSTLOGLAN   := 0;
                                V_VRSEGULOGLAN   := 0;
                            END;
                    END;

                    V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
                    V_PRODUTO       := C_ITCONSCOMPRA.CDSUBGRPROD;
                    INSERT INTO ITLANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        CDFILIALNFITROP,
                        NRLANCTONFITROP,
                        NRSEQITPRODROP,
                        CDITEMLANCROP,
                        QTITEMLANCROP,
                        VRITEMLANCROP,
                        CDGRUPPRODROP,
                        CDSUBGRPRODROP,
                        VRCUSTOLOGITROP,
                        VRCUSTSEGPROD
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        C_CONSCOMPRA.CDFILIAL,
                        C_CONSCOMPRA.NRLANCTONF,
                        '',
                        C_ITCONSCOMPRA.CDSUBGRPROD,
                        C_ITCONSCOMPRA.QUANTIDADE,
                        C_ITCONSCOMPRA.VALORTOTAL,
                        C_ITCONSCOMPRA.CDGRUPPROD,
                        C_ITCONSCOMPRA.CDSUBGRPROD,
                        V_VRCUSTLOGLAN,
                        V_VRSEGULOGLAN
                    );

                    V_CONSUMO       := V_CONSUMO + C_ITCONSCOMPRA.VALORTOTAL;
                END IF;

                IF V_PRIIMP = 0 THEN
                    FOR C_IMPOSTOCC IN(
                        SELECT CDIMPOSTO,
                               VALORIMPOSTO
                          FROM(
                            SELECT R.CDIMPOSTO,
                                   SUM(VRIMPOSTOIT)AS VALORIMPOSTO
                              FROM IMPOSTOROP     R,
                                   ITEMPROIMPOS   T,
                                   ITEMPRODUTO    I,
                                   PRODUTO        P,
                                   (
                                       SELECT A.CDFILISUBGR,
                                              A.CDSUBGRPROD,
                                              A.CDGRUPPROD,
                                              B.NMPRODUTO,
                                              B.CDARVPROD,
                                              B.CDPRODUTO
                                         FROM PRODUTO       B,
                                              NVSUBGRPROD   A
                                        WHERE(A.CDFILISUBGR             = V_CDFILISUBGR)AND
                                             (A.CDGRUPPROD              = V_CDGRUPPROD)AND
                                             (B.CDPRODUTO               = A.CDPRODUTO)AND
                                             (NVL(B.IDPRODATIVPR, 'S')  = 'S')
                                   )PROD
                             WHERE(I.CDFILIAL       = C_CONSCOMPRA.CDFILIAL)AND
                                  (I.NRLANCTONF     = C_CONSCOMPRA.NRLANCTONF)AND
                                  (I.CDFILIAL       = T.CDFILIAL)AND
                                  (I.NRLANCTONF     = T.NRLANCTONF)AND
                                  (I.NRSEQITPROD    = T.NRSEQITPROD)AND
                                  (T.NRITPROIMPOS   = '1')AND
                                  (T.CDIMPOSTO      = R.CDIMPOSTO)AND
                                  (R.CDFILIAL       = V_FILIALPAR)AND
                                  (R.IDTPIMPOSTOROP IN(
                                      'CC',
                                      'ST'
                                  ))AND
                                  (I.CDPRODUTO      = P.CDPRODUTO)AND
                                  (P.CDARVPROD LIKE PROD.CDARVPROD || '%')
                             GROUP BY R.CDIMPOSTO
                             ORDER BY R.CDIMPOSTO
                        )
                         WHERE ROWNUM <= 5
                    )LOOP
                        V_NRSEQRELACIT   := V_NRSEQRELACIT + 1;
                        INSERT INTO ITRELACITROP(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            NRITLANCROP,
                            NRSEQRELACITROP,
                            CDITRELITROP,
                            QTITRELITROP,
                            VRITRELITROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_NRITLANCROP,
                            V_NRSEQRELACIT,
                            C_IMPOSTOCC.CDIMPOSTO,
                            0,
                            ROUND(C_IMPOSTOCC.VALORIMPOSTO, 2)
                        );

                        V_PRIIMP         := 1;
                    END LOOP;

                END IF;

            END LOOP;

        END LOOP;

    END IF;

    IF V_IDNFSAIDAROP = 'S' THEN
        V_NRITLANCROP    := '00000';
        V_DTLANCROP := '01/01/1990';
        FOR C_CONSSAIDA IN(
            SELECT N.CDFILIRECE,
                   N.CDFILIEMIT,
                   N.CDCLIENTE,
                   N.CDTIPOOPER,
                   N.NRNOTAFISC,
                   N.CDFILIAL,
                   N.NRLANCTONF,
                   DTENTRSAID,
                   N.CDSERIE,
                   N.CDFORNECED,
                   N.IDSTATUS,
                   N.DTINIREFEFAT,
                   N.DTFINREFEFAT,
                   P.NUMERORE,
                   L.NRLANCESTQ
              FROM NOTAFISCAL    N,
                   LANCTOESTOQ   L,
                   (
                       SELECT NRNUMERNF   AS NUMERORE,
                              CDFILIAL,
                              DTNUMERNF   AS DATARE
                         FROM NUMERORNF
                        WHERE CDFILIAL     = P_CDFILIAL AND
                              IDENTRSAID   = 'S' AND
                              DTNUMERNF BETWEEN V_DTINIC AND P_DTFINAL
                   )P
             WHERE(N.CDFILIEMIT     = P_CDFILIAL)AND
                  (N.IDENTRSAIDOP   = 'S')AND
                  (N.DTENTRSAID BETWEEN V_DTINIC AND P_DTFINAL)AND
                  (N.CDFILIAL       = L.CDFILIAL(+))AND
                  (N.NRLANCTONF     = L.NRLANCTONF(+))AND
                  (N.CDFILIAL       = P.CDFILIAL(+))AND
                  (N.DTENTRSAID     = P.DATARE(+))AND
                  (N.NRSEQCAPADOC   = P.NUMERORE(+))AND
                  (N.IDSTATUS <> 'C')AND
                  (N.IDSISTEMA NOT IN(
                      'PAG',
                      'REC',
                      'FIS',
                      'CMP'
                  )OR
                   N.IDSISTEMA IS NULL)AND
                  NOT EXISTS(
                      SELECT CDTIPOOPER
                        FROM TIPOOPERACROP
                       WHERE CDFILIAL        = TIPOPARAM_DRO(P_CDFILIAL)AND
                             IDENTRSAIDOP    = 'S' AND
                             IDTPOPERACROP   = 'FS' AND
                             CDTIPOOPER      = N.CDTIPOOPER
                  )
        )LOOP
            V_NRLANCROP      := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_CONSSAIDA.DTENTRSAID THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_CONSSAIDA.DTENTRSAID;

                V_DTLANCROP     := C_CONSSAIDA.DTENTRSAID;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP,
                IDSTATUSROP,
                DTINIREFEROP,
                DTFINREFEROP,
                NUMEROREROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_CONSSAIDA.DTENTRSAID,
                'CS',
                C_CONSSAIDA.CDFORNECED,
                C_CONSSAIDA.CDTIPOOPER,
                C_CONSSAIDA.NRNOTAFISC,
                C_CONSSAIDA.CDSERIE,
                C_CONSSAIDA.CDFILIAL,
                C_CONSSAIDA.NRLANCTONF,
                V_CDFILCENTPME,
                C_CONSSAIDA.IDSTATUS,
                C_CONSSAIDA.DTINIREFEFAT,
                C_CONSSAIDA.DTFINREFEFAT,
                C_CONSSAIDA.NUMERORE
            );

            V_NRITLANCROP    := '00000';
            V_NRSEQRELACIT   := 0;
            V_PRODUTO        := 'N';
            V_NRSEQIT        := 'N';
            V_PRIIMP         := 0;
            FOR C_ITCONSSAIDA IN(
                SELECT AN.CDSUBGRPROD,
                       AN.CDGRUPPROD,
                       SUM(I.VRBRUTITEM)AS VALORTOTAL,
                       SUM(I.QTITEMPROD)AS QUANTIDADE
                  FROM ITEMPRODUTO    I
                 INNER JOIN PARAMFILIAL    PF ON I.CDFILIMOVI = PF.CDFILIAL
                 INNER JOIN PRODUTO        PP ON PP.CDPRODUTO = I.CDPRODUTO,
                       SUBGRPRODFIL   SG,
                       (
                           SELECT B.CDPRODUTO,
                                  A.CDGRUPPROD,
                                  A.CDSUBGRPROD,
                                  A.CDFILISUBGR
                             FROM PRODUTO   B,
                                  PRODUTO   C,
                                  (
                                      SELECT P.CDARVPROD AS CDPRODUTO,
                                             N.CDGRUPPROD,
                                             N.CDSUBGRPROD,
                                             N.CDFILISUBGR
                                        FROM PRODUTO       P,
                                             NVSUBGRPROD   N
                                       WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                             N.CDFILISUBGR   = V_CDFILISUBGR AND
                                             N.CDGRUPPROD    = V_CDGRUPPROD
                                  )A
                            WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                                  (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                                  (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                                  (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                            ORDER BY B.CDARVPROD
                       )AN
                 WHERE(I.CDFILIAL       = C_CONSSAIDA.CDFILIAL)AND
                      (I.NRLANCTONF     = C_CONSSAIDA.NRLANCTONF)AND
                      (SG.CDFILISUBGR   = V_CDFILISUBGR)AND
                      (SG.CDGRUPPROD    = V_CDGRUPPROD)AND
                      (AN.CDFILISUBGR   = SG.CDFILISUBGR)AND
                      (AN.CDGRUPPROD    = SG.CDGRUPPROD)AND
                      (AN.CDSUBGRPROD   = SG.CDSUBGRPROD)AND
                      (I.CDPRODMOVI     = AN.CDPRODUTO)AND
                      (I.CDPRODMOVI     =(
                          SELECT DISTINCT IT.CDPRODMOVI
                            FROM ITLANCTOEST IT
                           WHERE IT.CDFILIAL            = C_CONSSAIDA.CDFILIAL AND
                                 IT.NRLANCESTQ          = C_CONSSAIDA.NRLANCESTQ AND
                                 I.CDPRODMOVI           = IT.CDPRODMOVI AND
                                 NVL(IDEVOLESTOQ, 'S')  = 'S'
                      ))
                 GROUP BY AN.CDSUBGRPROD,
                          AN.CDGRUPPROD
                 ORDER BY AN.CDSUBGRPROD,
                          AN.CDGRUPPROD
            )LOOP
                IF(NVL(V_PRODUTO, 'N')<> C_ITCONSSAIDA.CDSUBGRPROD)THEN
                    V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
                    V_PRODUTO       := C_ITCONSSAIDA.CDSUBGRPROD;
                    INSERT INTO ITLANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        CDFILIALNFITROP,
                        NRLANCTONFITROP,
                        NRSEQITPRODROP,
                        CDITEMLANCROP,
                        QTITEMLANCROP,
                        VRITEMLANCROP,
                        CDGRUPPRODROP,
                        CDSUBGRPRODROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        C_CONSSAIDA.CDFILIAL,
                        C_CONSSAIDA.NRLANCTONF,
                        '',
                        C_ITCONSSAIDA.CDSUBGRPROD,
                        C_ITCONSSAIDA.QUANTIDADE,
                        C_ITCONSSAIDA.VALORTOTAL,
                        C_ITCONSSAIDA.CDGRUPPROD,
                        C_ITCONSSAIDA.CDSUBGRPROD
                    );

                    V_CONSUMO       := V_CONSUMO - C_ITCONSSAIDA.VALORTOTAL;
                END IF;

                IF V_PRIIMP = 0 THEN
                    FOR C_IMPOSTOCS IN(
                        SELECT CDIMPOSTO,
                               VALORIMPOSTO
                          FROM(
                            SELECT R.CDIMPOSTO,
                                   SUM(VRIMPOSTOIT)AS VALORIMPOSTO
                              FROM IMPOSTOROP     R,
                                   ITEMPROIMPOS   T,
                                   ITEMPRODUTO    I,
                                   PRODUTO        P,
                                   (
                                       SELECT A.CDFILISUBGR,
                                              A.CDSUBGRPROD,
                                              A.CDGRUPPROD,
                                              B.NMPRODUTO,
                                              B.CDARVPROD,
                                              B.CDPRODUTO
                                         FROM PRODUTO       B,
                                              NVSUBGRPROD   A
                                        WHERE(A.CDFILISUBGR             = V_CDFILISUBGR)AND
                                             (A.CDGRUPPROD              = V_CDGRUPPROD)AND
                                             (B.CDPRODUTO               = A.CDPRODUTO)AND
                                             (NVL(B.IDPRODATIVPR, 'S')  = 'S')
                                   )PROD
                             WHERE(I.CDFILIAL       = C_CONSSAIDA.CDFILIAL)AND
                                  (I.NRLANCTONF     = C_CONSSAIDA.NRLANCTONF)AND
                                  (I.CDFILIAL       = T.CDFILIAL)AND
                                  (I.NRLANCTONF     = T.NRLANCTONF)AND
                                  (I.NRSEQITPROD    = T.NRSEQITPROD)AND
                                  (T.NRITPROIMPOS   = '1')AND
                                  (T.CDIMPOSTO      = R.CDIMPOSTO)AND
                                  (R.CDFILIAL       = V_FILIALPAR)AND
                                  (R.IDTPIMPOSTOROP IN(
                                      'CS',
                                      'ST'
                                  ))AND
                                  (I.CDPRODUTO      = P.CDPRODUTO)AND
                                  (P.CDARVPROD LIKE PROD.CDARVPROD || '%')
                             GROUP BY R.CDIMPOSTO
                             ORDER BY R.CDIMPOSTO
                        )
                         WHERE ROWNUM <= 5
                    )LOOP
                        V_NRSEQRELACIT   := V_NRSEQRELACIT + 1;
                        INSERT INTO ITRELACITROP(
                            CDFILIAL,
                            NRROPFILIAL,
                            NRLANCROP,
                            NRITLANCROP,
                            NRSEQRELACITROP,
                            CDITRELITROP,
                            QTITRELITROP,
                            VRITRELITROP
                        )VALUES(
                            P_CDFILIAL,
                            V_NRROPFILIAL,
                            V_NRLANCROP,
                            V_NRITLANCROP,
                            V_NRSEQRELACIT,
                            C_IMPOSTOCS.CDIMPOSTO,
                            0,
                            ROUND(C_IMPOSTOCS.VALORIMPOSTO, 2)
                        );

                        V_PRIIMP         := 1;
                    END LOOP;

                END IF;

            END LOOP;

        END LOOP;

    END IF;

    IF(V_IDIMPLANTROP = 'S' OR V_IDAJUSTEROP = 'S')THEN
        V_DTLANCROP := '01/01/1990';
        SELECT MAX(CDARVPROD)AS CDPRODFIN,
               MIN(CDARVPROD)AS CDPRODINI
          INTO
            V_CDPRODFIN,
            V_CDPRODINI
          FROM PRODUTO       P,
               ITLANCTOEST   I
         WHERE CDFILIMOVI     = P_CDFILIAL AND
               DTLANCMOVI BETWEEN V_DTINIC AND P_DTFINAL AND
               I.CDPRODMOVI   = P.CDPRODUTO AND
               IDTIPOMOVI IN(
                   '0',
                   '1'
               );

        IF V_CDPRODFIN IS NOT NULL THEN
            SELECT LPAD(ABS(ROUND(DBMS_RANDOM.VALUE(9999, 9999999999))), 10, '0')
              INTO V_NRMOVIESTQ
              FROM DUAL;

            SELECT NVL(MAX(CASE WHEN (NVL(IDUTILALMOX,'N') = 'N') AND (NVL(IDUTILLCESTQ,'N') = 'N') AND (NVL(IDUTILLOTE,'N') = 'N')
                                THEN 'N'
                                ELSE 'S'
                           END),'N')
              INTO V_MOVIMCONSOL
              FROM PARAMFILIAL
             WHERE CDFILIAL = P_CDFILIAL;

            MOVIMENTACAO_ESTOQUE(P_CDFILIAL, NULL, NULL, NULL, 1,
                                 V_DTINIC, P_DTFINAL, V_CDPRODINI, V_CDPRODFIN, 3,
                                 1, V_NRMOVIESTQ, V_MOVIMCONSOL);

            V_PRODUTO   := '';
            V_NRSEQIT   := 'N';
            IF V_IDIMPLANTROP = 'S' THEN
                V_NRITLANCROP    := '00000';
                V_DTLANCROP := '01/01/1990';
                FOR C_IMPLANTACAO IN(
                    SELECT AN.CDGRUPPROD,
                           AN.CDSUBGRPROD,
                           DTLANCMOVIEST,
                           SUM(QTLANCAMENTO)QTLANCAMENTO,
                           SUM(VRDIFECUST)VRDIFECUST
                      FROM RELAT_MOVIEST   R,
                           PRODUTO         P,
                           SUBGRPRODFIL    SG,
                           (
                               SELECT B.CDPRODUTO,
                                      A.CDGRUPPROD,
                                      A.CDSUBGRPROD,
                                      A.CDFILISUBGR
                                 FROM PRODUTO   B,
                                      PRODUTO   C,
                                      (
                                          SELECT P.CDARVPROD AS CDPRODUTO,
                                                 N.CDGRUPPROD,
                                                 N.CDSUBGRPROD,
                                                 N.CDFILISUBGR
                                            FROM PRODUTO       P,
                                                 NVSUBGRPROD   N
                                           WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                                 N.CDFILISUBGR   = V_CDFILISUBGR AND
                                                 N.CDGRUPPROD    = V_CDGRUPPROD
                                      )A
                                WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                                      (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                                      (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                                      (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                                ORDER BY B.CDARVPROD
                           )AN
                     WHERE(R.CDFILIMOVIEST   = P_CDFILIAL)AND
                          (R.NRRELATMOVI     = V_NRMOVIESTQ)AND
                          (R.DTLANCMOVIEST BETWEEN V_DTINIC AND P_DTFINAL)AND
                          (SG.CDFILISUBGR    = V_CDFILISUBGR)AND
                          (SG.CDGRUPPROD     = V_CDGRUPPROD)AND
                          (AN.CDFILISUBGR    = SG.CDFILISUBGR)AND
                          (AN.CDGRUPPROD     = SG.CDGRUPPROD)AND
                          (AN.CDSUBGRPROD    = SG.CDSUBGRPROD)AND
                          (P.CDPRODUTO       = AN.CDPRODUTO)AND
                          (R.IDTIPOMOVIEST   = '0')AND
                          (R.CDPRODMOVIEST   = P.CDARVPROD)
                     GROUP BY DTLANCMOVIEST,
                              AN.CDGRUPPROD,
                              AN.CDSUBGRPROD
                     ORDER BY DTLANCMOVIEST,
                              AN.CDSUBGRPROD,
                              AN.CDGRUPPROD
                )LOOP
                    V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                    IF V_DTLANCROP <> C_IMPLANTACAO.DTLANCMOVIEST THEN
                        V_NRROPFILIAL   := NULL;
                        SELECT MAX(NRROPFILIAL)
                          INTO V_NRROPFILIAL
                          FROM ROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               DTFILIALROP   = C_IMPLANTACAO.DTLANCMOVIEST;

                        V_DTLANCROP     := C_IMPLANTACAO.DTLANCMOVIEST;
                        IF V_NRROPFILIAL IS NULL THEN
                            V_NRROPFILIAL := V_NRROPFILINI;
                        END IF;
                        SELECT MAX(NRLANCROP)
                          INTO V_NRLANCROP
                          FROM LANCROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               NRROPFILIAL   = V_NRROPFILIAL;

                        IF V_NRLANCROP IS NULL THEN
                            V_NRLANCROP   := 0;
                            V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                        ELSE
                            V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                        END IF;

                    END IF;

                    INSERT INTO LANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        DTLANCROP,
                        IDTPLANCROP,
                        CDCLIENTEROP,
                        CDTIPOOPERROP,
                        NRNOTAFISCROP,
                        CDSERIENFROP,
                        CDFILIALNFROP,
                        NRLANCTONFROP,
                        CDFILCENTROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        C_IMPLANTACAO.DTLANCMOVIEST,
                        'IM',
                        '',
                        '',
                        '',
                        '',
                        '',
                        '',
                        V_CDFILCENTPME
                    );

                    V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
                    INSERT INTO ITLANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        CDFILIALNFITROP,
                        NRLANCTONFITROP,
                        NRSEQITPRODROP,
                        CDITEMLANCROP,
                        QTITEMLANCROP,
                        VRITEMLANCROP,
                        CDGRUPPRODROP,
                        CDSUBGRPRODROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        '',
                        '',
                        '',
                        C_IMPLANTACAO.CDSUBGRPROD,
                        C_IMPLANTACAO.QTLANCAMENTO,
                        C_IMPLANTACAO.VRDIFECUST,
                        C_IMPLANTACAO.CDGRUPPROD,
                        C_IMPLANTACAO.CDSUBGRPROD
                    );

                    V_CONSUMO       := V_CONSUMO + C_IMPLANTACAO.VRDIFECUST;
                END LOOP;

            END IF;

            V_PRODUTO   := '';
            V_NRSEQIT   := 'N';
            IF V_IDAJUSTEROP = 'S' THEN
                V_NRITLANCROP    := '00000';
                V_DTLANCROP := '01/01/1990';
                FOR C_AJUSTE IN(
                    SELECT AN.CDGRUPPROD,
                           AN.CDSUBGRPROD,
                           DTLANCMOVIEST,
                           SUM(QTLANCAMENTO)QTLANCAMENTO,
                           SUM(VRDIFECUST)VRDIFECUST,
                           P.CDARVPROD,
                           P.IDPRODATIVPR
                      FROM RELAT_MOVIEST   R,
                           PRODUTO         P,
                           SUBGRPRODFIL    SG,
                           (
                               SELECT B.CDPRODUTO,
                                      A.CDGRUPPROD,
                                      A.CDSUBGRPROD,
                                      A.CDFILISUBGR
                                 FROM PRODUTO   B,
                                      PRODUTO   C,
                                      (
                                          SELECT P.CDARVPROD AS CDPRODUTO,
                                                 N.CDGRUPPROD,
                                                 N.CDSUBGRPROD,
                                                 N.CDFILISUBGR
                                            FROM PRODUTO       P,
                                                 NVSUBGRPROD   N
                                           WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                                 N.CDFILISUBGR   = V_CDFILISUBGR AND
                                                 N.CDGRUPPROD    = V_CDGRUPPROD
                                      )A
                                WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                                      (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                                      (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                                      (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                                ORDER BY B.CDARVPROD
                           )AN
                     WHERE(R.CDFILIMOVIEST   = P_CDFILIAL)AND
                          (R.NRRELATMOVI     = V_NRMOVIESTQ)AND
                          (R.DTLANCMOVIEST BETWEEN V_DTINIC AND P_DTFINAL)AND
                          (SG.CDGRUPPROD     = V_CDGRUPPROD)AND
                          (SG.CDFILISUBGR    = V_CDFILISUBGR)AND
                          (AN.CDFILISUBGR    = SG.CDFILISUBGR)AND
                          (AN.CDGRUPPROD     = SG.CDGRUPPROD)AND
                          (AN.CDSUBGRPROD    = SG.CDSUBGRPROD)AND
                          (P.CDPRODUTO       = AN.CDPRODUTO)AND
                          (R.IDTIPOMOVIEST   = '1')AND
                          (R.CDPRODMOVIEST   = P.CDARVPROD)
                     GROUP BY DTLANCMOVIEST,
                              AN.CDGRUPPROD,
                              AN.CDSUBGRPROD,
                              P.CDARVPROD,
                              P.IDPRODATIVPR
                     ORDER BY DTLANCMOVIEST,
                              AN.CDSUBGRPROD,
                              AN.CDGRUPPROD
                )LOOP
                    V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                    IF V_DTLANCROP <> C_AJUSTE.DTLANCMOVIEST THEN
                        V_NRROPFILIAL   := NULL;
                        SELECT MAX(NRROPFILIAL)
                          INTO V_NRROPFILIAL
                          FROM ROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               DTFILIALROP   = C_AJUSTE.DTLANCMOVIEST;

                        V_DTLANCROP     := C_AJUSTE.DTLANCMOVIEST;
                        IF V_NRROPFILIAL IS NULL THEN
                            V_NRROPFILIAL := V_NRROPFILINI;
                        END IF;
                        SELECT MAX(NRLANCROP)
                          INTO V_NRLANCROP
                          FROM LANCROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               NRROPFILIAL   = V_NRROPFILIAL;

                        IF V_NRLANCROP IS NULL THEN
                            V_NRLANCROP   := 0;
                            V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                        ELSE
                            V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                        END IF;

                    END IF;

                    INSERT INTO LANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        DTLANCROP,
                        IDTPLANCROP,
                        CDCLIENTEROP,
                        CDTIPOOPERROP,
                        NRNOTAFISCROP,
                        CDSERIENFROP,
                        CDFILIALNFROP,
                        NRLANCTONFROP,
                        CDFILCENTROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        C_AJUSTE.DTLANCMOVIEST,
                        'AJ',
                        '',
                        '',
                        '',
                        '',
                        '',
                        '',
                        V_CDFILCENTPME
                    );

                    V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
                    INSERT INTO ITLANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        NRITLANCROP,
                        CDFILIALNFITROP,
                        NRLANCTONFITROP,
                        NRSEQITPRODROP,
                        CDITEMLANCROP,
                        QTITEMLANCROP,
                        VRITEMLANCROP,
                        CDGRUPPRODROP,
                        CDSUBGRPRODROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        V_NRITLANCROP,
                        '',
                        '',
                        '',
                        C_AJUSTE.CDSUBGRPROD,
                        C_AJUSTE.QTLANCAMENTO,
                        C_AJUSTE.VRDIFECUST,
                        C_AJUSTE.CDGRUPPROD,
                        C_AJUSTE.CDSUBGRPROD
                    );

                END LOOP;

            END IF;

        END IF;

    END IF;

    DELETE FROM RELAT_MOVIEST
     WHERE(CDFILIMOVIEST   = P_CDFILIAL)AND
          (NRRELATMOVI     = V_NRMOVIESTQ);

    V_DTLANCROP     := '01/01/1990';
    V_DTLANCROP     := '01/01/1990';
    V_NRITLANCROP    := '00000';
    FOR C_RETIRADA IN(
        SELECT L.CDFILIAL,
               L.NRLANCESTQ,
               L.DTLANCESTQ
          FROM RETIRADAESTO   R,
               LANCTOESTOQ    L
         WHERE(L.CDFILIMOVI   = P_CDFILIAL)AND
              (L.DTLANCESTQ BETWEEN V_DTINIC AND P_DTFINAL)AND
              (L.IDTPLANCTO   = '3')AND
              (L.CDFILIAL     = R.CDFILIALLAN)AND
              (L.NRLANCESTQ   = R.NRLANCESTQ)
         ORDER BY L.NRLANCESTQ,
                  L.DTLANCESTQ
    )LOOP
        V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
        IF V_DTLANCROP <> C_RETIRADA.DTLANCESTQ THEN
            V_NRROPFILIAL   := NULL;
            SELECT MAX(NRROPFILIAL)
              INTO V_NRROPFILIAL
              FROM ROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   DTFILIALROP   = C_RETIRADA.DTLANCESTQ;

            V_DTLANCROP     := C_RETIRADA.DTLANCESTQ;
            IF V_NRROPFILIAL IS NULL THEN
                V_NRROPFILIAL := V_NRROPFILINI;
            END IF;
            SELECT MAX(NRLANCROP)
              INTO V_NRLANCROP
              FROM LANCROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   NRROPFILIAL   = V_NRROPFILIAL;

            IF V_NRLANCROP IS NULL THEN
                V_NRLANCROP   := 0;
                V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
            ELSE
                V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
            END IF;

        END IF;

        INSERT INTO LANCROPFILIAL(
            CDFILIAL,
            NRROPFILIAL,
            NRLANCROP,
            DTLANCROP,
            IDTPLANCROP,
            CDCLIENTEROP,
            CDTIPOOPERROP,
            NRNOTAFISCROP,
            CDSERIENFROP,
            CDFILIALNFROP,
            NRLANCTONFROP,
            CDFILCENTROP
        )VALUES(
            P_CDFILIAL,
            V_NRROPFILIAL,
            V_NRLANCROP,
            C_RETIRADA.DTLANCESTQ,
            'RE',
            '',
            '',
            '',
            '',
            C_RETIRADA.CDFILIAL,
            C_RETIRADA.NRLANCESTQ,
            V_CDFILCENTPME
        );

        FOR C_ITRETIRADA IN(
            SELECT CDPRODMOVI,
                   IT.QTLANCTOEST,
                   SG.CDGRUPPROD,
                   AN.CDSUBGRPROD,
                   (CUSTO_PRODUTO_LCEST(IT.CDFILIMOVI, IT.CDPRODMOVI, V_NRTIPOCUSTO, C_RETIRADA.DTLANCESTQ, SYSDATE,
                                        CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE)* IT.QTLANCTOEST)AS VRLANCTOESTQ
              FROM ITLANCTOEST    IT,
                   SUBGRPRODFIL   SG,
                   (
                       SELECT B.CDPRODUTO,
                              A.CDGRUPPROD,
                              A.CDSUBGRPROD,
                              A.CDFILISUBGR
                         FROM PRODUTO   B,
                              PRODUTO   C,
                              (
                                  SELECT P.CDARVPROD AS CDPRODUTO,
                                         N.CDGRUPPROD,
                                         N.CDSUBGRPROD,
                                         N.CDFILISUBGR
                                    FROM PRODUTO       P,
                                         NVSUBGRPROD   N
                                   WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                         N.CDFILISUBGR   = V_CDFILISUBGR AND
                                         N.CDGRUPPROD    = V_CDGRUPPROD
                              )A
                        WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                              (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                              (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                              (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                        ORDER BY B.CDARVPROD
                   )AN
             WHERE(IT.CDFILIAL               = C_RETIRADA.CDFILIAL)AND
                  (IT.NRLANCESTQ             = C_RETIRADA.NRLANCESTQ)AND
                  (NVL(IT.IDEVOLESTOQ, 'S')  = 'S')AND
                  SG.CDGRUPPROD             = V_CDGRUPPROD AND
                  SG.CDFILISUBGR            = V_CDFILISUBGR AND
                  SG.CDFILISUBGR            = AN.CDFILISUBGR AND
                  SG.CDGRUPPROD             = AN.CDGRUPPROD AND
                  SG.CDSUBGRPROD            = AN.CDSUBGRPROD AND
                  IT.CDPRODMOVI             = AN.CDPRODUTO
        )LOOP
            V_NRITLANCROP := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP,
                CDGRUPPRODROP,
                CDSUBGRPRODROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_RETIRADA.CDFILIAL,
                C_RETIRADA.NRLANCESTQ,
                '',
                C_ITRETIRADA.CDPRODMOVI,
                C_ITRETIRADA.QTLANCTOEST,
                C_ITRETIRADA.VRLANCTOESTQ,
                C_ITRETIRADA.CDGRUPPROD,
                C_ITRETIRADA.CDSUBGRPROD
            );

        END LOOP;

    END LOOP;

    IF(V_IDSERVRETROP <> 'S')THEN
        V_DTLANCROP := '01/01/1990';
        V_NRITLANCROP    := '00000';
        FOR C_RETIRADAPLA IN(
            SELECT DISTINCT PRO.CDSERVFILI,
                            PRO.DTCARDAPIO,
                            PRO.CDFILIALLANC,
                            PRO.NRLANCESTQ
              FROM(
                SELECT C.CDFILIAL,
                       C.CDSERVFILI,
                       DTCARDAPIO
                  FROM CARDADIA     C,
                       SERVICOROP   P
                 WHERE(C.CDFILIAL     = P_CDFILIAL)AND
                      (P.CDFILIAL     = V_FILIALPAR)AND
                      (C.CDSERVFILI   = P.CDSERVFILI)AND
                      (DTCARDAPIO BETWEEN V_DTINIC AND P_DTFINAL)
                 GROUP BY C.CDFILIAL,
                          C.CDSERVFILI,
                          DTCARDAPIO
            )COMENS,
                  (
                      SELECT CA.CDFILIAL,
                             CA.CDSERVFILI,
                             CA.DTCARDAPIO,
                             IL.CDFILIAL AS CDFILIALLANC,
                             IL.NRLANCESTQ
                        FROM CARDADIA      CA,
                             ITEMRETI      IR,
                             ITLANCTOEST   IL,
                             FILIAL        FI,
                             SERVICO       SE,
                             SERVICOROP    P,
                             PRODUTO       PRA
                       WHERE(CA.CDFILIAL               = P_CDFILIAL)AND
                            (P.CDFILIAL                = V_FILIALPAR)AND
                            (CA.CDSERVFILI             = P.CDSERVFILI)AND
                            (NOT IR.QTRETIRADA IS NULL AND
                                 IR.QTRETIRADA <> 0)AND
                            (IL.CDFILIAL               = IR.CDFIITLANEST)AND
                            (IL.NRLANCESTQ             = IR.NRLANCESTQ)AND
                            (IL.NRSEQUITEM             = IR.NRSEQUITEM)AND
                            (NVL(IL.IDEVOLESTOQ, 'S')  = 'S')AND
                            (CA.DTCARDAPIO BETWEEN V_DTINIC AND P_DTFINAL)AND
                            (CA.CDFILIAL               = IR.CDFILIAL)AND
                            (CA.NRCARDAPIO             = IR.NRCARDAPIO)AND
                            (PRA.CDPRODUTO             = IR.CDPRODUTO)AND
                            (FI.CDFILIAL               = CA.CDFILIAL)AND
                            (SE.CDFILIAL               = CA.CDFILIAL)AND
                            (SE.CDSERVFILI             = CA.CDSERVFILI)
                  )PRO
             WHERE(COMENS.CDFILIAL(+)     = PRO.CDFILIAL)AND
                  (COMENS.CDSERVFILI(+)   = PRO.CDSERVFILI)AND
                  (COMENS.DTCARDAPIO(+)   = PRO.DTCARDAPIO)
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_RETIRADAPLA.DTCARDAPIO THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_RETIRADAPLA.DTCARDAPIO;

                V_DTLANCROP     := C_RETIRADAPLA.DTCARDAPIO;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_RETIRADAPLA.DTCARDAPIO,
                'RP',
                '',
                '',
                '',
                '',
                C_RETIRADAPLA.CDFILIALLANC,
                C_RETIRADAPLA.NRLANCESTQ,
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := '00000';
            FOR C_ITRETIRADAPLA IN(
                SELECT R.CDFILIAL,
                       CDSERVFILI,
                       DTCARDAPIO,
                       SG.CDGRUPPROD,
                       AN.CDSUBGRPROD,
                       SUM(QTRETIRADA)QTRETIRADA,
                       SUM(VRCUSTPROD)AS VRCUSTPROD
                  FROM SUBGRPRODFIL SG,
                       (
                           SELECT B.CDPRODUTO,
                                  A.CDGRUPPROD,
                                  A.CDSUBGRPROD,
                                  A.CDFILISUBGR
                             FROM PRODUTO   B,
                                  PRODUTO   C,
                                  (
                                      SELECT P.CDARVPROD AS CDPRODUTO,
                                             N.CDGRUPPROD,
                                             N.CDSUBGRPROD,
                                             N.CDFILISUBGR
                                        FROM PRODUTO       P,
                                             NVSUBGRPROD   N
                                       WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                             N.CDFILISUBGR   = V_CDFILISUBGR AND
                                             N.CDGRUPPROD    = V_CDGRUPPROD
                                  )A
                            WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                                  (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                                  (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                                  (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                            ORDER BY B.CDARVPROD
                       )AN,
                       (
                           SELECT CDFILIAL,
                                  CDPRODUTO,
                                  CDSERVFILI,
                                  DTCARDAPIO,
                                  QTCOMEFETCAR,
                                  QTCLIEFETCAR,
                                  QTRETIRADA,
                                  CUSTO_PRODUTO(CDFILIAL, CDPRODUTO, V_NRTIPOCUSTO, DTCARDAPIO, SYSDATE)* QTRETIRADA AS VRCUSTPROD
                             FROM(
                               SELECT CA.CDFILIAL,
                                      CA.CDSERVFILI,
                                      CA.DTCARDAPIO,
                                      CDALMOXARIFE,
                                      CDLOCALESTOQ,
                                      NRLOTEESTQ,
                                      NRSUBLOTE,
                                      CA.QTCOMEFETCAR,
                                      CA.QTCLIEFETCAR,
                                      IR.CDPRODUTO,
                                      SUM(IR.QTRETIRADA * PRA.VRFATOCONV)AS QTRETIRADA
                                 FROM CARDADIA      CA,
                                      ITLANCTOEST   IL,
                                      SERVICOROP    P,
                                      ITEMRETI      IR,
                                      PRODUTO       PRA
                                WHERE(CA.CDFILIAL               = P_CDFILIAL)AND
                                     (P.CDFILIAL                = V_FILIALPAR)AND
                                     (CA.CDSERVFILI             = P.CDSERVFILI)AND
                                     (IDTPSERVICOROP            = 'R')AND
                                     (IL.CDFILIAL               = C_RETIRADAPLA.CDFILIALLANC)AND
                                     (IL.NRLANCESTQ             = C_RETIRADAPLA.NRLANCESTQ)AND
                                     (IL.CDFILIAL               = IR.CDFIITLANEST)AND
                                     (IL.NRLANCESTQ             = IR.NRLANCESTQ)AND
                                     (IL.NRSEQUITEM             = IR.NRSEQUITEM)AND
                                     (NVL(IL.IDEVOLESTOQ, 'S')  = 'S')AND
                                     (CA.CDFILIAL               = IR.CDFILIAL)AND
                                     (CA.NRCARDAPIO             = IR.NRCARDAPIO)AND
                                     (PRA.CDPRODUTO             = IR.CDPRODUTO)
                                GROUP BY CA.CDFILIAL,
                                         CA.CDSERVFILI,
                                         CA.DTCARDAPIO,
                                         CA.QTCOMEFETCAR,
                                         CA.QTCLIEFETCAR,
                                         CDALMOXARIFE,
                                         CDLOCALESTOQ,
                                         NRLOTEESTQ,
                                         NRSUBLOTE,
                                         IR.CDPRODUTO
                           )
                       )R
                 WHERE SG.CDFILISUBGR   = V_CDFILISUBGR AND
                       SG.CDGRUPPROD    = V_CDGRUPPROD AND
                       R.CDPRODUTO      = AN.CDPRODUTO AND
                       SG.CDFILISUBGR   = AN.CDFILISUBGR AND
                       SG.CDGRUPPROD    = AN.CDGRUPPROD AND
                       SG.CDSUBGRPROD   = AN.CDSUBGRPROD
                 GROUP BY R.CDFILIAL,
                          DTCARDAPIO,
                          CDSERVFILI,
                          SG.CDGRUPPROD,
                          AN.CDSUBGRPROD
            )LOOP
                V_NRITLANCROP := LPAD(V_NRITLANCROP + 1, 5, '0');
                INSERT INTO ITLANCROPFILIAL(
                    CDFILIAL,
                    NRROPFILIAL,
                    NRLANCROP,
                    NRITLANCROP,
                    CDFILIALNFITROP,
                    NRLANCTONFITROP,
                    NRSEQITPRODROP,
                    CDITEMLANCROP,
                    QTITEMLANCROP,
                    VRITEMLANCROP,
                    CDGRUPPRODROP,
                    CDSUBGRPRODROP
                )VALUES(
                    P_CDFILIAL,
                    V_NRROPFILIAL,
                    V_NRLANCROP,
                    V_NRITLANCROP,
                    C_RETIRADAPLA.CDFILIALLANC,
                    C_RETIRADAPLA.NRLANCESTQ,
                    '',
                    C_ITRETIRADAPLA.CDSERVFILI,
                    C_ITRETIRADAPLA.QTRETIRADA,
                    C_ITRETIRADAPLA.VRCUSTPROD,
                    C_ITRETIRADAPLA.CDGRUPPROD,
                    C_ITRETIRADAPLA.CDSUBGRPROD
                );

            END LOOP;

        END LOOP;

    END IF;

    IF(V_IDSERVRETROP = 'S')THEN
        V_DTLANCROP := '01/01/1990';
        V_NRITLANCROP    := '00000';
        FOR C_RETIRADAPLA IN(
            SELECT DISTINCT PRO.CDSERVFILI,
                            PRO.DTCARDAPIO,
                            PRO.CDFILIALLANC,
                            PRO.NRLANCESTQ
              FROM(
                SELECT C.CDFILIAL,
                       C.CDSERVFILI,
                       DTCARDAPIO
                  FROM CARDADIA C
                 WHERE(C.CDFILIAL = P_CDFILIAL)AND
                      (DTCARDAPIO BETWEEN V_DTINIC AND P_DTFINAL)
                 GROUP BY C.CDFILIAL,
                          C.CDSERVFILI,
                          DTCARDAPIO
            )COMENS,
                  (
                      SELECT CA.CDFILIAL,
                             CA.CDSERVFILI,
                             CA.DTCARDAPIO,
                             IL.CDFILIAL AS CDFILIALLANC,
                             IL.NRLANCESTQ
                        FROM CARDADIA      CA,
                             ITEMRETI      IR,
                             ITLANCTOEST   IL,
                             FILIAL        FI,
                             SERVICO       SE,
                             PRODUTO       PRA
                       WHERE(CA.CDFILIAL               = P_CDFILIAL)AND
                            (NOT IR.QTRETIRADA IS NULL AND
                                 IR.QTRETIRADA <> 0)AND
                            (IL.CDFILIAL               = IR.CDFIITLANEST)AND
                            (IL.NRLANCESTQ             = IR.NRLANCESTQ)AND
                            (IL.NRSEQUITEM             = IR.NRSEQUITEM)AND
                            (NVL(IL.IDEVOLESTOQ, 'S')  = 'S')AND
                            (CA.DTCARDAPIO BETWEEN V_DTINIC AND P_DTFINAL)AND
                            (CA.CDFILIAL               = IR.CDFILIAL)AND
                            (CA.NRCARDAPIO             = IR.NRCARDAPIO)AND
                            (PRA.CDPRODUTO             = IR.CDPRODUTO)AND
                            (FI.CDFILIAL               = CA.CDFILIAL)AND
                            (SE.CDFILIAL               = CA.CDFILIAL)AND
                            (SE.CDSERVFILI             = CA.CDSERVFILI)
                  )PRO
             WHERE(COMENS.CDFILIAL(+)     = PRO.CDFILIAL)AND
                  (COMENS.CDSERVFILI(+)   = PRO.CDSERVFILI)AND
                  (COMENS.DTCARDAPIO(+)   = PRO.DTCARDAPIO)
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_RETIRADAPLA.DTCARDAPIO THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_RETIRADAPLA.DTCARDAPIO;

                V_DTLANCROP     := C_RETIRADAPLA.DTCARDAPIO;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_RETIRADAPLA.DTCARDAPIO,
                'RP',
                '',
                '',
                '',
                '',
                C_RETIRADAPLA.CDFILIALLANC,
                C_RETIRADAPLA.NRLANCESTQ,
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := '00000';
            FOR C_ITRETIRADAPLA IN(
                SELECT R.CDFILIAL,
                       CDSERVFILI,
                       SG.CDGRUPPROD,
                       AN.CDSUBGRPROD,
                       DTCARDAPIO,
                       SUM(VRCUSTPROD)AS VRCUSTPROD,
                       SUM(QTRETIRADA)AS QTRETIRADA
                  FROM SUBGRPRODFIL SG,
                       (
                           SELECT B.CDPRODUTO,
                                  A.CDGRUPPROD,
                                  A.CDSUBGRPROD,
                                  A.CDFILISUBGR
                             FROM PRODUTO   B,
                                  PRODUTO   C,
                                  (
                                      SELECT P.CDARVPROD AS CDPRODUTO,
                                             N.CDGRUPPROD,
                                             N.CDSUBGRPROD,
                                             N.CDFILISUBGR
                                        FROM PRODUTO       P,
                                             NVSUBGRPROD   N
                                       WHERE P.CDPRODUTO     = N.CDPRODUTO AND
                                             N.CDFILISUBGR   = V_CDFILISUBGR AND
                                             N.CDGRUPPROD    = V_CDGRUPPROD
                                  )A
                            WHERE B.CDPRODUTO     = C.CDPRODUTO AND
                                  (A.CDFILISUBGR   = V_CDFILISUBGR)AND
                                  (A.CDGRUPPROD    = V_CDGRUPPROD)AND
                                  (C.CDARVPROD LIKE A.CDPRODUTO || '%')
                            ORDER BY B.CDARVPROD
                       )AN,
                       (
                           SELECT CDFILIAL,
                                  CDPRODUTO,
                                  CDSERVFILI,
                                  DTCARDAPIO,
                                  QTCOMEFETCAR,
                                  QTCLIEFETCAR,
                                  QTRETIRADA,
                                  CUSTO_PRODUTO_LCEST(CDFILIAL, CDPRODUTO, V_NRTIPOCUSTO, DTCARDAPIO, SYSDATE,
                                                      CDALMOXARIFE, CDLOCALESTOQ, NRLOTEESTQ, NRSUBLOTE)* QTRETIRADA AS VRCUSTPROD
                             FROM(
                               SELECT CA.CDFILIAL,
                                      CA.CDSERVFILI,
                                      CA.DTCARDAPIO,
                                      CDALMOXARIFE,
                                      CDLOCALESTOQ,
                                      NRLOTEESTQ,
                                      NRSUBLOTE,
                                      CA.QTCOMEFETCAR,
                                      CA.QTCLIEFETCAR,
                                      IR.CDPRODUTO,
                                      SUM(IR.QTRETIRADA * PRA.VRFATOCONV)AS QTRETIRADA
                                 FROM CARDADIA      CA,
                                      ITLANCTOEST   IL,
                                      ITEMRETI      IR,
                                      PRODUTO       PRA
                                WHERE(CA.CDFILIAL               = P_CDFILIAL)AND
                                     (IL.CDFILIAL               = C_RETIRADAPLA.CDFILIALLANC)AND
                                     (IL.NRLANCESTQ             = C_RETIRADAPLA.NRLANCESTQ)AND
                                     (IL.CDFILIAL               = IR.CDFIITLANEST)AND
                                     (IL.NRLANCESTQ             = IR.NRLANCESTQ)AND
                                     (IL.NRSEQUITEM             = IR.NRSEQUITEM)AND
                                     (NVL(IL.IDEVOLESTOQ, 'S')  = 'S')AND
                                     (CA.CDFILIAL               = IR.CDFILIAL)AND
                                     (CA.NRCARDAPIO             = IR.NRCARDAPIO)AND
                                     (PRA.CDPRODUTO             = IR.CDPRODUTO)
                                GROUP BY CA.CDFILIAL,
                                         CA.CDSERVFILI,
                                         CA.DTCARDAPIO,
                                         CA.QTCOMEFETCAR,
                                         CA.QTCLIEFETCAR,
                                         CDALMOXARIFE,
                                         CDLOCALESTOQ,
                                         NRLOTEESTQ,
                                         NRSUBLOTE,
                                         IR.CDPRODUTO
                           )
                       )R
                 WHERE SG.CDFILISUBGR   = V_CDFILISUBGR AND
                       SG.CDGRUPPROD    = V_CDGRUPPROD AND
                       SG.CDFILISUBGR   = AN.CDFILISUBGR AND
                       SG.CDGRUPPROD    = AN.CDGRUPPROD AND
                       SG.CDSUBGRPROD   = AN.CDSUBGRPROD AND
                       R.CDPRODUTO      = AN.CDPRODUTO
                 GROUP BY R.CDFILIAL,
                          DTCARDAPIO,
                          CDSERVFILI,
                          SG.CDGRUPPROD,
                          AN.CDSUBGRPROD
            )LOOP
                V_NRITLANCROP := LPAD(V_NRITLANCROP + 1, 5, '0');
                INSERT INTO ITLANCROPFILIAL(
                    CDFILIAL,
                    NRROPFILIAL,
                    NRLANCROP,
                    NRITLANCROP,
                    CDFILIALNFITROP,
                    NRLANCTONFITROP,
                    NRSEQITPRODROP,
                    CDITEMLANCROP,
                    QTITEMLANCROP,
                    VRITEMLANCROP,
                    CDGRUPPRODROP,
                    CDSUBGRPRODROP
                )VALUES(
                    P_CDFILIAL,
                    V_NRROPFILIAL,
                    V_NRLANCROP,
                    V_NRITLANCROP,
                    C_RETIRADAPLA.CDFILIALLANC,
                    C_RETIRADAPLA.NRLANCESTQ,
                    '',
                    C_ITRETIRADAPLA.CDSERVFILI,
                    C_ITRETIRADAPLA.QTRETIRADA,
                    C_ITRETIRADAPLA.VRCUSTPROD,
                    C_ITRETIRADAPLA.CDGRUPPROD,
                    C_ITRETIRADAPLA.CDSUBGRPROD
                );

            END LOOP;

        END LOOP;

    END IF;

    IF V_IDDESPDIVROP = 'S' THEN
        V_NRITLANCROP   := '00000';
        V_DTLANCROP     := '01/01/1990';
        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
                  (D.DTPRDESPFILI BETWEEN V_DTINIC AND P_DTFINAL)AND
                  ((DP.CDGRUPDESP   = V_CDGRUPDESP)OR
                   (V_CDGRUPDESP IS NULL))
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_DESPESASDIV.DTPRDESPFILI THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_DESPESASDIV.DTPRDESPFILI;

                V_DTLANCROP     := C_DESPESASDIV.DTPRDESPFILI;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            IF C_DESPESASDIV.IDDESTACAROP = 'S' THEN
                V_CONSUMO := V_CONSUMO + C_DESPESASDIV.VRREDESPFILI;
            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_DESPESASDIV.DTPRDESPFILI,
                DECODE(C_DESPESASDIV.IDDESTACAROP, 'S', 'CD', 'DD'),
                '',
                '',
                '',
                '',
                C_DESPESASDIV.CDFILIAL,
                '',
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_DESPESASDIV.CDFILIAL,
                '',
                '',
                C_DESPESASDIV.CDDESPPREV,
                1,
                C_DESPESASDIV.VRREDESPFILI
            );

        END LOOP;

    END IF;

    IF V_IDDESPMOBROP = 'S' THEN
        V_DTLANCROP     := '01/01/1990';
        V_NRITLANCROP   := '00000';
        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
                  (L.DTLANCDESFAT BETWEEN V_DTINIC AND P_DTFINAL)
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_DESPESASMO.DTLANCDESFAT THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_DESPESASMO.DTLANCDESFAT;

                V_DTLANCROP     := C_DESPESASMO.DTLANCDESFAT;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_DESPESASMO.DTLANCDESFAT,
                'DM',
                '',
                '',
                '',
                '',
                C_DESPESASMO.CDFILIAL,
                '',
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_DESPESASMO.CDFILIAL,
                '',
                '',
                C_DESPESASMO.CDDESPADMFAT,
                1,
                C_DESPESASMO.VRLANCDESFAT
            );

        END LOOP;

    END IF;

    IF V_IDDESPMOBROPCTB = 'S' THEN
        V_DTLANCROP     := '01/01/1990';
        V_NRITLANCROP   := '00000';
        IF V_DTINIC < P_DTINICIAL THEN
            V_DTFOLHA := P_DTINICIAL;
        ELSE
            V_DTFOLHA := V_DTINIC;
        END IF;

        FOR C_FOLHA IN(
            SELECT CDFILIAL,
                   CDCONTCTBL,
                   CDDESPADMFAT
              FROM DESPORCMOCCONT
             WHERE((CDFILIAL       = P_CDFILIAL)OR
                   (CDFILIAL       = 'T'))AND
                  CDDESPADMFAT   = '06'
        )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
                   TO_CHAR(DTCOMPCUSTO, 'MMYYYY')  = TO_CHAR(V_DTFOLHA, 'MMYYYY')
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> V_DTINIC THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = V_DTINIC;

                V_DTLANCROP     := V_DTINIC;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_DTFOLHA,
                'DM',
                '',
                '',
                '',
                '',
                C_FOLHA.CDFILIAL,
                '',
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_FOLHA.CDFILIAL,
                'CTB',
                '',
                C_FOLHA.CDDESPADMFAT,
                1,
                C_LANCCUST.VRLANC
            );

        END LOOP;
        END LOOP;

        FOR C_FOLHA IN(
            SELECT CDFILIAL,
                   CDCONTCTBL,
                   CDDESPADMFAT
              FROM DESPORCMOCCONT
             WHERE((CDFILIAL   = P_CDFILIAL)OR
                   (CDFILIAL   = 'T'))AND
                  CDDESPADMFAT <> '06'
        )LOOP FOR C_LANCCUST IN(
            SELECT VRLANC
              FROM(
                SELECT ROUND(SUM(VRLANCCUST), 2)VRLANC
                  FROM LANCCCUSTO
                 WHERE CDFILIAL                                 = P_CDFILIAL AND
                       CDCONTCTBL                               = C_FOLHA.CDCONTCTBL AND
                       TO_CHAR(TO_DATE(DTCOMPCUSTO), 'MMYYYY')  = TO_CHAR(TO_DATE(V_DTFOLHA), 'MMYYYY')
            )
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> V_DTINIC THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = V_DTINIC;

                V_DTLANCROP     := V_DTINIC;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_DTFOLHA,
                'DM',
                '',
                '',
                '',
                '',
                C_FOLHA.CDFILIAL,
                '',
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_FOLHA.CDFILIAL,
                'CTB',
                '',
                C_FOLHA.CDDESPADMFAT,
                1,
                C_LANCCUST.VRLANC
            );

        END LOOP;
        END LOOP;

    END IF;

    IF V_IDAPUMOCTPGROP = 'S' THEN
        V_DTLANCROP     := '01/01/1990';
        V_NRITLANCROP   := '00000';
        FOR C_CTPAGMOBRA IN(
            SELECT CP.IDSTATPAG,
                   CP.CDFILIBPAG,
                   CP.CDFILIAL,
                   CP.NRLANCTONF,
                   R.CDFIRATCTPAG,
                   NF.CDFORNECED,
                   NF.CDFILIRECE,
                   NF.NRNOTAFISC,
                   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,
                   DECODE(CP.VRATUAPAG, 0, 0, NVL(VRRATCCCTPAG, CP.VRATUAPAG))AS VRCONTARATEADA
              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
                     (NF.DTENTRSAID BETWEEN V_DTINIC AND P_DTFINAL))OR
                    (('VENCIMENTO'        = NVL(TC.IDDTAPUCTPAGAR, V_IDDTAPUCTPAGAR))AND
                     (CP.DTATUAVENPAG BETWEEN V_DTINIC AND P_DTFINAL))OR
                    (('BAIXA'             = NVL(TC.IDDTAPUCTPAGAR, V_IDDTAPUCTPAGAR))AND
                     (BP.DTBAIXAPAG BETWEEN V_DTINIC AND P_DTFINAL)))AND
                   TC.CDFILIAL         = V_FILIALPAR AND
                   TC.CDTPCTPAGAR      = CP.CDTPCTPAGAR AND
                   TC.IDTPCTPAGROP     = 'MAO' 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
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_CTPAGMOBRA.DTBAIXAPAG THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_CTPAGMOBRA.DTBAIXAPAG;

                V_DTLANCROP     := C_CTPAGMOBRA.DTBAIXAPAG;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                BEGIN
                    SELECT MAX(NRSEQVENPAG)
                      INTO V_NRMAXPARCELA
                      FROM CONTAPAGAR
                     WHERE CDFILIAL     = C_CTPAGMOBRA.CDFILIAL AND
                           NRLANCTONF   = C_CTPAGMOBRA.NRLANCTONF;

                EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                        V_NRMAXPARCELA := ' 00';
                END;

                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                NRSEQVENPAGROP,
                NRPARCELAPAGROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_CTPAGMOBRA.DTBAIXAPAG,
                'DM',
                C_CTPAGMOBRA.CDFORNECED,
                '',
                C_CTPAGMOBRA.NRNOTAFISC,
                '',
                C_CTPAGMOBRA.CDFILIAL,
                C_CTPAGMOBRA.NRLANCTONF,
                C_CTPAGMOBRA.NRSEQVENPAG,
                C_CTPAGMOBRA.NRPARCELAPAG,
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP,
                DSITLANCROPFILIAL
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                '',
                '',
                '',
                C_CTPAGMOBRA.CDTPCTPAGAR,
                1,
                C_CTPAGMOBRA.VRCONTARATEADA,
                'Parcela ' || C_CTPAGMOBRA.NRSEQVENPAG || ' de ' || V_NRMAXPARCELA
            );

        END LOOP;

    END IF;

    IF V_IDAPUCTPGROP = 'S' THEN
        V_NRITLANCROP   := '00000';
        V_DTLANCROP     := '01/01/1990';
        FOR C_CONTAPAGAR IN(
            SELECT CP.IDSTATPAG,
                   CP.CDFILIBPAG,
                   CP.CDFILIAL,
                   R.CDFIRATCTPAG,
                   CP.NRLANCTONF,
                   NF.CDFILIRECE,
                   NF.NRNOTAFISC,
                   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,
                   SUM(NVL(VRRATCCCTPAG, CP.VRATUAPAG))VRRATCCCTPAG,
                   CP.VRATUAPAG,
                   VRORIGPAG,
                   NF.CDFORNECED,
                   NVL(TC.IDDTAPUCTPAGAR, V_IDDTAPUCTPAGAR)AS IDDTAPUCTPAGAR
              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
                     (NF.DTENTRSAID BETWEEN V_DTINIC AND P_DTFINAL))OR
                    (('VENCIMENTO'        = NVL(TC.IDDTAPUCTPAGAR, V_IDDTAPUCTPAGAR))AND
                     (CP.DTATUAVENPAG BETWEEN V_DTINIC AND P_DTFINAL))OR
                    (('BAIXA'             = NVL(TC.IDDTAPUCTPAGAR, V_IDDTAPUCTPAGAR))AND
                     (BP.DTBAIXAPAG BETWEEN V_DTINIC AND P_DTFINAL)))AND
                   TC.CDFILIAL         = V_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))
             GROUP BY CP.IDSTATPAG,
                      CP.CDFILIBPAG,
                      CP.CDFILIAL,
                      R.CDFIRATCTPAG,
                      CP.NRLANCTONF,
                      NF.CDFILIRECE,
                      NF.NRNOTAFISC,
                      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)),
                      CP.VRATUAPAG,
                      VRORIGPAG,
                      NF.CDFORNECED,
                      NVL(TC.IDDTAPUCTPAGAR, V_IDDTAPUCTPAGAR)
             ORDER BY CDTPCTPAGAR,
                      DTBAIXAPAG
        )LOOP
            V_VALORCONTAPAGAR   := 0;
            IF C_CONTAPAGAR.VRATUAPAG <> 0 THEN
                V_VALORBRUTO     := CASE WHEN NVL(C_CONTAPAGAR.VRATUAPAG,0) = 0
                                         THEN 0
                                         ELSE (C_CONTAPAGAR.VRRATCCCTPAG / C_CONTAPAGAR.VRATUAPAG)
                                    END * C_CONTAPAGAR.VRORIGPAG;

                V_VALORLIQUIDO   :=(C_CONTAPAGAR.VRRATCCCTPAG);
            ELSE
                V_VALORBRUTO     := C_CONTAPAGAR.VRORIGPAG;
                V_VALORLIQUIDO   := C_CONTAPAGAR.VRATUAPAG;
            END IF;

            IF V_IDTPVRCTPAGAR = 'LIQUIDO' THEN
                V_VALORCONTAPAGAR := V_VALORLIQUIDO;
            ELSE
                V_VALORCONTAPAGAR := V_VALORBRUTO;
            END IF;

            BEGIN
                SELECT LPAD(CONT, 2, '0')
                  INTO V_NRMAXPARCELA
                  FROM(
                    SELECT COUNT(*)CONT
                      FROM CONTAPAGAR C
                     WHERE CDFILIAL     = C_CONTAPAGAR.CDFILIAL AND
                           NRLANCTONF   = C_CONTAPAGAR.NRLANCTONF
                     GROUP BY CDTPCTPAGAR
                )
                 WHERE ROWNUM = 1;

            EXCEPTION
                WHEN NO_DATA_FOUND THEN
                    V_NRMAXPARCELA := ' 00';
            END;

            SELECT LPAD(COUNT(*)+ 1, 2, '0')
              INTO V_NRPARCELA
              FROM ITLANCROPFILIAL   IT,
                   LANCROPFILIAL     L
             WHERE(L.CDFILIAL           = P_CDFILIAL)AND
                  (L.IDTPLANCROP        = 'DC')AND
                  (L.CDFILIAL           = IT.CDFILIAL)AND
                  (L.NRROPFILIAL        = IT.NRROPFILIAL)AND
                  (L.NRLANCROP          = IT.NRLANCROP)AND
                  (IT.NRLANCTONFITROP   = C_CONTAPAGAR.NRLANCTONF);

            IF C_CONTAPAGAR.IDDTAPUCTPAGAR <> 'VENCIMENTO' THEN
                V_STRINGPARCELA := 'Parcela 01 de 01';
            ELSE
                V_STRINGPARCELA := 'Parcela ' || V_NRPARCELA || ' de ' || V_NRMAXPARCELA;
            END IF;

            V_NRLANCROP         := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_CONTAPAGAR.DTBAIXAPAG THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_CONTAPAGAR.DTBAIXAPAG;

                V_DTLANCROP     := C_CONTAPAGAR.DTBAIXAPAG;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                NRSEQVENPAGROP,
                NRPARCELAPAGROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_CONTAPAGAR.DTBAIXAPAG,
                'DC',
                C_CONTAPAGAR.CDFORNECED,
                '',
                C_CONTAPAGAR.NRNOTAFISC,
                '',
                C_CONTAPAGAR.CDFILIAL,
                C_CONTAPAGAR.NRLANCTONF,
                C_CONTAPAGAR.NRSEQVENPAG,
                C_CONTAPAGAR.NRPARCELAPAG,
                V_CDFILCENTPME
            );

            V_NRITLANCROP       := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP,
                DSITLANCROPFILIAL
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                '',
                C_CONTAPAGAR.NRLANCTONF,
                '',
                C_CONTAPAGAR.CDTPCTPAGAR,
                1,
                V_VALORCONTAPAGAR,
                V_STRINGPARCELA
            );

            COMMIT;
        END LOOP;

    END IF;

    IF V_IDAPUCTRECROP = 'S' THEN
        V_NRITLANCROP    := '00000';
        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
                     (NF.DTENTRSAID BETWEEN V_DTINIC AND P_DTFINAL))OR
                    (('VENCIMENTO'        = V_IDDTAPUCTRECROP)AND
                     (CP.DTATUAVENREC BETWEEN V_DTINIC AND P_DTFINAL))OR
                    (('BAIXA'             = V_IDDTAPUCTRECROP)AND
                     (BP.DTBAIXAREC BETWEEN V_DTINIC AND P_DTFINAL)))AND
                   TC.CDFILIAL         = V_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
            V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_CREDDESP.DTBAIXAREC THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_CREDDESP.DTBAIXAREC;

                V_DTLANCROP     := C_CREDDESP.DTBAIXAREC;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_CREDDESP.DTBAIXAREC,
                'CR',
                C_CREDDESP.CDCLIENTE,
                '',
                C_CREDDESP.NRNOTAFISC,
                C_CREDDESP.CDSERIE,
                C_CREDDESP.CDFILIAL,
                C_CREDDESP.NRLANCTONF
            );

            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP,
                DSITLANCROPFILIAL
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_CREDDESP.CDFILIAL,
                '',
                '',
                C_CREDDESP.CDTPCTRECEB,
                1,
                C_CREDDESP.VRRATCCCTREC,
                'Reembolso com despesas'
            );

        END LOOP;
    END IF;

    IF V_IDAPUINVFILROP = 'S' THEN
        V_DTLANCROP     := '01/01/1990';
        V_NRITLANCROP   := '00000';
        FOR C_LANCINVEFILI IN(
            SELECT CDINVEST,
                   DTINVEFILI,
                   VRINVEFILI,
                   CDFILIAL
              FROM LANCINVEFILI
             WHERE CDFILIAL = P_CDFILIAL AND
                   DTINVEFILI BETWEEN V_DTINIC AND P_DTFINAL
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_LANCINVEFILI.DTINVEFILI THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_LANCINVEFILI.DTINVEFILI;

                V_DTLANCROP     := C_LANCINVEFILI.DTINVEFILI;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_LANCINVEFILI.DTINVEFILI,
                'IV',
                '',
                '',
                '',
                '',
                C_LANCINVEFILI.CDFILIAL,
                '',
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                C_LANCINVEFILI.CDFILIAL,
                '',
                '',
                C_LANCINVEFILI.CDINVEST,
                1,
                C_LANCINVEFILI.VRINVEFILI
            );

        END LOOP;

    END IF;

    IF V_IDAPURAINVCTPAGROP = 'S' THEN
        V_NRITLANCROP    := '00000';
        FOR C_CTPAGINV IN(
            SELECT CP.IDSTATPAG,
                   CP.CDFILIBPAG,
                   CP.CDFILIAL,
                   R.CDFIRATCTPAG,
                   CP.NRLANCTONF,
                   NF.CDFILIRECE,
                   NF.NRNOTAFISC,
                   CP.NRSEQVENPAG,
                   CP.NRPARCELAPAG,
                   TC.CDTPCTPAGAR,
                   (CP.DTATUAVENPAG)DTBAIXAPAG,
                   (DECODE(CP.VRATUAPAG, 0, 0, NVL(VRRATCCCTPAG, CP.VRATUAPAG)))AS VRCONTARATEADA,
                   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
                   (CP.DTATUAVENPAG BETWEEN V_DTINIC AND P_DTFINAL)AND
                   TC.CDFILIAL         = V_FILIALPAR AND
                   TC.CDTPCTPAGAR      = CP.CDTPCTPAGAR AND
                   TC.IDTPCTPAGROP     = 'INV' 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
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_CTPAGINV.DTBAIXAPAG THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_CTPAGINV.DTBAIXAPAG;

                V_DTLANCROP     := C_CTPAGINV.DTBAIXAPAG;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                BEGIN
                    SELECT MAX(NRSEQVENPAG)
                      INTO V_NRMAXPARCELA
                      FROM CONTAPAGAR
                     WHERE CDFILIAL     = C_CTPAGINV.CDFILIAL AND
                           NRLANCTONF   = C_CTPAGINV.NRLANCTONF;

                EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                        V_NRMAXPARCELA := ' 00';
                END;

                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                NRSEQVENPAGROP,
                NRPARCELAPAGROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_CTPAGINV.DTBAIXAPAG,
                'IV',
                C_CTPAGINV.CDFORNECED,
                '',
                C_CTPAGINV.NRNOTAFISC,
                '',
                C_CTPAGINV.CDFILIAL,
                C_CTPAGINV.NRLANCTONF,
                C_CTPAGINV.NRSEQVENPAG,
                C_CTPAGINV.NRPARCELAPAG,
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP,
                DSITLANCROPFILIAL
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                '',
                '',
                '',
                C_CTPAGINV.CDTPCTPAGAR,
                1,
                C_CTPAGINV.VRCONTARATEADA,
                'Parcela ' || C_CTPAGINV.NRSEQVENPAG || ' de ' || V_NRMAXPARCELA
            );

        END LOOP;
    END IF;

    IF V_IDDESPENTROP = 'S' THEN
        V_DTLANCROP        := '01/01/1990';
        V_NRITLANCROP      := '00000';
        V_DTDESPESA        := V_DTINIC;
        V_VRPEROTHERSRAT   := 0;
        WHILE TO_CHAR(V_DTDESPESA, 'YYYYMM')<= TO_CHAR(P_DTFINAL, 'YYYYMM')LOOP
            IF TO_CHAR(V_DTDESPESA, 'YYYYMM')= TO_CHAR(P_DTFINAL, 'YYYYMM')THEN
                V_DTDESPINI   := V_DTDESPESA;
                V_DTDESPFIM   := P_DTFINAL;
            ELSE
                V_DTDESPINI   := V_DTDESPESA;
                V_DTDESPFIM   := LAST_DAY(V_DTDESPESA);
            END IF;

            FOR C_PARCELASDESP IN(
                SELECT DISTINCT N.CDTIPOOPER,
                                N.NRNOTAFISC,
                                N.CDFILIAL,
                                N.CDFILIRECE,
                                N.NRLANCTONF,
                                N.DTENTRSAID,
                                C.DTATUAVENPAG,
                                N.CDSERIE,
                                N.CDFORNECED,
                                N.IDSTATUS,
                                N.DTINIREFEFAT,
                                N.DTFINREFEFAT,
                                N.IDENTRSAIDOP,
                                N.VRNOTAFISC
                  FROM NOTAFISCAL   N,
                       CONTAPAGAR   C,
                       RATEIONF     R,
                       (
                           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        = V_FILIALPAR
                       )TIPOOPER
                 WHERE(N.CDFILIAL               = C.CDFILIAL)AND
                      (N.NRLANCTONF             = C.NRLANCTONF)AND
                      (N.IDENTRSAIDOP           = 'E')AND
                      (N.CDFILIAL               = R.CDFILIAL(+))AND
                      (N.NRLANCTONF             = R.NRLANCTONF(+))AND
                      ((R.CDFILICCUST            = P_CDFILIAL)OR
                       (N.CDFILIRECE             = P_CDFILIAL))AND
                      (C.DTATUAVENPAG BETWEEN V_DTDESPINI AND V_DTDESPFIM)AND
                      (N.IDSISTEMA              = 'DFE')AND
                      (N.DTENTRSAID BETWEEN(C.DTATUAVENPAG - C.QTDIASVENPAG)AND C.DTATUAVENPAG)AND
                      (N.CDTIPOOPER             = TIPOOPER.CDTIPOOPER)AND
                      (N.IDENTRSAIDOP           = TIPOOPER.IDENTRSAIDOP)AND
                      (TIPOOPER.IDTPOPERACROP   = 'DE')AND
                      (V_FILIALPAR              = TIPOOPER.CDFILIAL)
                 ORDER BY DTATUAVENPAG
            )LOOP
                V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                IF V_DTLANCROP <> C_PARCELASDESP.DTATUAVENPAG THEN
                    V_NRROPFILIAL   := NULL;
                    SELECT MAX(NRROPFILIAL)
                      INTO V_NRROPFILIAL
                      FROM ROPFILIAL
                     WHERE CDFILIAL      = P_CDFILIAL AND
                           DTFILIALROP   = C_PARCELASDESP.DTATUAVENPAG;

                    V_DTLANCROP     := C_PARCELASDESP.DTATUAVENPAG;
                    IF V_NRROPFILIAL IS NULL THEN
                        V_NRROPFILIAL := V_NRROPFILINI;
                    END IF;
                    SELECT MAX(NRLANCROP)
                      INTO V_NRLANCROP
                      FROM LANCROPFILIAL
                     WHERE CDFILIAL      = P_CDFILIAL AND
                           NRROPFILIAL   = V_NRROPFILIAL;

                    IF V_NRLANCROP IS NULL THEN
                        V_NRLANCROP   := 0;
                        V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                    ELSE
                        V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                    END IF;

                END IF;

                INSERT INTO LANCROPFILIAL(
                    CDFILIAL,
                    NRROPFILIAL,
                    NRLANCROP,
                    DTLANCROP,
                    IDTPLANCROP,
                    CDCLIENTEROP,
                    CDTIPOOPERROP,
                    NRNOTAFISCROP,
                    CDSERIENFROP,
                    CDFILIALNFROP,
                    NRLANCTONFROP,
                    CDFILCENTROP,
                    IDSTATUSROP,
                    DTINIREFEROP,
                    DTFINREFEROP
                )VALUES(
                    P_CDFILIAL,
                    V_NRROPFILIAL,
                    V_NRLANCROP,
                    C_PARCELASDESP.DTATUAVENPAG,
                    'DE',
                    C_PARCELASDESP.CDFORNECED,
                    C_PARCELASDESP.CDTIPOOPER,
                    C_PARCELASDESP.NRNOTAFISC,
                    C_PARCELASDESP.CDSERIE,
                    C_PARCELASDESP.CDFILIAL,
                    C_PARCELASDESP.NRLANCTONF,
                    V_CDFILCENTPME,
                    C_PARCELASDESP.IDSTATUS,
                    C_PARCELASDESP.DTINIREFEFAT,
                    C_PARCELASDESP.DTFINREFEFAT
                );

                V_NRITLANCROP   := '00000';
                FOR C_ITPARCELASDESP IN(
                    SELECT DISTINCT I.CDPRODUTO,
                                    I.NRSEQITPROD,
                                    I.VRBRUTITEM,
                                    I.VRTOTAITEM   AS VRTOTALPROD,
                                    I.QTITEMPROD   AS QUANTIDADE,
                                    SUBSTR(P.CDARVPROD, 1, 1)NIVEL_INICIAL,
                                    P.CDARVPROD,
                                    NVL(IDDTAPUDESPENTR, V_IDDTAPUDESPENTR_GERAL)IDDTAPUDESPENTR
                      FROM ITEMPRODUTO   I,
                           PRODUTO       P,
                           PRODFILI      F,
                           (
                               SELECT DISTINCT A.CDPRODUTO AS CDPRODUTO,
                                               IDDTAPUDESPENTR
                                 FROM PRODUTOROP   R,
                                      PRODUTO      A,
                                      PRODUTO      B
                                WHERE(INSTR(A.CDARVPROD, B.CDARVPROD)  = 1)AND
                                     R.CDPRODUTO                      = B.CDPRODUTO AND
                                     R.CDFILIAL                       = V_FILIALPAR AND
                                     IDTPPRODUTOROP                   = 'DE'
                           )A
                     WHERE I.CDFILIAL               = C_PARCELASDESP.CDFILIAL AND
                           I.NRLANCTONF             = C_PARCELASDESP.NRLANCTONF AND
                           P.CDPRODUTO              = I.CDPRODUTO AND
                           F.CDFILIAL               = P_CDFILIAL AND
                           F.CDPRODUTO              = P.CDPRODESTO AND
                           NVL(F.IDCNTRESTOQ, 'N')  = 'N' AND
                           I.CDPRODUTO              = A.CDPRODUTO
                )LOOP 
                IF C_ITPARCELASDESP.IDDTAPUDESPENTR = 'VENCIMENTO' THEN
                    BEGIN
                        SELECT DECODE(COUNT(R.CDPRODUTO), 0, 'NAO', COUNT(R.CDPRODUTO))AS EXCECAO
                          INTO V_EXCECAO
                          FROM PRODUTOROP   R,
                               PRODUTO      B,
                               PRODUTO      A
                         WHERE R.CDPRODUTO      = B.CDPRODUTO AND
                               R.CDFILIAL       = V_FILIALPAR AND
                               A.CDPRODUTO      = C_ITPARCELASDESP.CDPRODUTO AND
                               A.CDARVPROD LIKE B.CDARVPROD || '%' AND
                               IDTPPRODUTOROP   = 'EX';

                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_EXCECAO := 'NAO';
                    END;

                    IF V_EXCECAO = 'NAO' THEN
                        V_VALOR_DESPESA := 0;
                        IF((V_IDAPUCTPGROP = 'S')OR(V_IDAPUMOCTPGROP = 'S')OR(V_IDAPURAINVCTPAGROP = 'S'))THEN
                            BEGIN
                                SELECT NVL(CDTPCTPAGAR, 'NAO')
                                  INTO V_HA_CONTAPAGAR
                                  FROM(
                                    SELECT CDTPCTPAGAR,
                                           CDNVPRODUTO
                                      FROM PRODUTO
                                     WHERE CDTPCTPAGAR IS NOT NULL AND
                                           CDARVPROD IN(
                                               SELECT MAX(T.CDARVPROD)
                                                 FROM PRODUTO T
                                                WHERE T.CDARVPROD BETWEEN C_ITPARCELASDESP.NIVEL_INICIAL AND C_ITPARCELASDESP.CDARVPROD
                                                GROUP BY T.CDNVPRODUTO
                                           )
                                     ORDER BY CDNVPRODUTO DESC
                                )
                                 WHERE ROWNUM = 1;

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_HA_CONTAPAGAR := 'NAO';
                            END;

                            IF V_HA_CONTAPAGAR <> 'NAO' 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)AND
                                                                  ((TP.IDTPCTPAGROP        = 'MAO' AND
                                                                    V_IDAPUMOCTPGROP       = 'S')OR
                                                                   (TP.IDTPCTPAGROP        = 'DSP' AND
                                                                    V_IDAPUCTPGROP         = 'S')OR
                                                                   (TP.IDTPCTPAGROP        = 'INV' AND
                                                                    V_IDAPURAINVCTPAGROP   = 'S'))
                                     WHERE C.NRLANCTONF     = C_PARCELASDESP.NRLANCTONF AND
                                           TP.CDTPCTPAGAR   = V_HA_CONTAPAGAR;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_CDTPCTPAGAR := 'NAO';
                                END;

                            ELSE
                                V_CDTPCTPAGAR := 'NAO';
                            END IF;

                        ELSE
                            V_CDTPCTPAGAR := 'NAO';
                        END IF;

                        IF V_CDTPCTPAGAR = 'NAO' THEN
                            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
                            V_VRRATENF      := 0;
                            IF C_ITPARCELASDESP.IDDTAPUDESPENTR = 'VENCIMENTO' THEN
                                BEGIN
                                    SELECT SUM(VRATUAPAG)VRATUAPAG,
                                           MAX(NRPARCELAPAG)
                                      INTO
                                        V_VRTOTAL_NF_PARCELADA,
                                        V_NRMAXPARCELA
                                      FROM CONTAPAGAR
                                     WHERE CDFILIAL     = C_PARCELASDESP.CDFILIAL AND
                                           NRLANCTONF   = C_PARCELASDESP.NRLANCTONF;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_VRTOTAL_NF_PARCELADA := 0;
                                END;

                                V_PERCENT_PROD_NF := CASE WHEN NVL(V_VRTOTAL_NF_PARCELADA,0) = 0
                                                          THEN 0
                                                          ELSE (C_ITPARCELASDESP.VRTOTALPROD * 100)
                                                     END / V_VRTOTAL_NF_PARCELADA;
                                BEGIN
                                    SELECT SUM(VRATUAPAG)VRATUAPAG
                                      INTO V_VRPARCELA_PERIODO
                                      FROM CONTAPAGAR
                                     WHERE CDFILIAL       = C_PARCELASDESP.CDFILIAL AND
                                           NRLANCTONF     = C_PARCELASDESP.NRLANCTONF AND
                                           DTATUAVENPAG   = C_PARCELASDESP.DTATUAVENPAG;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_VRPARCELA_PERIODO := 0;
                                END;

                                BEGIN
                                    SELECT LPAD(CONT, 2, '0')
                                      INTO V_NRMAXPARCELA
                                      FROM(
                                        SELECT COUNT(*)CONT
                                          FROM CONTAPAGAR C
                                         WHERE CDFILIAL     = C_PARCELASDESP.CDFILIAL AND
                                               NRLANCTONF   = C_PARCELASDESP.NRLANCTONF
                                         GROUP BY CDTPCTPAGAR
                                    )
                                     WHERE ROWNUM = 1;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_NRMAXPARCELA := 00;
                                END;

                                SELECT LPAD(COUNT(*)+ 1, 2, '0')
                                  INTO V_NRPARCELA
                                  FROM ITLANCROPFILIAL   IT,
                                       LANCROPFILIAL     L
                                 WHERE(L.CDFILIAL           = P_CDFILIAL)AND
                                      (L.IDTPLANCROP        = 'DE')AND
                                      (L.CDFILIAL           = IT.CDFILIAL)AND
                                      (L.NRROPFILIAL        = IT.NRROPFILIAL)AND
                                      (L.NRLANCROP          = IT.NRLANCROP)AND
                                      (IT.NRLANCTONFITROP   = C_PARCELASDESP.NRLANCTONF)AND
                                      (IT.CDITEMLANCROP     = C_ITPARCELASDESP.CDPRODUTO)AND
                                      (IT.NRSEQITPRODROP    = C_ITPARCELASDESP.NRSEQITPROD);

                                V_VALORPRODUTO_PARCELA   := CASE WHEN NVL(V_PERCENT_PROD_NF,0) = 0
                                                                 THEN 0
                                                                 ELSE V_VRPARCELA_PERIODO / 100 * V_PERCENT_PROD_NF
                                                            END;
                                BEGIN
                                    SELECT ROUND(NVL(SUM(R.VRPERRAT), 0), 2)
                                      INTO V_VRPEROTHERSRAT
                                      FROM RATEIONF R
                                     WHERE R.CDFILICCUST <> P_CDFILIAL AND
                                           R.CDFILIAL          = C_PARCELASDESP.CDFILIAL AND
                                           R.NRLANCTONF        = C_PARCELASDESP.NRLANCTONF AND
                                           R.NRSEQITPROD       = C_ITPARCELASDESP.NRSEQITPROD AND
                                           V_IDCONSPERRATFIL   = 'S';

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_VRPEROTHERSRAT := 0;
                                END;

                                IF V_VRPEROTHERSRAT < 100 THEN
                                    BEGIN
                                        SELECT NVL(R.VRRATENF, C_ITPARCELASDESP.VRTOTALPROD)AS VRRATENF,
                                               R.VRPERRAT
                                          INTO
                                            V_VALOR_RATEIO,
                                            V_PERCENT_RATEIO
                                          FROM RATEIONF R
                                         WHERE R.CDFILICCUST   = P_CDFILIAL AND
                                               R.CDFILIAL      = C_PARCELASDESP.CDFILIAL AND
                                               R.NRLANCTONF    = C_PARCELASDESP.NRLANCTONF AND
                                               R.NRSEQITPROD   = C_ITPARCELASDESP.NRSEQITPROD AND
                                               R.VRRATENF > 0;

                                    EXCEPTION
                                        WHEN NO_DATA_FOUND THEN
                                            V_VALOR_RATEIO     := C_ITPARCELASDESP.VRTOTALPROD;
                                            V_PERCENT_RATEIO   := 100;
                                    END;
                                ELSE
                                    V_VALOR_RATEIO := 0;
                                END IF;

                                IF V_VALOR_RATEIO > 0 THEN
                                    V_VALOR_RATEIO_PRODUTO_PARCELA   := CASE WHEN NVL(V_VALORPRODUTO_PARCELA,0) = 0
                                                                             THEN 0
                                                                             ELSE V_PERCENT_RATEIO / 100 * V_VALORPRODUTO_PARCELA
                                                                        END;
                                    V_VALOR_DESPESA                  := V_VALOR_RATEIO_PRODUTO_PARCELA;
                                ELSE
                                    IF V_VRPEROTHERSRAT = 100 THEN
                                        V_VALOR_DESPESA := 0;
                                    ELSE
                                        V_VALOR_DESPESA := V_VALORPRODUTO_PARCELA;
                                    END IF;
                                END IF;

                            ELSE
                                BEGIN
                                    SELECT NVL(SUM(R.VRRATENF), C_ITPARCELASDESP.VRTOTALPROD)AS VRRATENF
                                      INTO V_VRRATENF
                                      FROM RATEIONF R
                                     WHERE R.CDFILICCUST   = P_CDFILIAL AND
                                           R.CDFILIAL      = C_PARCELASDESP.CDFILIAL AND
                                           R.NRLANCTONF    = C_PARCELASDESP.NRLANCTONF AND
                                           R.NRSEQITPROD   = C_ITPARCELASDESP.NRSEQITPROD AND
                                           R.VRRATENF > 0;

                                EXCEPTION
                                    WHEN NO_DATA_FOUND THEN
                                        V_VRRATENF := 0;
                                END;

                                V_VALOR_DESPESA := V_VRRATENF;
                            END IF;

                            IF V_VALOR_DESPESA > 0 THEN
                                V_NRITLANCROP := LPAD(V_NRITLANCROP + 1, 5, '0');
                                INSERT INTO ITLANCROPFILIAL(
                                    CDFILIAL,
                                    NRROPFILIAL,
                                    NRLANCROP,
                                    NRITLANCROP,
                                    CDFILIALNFITROP,
                                    NRLANCTONFITROP,
                                    NRSEQITPRODROP,
                                    CDITEMLANCROP,
                                    QTITEMLANCROP,
                                    VRITEMLANCROP,
                                    DSITLANCROPFILIAL
                                )VALUES(
                                    P_CDFILIAL,
                                    V_NRROPFILIAL,
                                    V_NRLANCROP,
                                    V_NRITLANCROP,
                                    C_PARCELASDESP.CDFILIAL,
                                    C_PARCELASDESP.NRLANCTONF,
                                    C_ITPARCELASDESP.NRSEQITPROD,
                                    C_ITPARCELASDESP.CDPRODUTO,
                                    C_ITPARCELASDESP.QUANTIDADE,
                                    V_VALOR_DESPESA,
                                    'Parcela ' || V_NRPARCELA || ' de ' || V_NRMAXPARCELA
                                );

                            END IF;

                        END IF;

                    END IF;

                END IF;
                END LOOP;

            END LOOP;

            COMMIT;
            IF V_IDCONSPERRATFIL = 'N' THEN
                FOR C_DESPCOMPRA IN(
                    SELECT DISTINCT TIPO,
                                    NF.CDTIPOOPER,
                                    NF.NRNOTAFISC,
                                    NF.CDFILIAL,
                                    NF.CDFILIRECE,
                                    NF.NRLANCTONF,
                                    NF.DTENTRSAID,
                                    NF.CDSERIE,
                                    NF.CDFORNECED,
                                    NF.IDSTATUS,
                                    NF.DTINIREFEFAT,
                                    NF.DTFINREFEFAT,
                                    NF.CDFILICCUST,
                                    TIPOOPER.CDTPCTPAGAR,
                                    NF.VRNOTAFISC,
                                    NF.VRATUAPAG
                      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        = V_FILIALPAR
                    )TIPOOPER,
                          (
                              SELECT DISTINCT 'NF' AS TIPO,
                                              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,
                                              N.VRNOTAFISC,
                                              0 VRATUAPAG
                                FROM NOTAFISCAL    N,
                                     ITEMPRODUTO   I
                               WHERE(N.CDFILIRECE     = P_CDFILIAL)AND
                                    (N.IDENTRSAIDOP   = 'E')AND
                                    (N.DTENTRSAID BETWEEN V_DTDESPINI AND V_DTDESPFIM)AND
                                    (N.IDSTATUS <> 'C')AND
                                    (N.IDSISTEMA      = 'DFE')AND
                                    (N.CDFILIAL       = I.CDFILIAL)AND
                                    (N.NRLANCTONF     = I.NRLANCTONF)
                          )NF
                     WHERE(NF.CDTIPOOPER            = TIPOOPER.CDTIPOOPER)AND
                          (NF.IDENTRSAIDOP          = TIPOOPER.IDENTRSAIDOP)AND
                          (TIPOOPER.IDTPOPERACROP   = 'DE')AND
                          (V_FILIALPAR              = TIPOOPER.CDFILIAL)
                )LOOP
                    V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                    IF V_DTLANCROP <> C_DESPCOMPRA.DTENTRSAID THEN
                        V_NRROPFILIAL   := NULL;
                        SELECT MAX(NRROPFILIAL)
                          INTO V_NRROPFILIAL
                          FROM ROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               DTFILIALROP   = C_DESPCOMPRA.DTENTRSAID;

                        V_DTLANCROP     := C_DESPCOMPRA.DTENTRSAID;
                        IF V_NRROPFILIAL IS NULL THEN
                            V_NRROPFILIAL := V_NRROPFILINI;
                        END IF;
                        SELECT MAX(NRLANCROP)
                          INTO V_NRLANCROP
                          FROM LANCROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               NRROPFILIAL   = V_NRROPFILIAL;

                        IF V_NRLANCROP IS NULL THEN
                            V_NRLANCROP   := 0;
                            V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                        ELSE
                            V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                        END IF;

                    END IF;

                    INSERT INTO LANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        DTLANCROP,
                        IDTPLANCROP,
                        CDCLIENTEROP,
                        CDTIPOOPERROP,
                        NRNOTAFISCROP,
                        CDSERIENFROP,
                        CDFILIALNFROP,
                        NRLANCTONFROP,
                        CDFILCENTROP,
                        IDSTATUSROP,
                        DTINIREFEROP,
                        DTFINREFEROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        C_DESPCOMPRA.DTENTRSAID,
                        'DE',
                        C_DESPCOMPRA.CDFORNECED,
                        C_DESPCOMPRA.CDTIPOOPER,
                        C_DESPCOMPRA.NRNOTAFISC,
                        C_DESPCOMPRA.CDSERIE,
                        C_DESPCOMPRA.CDFILIAL,
                        C_DESPCOMPRA.NRLANCTONF,
                        V_CDFILCENTPME,
                        C_DESPCOMPRA.IDSTATUS,
                        C_DESPCOMPRA.DTINIREFEFAT,
                        C_DESPCOMPRA.DTFINREFEFAT
                    );

                    V_NRITLANCROP   := '00000';
                    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,
                                        NVL(IDDTAPUDESPENTR, V_IDDTAPUDESPENTR_GERAL)IDDTAPUDESPENTR,
                                        I.VRTOTAITEM   AS VRTOTALPROD
                          FROM ITEMPRODUTO   I,
                               PRODUTO       P,
                               PRODFILI      F,
                               (
                                   SELECT DISTINCT A.CDPRODUTO AS CDPRODUTO,
                                                   IDDTAPUDESPENTR
                                     FROM PRODUTOROP   R,
                                          PRODUTO      A,
                                          PRODUTO      B
                                    WHERE(INSTR(A.CDARVPROD, B.CDARVPROD)  = 1)AND
                                         R.CDPRODUTO                      = B.CDPRODUTO AND
                                         R.CDFILIAL                       = V_FILIALPAR AND
                                         IDTPPRODUTOROP                   = 'DE'
                               )PRODUTOROP
                         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
                               NVL(F.IDCNTRESTOQ, 'N')  = 'N' AND
                               I.CDPRODUTO              = PRODUTOROP.CDPRODUTO
                    )LOOP
                        BEGIN
                            SELECT IT.CDITEMLANCROP
                              INTO V_DESPESAEXISTE
                              FROM LANCROPFILIAL     L,
                                   ITLANCROPFILIAL   IT
                             WHERE L.CDFILIAL           = IT.CDFILIAL AND
                                   L.NRROPFILIAL        = IT.NRROPFILIAL AND
                                   L.NRLANCROP          = IT.NRLANCROP AND
                                   L.DTLANCROP BETWEEN V_DTDESPINI AND V_DTDESPFIM AND
                                   L.CDFILIAL           = P_CDFILIAL AND
                                   L.IDTPLANCROP        = 'DE' AND
                                   IT.NRLANCTONFITROP   = C_DESPCOMPRA.NRLANCTONF AND
                                   IT.CDITEMLANCROP     = C_ITDESPCOMPRA.CDPRODUTO  AND
                                   IT.NRSEQITPRODROP    = C_ITDESPCOMPRA.NRSEQITPROD;

                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_DESPESAEXISTE := NULL;
                        END;

                        IF V_DESPESAEXISTE IS NULL THEN
                            BEGIN
                                SELECT DECODE(COUNT(R.CDPRODUTO), 0, 'NAO', COUNT(R.CDPRODUTO))AS EXCECAO
                                  INTO V_EXCECAO
                                  FROM PRODUTOROP   R,
                                       PRODUTO      B,
                                       PRODUTO      A
                                 WHERE R.CDPRODUTO      = B.CDPRODUTO AND
                                       R.CDFILIAL       = V_FILIALPAR AND
                                       A.CDPRODUTO      = C_ITDESPCOMPRA.CDPRODUTO AND
                                       A.CDARVPROD LIKE B.CDARVPROD || '%' AND
                                       IDTPPRODUTOROP   = 'EX';

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_EXCECAO := 'NAO';
                            END;

                            IF V_EXCECAO = 'NAO' THEN
                                IF((V_IDAPUCTPGROP = 'S')OR(V_IDAPUMOCTPGROP = 'S')OR(V_IDAPURAINVCTPAGROP = 'S'))THEN
                                    BEGIN
                                        SELECT NVL(CDTPCTPAGAR, 'NAO')
                                          INTO V_HA_CONTAPAGAR
                                          FROM(
                                            SELECT CDTPCTPAGAR,
                                                   CDNVPRODUTO
                                              FROM PRODUTO
                                             WHERE CDTPCTPAGAR IS NOT NULL AND
                                                   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
                                                   )
                                             ORDER BY CDNVPRODUTO DESC
                                        )
                                         WHERE ROWNUM = 1;

                                    EXCEPTION
                                        WHEN NO_DATA_FOUND THEN
                                            V_HA_CONTAPAGAR := 'NAO';
                                    END;

                                    IF V_HA_CONTAPAGAR <> 'NAO' 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
                                                                          )AND
                                                                          ((TP.IDTPCTPAGROP        = 'MAO' AND
                                                                            V_IDAPUMOCTPGROP       = 'S')OR
                                                                           (TP.IDTPCTPAGROP        = 'DSP' AND
                                                                            V_IDAPUCTPGROP         = 'S')OR
                                                                           (TP.IDTPCTPAGROP        = 'INV' AND
                                                                            V_IDAPURAINVCTPAGROP   = 'S'))
                                             WHERE C.NRLANCTONF     = C_DESPCOMPRA.NRLANCTONF AND
                                                   TP.CDTPCTPAGAR   = V_HA_CONTAPAGAR;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_CDTPCTPAGAR := 'NAO';
                                        END;

                                    ELSE
                                        V_CDTPCTPAGAR := 'NAO';
                                    END IF;

                                ELSE
                                    V_CDTPCTPAGAR := 'NAO';
                                END IF;

                                IF V_CDTPCTPAGAR = 'NAO' THEN
                                    V_VALOR_DESPESA   := 0;
                                    IF C_ITDESPCOMPRA.IDDTAPUDESPENTR = 'VENCIMENTO' THEN
                                        BEGIN
                                            SELECT SUM(VRATUAPAG)VRATUAPAG
                                              INTO V_VRTOTAL_NF_PARCELADA
                                              FROM CONTAPAGAR
                                             WHERE CDFILIAL     = C_DESPCOMPRA.CDFILIAL AND
                                                   NRLANCTONF   = C_DESPCOMPRA.NRLANCTONF;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_VRTOTAL_NF_PARCELADA := 0;
                                        END;

                                        V_PERCENT_PROD_NF := CASE WHEN NVL(V_VRTOTAL_NF_PARCELADA,0) = 0
                                                                  THEN 0
                                                                  ELSE ROUND(C_ITDESPCOMPRA.VRTOTALPROD * 100 / V_VRTOTAL_NF_PARCELADA, 2)
                                                             END;
                                        BEGIN
                                            SELECT SUM(VRATUAPAG)VRATUAPAG
                                              INTO V_VRPARCELA_PERIODO
                                              FROM CONTAPAGAR
                                             WHERE CDFILIAL     = C_DESPCOMPRA.CDFILIAL AND
                                                   NRLANCTONF   = C_DESPCOMPRA.NRLANCTONF AND
                                                   DTATUAVENPAG BETWEEN V_DTDESPINI AND V_DTDESPFIM;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_VRPARCELA_PERIODO := 0;
                                        END; 

                                        /*MAXIMO DE PARCELAS*/

                                        BEGIN
                                            SELECT LPAD(CONT, 2, '0')
                                              INTO V_NRMAXPARCELA
                                              FROM(
                                                SELECT COUNT(*)CONT
                                                  FROM CONTAPAGAR C
                                                 WHERE CDFILIAL     = C_DESPCOMPRA.CDFILIAL AND
                                                       NRLANCTONF   = C_DESPCOMPRA.NRLANCTONF
                                                 GROUP BY CDTPCTPAGAR
                                            )
                                             WHERE ROWNUM = 1;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_NRMAXPARCELA := 00;
                                        END;

                                         /*NUMERO DE PARCELAS*/

                                        SELECT LPAD(COUNT(*)+ 1, 2, '0')
                                          INTO V_NRPARCELA
                                          FROM ITLANCROPFILIAL   IT,
                                               LANCROPFILIAL     L
                                         WHERE(L.CDFILIAL           = P_CDFILIAL)AND
                                              (L.IDTPLANCROP        = 'DE')AND
                                              (L.CDFILIAL           = IT.CDFILIAL)AND
                                              (L.NRROPFILIAL        = IT.NRROPFILIAL)AND
                                              (L.NRLANCROP          = IT.NRLANCROP)AND
                                              (IT.NRLANCTONFITROP   = C_DESPCOMPRA.NRLANCTONF)AND
                                              (IT.CDITEMLANCROP     = C_ITDESPCOMPRA.CDPRODUTO);

                                        V_VALORPRODUTO_PARCELA   := CASE WHEN NVL(V_PERCENT_PROD_NF,0) = 0
                                                                         THEN 0
                                                                         ELSE V_VRPARCELA_PERIODO / 100 * V_PERCENT_PROD_NF
                                                                    END;
                                        V_VALOR_DESPESA          := V_VALORPRODUTO_PARCELA;
                                        V_STRINGPARCELA          := 'Parcela ' || V_NRPARCELA || ' de ' || V_NRMAXPARCELA;
                                    ELSE
                                        V_VALOR_DESPESA   := C_ITDESPCOMPRA.VRBRUTITEM;
                                        V_STRINGPARCELA   := 'Parcela 01 de 01';
                                    END IF;
                                    
                                    IF V_VALOR_DESPESA > 0 THEN
                                        V_NRITLANCROP := LPAD(V_NRITLANCROP + 1, 5, '0');
                                        INSERT INTO ITLANCROPFILIAL(
                                            CDFILIAL,
                                            NRROPFILIAL,
                                            NRLANCROP,
                                            NRITLANCROP,
                                            CDFILIALNFITROP,
                                            NRLANCTONFITROP,
                                            NRSEQITPRODROP,
                                            CDITEMLANCROP,
                                            QTITEMLANCROP,
                                            VRITEMLANCROP,
                                            DSITLANCROPFILIAL
                                        )VALUES(
                                            P_CDFILIAL,
                                            V_NRROPFILIAL,
                                            V_NRLANCROP,
                                            V_NRITLANCROP,
                                            C_DESPCOMPRA.CDFILIAL,
                                            C_DESPCOMPRA.NRLANCTONF,
                                            C_ITDESPCOMPRA.NRSEQITPROD,
                                            C_ITDESPCOMPRA.CDPRODUTO,
                                            C_ITDESPCOMPRA.QUANTIDADE,
                                            V_VALOR_DESPESA,
                                            V_STRINGPARCELA
                                        );

                                    END IF;

                                END IF;

                            END IF;

                        END IF;

                    END LOOP;

                END LOOP;
            ELSE
                FOR C_DESPRATEIO 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,
                                    TIPOOPER.CDTPCTPAGAR,
                                    NF.VRNOTAFISC
                      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        = V_FILIALPAR
                    )TIPOOPER,
                          (
                              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,
                                              N.VRNOTAFISC
                                FROM NOTAFISCAL N
                               WHERE(N.CDFILIRECE     = P_CDFILIAL)AND
                                    (N.IDENTRSAIDOP   = 'E')AND
                                    (N.DTENTRSAID BETWEEN V_DTDESPINI AND V_DTDESPFIM)AND
                                    (N.IDSTATUS <> 'C')AND
                                    (N.IDSISTEMA      = 'DFE')
                              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,
                                              R.CDFILICCUST,
                                              N.IDENTRSAIDOP,
                                              N.VRNOTAFISC
                                FROM NOTAFISCAL   N,
                                     RATEIONF     R
                               WHERE(N.IDENTRSAIDOP   = 'E')AND
                                    (N.DTENTRSAID BETWEEN V_DTDESPINI AND V_DTDESPFIM)AND
                                    (N.IDSISTEMA      = 'DFE')AND
                                    (N.CDFILIAL       = R.CDFILIAL)AND
                                    (N.NRLANCTONF     = R.NRLANCTONF)AND
                                    (R.CDFILICCUST    = P_CDFILIAL)
                          )NF
                     WHERE(NF.CDTIPOOPER            = TIPOOPER.CDTIPOOPER)AND
                          (NF.IDENTRSAIDOP          = TIPOOPER.IDENTRSAIDOP)AND
                          (TIPOOPER.IDTPOPERACROP   = 'DE')AND
                          (V_FILIALPAR              = TIPOOPER.CDFILIAL)
                )LOOP
                    V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
                    IF V_DTLANCROP <> C_DESPRATEIO.DTENTRSAID THEN
                        V_NRROPFILIAL   := NULL;
                        SELECT MAX(NRROPFILIAL)
                          INTO V_NRROPFILIAL
                          FROM ROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               DTFILIALROP   = C_DESPRATEIO.DTENTRSAID;

                        V_DTLANCROP     := C_DESPRATEIO.DTENTRSAID;
                        IF V_NRROPFILIAL IS NULL THEN
                            V_NRROPFILIAL := V_NRROPFILINI;
                        END IF;
                        SELECT MAX(NRLANCROP)
                          INTO V_NRLANCROP
                          FROM LANCROPFILIAL
                         WHERE CDFILIAL      = P_CDFILIAL AND
                               NRROPFILIAL   = V_NRROPFILIAL;

                        IF V_NRLANCROP IS NULL THEN
                            V_NRLANCROP   := 0;
                            V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                        ELSE
                            V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                        END IF;

                    END IF;

                    INSERT INTO LANCROPFILIAL(
                        CDFILIAL,
                        NRROPFILIAL,
                        NRLANCROP,
                        DTLANCROP,
                        IDTPLANCROP,
                        CDCLIENTEROP,
                        CDTIPOOPERROP,
                        NRNOTAFISCROP,
                        CDSERIENFROP,
                        CDFILIALNFROP,
                        NRLANCTONFROP,
                        CDFILCENTROP,
                        IDSTATUSROP,
                        DTINIREFEROP,
                        DTFINREFEROP
                    )VALUES(
                        P_CDFILIAL,
                        V_NRROPFILIAL,
                        V_NRLANCROP,
                        C_DESPRATEIO.DTENTRSAID,
                        'DE',
                        C_DESPRATEIO.CDFORNECED,
                        C_DESPRATEIO.CDTIPOOPER,
                        C_DESPRATEIO.NRNOTAFISC,
                        C_DESPRATEIO.CDSERIE,
                        C_DESPRATEIO.CDFILIAL,
                        C_DESPRATEIO.NRLANCTONF,
                        V_CDFILCENTPME,
                        C_DESPRATEIO.IDSTATUS,
                        C_DESPRATEIO.DTINIREFEFAT,
                        C_DESPRATEIO.DTFINREFEFAT
                    );

                    V_NRITLANCROP   := '00000';
                    FOR C_ITDESPRATEIO IN(
                        SELECT DISTINCT I.CDPRODUTO,
                                        I.NRSEQITPROD,
                                        I.VRBRUTITEM,
                                        I.VRTOTAITEM,
                                        I.QTITEMPROD AS QUANTIDADE,
                                        SUBSTR(P.CDARVPROD, 1, 1)NIVEL_INICIAL,
                                        P.CDARVPROD,
                                        (VRTOTAITEM + VRFRETEITEM + VRSEGURITEM + VROUTRASDESPITEM + VRDESPADUANEIRA - VRENCARGODESC
                                        )AS VRTOTALPROD,
                                        NVL(IDDTAPUDESPENTR, V_IDDTAPUDESPENTR_GERAL)IDDTAPUDESPENTR
                          FROM ITEMPRODUTO   I,
                               PRODUTO       P,
                               PRODFILI      F,
                               (
                                   SELECT DISTINCT A.CDPRODUTO AS CDPRODUTO,
                                                   IDDTAPUDESPENTR
                                     FROM PRODUTOROP   R,
                                          PRODUTO      A,
                                          PRODUTO      B
                                    WHERE(INSTR(A.CDARVPROD, B.CDARVPROD)  = 1)AND
                                         R.CDPRODUTO                      = B.CDPRODUTO AND
                                         R.CDFILIAL                       = V_FILIALPAR AND
                                         IDTPPRODUTOROP                   = 'DE'
                               )A
                         WHERE I.CDFILIAL               = C_DESPRATEIO.CDFILIAL AND
                               I.NRLANCTONF             = C_DESPRATEIO.NRLANCTONF AND
                               P.CDPRODUTO              = I.CDPRODUTO AND
                               F.CDFILIAL               = P_CDFILIAL AND
                               F.CDPRODUTO              = P.CDPRODESTO AND
                               NVL(F.IDCNTRESTOQ, 'N')  = 'N' AND
                               I.CDPRODUTO              = A.CDPRODUTO
                    )LOOP
                        BEGIN
                            SELECT MAX(IT.CDITEMLANCROP)
                              INTO V_DESPESAEXISTE
                              FROM LANCROPFILIAL     L,
                                   ITLANCROPFILIAL   IT
                             WHERE L.CDFILIAL           = IT.CDFILIAL AND
                                   L.NRROPFILIAL        = IT.NRROPFILIAL AND
                                   L.NRLANCROP          = IT.NRLANCROP AND
                                   L.DTLANCROP BETWEEN V_DTDESPINI AND V_DTDESPFIM AND
                                   L.CDFILIAL           = P_CDFILIAL AND
                                   L.IDTPLANCROP        = 'DE' AND
                                   IT.NRLANCTONFITROP   = C_DESPRATEIO.NRLANCTONF AND
                                   IT.CDITEMLANCROP     = C_ITDESPRATEIO.CDPRODUTO AND
                                   IT.NRSEQITPRODROP    = C_ITDESPRATEIO.NRSEQITPROD;

                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_DESPESAEXISTE := NULL;
                        END;

                        IF V_DESPESAEXISTE IS NULL THEN
                            BEGIN
                                SELECT DECODE(COUNT(R.CDPRODUTO), 0, 'NAO', COUNT(R.CDPRODUTO))AS EXCECAO
                                  INTO V_EXCECAO
                                  FROM PRODUTOROP   R,
                                       PRODUTO      B,
                                       PRODUTO      A
                                 WHERE R.CDPRODUTO      = B.CDPRODUTO AND
                                       R.CDFILIAL       = V_FILIALPAR AND
                                       A.CDPRODUTO      = C_ITDESPRATEIO.CDPRODUTO AND
                                       A.CDARVPROD LIKE B.CDARVPROD || '%' AND
                                       IDTPPRODUTOROP   = 'EX';

                            EXCEPTION
                                WHEN NO_DATA_FOUND THEN
                                    V_EXCECAO := 'NAO';
                            END;

                            IF V_EXCECAO = 'NAO' THEN
                                V_VALOR_DESPESA := 0;
                                IF((V_IDAPUCTPGROP = 'S')OR(V_IDAPUMOCTPGROP = 'S')OR(V_IDAPURAINVCTPAGROP = 'S'))THEN
                                    BEGIN
                                        SELECT NVL(CDTPCTPAGAR, 'NAO')
                                          INTO V_HA_CONTAPAGAR
                                          FROM(
                                            SELECT CDTPCTPAGAR,
                                                   CDNVPRODUTO
                                              FROM PRODUTO
                                             WHERE CDTPCTPAGAR IS NOT NULL AND
                                                   CDARVPROD IN(
                                                       SELECT MAX(T.CDARVPROD)
                                                         FROM PRODUTO T
                                                        WHERE T.CDARVPROD BETWEEN C_ITDESPRATEIO.NIVEL_INICIAL AND C_ITDESPRATEIO
                                                        .CDARVPROD
                                                        GROUP BY T.CDNVPRODUTO
                                                   )
                                             ORDER BY CDNVPRODUTO DESC
                                        )
                                         WHERE ROWNUM = 1;

                                    EXCEPTION
                                        WHEN NO_DATA_FOUND THEN
                                            V_HA_CONTAPAGAR := 'NAO';
                                    END;

                                    IF V_HA_CONTAPAGAR <> 'NAO' 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
                                                                          (TIPOPARAM_DRO(NVL(NVL(C_DESPRATEIO.CDFILICCUST,C.CDFIMOVFLPAG), C.CDFILIAL))= TP.CDFILIAL
                                                                          )AND
                                                                          ((TP.IDTPCTPAGROP        = 'MAO' AND
                                                                            V_IDAPUMOCTPGROP       = 'S')OR
                                                                           (TP.IDTPCTPAGROP        = 'DSP' AND
                                                                            V_IDAPUCTPGROP         = 'S')OR
                                                                           (TP.IDTPCTPAGROP        = 'INV' AND
                                                                            V_IDAPURAINVCTPAGROP   = 'S'))
                                             WHERE C.NRLANCTONF     = C_DESPRATEIO.NRLANCTONF AND
                                                   TP.CDTPCTPAGAR   = V_HA_CONTAPAGAR;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_CDTPCTPAGAR := 'NAO';
                                        END;

                                    ELSE
                                        V_CDTPCTPAGAR := 'NAO';
                                    END IF;

                                ELSE
                                    V_CDTPCTPAGAR := 'NAO';
                                END IF;

                                IF V_CDTPCTPAGAR = 'NAO' THEN
                                    V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
                                    V_VRRATENF      := 0;
                                    IF C_ITDESPRATEIO.IDDTAPUDESPENTR = 'VENCIMENTO' THEN
                                        BEGIN
                                            SELECT SUM(VRATUAPAG)VRATUAPAG
                                              INTO V_VRTOTAL_NF_PARCELADA
                                              FROM CONTAPAGAR
                                             WHERE CDFILIAL     = C_DESPRATEIO.CDFILIAL AND
                                                   NRLANCTONF   = C_DESPRATEIO.NRLANCTONF;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_VRTOTAL_NF_PARCELADA := 0;
                                        END;

                                        V_PERCENT_PROD_NF        := CASE WHEN NVL(V_VRTOTAL_NF_PARCELADA,0) = 0
                                                                         THEN 0
                                                                         ELSE ROUND(C_ITDESPRATEIO.VRTOTALPROD * 100 / V_VRTOTAL_NF_PARCELADA, 2)
                                                                    END;
                                        BEGIN
                                            SELECT SUM(VRATUAPAG)VRATUAPAG
                                              INTO V_VRPARCELA_PERIODO
                                              FROM CONTAPAGAR
                                             WHERE CDFILIAL     = C_DESPRATEIO.CDFILIAL AND
                                                   NRLANCTONF   = C_DESPRATEIO.NRLANCTONF AND
                                                   DTATUAVENPAG BETWEEN V_DTDESPINI AND V_DTDESPFIM;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_VRPARCELA_PERIODO := 0;
                                        END;

                                        BEGIN
                                            SELECT NVL(
                                                LISTAGG(NRSEQVENPAG, ',')WITHIN GROUP(
                                                     ORDER BY NRSEQVENPAG ASC
                                                ), '00')
                                              INTO V_NRPARCELA
                                              FROM(
                                                SELECT NRSEQVENPAG
                                                  FROM CONTAPAGAR
                                                 WHERE CDFILIAL      = C_DESPRATEIO.CDFILIAL AND
                                                       NRLANCTONF    = C_DESPRATEIO.NRLANCTONF AND
                                                       DTATUAVENPAG BETWEEN V_DTDESPINI AND V_DTDESPFIM AND
                                                       CDTPCTPAGAR   = NVL((
                                                           SELECT MAX(P.CDTPCTPAGAR)
                                                             FROM PRODUTO P
                                                            WHERE P.CDTPCTPAGAR IS NOT NULL AND
                                                                  P.CDARVPROD = C_ITDESPRATEIO.CDARVPROD
                                                       ), CDTPCTPAGAR)
                                                 GROUP BY NRSEQVENPAG
                                            );

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_NRPARCELA := '00';
                                        END;

                                        V_VALORPRODUTO_PARCELA   := CASE WHEN NVL(V_PERCENT_PROD_NF,0) = 0
                                                                         THEN 0
                                                                         ELSE V_VRPARCELA_PERIODO / 100 * V_PERCENT_PROD_NF
                                                                    END;
                                        BEGIN
                                            SELECT R.VRPERRAT,
                                                   R.VRRATENF
                                              INTO
                                                V_PERCENT_RATEIO,
                                                V_VALOR_RATEIO
                                              FROM RATEIONF R
                                             WHERE R.CDFILICCUST   = P_CDFILIAL AND
                                                   R.CDFILIAL      = C_DESPRATEIO.CDFILIAL AND
                                                   R.NRLANCTONF    = C_DESPRATEIO.NRLANCTONF AND
                                                   R.NRSEQITPROD   = C_ITDESPRATEIO.NRSEQITPROD AND
                                                   R.VRRATENF > 0;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_VALOR_RATEIO := 0;
                                        END;

                                        IF V_VALOR_RATEIO > 0 THEN
                                            V_VALOR_RATEIO_PRODUTO_PARCELA   := CASE WHEN NVL(V_VALORPRODUTO_PARCELA,0) = 0
                                                                                     THEN 0
                                                                                     ELSE V_PERCENT_RATEIO / 100 * V_VALORPRODUTO_PARCELA
                                                                                END;
                                            V_VALOR_DESPESA                  := V_VALOR_RATEIO_PRODUTO_PARCELA;
                                        ELSE
                                            V_VALOR_DESPESA := V_VALORPRODUTO_PARCELA;
                                        END IF;

                                    ELSE
                                        BEGIN
                                            SELECT ROUND(SUM(NVL(R.VRPERRAT, 0)), 2)
                                              INTO V_VRPEROTHERSRAT
                                              FROM RATEIONF R
                                             WHERE R.CDFILICCUST <> P_CDFILIAL AND
                                                   R.CDFILIAL      = C_DESPRATEIO.CDFILIAL AND
                                                   R.NRLANCTONF    = C_DESPRATEIO.NRLANCTONF AND
                                                   R.NRSEQITPROD   = C_ITDESPRATEIO.NRSEQITPROD;

                                        EXCEPTION
                                            WHEN NO_DATA_FOUND THEN
                                                V_VRPEROTHERSRAT := 0;
                                        END;

                                        IF V_VRPEROTHERSRAT < 100 OR V_VRPEROTHERSRAT IS NULL THEN
                                            BEGIN
                                                SELECT NVL(SUM(R.VRRATENF), C_ITDESPRATEIO.VRTOTALPROD)AS VRRATENF
                                                  INTO V_VRRATENF
                                                  FROM RATEIONF R
                                                 WHERE R.CDFILICCUST   = P_CDFILIAL AND
                                                       R.CDFILIAL      = C_DESPRATEIO.CDFILIAL AND
                                                       R.NRLANCTONF    = C_DESPRATEIO.NRLANCTONF AND
                                                       R.NRSEQITPROD   = C_ITDESPRATEIO.NRSEQITPROD AND
                                                       R.VRRATENF > 0;

                                            EXCEPTION
                                                WHEN NO_DATA_FOUND THEN
                                                    V_VRRATENF := C_ITDESPRATEIO.VRTOTALPROD;
                                            END;

                                        ELSE
                                            V_VRRATENF := 0;
                                        END IF;

                                        V_VALOR_DESPESA := V_VRRATENF;
                                        IF V_VALOR_DESPESA > 0 THEN
                                            V_NRITLANCROP := LPAD(V_NRITLANCROP + 1, 5, '0');
                                            INSERT INTO ITLANCROPFILIAL(
                                                CDFILIAL,
                                                NRROPFILIAL,
                                                NRLANCROP,
                                                NRITLANCROP,
                                                CDFILIALNFITROP,
                                                NRLANCTONFITROP,
                                                NRSEQITPRODROP,
                                                CDITEMLANCROP,
                                                QTITEMLANCROP,
                                                VRITEMLANCROP
                                            )VALUES(
                                                P_CDFILIAL,
                                                V_NRROPFILIAL,
                                                V_NRLANCROP,
                                                V_NRITLANCROP,
                                                C_DESPRATEIO.CDFILIAL,
                                                C_DESPRATEIO.NRLANCTONF,
                                                C_ITDESPRATEIO.NRSEQITPROD,
                                                C_ITDESPRATEIO.CDPRODUTO,
                                                C_ITDESPRATEIO.QUANTIDADE,
                                                V_VALOR_DESPESA
                                            );

                                        END IF;

                                    END IF;

                                END IF;

                            END IF;

                        END IF;

                    END LOOP;

                END LOOP;
            END IF;

            V_DTDESPESA := ADD_MONTHS('01/' || TO_CHAR(TO_DATE(V_DTDESPESA), 'MM/YYYY'), 1);

        END LOOP;

    END IF;

    V_DTLANCROP     := '01/01/1990';
    V_NRITLANCROP   := '00000';
    FOR C_CUSTPADR IN(
        SELECT C.CDFILIAL,
               DTREFCUSPADR,
               SUM(C.VRCUSTPADR)AS VRCUSTPDR
          FROM CUSTPADR   C,
               SERVICO    S
         WHERE(C.CDFILIAL      = P_CDFILIAL)AND
              (C.DTREFCUSPADR BETWEEN V_DTINIC AND P_DTFINAL)AND
              (S.CDFILIAL      = C.CDFILIAL)AND
              (S.CDSERVFILI    = C.CDSERVFILI)AND
              (S.IDSERVPRINC   = 'S')
         GROUP BY C.CDFILIAL,
                  DTREFCUSPADR
    )LOOP
        V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
        IF V_DTLANCROP <> C_CUSTPADR.DTREFCUSPADR THEN
            V_NRROPFILIAL   := NULL;
            SELECT MAX(NRROPFILIAL)
              INTO V_NRROPFILIAL
              FROM ROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   DTFILIALROP   = C_CUSTPADR.DTREFCUSPADR;

            V_DTLANCROP     := C_CUSTPADR.DTREFCUSPADR;
            IF V_NRROPFILIAL IS NULL THEN
                V_NRROPFILIAL := V_NRROPFILINI;
            END IF;
            SELECT MAX(NRLANCROP)
              INTO V_NRLANCROP
              FROM LANCROPFILIAL
             WHERE CDFILIAL      = P_CDFILIAL AND
                   NRROPFILIAL   = V_NRROPFILIAL;

            IF V_NRLANCROP IS NULL THEN
                V_NRLANCROP   := 0;
                V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
            ELSE
                V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
            END IF;

        END IF;

        INSERT INTO LANCROPFILIAL(
            CDFILIAL,
            NRROPFILIAL,
            NRLANCROP,
            DTLANCROP,
            IDTPLANCROP,
            CDCLIENTEROP,
            CDTIPOOPERROP,
            NRNOTAFISCROP,
            CDSERIENFROP,
            CDFILIALNFROP,
            NRLANCTONFROP,
            CDFILCENTROP
        )VALUES(
            P_CDFILIAL,
            V_NRROPFILIAL,
            V_NRLANCROP,
            C_CUSTPADR.DTREFCUSPADR,
            'PP',
            '',
            '',
            '',
            '',
            C_CUSTPADR.CDFILIAL,
            '',
            V_CDFILCENTPME
        );

        V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
        INSERT INTO ITLANCROPFILIAL(
            CDFILIAL,
            NRROPFILIAL,
            NRLANCROP,
            NRITLANCROP,
            CDFILIALNFITROP,
            NRLANCTONFITROP,
            NRSEQITPRODROP,
            CDITEMLANCROP,
            QTITEMLANCROP,
            VRITEMLANCROP
        )VALUES(
            P_CDFILIAL,
            V_NRROPFILIAL,
            V_NRLANCROP,
            V_NRITLANCROP,
            C_CUSTPADR.CDFILIAL,
            '',
            '',
            '',
            1,
            C_CUSTPADR.VRCUSTPDR
        );

    END LOOP;

    IF V_IDAPURDEPRECROP = 'S' THEN
        V_DTLANCROP     := '01/01/1990';
        V_NRITLANCROP   := '00000';
        FOR C_DEPREC IN(
            SELECT SUM(VRRATOPERABE)AS VRRATOPERABE,
                   F.DTFECHABEM
              FROM CAPAOPERABEM     C,
                   FECHAMENTOBEM    F,
                   ITCAPAOPERABEM   IT,
                   TIPOOPERBEM      T,
                   RATOPERABEM      RT
             WHERE C.CDEMPRESA          = F.CDEMPRESA AND
                   C.IDSIMULFECHABEM    = F.IDSIMULFECHABEM AND
                   C.NRSEQFECHABEM      = F.NRSEQFECHABEM AND
                   F.DTFECHABEM BETWEEN V_DTINIC AND P_DTFINAL AND
                   C.CDEMPRESA          = IT.CDEMPRESA AND
                   C.IDSIMULOPERABEM    = IT.IDSIMULOPERABEM AND
                   C.NRSEQCAPAOPEBEM    = IT.NRSEQCAPAOPEBEM AND
                   C.CDEMPRESA          = T.CDEMPRESA AND
                   C.CDTIPOOPERBEM      = T.CDTIPOOPERBEM AND
                   IT.CDEMPRESA         = RT.CDEMPRESA AND
                   IT.IDSIMULOPERABEM   = RT.IDSIMULOPERABEM AND
                   IT.NRSEQCAPAOPEBEM   = RT.NRSEQCAPAOPEBEM AND
                   IT.NRSEQITCAPAOPE    = RT.NRSEQITCAPAOPE AND
                   RT.CDFILIAL          = P_CDFILIAL AND
                   T.IDTIPOOPERBEM      = '02' AND
                   T.CDEMPRESA          = V_CDEMPRESA AND
                   C.IDTIPOFECHAOPER    = V_IDTIPODEPRECROP
             GROUP BY F.DTFECHABEM
        )LOOP
            V_NRLANCROP     := LPAD(V_NRLANCROP + 1, 10, '0');
            IF V_DTLANCROP <> C_DEPREC.DTFECHABEM THEN
                V_NRROPFILIAL   := NULL;
                SELECT MAX(NRROPFILIAL)
                  INTO V_NRROPFILIAL
                  FROM ROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       DTFILIALROP   = C_DEPREC.DTFECHABEM;

                V_DTLANCROP     := C_DEPREC.DTFECHABEM;
                IF V_NRROPFILIAL IS NULL THEN
                    V_NRROPFILIAL := V_NRROPFILINI;
                END IF;
                SELECT MAX(NRLANCROP)
                  INTO V_NRLANCROP
                  FROM LANCROPFILIAL
                 WHERE CDFILIAL      = P_CDFILIAL AND
                       NRROPFILIAL   = V_NRROPFILIAL;

                IF V_NRLANCROP IS NULL THEN
                    V_NRLANCROP   := 0;
                    V_NRLANCROP   := LPAD(V_NRLANCROP + 1, 10, '0');
                ELSE
                    V_NRLANCROP := LPAD(V_NRLANCROP + 1, 10, '0');
                END IF;

            END IF;

            INSERT INTO LANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                DTLANCROP,
                IDTPLANCROP,
                CDCLIENTEROP,
                CDTIPOOPERROP,
                NRNOTAFISCROP,
                CDSERIENFROP,
                CDFILIALNFROP,
                NRLANCTONFROP,
                CDFILCENTROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                C_DEPREC.DTFECHABEM,
                'DB',
                '',
                '',
                '',
                '',
                '',
                '',
                V_CDFILCENTPME
            );

            V_NRITLANCROP   := LPAD(V_NRITLANCROP + 1, 5, '0');
            INSERT INTO ITLANCROPFILIAL(
                CDFILIAL,
                NRROPFILIAL,
                NRLANCROP,
                NRITLANCROP,
                CDFILIALNFITROP,
                NRLANCTONFITROP,
                NRSEQITPRODROP,
                CDITEMLANCROP,
                QTITEMLANCROP,
                VRITEMLANCROP
            )VALUES(
                P_CDFILIAL,
                V_NRROPFILIAL,
                V_NRLANCROP,
                V_NRITLANCROP,
                '',
                '',
                '',
                '',
                1,
                C_DEPREC.VRRATOPERABE
            );

        END LOOP;

    END IF;

    V_DTINICIO      := V_DTINIC;
    V_DTFINAL       := V_DTINIC;
    IF(V_IDPERCONSROP = 'P')AND(V_NRMESCONSROP > 1)AND(P_IDCONSANT = 'N')THEN
        V_NRCOUNT := 1;
        WHILE(V_NRCOUNT < V_NRMESCONSROP)LOOP
            SELECT '01/' || DECODE(TO_CHAR(V_DTINICIO, 'MM'), '01', '12/' || TO_CHAR(TO_NUMBER(TO_CHAR(V_DTINICIO, 'YYYY'))- 1), LPAD
            (TO_NUMBER(TO_CHAR(V_DTINICIO, 'MM'))- 1, 2, '0')|| '/' || TO_CHAR(V_DTINICIO, 'YYYY'))
              INTO V_DTINICIO
              FROM DUAL;

            V_DTFINAL   := LAST_DAY(TO_DATE(V_DTINICIO));
            V_NRCONS    := 0;
            SELECT COUNT(*)
              INTO V_NRCONS
              FROM ROPFILIAL
             WHERE CDFILIAL = P_CDFILIAL AND
                   DTFILIALROP BETWEEN V_DTINICIO AND V_DTFINAL;

            IF NVL(V_NRCONS, 0)= 0 THEN
                APURAC_RESOPERFILI(P_CDFILIAL, V_DTINICIO, V_DTFINAL, 'S');
            END IF;

            V_NRCOUNT   := V_NRCOUNT + 1;
        END LOOP;

    END IF;

    IF V_IDFECHDIAROP = 'S' THEN
        UPDATE APURACAOROP
           SET DTINIAPURACROP = P_DTFINAL + 1,
               DTFINAPURACROP = P_DTFINAL + 1
         WHERE CDFILIAL = P_CDFILIAL;

    END IF;

    V_DTINITEMP     := P_DTINICIAL;
    WHILE TO_CHAR(V_DTINITEMP, 'YYYYMM')<= TO_CHAR(P_DTFINAL, 'YYYYMM')LOOP
        IF TO_CHAR(V_DTINITEMP, 'YYYYMM')= TO_CHAR(P_DTFINAL, 'YYYYMM')THEN
            ETL_DRO(FIRST_DAY(P_DTFINAL), P_DTFINAL, P_CDFILIAL);
            EXIT;
        ELSE
            ETL_DRO(FIRST_DAY(V_DTINITEMP), LAST_DAY(V_DTINITEMP), P_CDFILIAL);
        END IF;

        V_DTINITEMP := ADD_MONTHS(V_DTINITEMP, 1);
    END LOOP;
END;
/