CREATE OR REPLACE PROCEDURE RECALCULA_PRECOPED(
   P_CDFILIAL      IN PEDIDO.CDFILIAL%TYPE,
   P_NRPEDINICIAL  IN PEDIDO.NRPEDIDO%TYPE,
   P_NRPEDFINAL    IN PEDIDO.NRPEDIDO%TYPE,
   P_CDPRECOORIG   IN PRECOBASE.CDPRECOBASE%TYPE DEFAULT NULL,
   P_CDPRECODEST   IN PRECOBASE.CDPRECOBASE%TYPE DEFAULT NULL,
   P_IDDTATUAL     IN PARAMPEDIDO.IDOBRTPOPPED%TYPE DEFAULT 'N',
   P_NRERRO        OUT NUMBER
) IS

   V_CTPRODSEMPRECO  NUMBER;
   V_CTITEMAPROVADO  NUMBER;
   V_NRERRO          NUMBER;
BEGIN
   P_NRERRO := 0;
   IF P_CDPRECOORIG IS NULL THEN
      /* Apenas altera os precos dos itens      */
      FOR C_PEDIDO IN
         (SELECT
             PE.CDFILIAL, PE.NRPEDIDO, PE.CDPRECOBASE, PE.DTPEDIDO
          FROM PEDIDO PE
          WHERE PE.CDFILIAL    = P_CDFILIAL
            AND PE.NRPEDIDO BETWEEN P_NRPEDINICIAL AND P_NRPEDFINAL
            AND PE.IDSITPEDIDO IN ('1', '2')) LOOP

         SELECT SUM(CTPROD) INTO V_CTPRODSEMPRECO
         FROM
           (SELECT COUNT(*) CTPROD
            FROM ITEMPEDIDO IT
            WHERE IT.CDFILIAL    = P_CDFILIAL
              AND IT.NRPEDIDO    = C_PEDIDO.NRPEDIDO
            UNION ALL
            SELECT - COUNT(*) CTPROD
            FROM ITEMPEDIDO IT, ITEMPREBASE PB
            WHERE IT.CDFILIAL    = P_CDFILIAL
              AND IT.NRPEDIDO    = C_PEDIDO.NRPEDIDO
              AND PB.CDPRECOBASE = C_PEDIDO.CDPRECOBASE
              AND IT.CDPRODUTO   = PB.CDPRODUTO
              AND (DECODE(P_IDDTATUAL, 'S', SYSDATE, IT.DTENTITPED) BETWEEN PB.DTINIVALIPB(+) AND NVL(PB.DTFIMVALIPB(+), SYSDATE+1)));


         SELECT COUNT(*) INTO V_CTITEMAPROVADO
         FROM ITEMPEDIDO IT
         WHERE IT.CDFILIAL     = P_CDFILIAL
           AND IT.NRPEDIDO     = C_PEDIDO.NRPEDIDO
           AND IT.IDSITITEMPED > '2';

         IF (V_CTPRODSEMPRECO = 0) AND
            (V_CTITEMAPROVADO = 0) THEN
            ALTERA_PRECOITENS(P_CDFILIAL, C_PEDIDO.NRPEDIDO, C_PEDIDO.CDPRECOBASE, P_IDDTATUAL, V_NRERRO);

            IF P_NRERRO = 0 THEN
               P_NRERRO := V_NRERRO;
            END IF;
         ELSE
            IF (V_CTPRODSEMPRECO <> 0) AND (P_NRERRO = 0) THEN
               P_NRERRO := 2;
            END IF;
            IF (V_CTITEMAPROVADO <> 0) AND (P_NRERRO = 0) THEN
               P_NRERRO := 3;
            END IF;
         END IF;
      END LOOP;
   ELSE
      /* Altera o preco base e os precos dos itens      */
      FOR C_PEDIDO IN
         (SELECT
             PE.CDFILIAL, PE.NRPEDIDO, PE.CDPRECOBASE, PE.DTPEDIDO
          FROM PEDIDO PE
          WHERE PE.CDFILIAL    = P_CDFILIAL
            AND PE.NRPEDIDO BETWEEN P_NRPEDINICIAL AND P_NRPEDFINAL
            AND PE.CDPRECOBASE = P_CDPRECOORIG
            AND PE.IDSITPEDIDO IN ('1', '2')) LOOP

         SELECT SUM(CTPROD) INTO V_CTPRODSEMPRECO
         FROM
           (SELECT COUNT(*) CTPROD
            FROM ITEMPEDIDO IT
            WHERE IT.CDFILIAL    = P_CDFILIAL
              AND IT.NRPEDIDO    = C_PEDIDO.NRPEDIDO
            UNION ALL
            SELECT - COUNT(*) CTPROD
            FROM ITEMPEDIDO IT, ITEMPREBASE PB
            WHERE IT.CDFILIAL    = P_CDFILIAL
              AND IT.NRPEDIDO    = C_PEDIDO.NRPEDIDO
              AND PB.CDPRECOBASE = P_CDPRECODEST
              AND IT.CDPRODUTO   = PB.CDPRODUTO
              AND (DECODE(P_IDDTATUAL, 'S', SYSDATE, IT.DTENTITPED) BETWEEN PB.DTINIVALIPB(+) AND NVL(PB.DTFIMVALIPB(+), SYSDATE+1)));


         SELECT COUNT(*) INTO V_CTITEMAPROVADO
         FROM ITEMPEDIDO IT
         WHERE IT.CDFILIAL     = P_CDFILIAL
           AND IT.NRPEDIDO     = C_PEDIDO.NRPEDIDO
           AND IT.IDSITITEMPED > '2';

         IF (V_CTPRODSEMPRECO = 0) AND
            (V_CTITEMAPROVADO = 0) THEN
            ALTERA_PRECOITENS(P_CDFILIAL, C_PEDIDO.NRPEDIDO, P_CDPRECODEST, P_IDDTATUAL,  V_NRERRO);

            IF P_NRERRO = 0 THEN
               P_NRERRO := V_NRERRO;
            END IF;

            UPDATE PEDIDO
            SET CDPRECOBASE = P_CDPRECODEST
            WHERE CDFILIAL = P_CDFILIAL
              AND NRPEDIDO = C_PEDIDO.NRPEDIDO;
         ELSE
            IF (V_CTPRODSEMPRECO <> 0) AND (P_NRERRO = 0) THEN
               P_NRERRO := 2;
            END IF;
            IF (V_CTITEMAPROVADO <> 0) AND (P_NRERRO = 0) THEN
               P_NRERRO := 3;
            END IF;
         END IF;
      END LOOP;
   END IF;
END;
/