IF EXISTS (SELECT
              *
           FROM
              SYSOBJECTS O, SYSUSERS U
           WHERE
              O.NAME = 'REPLICA_PRODUTO_FILIAL' AND
              TYPE = 'P' 		            AND
              U.UID = O.UID               AND
              U.NAME = USER)

DROP PROCEDURE REPLICA_PRODUTO_FILIAL
GO

CREATE PROCEDURE REPLICA_PRODUTO_FILIAL(
                            @P_CDFILIORIG    VARCHAR(4),
                            @P_CDFILIDEST    VARCHAR(2000),
                            @P_IDTPFILDEST   VARCHAR,
                            @P_IDTPCONFPAR   INTEGER,
                            @P_CDPRODINIC    VARCHAR(20),
                            @P_CDPRODFINA    VARCHAR(20),
                            @P_LISTAPARAM    VARCHAR(100))
AS
BEGIN
   DECLARE @V_NRPRODUTO       NUMERIC(15,5);

   DECLARE @V_CDFILIAL        VARCHAR(4);
   DECLARE @V_CDPRODUTO       VARCHAR(20);
   DECLARE @V_IDCNTRESTOQ     VARCHAR(4);
   DECLARE @V_QTESTOQMIN      NUMERIC(15,5);   
   DECLARE @V_IDPRODATIV      VARCHAR(4);
   DECLARE @V_CDALMOXARIFE    VARCHAR(6);
   DECLARE @V_CDLOCALESTOQ    VARCHAR(6);
   DECLARE @V_NRTEMPREPEST    NUMERIC(15,5);
   DECLARE @V_CDALMOXINSPR    VARCHAR(6);
   DECLARE @V_CDLOCALINSPR    VARCHAR(6);
   DECLARE @V_CDALMOXPRACA    VARCHAR(6);
   DECLARE @V_CDLOCALPRACA    VARCHAR(6);
   DECLARE @V_CDALMOXPRCLI    VARCHAR(6);
   DECLARE @V_CDLOCALPRCLI    VARCHAR(6);
   DECLARE @V_CDALMOXPRSEMACA VARCHAR(6);
   DECLARE @V_CDLOCALPRSEMACA VARCHAR(6);
   DECLARE @V_CDALMOXQUAR     VARCHAR(6);
   DECLARE @V_CDLOCALQUAR     VARCHAR(6);
   DECLARE @V_NRDIASESTOQ     VARCHAR(6);
   DECLARE @V_VRPTOLQTITAF    NUMERIC(15,5);
   DECLARE @V_VRPTOLVRITAF    NUMERIC(15,5);
   DECLARE @V_VRTXADMPROD     NUMERIC(15,5);
   DECLARE @V_QTESTMINSAID    NUMERIC(15,5);
   DECLARE @V_IDUTCTRLLOTE    VARCHAR(3);
   DECLARE @V_IDUTPRORIGOBRIG VARCHAR(3);
   DECLARE @V_VRMINEMPENHOAF  NUMERIC(15,5);
   DECLARE @V_VRMINEMPENHOOL  NUMERIC(15,5);
   DECLARE @V_CDFILAGRUP      VARCHAR(4);
   DECLARE @V_CDFILLISTA      VARCHAR(4);
   DECLARE @V_COUNT           INTEGER;

   BEGIN TRANSACTION;	
   IF @P_IDTPFILDEST = 'F' BEGIN
      IF @P_IDTPCONFPAR = 0 BEGIN
         DECLARE  C_PRODFILI CURSOR FOR
            SELECT D.CDFILIAL, D.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                   O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                   O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                   O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,  O.IDUTPRORIGOBRIG,
                   O.VRMINEMPENHOAF,  O.VRMINEMPENHOOL
            FROM PRODFILI O, PRODFILI D, PRODUTO P
            WHERE O.CDFILIAL = @P_CDFILIORIG
              AND O.CDPRODUTO = P.CDPRODUTO
              AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA
              AND D.CDFILIAL = @P_CDFILIDEST
              AND D.CDPRODUTO = O.CDPRODUTO;

         OPEN C_PRODFILI;
         FETCH NEXT FROM C_PRODFILI INTO @V_CDFILIAL, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                   @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                   @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                   @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE, @V_IDUTPRORIGOBRIG,
                   @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

         WHILE (@@FETCH_STATUS = 0) BEGIN
            IF SUBSTRING(@P_LISTAPARAM,1,1) = 'S' BEGIN
               UPDATE PRODFILI
               SET  IDCNTRESTOQ  =  @V_IDCNTRESTOQ,
                    QTESTOQMIN   =  @V_QTESTOQMIN ,
                    IDPRODATIV   =  @V_IDPRODATIV ,
                    NRTEMPREPEST =  @V_NRTEMPREPEST,
                    NRDIASESTOQ  =  @V_NRDIASESTOQ,
                    VRPTOLQTITAF =  @V_VRPTOLQTITAF,
                    VRPTOLVRITAF =  @V_VRPTOLVRITAF,
                    VRTXADMPROD  =  @V_VRTXADMPROD,
                    QTESTMINSAID =  @V_QTESTMINSAID,
                    IDUTCTRLLOTE =  @V_IDUTCTRLLOTE,
                    IDUTPRORIGOBRIG = @V_IDUTPRORIGOBRIG,
                    VRMINEMPENHOAF  = @V_VRMINEMPENHOAF,
                    VRMINEMPENHOOL  = @V_VRMINEMPENHOOL,
                    CDALMOXARIFE =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXARIFE),CDALMOXARIFE),
                    CDLOCALESTOQ =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ),
                    CDALMOXINSPR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXINSPR),CDALMOXINSPR),
                    CDLOCALINSPR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR),
                    CDALMOXPRACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRACA), CDALMOXPRACA),
                    CDLOCALPRACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA),
                    CDALMOXPRCLI =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRCLI), CDALMOXPRCLI),
                    CDLOCALPRCLI =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI),
                    CDALMOXPRSEMACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA),
                    CDLOCALPRSEMACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA),
                    CDALMOXQUAR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL,  @V_CDALMOXQUAR),CDALMOXQUAR),
                    CDLOCALQUAR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR)
               WHERE CDFILIAL = @V_CDFILIAL
                 AND CDPRODUTO = @V_CDPRODUTO;
            END 
            ELSE BEGIN
               UPDATE PRODFILI
               SET  IDPRODATIV   =  (CASE SUBSTRING(@P_LISTAPARAM,2,1) WHEN 'S' THEN @V_IDPRODATIV ELSE IDPRODATIV END),
                    IDCNTRESTOQ  =  (CASE SUBSTRING(@P_LISTAPARAM,3,1) WHEN 'S' THEN @V_IDCNTRESTOQ ELSE IDCNTRESTOQ END),
                    IDUTCTRLLOTE =  (CASE SUBSTRING(@P_LISTAPARAM,4,1) WHEN 'S' THEN @V_IDUTCTRLLOTE ELSE IDUTCTRLLOTE END) ,
                    NRDIASESTOQ  =  (CASE SUBSTRING(@P_LISTAPARAM,5,1) WHEN 'S' THEN @V_NRDIASESTOQ ELSE NRDIASESTOQ END),
                    VRPTOLQTITAF =  (CASE SUBSTRING(@P_LISTAPARAM,6,1) WHEN 'S' THEN @V_VRPTOLQTITAF ELSE VRPTOLQTITAF END),
                    VRPTOLVRITAF =  (CASE SUBSTRING(@P_LISTAPARAM,7,1) WHEN 'S' THEN @V_VRPTOLVRITAF ELSE VRPTOLVRITAF END),
                    QTESTOQMIN   =  (CASE SUBSTRING(@P_LISTAPARAM,8,1) WHEN 'S' THEN @V_QTESTOQMIN ELSE QTESTOQMIN END) ,
                    QTESTMINSAID =  (CASE SUBSTRING(@P_LISTAPARAM,9,1) WHEN 'S' THEN @V_QTESTMINSAID ELSE QTESTMINSAID END),
                    VRTXADMPROD  =  (CASE SUBSTRING(@P_LISTAPARAM,12,1) WHEN 'S' THEN @V_VRTXADMPROD ELSE VRTXADMPROD END),
                    NRTEMPREPEST =  (CASE SUBSTRING(@P_LISTAPARAM,10,1) WHEN 'S' THEN @V_NRTEMPREPEST ELSE NRTEMPREPEST END),
                    CDALMOXARIFE =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXARIFE),CDALMOXARIFE) ELSE CDALMOXARIFE END),
                    CDLOCALESTOQ =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ) ELSE  CDLOCALESTOQ END),
                    CDALMOXINSPR =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                           ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXINSPR),CDALMOXINSPR) ELSE CDALMOXINSPR END),
                    CDLOCALINSPR =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR) ELSE CDLOCALINSPR END),
                    CDALMOXPRACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRACA), CDALMOXPRACA) ELSE CDALMOXPRACA END),
                    CDLOCALPRACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA) ELSE CDLOCALPRACA END),
                    CDALMOXPRCLI =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRCLI), CDALMOXPRCLI) ELSE CDALMOXPRCLI END),
                    CDLOCALPRCLI =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI) ELSE CDLOCALPRCLI END),
                    CDALMOXPRSEMACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                         ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA) ELSE CDALMOXPRSEMACA END),
                    CDLOCALPRSEMACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                         ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA) ELSE CDLOCALPRSEMACA END),
                    CDALMOXQUAR  =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                         ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL,  @V_CDALMOXQUAR),CDALMOXQUAR) ELSE CDALMOXQUAR END),
                    CDLOCALQUAR  =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                          ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR) ELSE CDLOCALQUAR END),
                    IDUTPRORIGOBRIG =  (CASE SUBSTRING(@P_LISTAPARAM,13,1) WHEN 'S' THEN @V_IDUTPRORIGOBRIG ELSE IDUTPRORIGOBRIG END),
                    VRMINEMPENHOAF  =  (CASE SUBSTRING(@P_LISTAPARAM,14,1) WHEN 'S' THEN @V_VRMINEMPENHOAF ELSE VRMINEMPENHOAF END),
                    VRMINEMPENHOOL  =  (CASE SUBSTRING(@P_LISTAPARAM,15,1) WHEN 'S' THEN @V_VRMINEMPENHOOL ELSE VRMINEMPENHOOL END)
               WHERE CDFILIAL = @V_CDFILIAL
                 AND CDPRODUTO = @V_CDPRODUTO;
            END;
            FETCH NEXT FROM C_PRODFILI INTO @V_CDFILIAL, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                   @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                   @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                   @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                   @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;
         END;
         CLOSE C_PRODFILI;
         DEALLOCATE C_PRODFILI;
      END
      ELSE BEGIN
         IF @P_IDTPCONFPAR = 1 BEGIN
            DECLARE C_PRODFILIDIF CURSOR FOR
               SELECT P.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                      O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                      O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                      O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                      O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
                FROM PRODFILI O, PRODUTO P
               WHERE O.CDFILIAL = @P_CDFILIORIG
                 AND O.CDPRODUTO = P.CDPRODUTO
                 AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA;

            OPEN C_PRODFILIDIF;
            FETCH NEXT FROM C_PRODFILIDIF INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                      @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                      @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                      @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                      @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

            WHILE (@@FETCH_STATUS = 0) BEGIN


               SELECT @V_NRPRODUTO = COUNT(*)
               FROM PRODFILI
               WHERE CDFILIAL = @P_CDFILIDEST
                  AND CDPRODUTO = @V_CDPRODUTO;

               IF (ISNULL(@V_NRPRODUTO,0) = 0) BEGIN
                  INSERT INTO PRODFILI
                     (CDFILIAL, CDPRODUTO, IDCNTRESTOQ, QTESTOQMIN, IDPRODATIV, NRTEMPREPEST,  NRDIASESTOQ,
                     VRPTOLQTITAF,  VRPTOLVRITAF, VRTXADMPROD, QTESTMINSAID, IDUTCTRLLOTE,
                      CDALMOXARIFE, CDLOCALESTOQ,  CDALMOXINSPR, CDLOCALINSPR, CDALMOXPRACA, CDLOCALPRACA,
                      CDALMOXPRCLI, CDLOCALPRCLI,  CDALMOXPRSEMACA, CDLOCALPRSEMACA, CDALMOXQUAR, CDLOCALQUAR,
                      IDUTPRORIGOBRIG, VRMINEMPENHOAF, VRMINEMPENHOOL)
                  VALUES
                     (@P_CDFILIDEST, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV,
                      @V_NRTEMPREPEST, @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF,
                      @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                      TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXARIFE),
                      TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),
                      TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXINSPR),
                      TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXINSPR,  @V_CDLOCALINSPR),
                      TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRACA),
                      TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRACA, @V_CDLOCALPRACA),
                      TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRCLI),
                      TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),
                      TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRSEMACA),
                      TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA),
                      TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXQUAR),
                      TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXQUAR, @V_CDLOCALQUAR ),
                      @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL);
               END;
               FETCH NEXT FROM C_PRODFILIDIF INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                      @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                      @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                      @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                      @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

            END;
            CLOSE C_PRODFILIDIF;
            DEALLOCATE C_PRODFILIDIF;
         END
         ELSE BEGIN
            SET @V_COUNT = 0;
            IF @P_IDTPCONFPAR = 2 BEGIN
               DECLARE C_PRODFILITOD CURSOR FOR
                  SELECT P.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                         O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                         O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                         O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                         O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
                   FROM PRODFILI O, PRODUTO P
                  WHERE O.CDFILIAL = @P_CDFILIORIG
                    AND O.CDPRODUTO = P.CDPRODUTO
                    AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA;

               OPEN C_PRODFILITOD;
               FETCH NEXT FROM C_PRODFILITOD INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                         @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                         @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                         @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                         @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;
               WHILE (@@FETCH_STATUS = 0) BEGIN

                  SELECT @V_NRPRODUTO = COUNT(*)
                  FROM PRODFILI
                  WHERE CDFILIAL = @P_CDFILIDEST
                     AND CDPRODUTO = @V_CDPRODUTO;

                  SET @V_COUNT = @V_COUNT + 1;

                  IF (ISNULL(@V_NRPRODUTO,0) = 0) BEGIN
                     INSERT INTO PRODFILI
                        (CDFILIAL, CDPRODUTO, IDCNTRESTOQ, QTESTOQMIN, IDPRODATIV, NRTEMPREPEST, NRDIASESTOQ, VRPTOLQTITAF,
                         VRPTOLVRITAF, VRTXADMPROD, QTESTMINSAID, IDUTCTRLLOTE, CDALMOXARIFE, CDLOCALESTOQ, CDALMOXINSPR,
		                   CDLOCALINSPR, CDALMOXPRACA, CDLOCALPRACA, CDALMOXPRCLI, CDLOCALPRCLI, CDALMOXPRSEMACA,
			                CDLOCALPRSEMACA, CDALMOXQUAR, CDLOCALQUAR, IDUTPRORIGOBRIG, VRMINEMPENHOAF, VRMINEMPENHOOL)
                     VALUES
                        (@P_CDFILIDEST, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV,
                         @V_NRTEMPREPEST, @V_NRDIASESTOQ, @V_VRPTOLQTITAF,
                         @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                         TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXARIFE),
                         TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),
                         TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXINSPR),
                         TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXINSPR,  @V_CDLOCALINSPR),
                         TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRACA),
                         TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRACA, @V_CDLOCALPRACA),
                         TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRCLI),
                         TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),
                         TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRSEMACA),
                         TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA),
                         TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXQUAR),
                         TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXQUAR, @V_CDLOCALQUAR ),
                         @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL);
                  END
                  ELSE BEGIN
                     UPDATE PRODFILI
                     SET  IDCNTRESTOQ  =  @V_IDCNTRESTOQ,
                          QTESTOQMIN   =  @V_QTESTOQMIN ,
                          IDPRODATIV   =  @V_IDPRODATIV ,
                          NRTEMPREPEST =  @V_NRTEMPREPEST,
                          NRDIASESTOQ  =  @V_NRDIASESTOQ,
                          VRPTOLQTITAF =  @V_VRPTOLQTITAF,
                          VRPTOLVRITAF =  @V_VRPTOLVRITAF,
                          VRTXADMPROD  =  @V_VRTXADMPROD,
                          QTESTMINSAID =  @V_QTESTMINSAID,
                          IDUTCTRLLOTE =  @V_IDUTCTRLLOTE,
                          IDUTPRORIGOBRIG = @V_IDUTPRORIGOBRIG,
                          VRMINEMPENHOAF  = @V_VRMINEMPENHOAF,
                          VRMINEMPENHOOL  = @V_VRMINEMPENHOOL,
                          CDALMOXARIFE =  ISNULL(TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXARIFE),CDALMOXARIFE),
                          CDLOCALESTOQ =  ISNULL(TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ),
                          CDALMOXINSPR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXINSPR),CDALMOXINSPR),
                          CDLOCALINSPR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR),
                          CDALMOXPRACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRACA), CDALMOXPRACA),
                          CDLOCALPRACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA),
                          CDALMOXPRCLI =  ISNULL(TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRCLI), CDALMOXPRCLI),
                          CDLOCALPRCLI =  ISNULL(TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI),
                          CDALMOXPRSEMACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA),
                          CDLOCALPRSEMACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA),
                          CDALMOXQUAR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@P_CDFILIDEST, @V_CDALMOXQUAR),CDALMOXQUAR),
                          CDLOCALQUAR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@P_CDFILIDEST, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR)
                     WHERE CDFILIAL = @P_CDFILIDEST
                       AND CDPRODUTO = @V_CDPRODUTO;
                  END;
                  IF @V_COUNT > 1000 BEGIN
                     SET @V_COUNT = 0;
                     COMMIT TRANSACTION;
                     BEGIN TRANSACTION;
                  END;

                  FETCH NEXT FROM C_PRODFILITOD INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                            @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                            @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                            @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                            @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

               END;
               CLOSE C_PRODFILITOD;
               DEALLOCATE C_PRODFILITOD;
            END;
         END;
      END;
   END
   ELSE BEGIN
      SET @V_COUNT = 0;
      IF @P_IDTPFILDEST = 'G' BEGIN
         IF @P_IDTPCONFPAR = 0 BEGIN
            DECLARE C_PRODFILI CURSOR FOR
               SELECT D.CDFILIAL, D.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                      O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                      O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                      O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                      O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
               FROM PRODFILI O, PRODFILI D, PRODUTO P, FILGRUPFILI A, GRUPFILI G
               WHERE O.CDFILIAL = @P_CDFILIORIG
                 AND O.CDPRODUTO = P.CDPRODUTO
                 AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA
                 AND G.CDGRUPFILI = @P_CDFILIDEST
                 AND G.CDGRUPFILI = A.CDGRUPFILI
                 AND D.CDFILIAL = A.CDFILIAL
                 AND D.CDPRODUTO = O.CDPRODUTO;

            OPEN C_PRODFILI;
            FETCH NEXT FROM C_PRODFILI INTO @V_CDFILIAL, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                      @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                      @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                      @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                      @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

            WHILE (@@FETCH_STATUS = 0) BEGIN

               IF SUBSTRING(@P_LISTAPARAM,1,1) = 'S' BEGIN
                  UPDATE PRODFILI
                  SET  IDCNTRESTOQ  =  @V_IDCNTRESTOQ,
                       QTESTOQMIN   =  @V_QTESTOQMIN ,
                       IDPRODATIV   =  @V_IDPRODATIV ,
                       NRTEMPREPEST =  @V_NRTEMPREPEST,
                       NRDIASESTOQ  =  @V_NRDIASESTOQ,
                       VRPTOLQTITAF =  @V_VRPTOLQTITAF,
                       VRPTOLVRITAF =  @V_VRPTOLVRITAF,
                       VRTXADMPROD  =  @V_VRTXADMPROD,
                       QTESTMINSAID =  @V_QTESTMINSAID,
                       IDUTCTRLLOTE =  @V_IDUTCTRLLOTE,
                       IDUTPRORIGOBRIG = @V_IDUTPRORIGOBRIG,
                       VRMINEMPENHOAF  = @V_VRMINEMPENHOAF,
                       VRMINEMPENHOOL  = @V_VRMINEMPENHOOL,
                       CDALMOXARIFE =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXARIFE),CDALMOXARIFE),
                       CDLOCALESTOQ =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ),
                       CDALMOXINSPR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXINSPR),CDALMOXINSPR),
                       CDLOCALINSPR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR),
                       CDALMOXPRACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRACA), CDALMOXPRACA),
                       CDLOCALPRACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA),
                       CDALMOXPRCLI =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRCLI), CDALMOXPRCLI),
                       CDLOCALPRCLI =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI),
                       CDALMOXPRSEMACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA),
                       CDLOCALPRSEMACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA),
                       CDALMOXQUAR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL,  @V_CDALMOXQUAR),CDALMOXQUAR),
                       CDLOCALQUAR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR)
                  WHERE CDFILIAL = @V_CDFILIAL
                    AND CDPRODUTO = @V_CDPRODUTO;
               END
               ELSE BEGIN
                  UPDATE PRODFILI
                  SET  IDPRODATIV   =  (CASE SUBSTRING(@P_LISTAPARAM,2,1) WHEN 'S' THEN @V_IDPRODATIV ELSE IDPRODATIV END),
                       IDCNTRESTOQ  =  (CASE SUBSTRING(@P_LISTAPARAM,3,1) WHEN 'S' THEN @V_IDCNTRESTOQ ELSE IDCNTRESTOQ END),
                       IDUTCTRLLOTE =  (CASE SUBSTRING(@P_LISTAPARAM,4,1) WHEN 'S' THEN @V_IDUTCTRLLOTE ELSE IDUTCTRLLOTE END),
                       NRDIASESTOQ  =  (CASE SUBSTRING(@P_LISTAPARAM,5,1) WHEN 'S' THEN @V_NRDIASESTOQ ELSE NRDIASESTOQ END),
                       VRPTOLQTITAF =  (CASE SUBSTRING(@P_LISTAPARAM,6,1) WHEN 'S' THEN @V_VRPTOLQTITAF ELSE VRPTOLQTITAF END),
                       VRPTOLVRITAF =  (CASE SUBSTRING(@P_LISTAPARAM,7,1) WHEN 'S' THEN @V_VRPTOLVRITAF ELSE VRPTOLVRITAF END),
                       QTESTOQMIN   =  (CASE SUBSTRING(@P_LISTAPARAM,8,1) WHEN 'S' THEN @V_QTESTOQMIN ELSE QTESTOQMIN END) ,
                       QTESTMINSAID =  (CASE SUBSTRING(@P_LISTAPARAM,9,1) WHEN 'S' THEN @V_QTESTMINSAID ELSE QTESTMINSAID END),
                       VRTXADMPROD  =  (CASE SUBSTRING(@P_LISTAPARAM,10,1) WHEN 'S' THEN @V_VRTXADMPROD ELSE VRTXADMPROD END),
                       NRTEMPREPEST =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN @V_NRTEMPREPEST ELSE NRTEMPREPEST END),
                       CDALMOXARIFE =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXARIFE),CDALMOXARIFE) ELSE CDALMOXARIFE END),
                       CDLOCALESTOQ =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ) ELSE CDLOCALESTOQ END),
                       CDALMOXINSPR =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                              ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXINSPR),CDALMOXINSPR) ELSE CDALMOXINSPR END),
                       CDLOCALINSPR =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR) ELSE CDLOCALINSPR END),
                       CDALMOXPRACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRACA), CDALMOXPRACA) ELSE CDALMOXPRACA END),
                       CDLOCALPRACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA) ELSE CDLOCALPRACA END),
                       CDALMOXPRCLI =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRCLI), CDALMOXPRCLI) ELSE CDALMOXPRCLI END),
                       CDLOCALPRCLI =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI) ELSE CDLOCALPRCLI END),
                       CDALMOXPRSEMACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                            ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA) ELSE CDALMOXPRSEMACA END),
                       CDLOCALPRSEMACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                            ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA) ELSE CDLOCALPRSEMACA END),
                       CDALMOXQUAR  =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                            ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL,  @V_CDALMOXQUAR),CDALMOXQUAR) ELSE CDALMOXQUAR END),
                       CDLOCALQUAR  =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                             ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR) ELSE CDLOCALQUAR END),
                       IDUTPRORIGOBRIG =  (CASE SUBSTRING(@P_LISTAPARAM,13,1) WHEN 'S' THEN @V_IDUTPRORIGOBRIG ELSE IDUTPRORIGOBRIG END),
                       VRMINEMPENHOAF  =  (CASE SUBSTRING(@P_LISTAPARAM,14,1) WHEN 'S' THEN @V_VRMINEMPENHOAF ELSE VRMINEMPENHOAF END),
                       VRMINEMPENHOOL  =  (CASE SUBSTRING(@P_LISTAPARAM,15,1) WHEN 'S' THEN @V_VRMINEMPENHOOL ELSE VRMINEMPENHOOL END)
                  WHERE CDFILIAL = @V_CDFILIAL
                    AND CDPRODUTO = @V_CDPRODUTO;
                  IF @V_COUNT > 1000 BEGIN
                      SET @V_COUNT = 0;
                      COMMIT TRANSACTION;
                      BEGIN TRANSACTION;
                  END;
               END;
               FETCH NEXT FROM C_PRODFILI INTO @V_CDFILIAL, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                      @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                      @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                      @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                      @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;
            END;
            CLOSE C_PRODFILI;
            DEALLOCATE C_PRODFILI;
         END
         ELSE BEGIN
            SET @V_COUNT = 0;
            IF @P_IDTPCONFPAR = 1 BEGIN
               DECLARE C_PRODFILIDIF CURSOR FOR
                  SELECT P.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                         O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                         O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                         O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                         O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
                   FROM PRODFILI O, PRODUTO P
                  WHERE O.CDFILIAL = @P_CDFILIORIG
                    AND O.CDPRODUTO = P.CDPRODUTO
                    AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA;

               OPEN C_PRODFILIDIF;
               FETCH NEXT FROM C_PRODFILIDIF INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                         @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                         @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                         @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                         @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

               WHILE (@@FETCH_STATUS = 0) BEGIN

                  DECLARE C_AGRUPFILIAL CURSOR FOR
                     SELECT CDFILIAL
                       FROM GRUPFILI G, FILGRUPFILI F
                      WHERE G.CDGRUPFILI = @P_CDFILIDEST
                        AND G.CDGRUPFILI = F.CDGRUPFILI;
                  FETCH NEXT FROM C_AGRUPFILIAL INTO @V_CDFILAGRUP;
                  WHILE (@@FETCH_STATUS = 0) BEGIN

                     SELECT @V_NRPRODUTO = COUNT(*)
                     FROM PRODFILI
                     WHERE CDFILIAL = @V_CDFILAGRUP
                        AND CDPRODUTO = @V_CDPRODUTO;

                     IF (ISNULL(@V_NRPRODUTO,0) = 0) BEGIN
                        SET @V_COUNT = @V_COUNT + 1;                     
                        INSERT INTO PRODFILI
                           (CDFILIAL, CDPRODUTO, IDCNTRESTOQ, QTESTOQMIN, IDPRODATIV, NRTEMPREPEST,NRDIASESTOQ,
                            VRPTOLQTITAF,  VRPTOLVRITAF, VRTXADMPROD, QTESTMINSAID, IDUTCTRLLOTE,
                            CDALMOXARIFE, CDLOCALESTOQ, CDALMOXINSPR, CDLOCALINSPR, CDALMOXPRACA, CDLOCALPRACA,
                            CDALMOXPRCLI, CDLOCALPRCLI,  CDALMOXPRSEMACA, CDLOCALPRSEMACA, CDALMOXQUAR, CDLOCALQUAR,
                            IDUTPRORIGOBRIG, VRMINEMPENHOAF, VRMINEMPENHOOL)
                        VALUES
                           (@V_CDFILAGRUP, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV,
                            @V_NRTEMPREPEST, @V_NRDIASESTOQ, @V_VRPTOLQTITAF,
                            @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                            TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXARIFE),
                            TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),
                            TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXINSPR),
                            TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXINSPR, @V_CDLOCALINSPR),
                            TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRACA),
                            TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRACA, @V_CDLOCALPRACA),
                            TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRCLI),
                            TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),
                            TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRSEMACA),
                            TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA),
                            TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXQUAR),
                            TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXQUAR, @V_CDLOCALQUAR),
                            @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL);
                     END;
                     IF @V_COUNT > 1000 BEGIN
                         SET @V_COUNT = 0;
                         COMMIT TRANSACTION;
                         BEGIN TRANSACTION;
                     END;
                     FETCH NEXT FROM C_AGRUPFILIAL INTO @V_CDFILAGRUP;
                  END;
                  CLOSE C_AGRUPFILIAL;
                  DEALLOCATE C_AGRUPFILIAL;

                  FETCH NEXT FROM C_PRODFILIDIF INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                         @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                         @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                         @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                         @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

               END;
               CLOSE C_PRODFILIDIF;
               DEALLOCATE C_PRODFILIDIF;
            END
            ELSE BEGIN
               SET @V_COUNT = 0;
               IF @P_IDTPCONFPAR = 2 BEGIN
                  DECLARE C_PRODFILITOD CURSOR FOR
                     SELECT P.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                            O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                            O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                            O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                            O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
                      FROM PRODFILI O, PRODUTO P
                     WHERE O.CDFILIAL = @P_CDFILIORIG
                       AND O.CDPRODUTO = P.CDPRODUTO
                       AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA;
                  OPEN C_PRODFILITOD;
                  FETCH NEXT FROM C_PRODFILITOD INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                            @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                            @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                            @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                            @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

                  WHILE (@@FETCH_STATUS = 0) BEGIN

                     DECLARE C_AGRUPFILIAL CURSOR FOR
                        SELECT CDFILIAL
                          FROM GRUPFILI G, FILGRUPFILI F
                         WHERE G.CDGRUPFILI = @P_CDFILIDEST
                           AND G.CDGRUPFILI = F.CDGRUPFILI;
                     FETCH NEXT FROM C_AGRUPFILIAL INTO @V_CDFILAGRUP;
                     WHILE (@@FETCH_STATUS = 0) BEGIN

                        SELECT @V_NRPRODUTO = COUNT(*)
                        FROM PRODFILI
                        WHERE CDFILIAL = @V_CDFILAGRUP
                           AND CDPRODUTO = @V_CDPRODUTO;

                        SET @V_COUNT = @V_COUNT + 1;

                        IF (ISNULL(@V_NRPRODUTO,0) = 0) BEGIN
                           INSERT INTO PRODFILI
                              (CDFILIAL, CDPRODUTO, IDCNTRESTOQ, QTESTOQMIN, IDPRODATIV, NRTEMPREPEST, NRDIASESTOQ, VRPTOLQTITAF,
                               VRPTOLVRITAF, VRTXADMPROD, QTESTMINSAID, IDUTCTRLLOTE,
                               CDALMOXARIFE, CDLOCALESTOQ, CDALMOXINSPR,  CDLOCALINSPR, CDALMOXPRACA, CDLOCALPRACA, CDALMOXPRCLI,
                               CDLOCALPRCLI,  CDALMOXPRSEMACA, CDLOCALPRSEMACA, CDALMOXQUAR, CDLOCALQUAR,
                               IDUTPRORIGOBRIG, VRMINEMPENHOAF, VRMINEMPENHOOL)
                           VALUES
                              (@V_CDFILAGRUP, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN,
                               @V_IDPRODATIV, @V_NRTEMPREPEST, @V_NRDIASESTOQ, @V_VRPTOLQTITAF,
                               @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXARIFE),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXINSPR),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXINSPR, @V_CDLOCALINSPR),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRACA),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRACA, @V_CDLOCALPRACA),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRCLI),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRSEMACA),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRSEMACA,@V_CDLOCALPRSEMACA),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXQUAR),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXQUAR, @V_CDLOCALQUAR),
                               @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL);
                        END
                        ELSE BEGIN
                           UPDATE PRODFILI
                           SET  IDCNTRESTOQ  =  @V_IDCNTRESTOQ,
                                QTESTOQMIN   =  @V_QTESTOQMIN ,
                                IDPRODATIV   =  @V_IDPRODATIV ,
                                NRTEMPREPEST =  @V_NRTEMPREPEST,
                                NRDIASESTOQ  =  @V_NRDIASESTOQ,
                                VRPTOLQTITAF =  @V_VRPTOLQTITAF,
                                VRPTOLVRITAF =  @V_VRPTOLVRITAF,
                                VRTXADMPROD  =  @V_VRTXADMPROD,
                                QTESTMINSAID =  @V_QTESTMINSAID,
                                IDUTCTRLLOTE =  @V_IDUTCTRLLOTE,
                                IDUTPRORIGOBRIG = @V_IDUTPRORIGOBRIG,
                                VRMINEMPENHOAF  = @V_VRMINEMPENHOAF,
                                VRMINEMPENHOOL  = @V_VRMINEMPENHOOL,
                                CDALMOXARIFE =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXARIFE),CDALMOXARIFE),
                                CDLOCALESTOQ =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ),
                                CDALMOXINSPR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXINSPR),CDALMOXINSPR),
                                CDLOCALINSPR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR),
                                CDALMOXPRACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRACA), CDALMOXPRACA),
                                CDLOCALPRACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA),
                                CDALMOXPRCLI =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRCLI), CDALMOXPRCLI),
                                CDLOCALPRCLI =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI),
                                CDALMOXPRSEMACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA),
                                CDLOCALPRSEMACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA),
                                CDALMOXQUAR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILAGRUP, @V_CDALMOXQUAR),CDALMOXQUAR),
                                CDLOCALQUAR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILAGRUP, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR)
                           WHERE CDFILIAL = @V_CDFILAGRUP
                             AND CDPRODUTO = @V_CDPRODUTO;
                        END;
                        IF @V_COUNT > 1000 BEGIN
                            SET @V_COUNT = 0;
                            COMMIT TRANSACTION;
                            BEGIN TRANSACTION;
                        END;
                        FETCH NEXT FROM C_AGRUPFILIAL INTO @V_CDFILAGRUP;
                     END;
                     CLOSE C_AGRUPFILIAL;
                     DEALLOCATE C_AGRUPFILIAL;
                     FETCH NEXT FROM C_PRODFILITOD INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                            @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                            @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                            @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                            @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;
                  END;
                  CLOSE C_PRODFILITOD;
                  DEALLOCATE C_PRODFILITOD;
               END;
            END;
         END;
      END
      ELSE BEGIN
         SET @V_COUNT = 0;
         IF @P_IDTPFILDEST = 'L' BEGIN
            IF @P_IDTPCONFPAR = 0 BEGIN
               DECLARE C_LISTAFIL CURSOR FOR
                  SELECT CDFILIAL
                    FROM FILIAL
                    WHERE (CHARINDEX('_'+CDFILIAL+'_', @P_CDFILIDEST) > 0);

               OPEN C_LISTAFIL;
               FETCH NEXT FROM C_LISTAFIL INTO @V_CDFILLISTA;
               WHILE (@@FETCH_STATUS = 0) BEGIN

                  DECLARE C_PRODFILI CURSOR FOR
                     SELECT D.CDFILIAL, D.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                            O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                            O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                            O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                            O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
                     FROM PRODFILI O, PRODFILI D, PRODUTO P
                     WHERE O.CDFILIAL = @P_CDFILIORIG
                       AND O.CDPRODUTO = P.CDPRODUTO
                       AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA
                       AND D.CDFILIAL = @V_CDFILLISTA
                       AND D.CDPRODUTO = O.CDPRODUTO;

                  OPEN C_PRODFILI;
                  FETCH NEXT FROM C_PRODFILI INTO @V_CDFILIAL, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                            @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                            @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                            @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                            @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

                  WHILE (@@FETCH_STATUS = 0) BEGIN
                     SET @V_COUNT = @V_COUNT + 1;
                     IF SUBSTRING(@P_LISTAPARAM,1,1) = 'S' BEGIN
                        UPDATE PRODFILI
                        SET  IDCNTRESTOQ  =  @V_IDCNTRESTOQ,
                             QTESTOQMIN   =  @V_QTESTOQMIN ,
                             IDPRODATIV   =  @V_IDPRODATIV ,
                             NRTEMPREPEST =  @V_NRTEMPREPEST,
                             NRDIASESTOQ  =  @V_NRDIASESTOQ,
                             VRPTOLQTITAF =  @V_VRPTOLQTITAF,
                             VRPTOLVRITAF =  @V_VRPTOLVRITAF,
                             VRTXADMPROD  =  @V_VRTXADMPROD,
                             QTESTMINSAID =  @V_QTESTMINSAID,
                             IDUTCTRLLOTE =  @V_IDUTCTRLLOTE,
                             IDUTPRORIGOBRIG = @V_IDUTPRORIGOBRIG,
                             VRMINEMPENHOAF  = @V_VRMINEMPENHOAF,
                             VRMINEMPENHOOL  = @V_VRMINEMPENHOOL,
                             CDALMOXARIFE =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXARIFE),CDALMOXARIFE),
                             CDLOCALESTOQ =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ),
                             CDALMOXINSPR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXINSPR),CDALMOXINSPR),
                             CDLOCALINSPR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR),
                             CDALMOXPRACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRACA), CDALMOXPRACA),
                             CDLOCALPRACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA),
                             CDALMOXPRCLI =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRCLI), CDALMOXPRCLI),
                             CDLOCALPRCLI =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI),
                             CDALMOXPRSEMACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA),
                             CDLOCALPRSEMACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA),
                             CDALMOXQUAR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL,  @V_CDALMOXQUAR),CDALMOXQUAR),
                             CDLOCALQUAR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR)
                        WHERE CDFILIAL = @V_CDFILIAL
                          AND CDPRODUTO = @V_CDPRODUTO;
                     END
                     ELSE BEGIN
                        UPDATE PRODFILI
                        SET  IDPRODATIV   =  (CASE SUBSTRING(@P_LISTAPARAM,2,1) WHEN 'S' THEN @V_IDPRODATIV ELSE IDPRODATIV END),
                             IDCNTRESTOQ  =  (CASE SUBSTRING(@P_LISTAPARAM,3,1) WHEN 'S' THEN @V_IDCNTRESTOQ ELSE IDCNTRESTOQ END),
                             IDUTCTRLLOTE =  (CASE SUBSTRING(@P_LISTAPARAM,4,1) WHEN 'S' THEN @V_IDUTCTRLLOTE ELSE IDUTCTRLLOTE END),
                             NRDIASESTOQ  =  (CASE SUBSTRING(@P_LISTAPARAM,5,1) WHEN 'S' THEN @V_NRDIASESTOQ ELSE NRDIASESTOQ END),
                             VRPTOLQTITAF =  (CASE SUBSTRING(@P_LISTAPARAM,6,1) WHEN 'S' THEN @V_VRPTOLQTITAF ELSE VRPTOLQTITAF END),
                             VRPTOLVRITAF =  (CASE SUBSTRING(@P_LISTAPARAM,7,1) WHEN 'S' THEN @V_VRPTOLVRITAF ELSE VRPTOLVRITAF END),
                             QTESTOQMIN   =  (CASE SUBSTRING(@P_LISTAPARAM,8,1) WHEN 'S' THEN @V_QTESTOQMIN ELSE QTESTOQMIN END) ,
                             QTESTMINSAID =  (CASE SUBSTRING(@P_LISTAPARAM,9,1) WHEN 'S' THEN @V_QTESTMINSAID ELSE QTESTMINSAID END),
                             VRTXADMPROD  =  (CASE SUBSTRING(@P_LISTAPARAM,10,1) WHEN 'S' THEN @V_VRTXADMPROD ELSE VRTXADMPROD END),
                             NRTEMPREPEST =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN @V_NRTEMPREPEST ELSE NRTEMPREPEST END),
                             CDALMOXARIFE =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXARIFE),CDALMOXARIFE) ELSE CDALMOXARIFE END),
                             CDLOCALESTOQ =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ) ELSE CDLOCALESTOQ END),
                             CDALMOXINSPR =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                    ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXINSPR),CDALMOXINSPR) ELSE CDALMOXINSPR END),
                             CDLOCALINSPR =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR) ELSE CDLOCALINSPR END),
                             CDALMOXPRACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRACA), CDALMOXPRACA) ELSE CDALMOXPRACA END),
                             CDLOCALPRACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA) ELSE CDLOCALPRACA END),
                             CDALMOXPRCLI =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRCLI), CDALMOXPRCLI) ELSE CDALMOXPRCLI END),
                             CDLOCALPRCLI =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI) ELSE CDLOCALPRCLI END),
                             CDALMOXPRSEMACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA) ELSE CDALMOXPRSEMACA END),
                             CDLOCALPRSEMACA =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA) ELSE CDLOCALPRSEMACA END),
                             CDALMOXQUAR  =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILIAL,  @V_CDALMOXQUAR),CDALMOXQUAR) ELSE CDALMOXQUAR END),
                             CDLOCALQUAR  =  (CASE SUBSTRING(@P_LISTAPARAM,11,1) WHEN 'S' THEN
                                                   ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILIAL, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR) ELSE CDLOCALQUAR END),
                             IDUTPRORIGOBRIG =  (CASE SUBSTRING(@P_LISTAPARAM,13,1) WHEN 'S' THEN @V_IDUTPRORIGOBRIG ELSE IDUTPRORIGOBRIG END),
                             VRMINEMPENHOAF  =  (CASE SUBSTRING(@P_LISTAPARAM,14,1) WHEN 'S' THEN @V_VRMINEMPENHOAF ELSE VRMINEMPENHOAF END),
                             VRMINEMPENHOOL  =  (CASE SUBSTRING(@P_LISTAPARAM,15,1) WHEN 'S' THEN @V_VRMINEMPENHOOL ELSE VRMINEMPENHOOL END)
                        WHERE CDFILIAL = @V_CDFILIAL
                          AND CDPRODUTO = @V_CDPRODUTO;
                     END;
                     FETCH NEXT FROM C_PRODFILI INTO @V_CDFILIAL, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                               @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                               @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                               @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                               @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

                     IF @V_COUNT > 1000 BEGIN
                        SET @V_COUNT = 0;
                        COMMIT TRANSACTION;
                        BEGIN TRANSACTION;
                     END;
                  END;
                  CLOSE C_PRODFILI;
                  DEALLOCATE C_PRODFILI;

                  FETCH NEXT FROM C_LISTAFIL INTO @V_CDFILLISTA;
               END;
               CLOSE C_LISTAFIL;
               DEALLOCATE C_LISTAFIL;
            END
            ELSE BEGIN
               SET @V_COUNT = 0;
               IF @P_IDTPCONFPAR = 1 BEGIN
                  DECLARE C_PRODFILIDIF CURSOR FOR
                     SELECT P.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                            O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                            O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                            O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                            O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
                      FROM PRODFILI O, PRODUTO P
                     WHERE O.CDFILIAL = @P_CDFILIORIG
                       AND O.CDPRODUTO = P.CDPRODUTO
                       AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA;
                  OPEN C_PRODFILIDIF;
                  FETCH NEXT FROM C_PRODFILIDIF INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                            @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                            @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                            @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                            @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

                  WHILE (@@FETCH_STATUS = 0) BEGIN
                     DECLARE C_LISTAFIL CURSOR FOR
                        SELECT CDFILIAL
                          FROM FILIAL
                          WHERE (CHARINDEX('_'+CDFILIAL+'_', @P_CDFILIDEST) > 0);
                     OPEN C_LISTAFIL;
                     FETCH NEXT FROM C_LISTAFIL INTO @V_CDFILLISTA;
                     WHILE (@@FETCH_STATUS = 0) BEGIN

                        SELECT @V_NRPRODUTO = CDPRODUTO
                        FROM PRODFILI
                        WHERE CDFILIAL = @V_CDFILLISTA
                           AND CDPRODUTO = @V_CDPRODUTO;

                        IF (ISNULL(@V_NRPRODUTO,0) = 0) BEGIN
                           SET @V_COUNT = @V_COUNT +1;
                           INSERT INTO PRODFILI
                              (CDFILIAL, CDPRODUTO, IDCNTRESTOQ, QTESTOQMIN, IDPRODATIV, NRTEMPREPEST, NRDIASESTOQ,
                               VRPTOLQTITAF,  VRPTOLVRITAF, VRTXADMPROD, QTESTMINSAID, IDUTCTRLLOTE,
                               CDALMOXARIFE, CDLOCALESTOQ, CDALMOXINSPR, CDLOCALINSPR, CDALMOXPRACA, CDLOCALPRACA,
                               CDALMOXPRCLI, CDLOCALPRCLI,  CDALMOXPRSEMACA, CDLOCALPRSEMACA, CDALMOXQUAR, CDLOCALQUAR,
                               IDUTPRORIGOBRIG, VRMINEMPENHOAF, VRMINEMPENHOOL)
                           VALUES
                              (@V_CDFILLISTA, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV,
                               @V_NRTEMPREPEST, @V_NRDIASESTOQ, @V_VRPTOLQTITAF,
                               @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXARIFE),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA,@V_CDALMOXINSPR),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXINSPR, @V_CDLOCALINSPR),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA,@V_CDALMOXPRACA),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRACA, @V_CDLOCALPRACA),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA,@V_CDALMOXPRCLI),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA,@V_CDALMOXPRSEMACA),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA),
                               TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA,@V_CDALMOXQUAR),
                               TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXQUAR, @V_CDLOCALQUAR),
                               @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL);
                        END;
                        IF @V_COUNT > 1000 BEGIN
                           SET @V_COUNT = 0;
                           COMMIT TRANSACTION;
                           BEGIN TRANSACTION;
                        END;
                        FETCH NEXT FROM C_LISTAFIL INTO @V_CDFILLISTA;
                     END;
                     CLOSE C_LISTAFIL;
                     DEALLOCATE C_LISTAFIL;
                     FETCH NEXT FROM C_PRODFILIDIF INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                                                 @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                                                 @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                                                 @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                                                 @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;
                  END;
                  CLOSE C_PRODFILIDIF;
                  DEALLOCATE C_PRODFILIDIF;
               END
               ELSE BEGIN
                  SET @V_COUNT = 0;
                  IF @P_IDTPCONFPAR = 2 BEGIN
                     DECLARE C_PRODFILITOD CURSOR FOR
                        SELECT P.CDPRODUTO, O.IDCNTRESTOQ, O.QTESTOQMIN, O.IDPRODATIV, O.CDALMOXARIFE, O.CDLOCALESTOQ, O.NRTEMPREPEST,
                               O.CDALMOXINSPR, O.CDLOCALINSPR, O.CDALMOXPRACA, O.CDLOCALPRACA, O.CDALMOXPRCLI, O.CDLOCALPRCLI,
                               O.CDALMOXPRSEMACA, O.CDLOCALPRSEMACA, O.CDALMOXQUAR, O.CDLOCALQUAR,
                               O.NRDIASESTOQ, O.VRPTOLQTITAF, O.VRPTOLVRITAF, O.VRTXADMPROD, O.QTESTMINSAID, O.IDUTCTRLLOTE,
                               O.IDUTPRORIGOBRIG, O.VRMINEMPENHOAF, O.VRMINEMPENHOOL
                         FROM PRODFILI O, PRODUTO P
                        WHERE O.CDFILIAL = @P_CDFILIORIG
                          AND O.CDPRODUTO = P.CDPRODUTO
                          AND CDARVPROD BETWEEN @P_CDPRODINIC AND @P_CDPRODFINA;

                     OPEN C_PRODFILITOD;
                     FETCH NEXT FROM C_PRODFILITOD INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                               @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                               @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                               @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                               @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;

                     WHILE (@@FETCH_STATUS = 0) BEGIN

                        DECLARE C_LISTAFIL CURSOR FOR
                           SELECT CDFILIAL
                             FROM FILIAL
                            WHERE (CHARINDEX('_'+CDFILIAL+'_', @P_CDFILIDEST) > 0);

                        OPEN C_LISTAFIL;
                        FETCH NEXT FROM C_LISTAFIL INTO @V_CDFILLISTA;
                        WHILE (@@FETCH_STATUS = 0) BEGIN

                           SELECT @V_NRPRODUTO = COUNT(*)
                           FROM PRODFILI
                           WHERE CDFILIAL = @V_CDFILLISTA
                              AND CDPRODUTO = @V_CDPRODUTO;
                           SET @V_COUNT = @V_COUNT + 1;
                           IF (ISNULL(@V_NRPRODUTO,0) = 0) BEGIN
                              INSERT INTO PRODFILI
                                 (CDFILIAL, CDPRODUTO, IDCNTRESTOQ, QTESTOQMIN, IDPRODATIV, NRTEMPREPEST, NRDIASESTOQ, VRPTOLQTITAF,
                                  VRPTOLVRITAF, VRTXADMPROD, QTESTMINSAID, IDUTCTRLLOTE,
                                  CDALMOXARIFE, CDLOCALESTOQ, CDALMOXINSPR,  CDLOCALINSPR, CDALMOXPRACA, CDLOCALPRACA, CDALMOXPRCLI,
                                  CDLOCALPRCLI,  CDALMOXPRSEMACA, CDLOCALPRSEMACA, CDALMOXQUAR, CDLOCALQUAR,
                                  IDUTPRORIGOBRIG, VRMINEMPENHOAF, VRMINEMPENHOOL )
                              VALUES
                                 (@V_CDFILLISTA, @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV,
                                  @V_NRTEMPREPEST, @V_NRDIASESTOQ, @V_VRPTOLQTITAF,
                                  @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                                  TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXARIFE),
                                  TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),
                                  TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXINSPR),
                                  TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXINSPR, @V_CDLOCALINSPR),
                                  TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXPRACA),
                                  TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRACA, @V_CDLOCALPRACA),
                                  TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXPRCLI),
                                  TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),
                                  TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXPRSEMACA),
                                  TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA),
                                  TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXQUAR),
                                  TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXQUAR, @V_CDLOCALQUAR),
                                  @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL);
                           END
                           ELSE BEGIN
                              UPDATE PRODFILI
                              SET  IDCNTRESTOQ  =  @V_IDCNTRESTOQ,
                                   QTESTOQMIN   =  @V_QTESTOQMIN ,
                                   IDPRODATIV   =  @V_IDPRODATIV ,
                                   NRTEMPREPEST =  @V_NRTEMPREPEST,
                                   NRDIASESTOQ  =  @V_NRDIASESTOQ,
                                   VRPTOLQTITAF =  @V_VRPTOLQTITAF,
                                   VRPTOLVRITAF =  @V_VRPTOLVRITAF,
                                   VRTXADMPROD  =  @V_VRTXADMPROD,
                                   QTESTMINSAID =  @V_QTESTMINSAID,
                                   IDUTCTRLLOTE =  @V_IDUTCTRLLOTE,
                                   IDUTPRORIGOBRIG = @V_IDUTPRORIGOBRIG,
                                   VRMINEMPENHOAF  = @V_VRMINEMPENHOAF,
                                   VRMINEMPENHOOL  = @V_VRMINEMPENHOOL,
                                   CDALMOXARIFE =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXARIFE),CDALMOXARIFE),
                                   CDLOCALESTOQ =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXARIFE, @V_CDLOCALESTOQ),CDLOCALESTOQ),
                                   CDALMOXINSPR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXINSPR),CDALMOXINSPR),
                                   CDLOCALINSPR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXINSPR,@V_CDLOCALINSPR),CDLOCALINSPR),
                                   CDALMOXPRACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXPRACA), CDALMOXPRACA),
                                   CDLOCALPRACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRACA, @V_CDLOCALPRACA), CDLOCALPRACA),
                                   CDALMOXPRCLI =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXPRCLI), CDALMOXPRCLI),
                                   CDLOCALPRCLI =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI),CDLOCALPRCLI),
                                   CDALMOXPRSEMACA =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXPRSEMACA),CDALMOXPRSEMACA),
                                   CDLOCALPRSEMACA =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA), CDLOCALPRSEMACA),
                                   CDALMOXQUAR =  ISNULL(TEKNISA.VERIFICA_ALMOX(@V_CDFILLISTA, @V_CDALMOXQUAR),CDALMOXQUAR),
                                   CDLOCALQUAR =  ISNULL(TEKNISA.VERIFICA_LOCAL(@V_CDFILLISTA, @V_CDALMOXQUAR,  @V_CDLOCALQUAR),CDLOCALQUAR)
                              WHERE CDFILIAL = @V_CDFILLISTA
                                AND CDPRODUTO = @V_CDPRODUTO;
                           END;
                           IF @V_COUNT > 1000 BEGIN
                              SET @V_COUNT = 0;
                              COMMIT TRANSACTION;
                              BEGIN TRANSACTION;
                           END;
                           FETCH NEXT FROM C_LISTAFIL INTO @V_CDFILLISTA;
                        END;
                        CLOSE C_LISTAFIL;
                        DEALLOCATE C_LISTAFIL;
                        FETCH NEXT FROM C_PRODFILITOD INTO @V_CDPRODUTO, @V_IDCNTRESTOQ, @V_QTESTOQMIN, @V_IDPRODATIV, @V_CDALMOXARIFE,
                                                       @V_CDLOCALESTOQ, @V_NRTEMPREPEST, @V_CDALMOXINSPR, @V_CDLOCALINSPR, @V_CDALMOXPRACA, @V_CDLOCALPRACA, @V_CDALMOXPRCLI, @V_CDLOCALPRCLI,
                                                       @V_CDALMOXPRSEMACA, @V_CDLOCALPRSEMACA, @V_CDALMOXQUAR, @V_CDLOCALQUAR,
                                                       @V_NRDIASESTOQ, @V_VRPTOLQTITAF, @V_VRPTOLVRITAF, @V_VRTXADMPROD, @V_QTESTMINSAID, @V_IDUTCTRLLOTE,
                                                       @V_IDUTPRORIGOBRIG, @V_VRMINEMPENHOAF, @V_VRMINEMPENHOOL;
                     END;
                     CLOSE C_PRODFILITOD;
                     DEALLOCATE C_PRODFILITOD;
                  END;
               END;
            END;
         END;
      END;
   END;
   COMMIT TRANSACTION;
END;

GO
-- VERSAO --
EXEC ATUVEROBJ 'REPLICA_PRODUTO_FILIAL','5.80.000'
GO

