IF EXISTS (SELECT *
           FROM SYSOBJECTS O, SYSUSERS U
           WHERE O.NAME = 'RECALCULA_PRECOPED' AND
	              TYPE = 'P' 			           AND
      	        U.UID = O.UID                 AND
      	        U.NAME = USER)
DROP PROCEDURE RECALCULA_PRECOPED
GO
CREATE PROCEDURE RECALCULA_PRECOPED @P_CDFILIAL      VARCHAR(4),
                                    @P_NRPEDINICIAL  VARCHAR(10),
                                    @P_NRPEDFINAL    VARCHAR(10),
                                    @P_CDPRECOORIG   VARCHAR(2) = NULL,
                                    @P_CDPRECODEST   VARCHAR(2) = NULL,
                                    @P_NRERRO        INTEGER OUTPUT
AS

  DECLARE @V_CTPRODSEMPRECO  DOUBLE PRECISION;
  DECLARE @V_CTITEMAPROVADO  DOUBLE PRECISION;

  DECLARE @V_CDFILIAL      VARCHAR(4);
  DECLARE @V_NRPEDIDO      VARCHAR(10);
  DECLARE @V_CDPRECOBASE   VARCHAR(2);
  DECLARE @V_DTPEDIDO      DATETIME;

  DECLARE @V_NRERRO        INTEGER;
BEGIN
   SET @P_NRERRO = 0;
   IF (@P_CDPRECOORIG IS NULL) BEGIN
      DECLARE C_PEDIDO CURSOR FOR
      SELECT PE.CDFILIAL, PE.NRPEDIDO, PE.CDPRECOBASE
      FROM PEDIDO PE
      WHERE PE.CDFILIAL    = @P_CDFILIAL
        AND PE.NRPEDIDO BETWEEN @P_NRPEDINICIAL AND @P_NRPEDFINAL
        AND PE.IDSITPEDIDO IN ('1', '2')
      OPEN C_PEDIDO;

      FETCH NEXT FROM C_PEDIDO INTO @V_CDFILIAL, @V_NRPEDIDO, @V_CDPRECOBASE;
      WHILE (@@FETCH_STATUS = 0) BEGIN

         EXECUTE ALTERA_PRECOITENS @P_CDFILIAL, @V_NRPEDIDO, @V_CDPRECOBASE, @P_NRERRO = @V_NRERRO OUTPUT;

         IF @P_NRERRO = 0 BEGIN
            SET @P_NRERRO = @V_NRERRO;
         END;

         FETCH NEXT FROM C_PEDIDO INTO @V_CDFILIAL, @V_NRPEDIDO, @V_CDPRECOBASE;
      END;
      CLOSE C_PEDIDO;
      DEALLOCATE C_PEDIDO;
   END
   ELSE BEGIN
      DECLARE C_PEDIDO CURSOR FOR
      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')
      OPEN C_PEDIDO;

      FETCH NEXT FROM C_PEDIDO INTO @V_CDFILIAL, @V_NRPEDIDO, @V_CDPRECOBASE, @V_DTPEDIDO;
      WHILE (@@FETCH_STATUS = 0) BEGIN

         SELECT V_CTPRODSEMPRECO = SUM(A.CTPROD)
         FROM
           (SELECT COUNT(*) AS CTPROD
            FROM ITEMPEDIDO IT
            WHERE IT.CDFILIAL    = @P_CDFILIAL
              AND IT.NRPEDIDO    = @V_NRPEDIDO
            UNION ALL
            SELECT - COUNT(*) AS CTPROD
            FROM ITEMPEDIDO IT, ITEMPREBASE PB
            WHERE IT.CDFILIAL    = @P_CDFILIAL
              AND IT.NRPEDIDO    = @V_NRPEDIDO
              AND PB.CDPRECOBASE = @P_CDPRECODEST
              AND IT.CDPRODUTO   = PB.CDPRODUTO) A;

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

         IF (@V_CTPRODSEMPRECO = 0) AND
            (@V_CTITEMAPROVADO = 0) BEGIN

            EXECUTE ALTERA_PRECOITENS @P_CDFILIAL, @V_NRPEDIDO, @P_CDPRECODEST, @P_NRERRO = @V_NRERRO OUTPUT;

            IF @P_NRERRO = 0 BEGIN
               SET @P_NRERRO = @V_NRERRO;
            END;

            UPDATE PEDIDO
            SET CDPRECOBASE = @P_CDPRECODEST
            WHERE CDFILIAL = @P_CDFILIAL
              AND NRPEDIDO = @V_NRPEDIDO;
         END
         ELSE BEGIN
            IF (@V_CTPRODSEMPRECO = 0) AND (@P_NRERRO = 0) BEGIN
               SET @P_NRERRO = 2;
            END;
            IF (@V_CTITEMAPROVADO = 0) AND (@P_NRERRO = 0) BEGIN
               SET @P_NRERRO = 3;
            END;
         END;

         FETCH NEXT FROM C_PEDIDO INTO @V_CDFILIAL, @V_NRPEDIDO, @V_CDPRECOBASE, @V_DTPEDIDO;
      END;
      CLOSE C_PEDIDO;
      DEALLOCATE C_PEDIDO;
   END;
END;
GO


-- VERSAO --
EXEC ATUVEROBJ 'RECALCULA_PRECOPED','4.05.126';
GO

