create or replace PROCEDURE EXPORTA_TBPRECO(
    P_CDFILIALDES   IN  VARCHAR2, 
    P_CDFILIALORI   IN  VARCHAR2, 
    P_CDTABEPRECORI IN  VARCHAR2, 
    P_NRORG         IN  VARCHAR2,
    P_CDPRODUTO     IN  VARCHAR2, 
    P_TCDPRODUTO    IN  VARCHAR2, 
    P_DATAATUAL     IN  DATE, 
    P_CDTABEPRECDES IN  VARCHAR2,
    P_CDTABEPRECNEW IN  VARCHAR2, 
    P_NMTABEPRECNEW IN  VARCHAR2, 
    P_DTINIVGPREC   IN  DATE, 
    P_DTFINVGPREC   IN  DATE,
    P_DTVGPROD      IN  DATE, 
    P_TYPE          IN  VARCHAR2, 
    P_OK            OUT VARCHAR2
)
AS
    V_CDFILIALDES               VARCHAR(4);
    V_CDTABEPRECDES             VARCHAR(3);
    V_DTINIVGPRECORI            DATE;
    V_DTINIVGPREC               DATE;
    V_DTFINVGPREC               DATE;
    V_COMMIT                    NUMBER;
    V_COUNT                     NUMBER;
    V_TABPREC                   VARCHAR2(3);
    V_TABVEND                   VARCHAR2(3);
    V_EXIST_PROD_ITEMPRECO      NUMBER;
    V_EXIST_PROD_ITEMPRECODIA   NUMBER;
    V_QUANTPRODUTO              NUMBER;
    V_CDPRODUTO                 VARCHAR(10);
PROCEDURE COMITA AS
BEGIN
    V_COUNT := V_COUNT + 1;
    IF V_COUNT = V_COMMIT THEN
        COMMIT;
        V_COUNT := 0;
    END IF;
END;
BEGIN
    V_COMMIT := 250;
    V_COUNT := 0;
    
    IF P_TYPE = 'E_MULTFILI' THEN
        IF P_TCDPRODUTO = 'T' THEN
            DELETE FROM ITEMPRECODIA I
                WHERE (INSTR(P_CDFILIALDES, I.CDFILIAL) <> 0)
                    AND I.NRORG = P_NRORG
                    AND (EXISTS(SELECT 1
                                    FROM TABEVEND T
                                    WHERE T.CDFILIAL = I.CDFILIAL
                                        AND T.CDTABEPREC = I.CDTABEPREC
                                        AND T.DTINIVGPREC = I.DTINIVGPREC
                                        AND SYSDATE BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC
                                        AND T.NRORG = I.NRORG));

            DELETE FROM ITEMPRECO I
                WHERE (INSTR(P_CDFILIALDES, I.CDFILIAL) <> 0)
                    AND I.NRORG = P_NRORG
                    AND (EXISTS(SELECT 1
                                    FROM TABEVEND T
                                    WHERE T.CDFILIAL = I.CDFILIAL
                                        AND T.CDTABEPREC = I.CDTABEPREC
                                        AND T.DTINIVGPREC = I.DTINIVGPREC
                                        AND SYSDATE BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC
                                        AND T.NRORG = I.NRORG));

            FOR C_FILIDES IN(
                SELECT CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC,
                        (SELECT T.DTINIVGPREC
                            FROM TABEVEND T
                            WHERE (T.CDFILIAL      = P_CDFILIALORI)
                                AND (T.CDTABEPREC  = P_CDTABEPRECORI)
                                AND (P_DATAATUAL BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC)
                                AND (T.NRORG       = P_NRORG)
                        ) AS DTINIVGPRECORI
                    FROM TABEVEND
                    WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0)
                        AND NRORG = P_NRORG
                        AND (SYSDATE BETWEEN DTINIVGPREC AND DTFINVGPREC)
            ) LOOP

            V_CDFILIALDES    := C_FILIDES.CDFILIAL;
            V_CDTABEPRECDES  := C_FILIDES.CDTABEPREC;
            V_DTINIVGPRECORI := C_FILIDES.DTINIVGPRECORI;
            V_DTINIVGPREC    := C_FILIDES.DTINIVGPREC;
            V_DTFINVGPREC    := C_FILIDES.DTFINVGPREC;
            
            IF V_DTINIVGPRECORI IS NULL THEN
                P_OK := 'ERROV';
                ROLLBACK;
                EXIT;
            END IF;

            INSERT INTO ITEMPRECO
                (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
            SELECT
                V_CDFILIALDES, V_CDTABEPRECDES, V_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
            FROM
                ITEMPRECO I, PRODUTO R
            WHERE
                (I.CDFILIAL    = P_CDFILIALORI   ) AND
                (I.CDTABEPREC  = P_CDTABEPRECORI ) AND
                (I.DTINIVGPREC = V_DTINIVGPRECORI) AND
                (I.NRORG       = P_NRORG 		 ) AND
                (R.CDPRODUTO   = I.CDPRODUTO     ) AND
                (R.NRORG 	   = I.NRORG    	 ) AND
                (NVL(R.IDPRODATIVPR,'S') = 'S'   );

            INSERT INTO ITEMPRECODIA
                (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, IDVISUACUPOM,
                IDTPMODGERAL, IDTPMODDLV, VRPRECODIADLV, IDTPMODCMD, VRPRECODIACMD, NRORG)
            SELECT
                V_CDFILIALDES, V_CDTABEPRECDES, V_DTINIVGPREC,
                CASE
                    WHEN
                        I.DTINIVALPREC <= TO_DATE(V_DTINIVGPREC, 'DD/MM/YY HH24:MI:SS')
                    THEN
                        TO_DATE(V_DTINIVGPREC, 'DD/MM/YY HH24:MI:SS')
                    ELSE
                        I.DTINIVALPREC
                    END,
                CASE
                    WHEN
                        I.DTFINVALPREC >= TO_DATE(V_DTFINVGPREC, 'DD/MM/YY HH24:MI:SS')
                    THEN
                        TO_DATE(V_DTFINVGPREC, 'DD/MM/YY HH24:MI:SS')
                    ELSE
                        I.DTFINVALPREC
                    END,
                I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR, I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR,
                I.IDDESCACREPR, I.CDTIPOCONSPD, I.IDVISUACUPOM,I.IDTPMODGERAL, I.IDTPMODDLV, I.VRPRECODIADLV, I.IDTPMODCMD,
                I.VRPRECODIACMD, I.NRORG
            FROM
                ITEMPRECODIA I, PRODUTO R
            WHERE
                (I.CDFILIAL    = P_CDFILIALORI    ) AND
                (I.CDTABEPREC  = P_CDTABEPRECORI  ) AND
                (I.DTINIVGPREC = V_DTINIVGPRECORI ) AND
                (I.NRORG       = P_NRORG 		  ) AND
                (R.CDPRODUTO   = I.CDPRODUTO      ) AND
                (R.NRORG 	   = I.NRORG    	  ) AND
                (NVL(R.IDPRODATIVPR,'S') = 'S'    );
            COMITA;
            P_OK := 'OK';
            END LOOP;
        ELSE
            FOR C_FILIDES IN(
                SELECT CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC,
                        (SELECT T.DTINIVGPREC
                            FROM TABEVEND T
                            WHERE (T.CDFILIAL = P_CDFILIALORI)
                                AND (T.CDTABEPREC  = P_CDTABEPRECORI)
                                AND (P_DATAATUAL BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC)
                                AND (T.NRORG = P_NRORG)
                        ) AS DTINIVGPRECORI
                    FROM TABEVEND
                    WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0)
                        AND NRORG = P_NRORG
                        AND (SYSDATE BETWEEN DTINIVGPREC AND DTFINVGPREC)
            ) LOOP

            V_CDFILIALDES    := C_FILIDES.CDFILIAL;
            V_CDTABEPRECDES  := C_FILIDES.CDTABEPREC;
            V_DTINIVGPRECORI := C_FILIDES.DTINIVGPRECORI;
            V_DTINIVGPREC    := C_FILIDES.DTINIVGPREC;
            V_DTFINVGPREC    := C_FILIDES.DTFINVGPREC;

            IF V_DTINIVGPRECORI IS NULL THEN
                P_OK := 'ERROV';
                ROLLBACK;
                EXIT;
            END IF;

            FOR C_PRODUTOEXP IN (
                SELECT DISTINCT CDPRODUTO
                    FROM PRODEXPOTACAOPR
                    WHERE CDFILIAL = V_CDFILIALDES
                        AND NRORG = P_NRORG
            ) LOOP

            V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

            DELETE FROM ITEMPRECODIA I
            WHERE I.CDFILIAL = V_CDFILIALDES
                AND I.CDPRODUTO = V_CDPRODUTO
                AND I.NRORG = P_NRORG
                AND I.CDTABEPREC = V_CDTABEPRECDES;

            DELETE FROM ITEMPRECO I
            WHERE I.CDFILIAL = V_CDFILIALDES
                AND I.CDPRODUTO = V_CDPRODUTO
                AND I.NRORG = P_NRORG
                AND I.CDTABEPREC = V_CDTABEPRECDES;

            INSERT INTO ITEMPRECO
                (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
            SELECT
                V_CDFILIALDES, V_CDTABEPRECDES, V_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
            FROM
                ITEMPRECO I, PRODUTO R
            WHERE
                (I.CDFILIAL    = P_CDFILIALORI         ) AND
                (I.CDTABEPREC  = P_CDTABEPRECORI       ) AND
                (I.DTINIVGPREC = V_DTINIVGPRECORI      ) AND
                (I.NRORG       = P_NRORG 		       ) AND
                (I.CDPRODUTO   = V_CDPRODUTO           ) AND
                (R.CDPRODUTO   = I.CDPRODUTO           ) AND
                (R.NRORG 	   = I.NRORG    	       ) AND
                (NVL(R.IDPRODATIVPR,'S') = 'S'         );

            COMMIT;

            INSERT INTO ITEMPRECODIA
                (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, IDVISUACUPOM,
                IDTPMODGERAL, IDTPMODDLV, VRPRECODIADLV, IDTPMODCMD, VRPRECODIACMD, NRORG)
            SELECT
                V_CDFILIALDES, V_CDTABEPRECDES, V_DTINIVGPREC,
                CASE
                    WHEN
                        I.DTINIVALPREC <= TO_DATE(V_DTINIVGPREC, 'DD/MM/YY HH24:MI:SS')
                    THEN
                        TO_DATE(V_DTINIVGPREC, 'DD/MM/YY HH24:MI:SS')
                    ELSE
                        I.DTINIVALPREC
                    END,
                CASE
                    WHEN
                        I.DTFINVALPREC >= TO_DATE(V_DTFINVGPREC, 'DD/MM/YY HH24:MI:SS')
                    THEN
                        TO_DATE(V_DTFINVGPREC, 'DD/MM/YY HH24:MI:SS')
                    ELSE
                        I.DTFINVALPREC
                    END,
                I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR, I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR,
                I.IDDESCACREPR, I.CDTIPOCONSPD, I.IDVISUACUPOM,I.IDTPMODGERAL, I.IDTPMODDLV, I.VRPRECODIADLV, I.IDTPMODCMD,
                I.VRPRECODIACMD, I.NRORG
            FROM
                ITEMPRECODIA I, PRODUTO R
            WHERE
                (I.CDFILIAL    = P_CDFILIALORI         ) AND
                (I.CDTABEPREC  = P_CDTABEPRECORI       ) AND
                (I.DTINIVGPREC = V_DTINIVGPRECORI      ) AND
                (I.NRORG       = P_NRORG 		       ) AND
                (I.CDPRODUTO   = V_CDPRODUTO           ) AND
                (R.CDPRODUTO   = I.CDPRODUTO           ) AND
                (R.NRORG 	   = I.NRORG    	       ) AND
                (NVL(R.IDPRODATIVPR,'S') = 'S'         );
            END LOOP;
            COMITA;
            P_OK := 'OK';
            END LOOP;

            DELETE FROM PRODEXPOTACAOPR 
                WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0) 
                    AND CDTABEPREC IS NULL 
                    AND NRORG = P_NRORG;
        END IF;
    ELSIF P_TYPE = 'E_FILIPC' THEN
        IF P_TCDPRODUTO = 'T' THEN
            DELETE FROM ITEMPRECODIA I
                WHERE (INSTR(P_CDFILIALDES, I.CDFILIAL) <> 0)
                    AND I.NRORG = P_NRORG
                    AND I.CDTABEPREC  = P_CDTABEPRECDES;

            DELETE FROM ITEMPRECO I
                WHERE (INSTR(P_CDFILIALDES, I.CDFILIAL) <> 0)
                    AND I.NRORG = P_NRORG
                    AND I.CDTABEPREC = P_CDTABEPRECDES;

            DELETE FROM TABEVEND T
                WHERE (INSTR(P_CDFILIALDES, T.CDFILIAL) <> 0)
                    AND T.CDTABEPREC = P_CDTABEPRECDES
                    AND T.NRORG = P_NRORG;

            IF P_DTINIVGPREC IS NULL OR P_DTFINVGPREC IS NULL THEN
                INSERT INTO TABEVEND
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                SELECT
                    P_CDFILIALDES, P_CDTABEPRECDES, DTINIVGPREC, DTFINVGPREC, NRORG
                FROM
                    TABEVEND
                WHERE
                    (CDFILIAL   = P_CDFILIALORI)   AND
                    (CDTABEPREC = P_CDTABEPRECORI) AND
                    (NRORG 		= P_NRORG);

                INSERT INTO ITEMPRECO
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                    VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                    IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                SELECT
                    P_CDFILIALDES, P_CDTABEPRECDES, I.DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                    I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                    I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                FROM
                    ITEMPRECO I, PRODUTO R
                WHERE
                    (I.CDFILIAL   = P_CDFILIALORI)   AND
                    (I.CDTABEPREC = P_CDTABEPRECORI) AND
                    (I.NRORG 	  = P_NRORG) 		 AND
                    (R.CDPRODUTO  = I.CDPRODUTO)     AND
                    (R.NRORG 	  = I.NRORG)		 AND
                    (NVL(R.IDPRODATIVPR,'S') = 'S');

                INSERT INTO ITEMPRECODIA
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                    HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                SELECT
                    P_CDFILIALDES, P_CDTABEPRECDES, I.DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                    I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                FROM
                    ITEMPRECODIA I,  PRODUTO R
                WHERE
                    (I.CDFILIAL   = P_CDFILIALORI)   AND
                    (I.CDTABEPREC = P_CDTABEPRECORI) AND
                    (I.NRORG 	  = P_NRORG)		 AND
                    (R.CDPRODUTO  = I.CDPRODUTO)	 AND
                    (R.NRORG 	  = I.NRORG)		 AND
                    (NVL(R.IDPRODATIVPR,'S') = 'S');

                P_OK := 'OK';
                V_DTINIVGPRECORI := NULL;

                BEGIN
                    SELECT T.DTINIVGPREC
                    INTO V_DTINIVGPRECORI
                    FROM TABEVEND T
                    WHERE (T.CDFILIAL      = P_CDFILIALORI)
                      AND (T.CDTABEPREC  = P_CDTABEPRECORI)
                      AND SYSDATE BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC
                      AND (T.NRORG       = P_NRORG);
                EXCEPTION
                    WHEN NO_DATA_FOUND THEN
                        V_DTINIVGPRECORI := NULL;
                END;

                IF V_DTINIVGPRECORI IS NULL THEN
                    P_OK := 'ERROV';
                    ROLLBACK;
                END IF;
            ELSE
                INSERT INTO TABEVEND
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                VALUES
                    (P_CDFILIALDES, P_CDTABEPRECDES, P_DTINIVGPREC, P_DTFINVGPREC, P_NRORG);

                INSERT INTO ITEMPRECO
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                    VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                    IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                SELECT
                    P_CDFILIALDES, P_CDTABEPRECDES, P_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                    I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                    I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                FROM
                    ITEMPRECO I, PRODUTO R
                WHERE
                    (I.CDFILIAL    = P_CDFILIALORI)   AND
                    (I.CDTABEPREC  = P_CDTABEPRECORI) AND
                    (I.DTINIVGPREC = P_DTVGPROD)      AND
                    (I.NRORG 	   = P_NRORG) 		  AND
                    (R.CDPRODUTO   = I.CDPRODUTO)     AND
                    (R.NRORG 	   = I.NRORG)		  AND
                    (NVL(R.IDPRODATIVPR,'S') = 'S');

                INSERT INTO ITEMPRECODIA
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                    HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                SELECT
                    P_CDFILIALDES, P_CDTABEPRECDES, P_DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                    I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                FROM
                    ITEMPRECODIA I,  PRODUTO R
                WHERE
                    (I.CDFILIAL   = P_CDFILIALORI)   AND
                    (I.CDTABEPREC = P_CDTABEPRECORI) AND
                    (I.DTINIVGPREC= P_DTVGPROD)      AND
                    (I.NRORG 	  = P_NRORG)		 AND
                    (R.CDPRODUTO  = I.CDPRODUTO)	 AND
                    (R.NRORG 	  = I.NRORG)		 AND
                    (NVL(R.IDPRODATIVPR,'S') = 'S');

                P_OK := 'OK';
            END IF;
        ELSE
            SELECT COUNT(CDPRODUTO) INTO V_EXIST_PROD_ITEMPRECO --<<<
                FROM ITEMPRECO;

            SELECT COUNT(CDPRODUTO) INTO V_EXIST_PROD_ITEMPRECODIA 
                FROM ITEMPRECODIA;

            IF V_EXIST_PROD_ITEMPRECO = 0 AND V_EXIST_PROD_ITEMPRECODIA = 0 THEN
                DELETE FROM TABEVEND T
                    WHERE (INSTR(P_CDFILIALDES, T.CDFILIAL) <> 0)
                        AND T.CDTABEPREC = P_CDTABEPRECDES
                        AND T.NRORG = P_NRORG;
            END IF;

            FOR C_PRODUTOEXP IN (
                SELECT DISTINCT CDPRODUTO
                    FROM PRODEXPOTACAOPR
                    WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0)
                        AND CDTABEPREC = P_CDTABEPRECDES
                        AND NRORG = P_NRORG
            ) LOOP

            V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

            DELETE FROM ITEMPRECODIA I
                WHERE (INSTR(P_CDFILIALDES, I.CDFILIAL) <> 0)
                    AND I.CDPRODUTO = V_CDPRODUTO
                    AND I.NRORG = P_NRORG
                    AND I.CDTABEPREC  = P_CDTABEPRECDES;

            DELETE FROM ITEMPRECO I
                WHERE (INSTR(P_CDFILIALDES, I.CDFILIAL) <> 0)
                    AND I.CDPRODUTO = V_CDPRODUTO
                    AND I.NRORG = P_NRORG
                    AND I.CDTABEPREC  = P_CDTABEPRECDES;
            END LOOP;

            SELECT MAX (CDTABEPREC) INTO V_TABVEND
                FROM TABEVEND
                WHERE
                    (CDFILIAL   = P_CDFILIALDES)   AND
                    (CDTABEPREC = P_CDTABEPRECDES) AND
                    (NRORG 	    = P_NRORG);

            IF V_TABVEND IS NULL THEN
                IF P_DTINIVGPREC IS NULL OR P_DTFINVGPREC IS NULL THEN
                    INSERT INTO TABEVEND
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                    SELECT
                        P_CDFILIALDES, P_CDTABEPRECDES, DTINIVGPREC, DTFINVGPREC, NRORG
                    FROM
                        TABEVEND
                    WHERE
                        (CDFILIAL   = P_CDFILIALORI)   AND
                        (CDTABEPREC = P_CDTABEPRECORI) AND
                        (NRORG 		= P_NRORG);

                    FOR C_PRODUTOEXP IN (
                        SELECT DISTINCT CDPRODUTO
                            FROM PRODEXPOTACAOPR
                            WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0)
                                AND CDTABEPREC = P_CDTABEPRECDES
                                AND NRORG = P_NRORG
                    ) LOOP

                    V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

                    INSERT INTO ITEMPRECO
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                        VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                        IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                    SELECT
                        P_CDFILIALDES, P_CDTABEPRECDES, I.DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                        I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                        I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                    FROM
                        ITEMPRECO I, PRODUTO R
                    WHERE
                        (I.CDFILIAL   = P_CDFILIALORI           ) AND
                        (I.CDTABEPREC = P_CDTABEPRECORI         ) AND
                        (I.NRORG 	  = P_NRORG                 ) AND
                        (I.CDPRODUTO  = V_CDPRODUTO             ) AND
                        (R.CDPRODUTO  = I.CDPRODUTO             ) AND
                        (R.NRORG 	  = I.NRORG                 ) AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S'          );

                    INSERT INTO ITEMPRECODIA
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                        HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                    SELECT
                        P_CDFILIALDES, P_CDTABEPRECDES, I.DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                        I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                    FROM
                        ITEMPRECODIA I,  PRODUTO R
                    WHERE
                        (I.CDFILIAL   = P_CDFILIALORI          ) AND
                        (I.CDTABEPREC = P_CDTABEPRECORI        ) AND
                        (I.NRORG 	  = P_NRORG                ) AND
                        (I.CDPRODUTO  = V_CDPRODUTO            ) AND
                        (R.CDPRODUTO  = I.CDPRODUTO            ) AND
                        (R.NRORG 	  = I.NRORG                ) AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S'         );
                        
                    END LOOP;

                    DELETE FROM PRODEXPOTACAOPR 
                        WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0) 
                            AND CDTABEPREC = P_CDTABEPRECDES 
                            AND NRORG = P_NRORG;

                    P_OK := 'OK';
                    V_DTINIVGPRECORI := NULL;

                    BEGIN
                        SELECT T.DTINIVGPREC INTO V_DTINIVGPRECORI
                            FROM TABEVEND T
                            WHERE (T.CDFILIAL = P_CDFILIALORI)
                                AND (T.CDTABEPREC = P_CDTABEPRECORI)
                                AND SYSDATE BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC
                                AND (T.NRORG = P_NRORG);
                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_DTINIVGPRECORI := NULL;
                    END;
                    
                    IF V_DTINIVGPRECORI IS NULL THEN
                        P_OK := 'ERROV';
                        ROLLBACK;
                    END IF;
                ELSE
                    INSERT INTO TABEVEND
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                    VALUES
                        (P_CDFILIALDES, P_CDTABEPRECDES, P_DTINIVGPREC, P_DTFINVGPREC, P_NRORG);

                    FOR C_PRODUTOEXP IN (
                        SELECT DISTINCT CDPRODUTO
                            FROM PRODEXPOTACAOPR
                            WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0)
                                AND CDTABEPREC = P_CDTABEPRECDES
                                AND NRORG = P_NRORG
                    ) LOOP

                    V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

                    INSERT INTO ITEMPRECO
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                        VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                        IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                    SELECT
                        P_CDFILIALDES, P_CDTABEPRECDES, P_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                        I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                        I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                    FROM
                        ITEMPRECO I, PRODUTO R
                    WHERE
                        (I.CDFILIAL    = P_CDFILIALORI         ) AND
                        (I.CDTABEPREC  = P_CDTABEPRECORI       ) AND
                        (I.DTINIVGPREC = P_DTVGPROD            ) AND
                        (I.NRORG 	   = P_NRORG               ) AND
                        (I.CDPRODUTO   = V_CDPRODUTO           ) AND
                        (R.CDPRODUTO   = I.CDPRODUTO           ) AND
                        (R.NRORG 	   = I.NRORG               ) AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S'         );

                    INSERT INTO ITEMPRECODIA
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                        HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                    SELECT
                        P_CDFILIALDES, P_CDTABEPRECDES, P_DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                        I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                    FROM
                        ITEMPRECODIA I,  PRODUTO R
                    WHERE
                        (I.CDFILIAL    = P_CDFILIALORI         ) AND
                        (I.CDTABEPREC  = P_CDTABEPRECORI       ) AND
                        (I.DTINIVGPREC = P_DTVGPROD            ) AND
                        (I.NRORG 	   = P_NRORG               ) AND
                        (I.CDPRODUTO   = V_CDPRODUTO           ) AND
                        (R.CDPRODUTO   = I.CDPRODUTO           ) AND
                        (R.NRORG 	   = I.NRORG               ) AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S'         );

                    END LOOP;

                    DELETE FROM PRODEXPOTACAOPR 
                        WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0) 
                            AND CDTABEPREC = P_CDTABEPRECDES 
                            AND NRORG = P_NRORG;
                    P_OK := 'OK';
                END IF;
            ELSE
                FOR C_TABVEND IN (
                    SELECT DTINIVGPREC
                        FROM TABEVEND
                        WHERE (CDFILIAL = P_CDFILIALDES)
                            AND (CDTABEPREC = P_CDTABEPRECDES) 
                            AND (NRORG = P_NRORG)
                ) LOOP

                V_DTINIVGPREC := C_TABVEND.DTINIVGPREC;

                FOR C_PRODUTOEXP IN (
                    SELECT DISTINCT CDPRODUTO
                        FROM PRODEXPOTACAOPR
                        WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0)
                            AND CDTABEPREC = P_CDTABEPRECDES
                            AND NRORG = P_NRORG
                ) LOOP

                V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

                INSERT INTO ITEMPRECO
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR, 
                    VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                    IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD,
                    VRPRECMAXITE, VRMKUPITEM, IDPRODPROMOC, NRORG)
                SELECT
                    P_CDFILIALDES, P_CDTABEPRECDES, V_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR, 
                    I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                    I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD,
                    I.VRPRECMAXITE, I.VRMKUPITEM, I.IDPRODPROMOC, I.NRORG
                FROM ITEMPRECO I, TABEVEND T
                WHERE
                    (I.CDFILIAL    = P_CDFILIALORI       ) AND
                    (I.CDTABEPREC  = P_CDTABEPRECORI     ) AND
                    (I.CDPRODUTO   = V_CDPRODUTO         ) AND
                    (I.NRORG       = P_NRORG             ) AND
                    (I.CDFILIAL    = T.CDFILIAL          ) AND
                    (I.CDTABEPREC  = T.CDTABEPREC        ) AND
                    (I.DTINIVGPREC = T.DTINIVGPREC       ) AND
                    (I.NRORG       = T.NRORG             ) AND
                    (SYSDATE BETWEEN I.DTINIVGPREC AND T.DTFINVGPREC);

                INSERT INTO ITEMPRECODIA
                    (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR, HRINIPRECDIA, CDTIPOCONSPD,
                    HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, DTINIVALPREC, DTFINVALPREC, IDVISUACUPOM, NRORG)
                SELECT
                    P_CDFILIALDES, P_CDTABEPRECDES, V_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR, I.HRINIPRECDIA, I.CDTIPOCONSPD,
                    I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.DTINIVALPREC, I.DTFINVALPREC, I.IDVISUACUPOM, I.NRORG
                FROM ITEMPRECODIA I, TABEVEND T
                WHERE
                    (I.CDFILIAL    = P_CDFILIALORI       ) AND
                    (I.CDTABEPREC  = P_CDTABEPRECORI     ) AND
                    (I.CDPRODUTO   = V_CDPRODUTO         ) AND
                    (I.NRORG       = P_NRORG             ) AND
                    (I.CDFILIAL    = T.CDFILIAL          ) AND
                    (I.CDTABEPREC  = T.CDTABEPREC        ) AND
                    (I.DTINIVGPREC = T.DTINIVGPREC       ) AND
                    (I.NRORG       = T.NRORG             ) AND
                    (SYSDATE BETWEEN I.DTINIVGPREC AND T.DTFINVGPREC);

                P_OK := 'OK';

                END LOOP;
                END LOOP;

                DELETE FROM PRODEXPOTACAOPR 
                    WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0) 
                        AND CDTABEPREC = P_CDTABEPRECDES 
                        AND NRORG = P_NRORG;

            END IF;
            COMMIT;
        END IF;
    ELSIF P_TYPE = 'E_FILINEWPC' THEN
        IF P_TCDPRODUTO = 'T' THEN
            SELECT MAX (CDTABEPREC)
                INTO V_TABPREC
                FROM TABEPREC
                WHERE
                    (CDFILIAL   = P_CDFILIALDES)   AND
                    (CDTABEPREC = P_CDTABEPRECNEW) AND
                    (NRORG 	    = P_NRORG);

            IF V_TABPREC IS NULL THEN
                INSERT INTO TABEPREC
                    (CDFILIAL, CDTABEPREC, NMTABEPREC, NRORG)
                VALUES
                    (P_CDFILIALDES, P_CDTABEPRECNEW, P_NMTABEPRECNEW, P_NRORG);

                V_CDTABEPRECDES := P_CDTABEPRECNEW;

                IF P_DTINIVGPREC IS NULL OR P_DTFINVGPREC IS NULL THEN
                    INSERT INTO TABEVEND
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                    SELECT
                        P_CDFILIALDES, V_CDTABEPRECDES, DTINIVGPREC, DTFINVGPREC, NRORG
                    FROM
                        TABEVEND
                    WHERE
                        (CDFILIAL   = P_CDFILIALORI)   AND
                        (CDTABEPREC = P_CDTABEPRECORI) AND
                        (NRORG 		= P_NRORG);

                    INSERT INTO ITEMPRECO
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                        VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                        IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                    SELECT
                        P_CDFILIALDES, V_CDTABEPRECDES, I.DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                        I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                        I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                    FROM
                        ITEMPRECO I, PRODUTO R
                    WHERE
                        (I.CDFILIAL   = P_CDFILIALORI)   AND
                        (I.CDTABEPREC = P_CDTABEPRECORI) AND
                        (I.NRORG 	  = P_NRORG) 		 AND
                        (R.CDPRODUTO  = I.CDPRODUTO)     AND
                        (R.NRORG 	  = I.NRORG)		 AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S');

                    INSERT INTO ITEMPRECODIA
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                        HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                    SELECT
                        P_CDFILIALDES, V_CDTABEPRECDES, I.DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                        I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                    FROM
                        ITEMPRECODIA I,  PRODUTO R
                    WHERE
                        (I.CDFILIAL   = P_CDFILIALORI)   AND
                        (I.CDTABEPREC = P_CDTABEPRECORI) AND
                        (I.NRORG 	  = P_NRORG)		 AND
                        (R.CDPRODUTO  = I.CDPRODUTO)	 AND
                        (R.NRORG 	  = I.NRORG)		 AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S');

                    P_OK := 'OK';
                    V_DTINIVGPRECORI := NULL;

                    BEGIN
                        SELECT T.DTINIVGPREC
                        INTO V_DTINIVGPRECORI
                        FROM TABEVEND T
                        WHERE (T.CDFILIAL      = P_CDFILIALORI)
                          AND (T.CDTABEPREC  = P_CDTABEPRECORI)
                          AND SYSDATE BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC
                          AND (T.NRORG       = P_NRORG);
                    EXCEPTION
                        WHEN NO_DATA_FOUND THEN
                            V_DTINIVGPRECORI := NULL;
                    END;

                    IF V_DTINIVGPRECORI IS NULL THEN
                        P_OK := 'ERROV';
                        ROLLBACK;
                    END IF;
                ELSE
                    INSERT INTO TABEVEND
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                    VALUES
                        (P_CDFILIALDES, V_CDTABEPRECDES, P_DTINIVGPREC, P_DTFINVGPREC, P_NRORG);

                    INSERT INTO ITEMPRECO
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                        VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                        IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                    SELECT
                        P_CDFILIALDES, V_CDTABEPRECDES, P_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                        I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                        I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                    FROM
                        ITEMPRECO I, PRODUTO R
                    WHERE
                        (I.CDFILIAL    = P_CDFILIALORI)   AND
                        (I.CDTABEPREC  = P_CDTABEPRECORI) AND
                        (I.DTINIVGPREC = P_DTVGPROD)      AND
                        (I.NRORG 	   = P_NRORG) 		  AND
                        (R.CDPRODUTO   = I.CDPRODUTO)     AND
                        (R.NRORG 	   = I.NRORG)		  AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S');

                    INSERT INTO ITEMPRECODIA
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                        HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                    SELECT
                        P_CDFILIALDES, V_CDTABEPRECDES, P_DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                        I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                    FROM
                        ITEMPRECODIA I,  PRODUTO R
                    WHERE
                        (I.CDFILIAL   = P_CDFILIALORI)   AND
                        (I.CDTABEPREC = P_CDTABEPRECORI) AND
                        (I.DTINIVGPREC= P_DTVGPROD)      AND
                        (I.NRORG 	  = P_NRORG)		 AND
                        (R.CDPRODUTO  = I.CDPRODUTO)	 AND
                        (R.NRORG 	  = I.NRORG)		 AND
                        (NVL(R.IDPRODATIVPR,'S') = 'S');

                    P_OK := 'OK';
                END IF;
                COMMIT;
            ELSE
                P_OK := 'ERROA';
                ROLLBACK;
            END IF;
        ELSE
            SELECT MAX (CDTABEPREC)
                INTO V_TABPREC
                FROM TABEPREC
                WHERE
                    (CDFILIAL   = P_CDFILIALDES)   AND
                    (CDTABEPREC = P_CDTABEPRECNEW) AND
                    (NRORG 	    = P_NRORG);

            IF V_TABPREC IS NULL THEN
                INSERT INTO TABEPREC
                    (CDFILIAL, CDTABEPREC, NMTABEPREC, NRORG)
                VALUES
                    (P_CDFILIALDES, P_CDTABEPRECNEW, P_NMTABEPRECNEW, P_NRORG);

                V_CDTABEPRECDES := P_CDTABEPRECNEW;

                SELECT MAX (CDTABEPREC)
                    INTO V_TABVEND
                    FROM TABEVEND
                    WHERE
                        (CDFILIAL   = P_CDFILIALDES)   AND
                        (CDTABEPREC = P_CDTABEPRECNEW) AND
                        (NRORG 	    = P_NRORG);

                IF V_TABVEND IS NULL THEN
                    IF P_DTINIVGPREC IS NULL OR P_DTFINVGPREC IS NULL THEN
                        INSERT INTO TABEVEND
                            (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                        SELECT P_CDFILIALDES, V_CDTABEPRECDES, DTINIVGPREC, DTFINVGPREC, NRORG
                            FROM TABEVEND
                            WHERE (CDFILIAL = P_CDFILIALORI)   
                                AND (CDTABEPREC = P_CDTABEPRECORI) 
                                AND (NRORG = P_NRORG);

                        FOR C_PRODUTOEXP IN (
                            SELECT DISTINCT CDPRODUTO
                                FROM PRODEXPOTACAOPR
                                WHERE CDFILIAL = P_CDFILIALDES
                                    AND CDTABEPREC = P_CDTABEPRECNEW
                                    AND NRORG = P_NRORG
                        ) LOOP

                        V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

                        INSERT INTO ITEMPRECO
                            (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                            VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                            IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                        SELECT
                            P_CDFILIALDES, V_CDTABEPRECDES, I.DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                            I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                            I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                        FROM
                            ITEMPRECO I, PRODUTO R
                        WHERE
                            (I.CDFILIAL   = P_CDFILIALORI           ) AND
                            (I.CDTABEPREC = P_CDTABEPRECORI         ) AND
                            (I.NRORG 	  = P_NRORG                 ) AND
                            (I.CDPRODUTO  = V_CDPRODUTO             ) AND
                            (R.CDPRODUTO  = I.CDPRODUTO             ) AND
                            (R.NRORG 	  = I.NRORG                 ) AND
                            (NVL(R.IDPRODATIVPR,'S') = 'S'          );

                        INSERT INTO ITEMPRECODIA
                            (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                            HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                        SELECT
                            P_CDFILIALDES, V_CDTABEPRECDES, I.DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                            I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                        FROM
                            ITEMPRECODIA I,  PRODUTO R
                        WHERE
                            (I.CDFILIAL   = P_CDFILIALORI          ) AND
                            (I.CDTABEPREC = P_CDTABEPRECORI        ) AND
                            (I.NRORG 	  = P_NRORG                ) AND
                            (I.CDPRODUTO  = V_CDPRODUTO            ) AND
                            (R.CDPRODUTO  = I.CDPRODUTO            ) AND
                            (R.NRORG 	  = I.NRORG                ) AND
                            (NVL(R.IDPRODATIVPR,'S') = 'S'         );

                        END LOOP;

                        DELETE FROM PRODEXPOTACAOPR 
                            WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0) 
                                AND CDTABEPREC = P_CDTABEPRECNEW 
                                AND NRORG = P_NRORG;

                        P_OK := 'OK';
                        V_DTINIVGPRECORI := NULL;

                        BEGIN
                            SELECT T.DTINIVGPREC
                                INTO V_DTINIVGPRECORI
                                FROM TABEVEND T
                                WHERE (T.CDFILIAL      = P_CDFILIALORI)
                                    AND (T.CDTABEPREC  = P_CDTABEPRECORI)
                                    AND SYSDATE BETWEEN T.DTINIVGPREC AND T.DTFINVGPREC
                                    AND (T.NRORG       = P_NRORG);
                        EXCEPTION
                            WHEN NO_DATA_FOUND THEN
                                V_DTINIVGPRECORI := NULL;
                        END;

                        IF V_DTINIVGPRECORI IS NULL THEN
                            P_OK := 'ERROV';
                            ROLLBACK;
                        END IF;
                    ELSE
                        INSERT INTO TABEVEND
                            (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTFINVGPREC, NRORG)
                        VALUES
                            (P_CDFILIALDES, V_CDTABEPRECDES, P_DTINIVGPREC, P_DTFINVGPREC, P_NRORG);

                        FOR C_PRODUTOEXP IN (
                            SELECT DISTINCT CDPRODUTO
                                FROM PRODEXPOTACAOPR
                                WHERE CDFILIAL   = P_CDFILIALDES
                                    AND CDTABEPREC = P_CDTABEPRECNEW
                                    AND NRORG = P_NRORG
                        ) LOOP

                        V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

                        INSERT INTO ITEMPRECO
                            (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR,
                            VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                            IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG)
                        SELECT
                            P_CDFILIALDES, V_CDTABEPRECDES, P_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR,
                            I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                            I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG
                        FROM
                            ITEMPRECO I, PRODUTO R
                        WHERE
                            (I.CDFILIAL    = P_CDFILIALORI         ) AND
                            (I.CDTABEPREC  = P_CDTABEPRECORI       ) AND
                            (I.DTINIVGPREC = P_DTVGPROD            ) AND
                            (I.NRORG 	   = P_NRORG               ) AND
                            (I.CDPRODUTO   = V_CDPRODUTO           ) AND
                            (R.CDPRODUTO   = I.CDPRODUTO           ) AND
                            (R.NRORG 	   = I.NRORG               ) AND
                            (NVL(R.IDPRODATIVPR,'S') = 'S'         );

                        INSERT INTO ITEMPRECODIA
                            (CDFILIAL, CDTABEPREC, DTINIVGPREC, DTINIVALPREC, DTFINVALPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR,
                            HRINIPRECDIA, HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, CDTIPOCONSPD, NRORG)
                        SELECT
                            P_CDFILIALDES, V_CDTABEPRECDES, P_DTINIVGPREC, I.DTINIVALPREC, I.DTFINVALPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR,
                            I.HRINIPRECDIA, I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.CDTIPOCONSPD, I.NRORG
                        FROM
                            ITEMPRECODIA I,  PRODUTO R
                        WHERE
                            (I.CDFILIAL    = P_CDFILIALORI         ) AND
                            (I.CDTABEPREC  = P_CDTABEPRECORI       ) AND
                            (I.DTINIVGPREC = P_DTVGPROD            ) AND
                            (I.NRORG 	   = P_NRORG               ) AND
                            (I.CDPRODUTO   = V_CDPRODUTO           ) AND
                            (R.CDPRODUTO   = I.CDPRODUTO           ) AND
                            (R.NRORG 	   = I.NRORG               ) AND
                            (NVL(R.IDPRODATIVPR,'S') = 'S'         );

                        END LOOP;

                        DELETE FROM PRODEXPOTACAOPR 
                            WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0) 
                                AND CDTABEPREC = P_CDTABEPRECNEW 
                                AND NRORG = P_NRORG;

                        P_OK := 'OK';
                    END IF;
                ELSE
                    FOR C_TABVEND IN (
                        SELECT DTINIVGPREC
                            FROM TABEVEND
                            WHERE
                                (CDFILIAL   = P_CDFILIALDES)   AND
                                (CDTABEPREC = P_CDTABEPRECNEW) AND
                                (NRORG 	    = P_NRORG)
                    ) LOOP

                    V_DTINIVGPREC := C_TABVEND.DTINIVGPREC;

                    FOR C_PRODUTOEXP IN (
                        SELECT DISTINCT CDPRODUTO
                            FROM PRODEXPOTACAOPR
                            WHERE CDFILIAL = P_CDFILIALDES
                                AND CDTABEPREC = P_CDTABEPRECNEW
                                AND NRORG = P_NRORG
                    ) LOOP

                    V_CDPRODUTO := C_PRODUTOEXP.CDPRODUTO;

                    INSERT INTO ITEMPRECO
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR, 
                        VRPRECITEM, VRPRESUGITEM, VRPRECITEMCL, VRPRECITEMDLV, VRPRECITEMCMD,
                        IDPRECVARIA, VRCUSTITEM, HRINIVENPROD, HRFIMVENPROD, NRORG,
                        VRPRECMAXITE, VRMKUPITEM, IDPRODPROMOC)
                    SELECT
                        P_CDFILIALDES, V_CDTABEPRECDES, V_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR, 
                        I.VRPRECITEM, I.VRPRESUGITEM, I.VRPRECITEMCL, I.VRPRECITEMDLV, I.VRPRECITEMCMD,
                        I.IDPRECVARIA, I.VRCUSTITEM, I.HRINIVENPROD, I.HRFIMVENPROD, I.NRORG,
                        I.VRPRECMAXITE, I.VRMKUPITEM, I.IDPRODPROMOC
                    FROM ITEMPRECO I, TABEVEND T
                    WHERE
                        (I.CDFILIAL    = P_CDFILIALORI       ) AND
                        (I.CDTABEPREC  = P_CDTABEPRECORI     ) AND
                        (I.CDPRODUTO   = V_CDPRODUTO         ) AND
                        (I.NRORG       = P_NRORG             ) AND
                        (I.CDFILIAL    = T.CDFILIAL          ) AND
                        (I.CDTABEPREC  = T.CDTABEPREC        ) AND
                        (I.DTINIVGPREC = T.DTINIVGPREC       ) AND
                        (I.NRORG       = T.NRORG             ) AND
                        (SYSDATE BETWEEN I.DTINIVGPREC AND T.DTFINVGPREC);

                    INSERT INTO ITEMPRECODIA
                        (CDFILIAL, CDTABEPREC, DTINIVGPREC, CDPRODUTO, CDPRPAITABPR, NRDIASEMANPR, HRINIPRECDIA, CDTIPOCONSPD,
                        HRFINPRECDIA, VRPRECODIA, IDPERVALORPR, IDDESCACREPR, DTINIVALPREC, DTFINVALPREC, IDVISUACUPOM, NRORG)
                    SELECT
                        P_CDFILIALDES, V_CDTABEPRECDES, V_DTINIVGPREC, I.CDPRODUTO, I.CDPRPAITABPR, I.NRDIASEMANPR, I.HRINIPRECDIA, I.CDTIPOCONSPD,
                        I.HRFINPRECDIA, I.VRPRECODIA, I.IDPERVALORPR, I.IDDESCACREPR, I.DTINIVALPREC, I.DTFINVALPREC, I.IDVISUACUPOM, I.NRORG
                    FROM ITEMPRECODIA I, TABEVEND T
                    WHERE
                        (I.CDFILIAL    = P_CDFILIALORI       ) AND
                        (I.CDTABEPREC  = P_CDTABEPRECORI     ) AND
                        (I.CDPRODUTO   = V_CDPRODUTO         ) AND
                        (I.NRORG       = P_NRORG             ) AND
                        (I.CDFILIAL    = T.CDFILIAL          ) AND
                        (I.CDTABEPREC  = T.CDTABEPREC        ) AND
                        (I.DTINIVGPREC = T.DTINIVGPREC       ) AND
                        (I.NRORG       = T.NRORG             ) AND
                        (SYSDATE BETWEEN I.DTINIVGPREC AND T.DTFINVGPREC);

                    P_OK := 'OK';
                    END LOOP;
                    END LOOP;

                    DELETE FROM PRODEXPOTACAOPR 
                        WHERE (INSTR(P_CDFILIALDES, CDFILIAL) <> 0) 
                            AND CDTABEPREC = P_CDTABEPRECNEW 
                            AND NRORG = P_NRORG;
                END IF;
                COMMIT;
            ELSE
                P_OK := 'ERROA';
                ROLLBACK;
            END IF;
        END IF;
    END IF;

    COMMIT;
END;