--ATUALIZA_UPDATE_LOGMOVIEST
IF EXISTS(SELECT
             NAME
          FROM
             SYSOBJECTS
          WHERE
             NAME = 'ATUALIZA_UPDATE_LOGMOVIEST' AND
             TYPE = 'TR')

DROP TRIGGER ATUALIZA_UPDATE_LOGMOVIEST
GO

CREATE TRIGGER ATUALIZA_UPDATE_LOGMOVIEST
ON ITLANCTOEST
FOR UPDATE

AS

DECLARE @V_DTATUAL      DATETIME;
DECLARE @V_IDCTRLESTQLG VARCHAR(01);
DECLARE @V_IDESONLINELG VARCHAR(01);
DECLARE @V_IDPESTNEGALG VARCHAR(01);
DECLARE @V_CDOPERADORLG VARCHAR(12);
DECLARE @V_COUNT        NUMERIC;

DECLARE @V_NEW_CDFILIAL     VARCHAR(04);
DECLARE @V_NEW_NRLANCESTQ   VARCHAR(10);
DECLARE @V_NEW_NRSEQUITEM   VARCHAR(10);
DECLARE @V_NEW_CDFILIMOVI   VARCHAR(04);
DECLARE @V_NEW_CDPRODMOVI   VARCHAR(10);
DECLARE @V_NEW_QTLANCTOEST  NUMERIC(15,5);
DECLARE @V_NEW_VRLANCTOEST  NUMERIC(15,5);
DECLARE @V_NEW_IDALTCMEDMV  VARCHAR(01);
DECLARE @V_NEW_IDEVOLESTOQ  VARCHAR(01);
DECLARE @V_NEW_IDTIPOMOVI   VARCHAR(01);
DECLARE @V_NEW_QTTOTLANCTO  NUMERIC(15,5);
DECLARE @V_NEW_DTLANCMOVI   DATETIME;
DECLARE @V_NEW_DSLANCESTIT  VARCHAR(60);
DECLARE @V_NEW_CDTIPOOPER   VARCHAR(02);
DECLARE @V_NEW_IDENTRSAIDOP VARCHAR(01);
DECLARE @V_NEW_CDALMOXARIFE VARCHAR(06);
DECLARE @V_NEW_CDLOCALESTOQ VARCHAR(06);
DECLARE @V_NEW_NRLOTEESTQ   VARCHAR(20);
DECLARE @V_NEW_NRSUBLOTE    VARCHAR(04);
DECLARE @V_NEW_NRLANCORIGRP VARCHAR(14);
DECLARE @V_NEW_IDORDEMCLC   VARCHAR(01);

DECLARE @V_OLD_CDFILIAL     VARCHAR(04);
DECLARE @V_OLD_NRLANCESTQ   VARCHAR(10);
DECLARE @V_OLD_CDFILIMOVI   VARCHAR(04);
DECLARE @V_OLD_CDPRODMOVI   VARCHAR(10);
DECLARE @V_OLD_QTLANCTOEST  NUMERIC(15,5);
DECLARE @V_OLD_VRLANCTOEST  NUMERIC(15,5);
DECLARE @V_OLD_IDALTCMEDMV  VARCHAR(01);
DECLARE @V_OLD_IDEVOLESTOQ  VARCHAR(01);
DECLARE @V_OLD_IDTIPOMOVI   VARCHAR(01);
DECLARE @V_OLD_QTTOTLANCTO  NUMERIC(15,5);
DECLARE @V_OLD_CDALMOXARIFE VARCHAR(06);
DECLARE @V_OLD_CDLOCALESTOQ VARCHAR(06);
DECLARE @V_OLD_NRLOTEESTQ   VARCHAR(20);
DECLARE @V_OLD_NRSUBLOTE    VARCHAR(04);
DECLARE @V_OLD_NRLANCORIGRP VARCHAR(14);
DECLARE @V_OLD_DTLANCMOVI   DATETIME;

BEGIN

   SET @V_DTATUAL = GETDATE();

   DECLARE C_MODIFY CURSOR FOR
   SELECT I.CDFILIAL,     I.NRLANCESTQ,   I.NRSEQUITEM,
          I.CDFILIMOVI,   I.CDPRODMOVI,   I.QTLANCTOEST,
          I.VRLANCTOEST,  I.IDALTCMEDMV,  I.IDEVOLESTOQ,
          I.IDTIPOMOVI,   I.QTTOTLANCTO,  I.DTLANCMOVI,
          I.DSLANCESTIT,  I.CDTIPOOPER,   I.IDENTRSAIDOP,
          I.CDALMOXARIFE, I.CDLOCALESTOQ, I.NRLOTEESTQ,
          I.NRSUBLOTE,    I.NRLANCORIGRP,
          D.CDFILIAL,     D.NRLANCESTQ,   D.CDFILIMOVI,
          D.CDPRODMOVI,   D.QTLANCTOEST,  D.VRLANCTOEST,
          D.IDALTCMEDMV,  D.IDEVOLESTOQ,  D.IDTIPOMOVI,
          D.QTTOTLANCTO,  D.CDALMOXARIFE, D.CDLOCALESTOQ,
          D.NRLOTEESTQ,   D.NRSUBLOTE,    D.NRLANCORIGRP,
          I.IDORDEMCLC,   D.DTLANCMOVI
   FROM INSERTED I, DELETED D
   WHERE (I.CDFILIAL   = D.CDFILIAL)   AND
         (I.NRLANCESTQ = D.NRLANCESTQ) AND
         (I.NRSEQUITEM = D.NRSEQUITEM);

   OPEN C_MODIFY;

   FETCH NEXT FROM C_MODIFY INTO @V_NEW_CDFILIAL,     @V_NEW_NRLANCESTQ,   @V_NEW_NRSEQUITEM,
                                 @V_NEW_CDFILIMOVI,   @V_NEW_CDPRODMOVI,   @V_NEW_QTLANCTOEST,
                                 @V_NEW_VRLANCTOEST,  @V_NEW_IDALTCMEDMV,  @V_NEW_IDEVOLESTOQ,
                                 @V_NEW_IDTIPOMOVI,   @V_NEW_QTTOTLANCTO,  @V_NEW_DTLANCMOVI,
                                 @V_NEW_DSLANCESTIT,  @V_NEW_CDTIPOOPER,   @V_NEW_IDENTRSAIDOP,
                                 @V_NEW_CDALMOXARIFE, @V_NEW_CDLOCALESTOQ, @V_NEW_NRLOTEESTQ,
                                 @V_NEW_NRSUBLOTE,    @V_NEW_NRLANCORIGRP,
                                 @V_OLD_CDFILIAL,     @V_OLD_NRLANCESTQ,   @V_OLD_CDFILIMOVI,
                                 @V_OLD_CDPRODMOVI,   @V_OLD_QTLANCTOEST,  @V_OLD_VRLANCTOEST,
                                 @V_OLD_IDALTCMEDMV,  @V_OLD_IDEVOLESTOQ,  @V_OLD_IDTIPOMOVI,
                                 @V_OLD_QTTOTLANCTO,  @V_OLD_CDALMOXARIFE, @V_OLD_CDLOCALESTOQ,
                                 @V_OLD_NRLOTEESTQ,   @V_OLD_NRSUBLOTE,    @V_NEW_NRLANCORIGRP,
                                 @V_NEW_IDORDEMCLC,   @V_OLD_DTLANCMOVI;

   WHILE (@@FETCH_STATUS = 0) BEGIN
      IF (@V_NEW_CDFILIMOVI   <> @V_OLD_CDFILIMOVI)   OR (@V_NEW_CDPRODMOVI   <> @V_OLD_CDPRODMOVI)   OR
         (@V_NEW_CDALMOXARIFE <> @V_OLD_CDALMOXARIFE) OR (@V_NEW_CDLOCALESTOQ <> @V_OLD_CDLOCALESTOQ) OR
         (@V_NEW_NRLOTEESTQ   <> @V_OLD_NRLOTEESTQ)   OR (@V_NEW_NRSUBLOTE    <> @V_OLD_NRSUBLOTE)    BEGIN

         SELECT @V_COUNT = COUNT(*)
         FROM MOVILOTE
         WHERE NRMOVILOTE   = @V_OLD_CDFILIAL + @V_OLD_NRLANCESTQ AND
               CDFILIAL     = @V_OLD_CDFILIMOVI                   AND
               CDALMOXARIFE = @V_OLD_CDALMOXARIFE                 AND
               CDLOCALESTOQ = @V_OLD_CDLOCALESTOQ                 AND
               NRLOTEESTQ   = @V_OLD_NRLOTEESTQ                   AND
               NRSUBLOTE    = @V_OLD_NRSUBLOTE                    AND
               CDPRODUTO    = @V_OLD_CDPRODMOVI;

         IF @V_COUNT = 0 BEGIN
            IF (@V_OLD_IDTIPOMOVI IN ('0', '1')) OR (@V_OLD_QTTOTLANCTO > 0) BEGIN
               INSERT INTO MOVILOTE
                  (NRMOVILOTE, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ, CDPRODUTO,
                   NRLOTEESTQ, NRSUBLOTE, NRLANCTOORIG, DTMOVILOTE,  DTMOVILOTEANT)
               VALUES
                  (@V_OLD_CDFILIAL + @V_OLD_NRLANCESTQ, @V_OLD_CDFILIMOVI, @V_OLD_CDALMOXARIFE,
                   @V_OLD_CDLOCALESTOQ, @V_OLD_CDPRODMOVI, @V_OLD_NRLOTEESTQ, @V_OLD_NRSUBLOTE,
                   @V_OLD_NRLANCORIGRP, @V_NEW_DTLANCMOVI, @V_OLD_DTLANCMOVI);
            END;
         END;
      END;

      SELECT @V_COUNT = COUNT(*)
      FROM MOVILOTE
      WHERE NRMOVILOTE   = @V_NEW_CDFILIAL + @V_NEW_NRLANCESTQ AND
            CDFILIAL     = @V_NEW_CDFILIMOVI                   AND
            CDALMOXARIFE = @V_NEW_CDALMOXARIFE                 AND
            CDLOCALESTOQ = @V_NEW_CDLOCALESTOQ                 AND
            NRLOTEESTQ   = @V_NEW_NRLOTEESTQ                   AND
            NRSUBLOTE    = @V_NEW_NRSUBLOTE                    AND
            CDPRODUTO    = @V_NEW_CDPRODMOVI;

      IF @V_COUNT = 0 BEGIN
         IF (@V_NEW_IDTIPOMOVI IN ('0', '1')) OR (@V_NEW_QTTOTLANCTO > 0) BEGIN
            INSERT INTO MOVILOTE
               (NRMOVILOTE, CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ, CDPRODUTO,
               NRLOTEESTQ, NRSUBLOTE, NRLANCTOORIG, DTMOVILOTE,  DTMOVILOTEANT)
            VALUES
               (@V_NEW_CDFILIAL + @V_NEW_NRLANCESTQ, @V_NEW_CDFILIMOVI, @V_NEW_CDALMOXARIFE,
                @V_NEW_CDLOCALESTOQ, @V_NEW_CDPRODMOVI, @V_NEW_NRLOTEESTQ, @V_NEW_NRSUBLOTE,
                @V_NEW_NRLANCORIGRP, @V_NEW_DTLANCMOVI, @V_NEW_DTLANCMOVI);
         END;
      END;

      IF @V_NEW_IDTIPOMOVI = '3' BEGIN
         SET @V_NEW_IDORDEMCLC = '9';
      END
      ELSE IF @V_NEW_IDTIPOMOVI = '4' BEGIN
         SET @V_NEW_IDORDEMCLC = '8';
      END
      ELSE IF @V_NEW_IDTIPOMOVI = '5' BEGIN
         SET @V_NEW_IDORDEMCLC = '7';
      END;

      UPDATE ITLANCTOEST
      SET IDORDEMCLC = @V_NEW_IDORDEMCLC
      WHERE CDFILIAL    = @V_NEW_CDFILIAL   AND
            NRLANCESTQ  = @V_NEW_NRLANCESTQ AND
            NRSEQUITEM  = @V_NEW_NRSEQUITEM;

      FETCH NEXT FROM C_MODIFY INTO @V_NEW_CDFILIAL,     @V_NEW_NRLANCESTQ,   @V_NEW_NRSEQUITEM,
                                    @V_NEW_CDFILIMOVI,   @V_NEW_CDPRODMOVI,   @V_NEW_QTLANCTOEST,
                                    @V_NEW_VRLANCTOEST,  @V_NEW_IDALTCMEDMV,  @V_NEW_IDEVOLESTOQ,
                                    @V_NEW_IDTIPOMOVI,   @V_NEW_QTTOTLANCTO,  @V_NEW_DTLANCMOVI,
                                    @V_NEW_DSLANCESTIT,  @V_NEW_CDTIPOOPER,   @V_NEW_IDENTRSAIDOP,
                                    @V_NEW_CDALMOXARIFE, @V_NEW_CDLOCALESTOQ, @V_NEW_NRLOTEESTQ,
                                    @V_NEW_NRSUBLOTE,    @V_NEW_NRLANCORIGRP,
                                    @V_OLD_CDFILIAL,     @V_OLD_NRLANCESTQ,   @V_OLD_CDFILIMOVI,
                                    @V_OLD_CDPRODMOVI,   @V_OLD_QTLANCTOEST,  @V_OLD_VRLANCTOEST,
                                    @V_OLD_IDALTCMEDMV,  @V_OLD_IDEVOLESTOQ,  @V_OLD_IDTIPOMOVI,
                                    @V_OLD_QTTOTLANCTO,  @V_OLD_CDALMOXARIFE, @V_OLD_CDLOCALESTOQ,
                                    @V_OLD_NRLOTEESTQ,   @V_OLD_NRSUBLOTE,    @V_NEW_NRLANCORIGRP,
                                    @V_NEW_IDORDEMCLC,   @V_OLD_DTLANCMOVI;

   END;
   CLOSE C_MODIFY;
   DEALLOCATE C_MODIFY;
END;
GO


-- VERSAO --
EXEC ATUVEROBJ 'ATUALIZA_UPDATE_LOGMOVIEST','5.01.010';
GO

