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

DROP TRIGGER ATUALIZA_INSERT_ITLANCTOEST
GO

CREATE TRIGGER ATUALIZA_INSERT_ITLANCTOEST
ON ITLANCTOEST
FOR INSERT

AS

DECLARE @V_NEW_DTLANCMOVI   DATETIME;
DECLARE @V_NEW_IDALTCMEDMV  VARCHAR(01);
DECLARE @V_NEW_IDENTRSAIDOP VARCHAR(01);
DECLARE @V_NEW_CDTIPOOPER   VARCHAR(02);
DECLARE @V_NEW_DSLANCESTIT  VARCHAR(60);
DECLARE @V_NEW_CDFILIAL     VARCHAR(04);
DECLARE @V_NEW_NRLANCESTQ   VARCHAR(10);
DECLARE @V_NEW_NRSEQUITEM   VARCHAR(04);

DECLARE @V_NEW_CDFILIMOVI   VARCHAR(04);
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_CDPRODMOVI   VARCHAR(10);
DECLARE @V_NEW_IDORDEMCLC   VARCHAR(01);

DECLARE @V_COUNT            NUMERIC;

DECLARE @V_NEW_IDTIPOMOVI   VARCHAR(01);
DECLARE @V_NEW_QTTOTLANCTO  NUMERIC(15,5);
DECLARE @V_NEW_NRLANCORIGRP VARCHAR(14);
DECLARE @V_TESTE            VARCHAR(50);

BEGIN

   DECLARE C_INSLANCEST CURSOR FOR
   SELECT DTLANCMOVI, IDALTCMEDMV,  IDENTRSAIDOP,
          CDTIPOOPER, DSLANCESTIT,  CDFILIAL, NRLANCESTQ,
          CDFILIMOVI, CDALMOXARIFE, CDLOCALESTOQ,
          NRLOTEESTQ, NRSUBLOTE,    CDPRODMOVI,
          IDTIPOMOVI, QTTOTLANCTO,  NRLANCORIGRP,
          IDORDEMCLC, NRSEQUITEM
   FROM INSERTED
   WHERE (CDFILIAL+NRLANCESTQ) NOT IN (SELECT CDFILIAL+NRLANCESTQ
                                       FROM DELETED);

   OPEN C_INSLANCEST;

   FETCH NEXT FROM C_INSLANCEST INTO @V_NEW_DTLANCMOVI,   @V_NEW_IDALTCMEDMV,
                                   @V_NEW_IDENTRSAIDOP, @V_NEW_CDTIPOOPER,
                                   @V_NEW_DSLANCESTIT,  @V_NEW_CDFILIAL,
                                   @V_NEW_NRLANCESTQ,   @V_NEW_CDFILIMOVI,
                                   @V_NEW_CDALMOXARIFE, @V_NEW_CDLOCALESTOQ,
                                   @V_NEW_NRLOTEESTQ,   @V_NEW_NRSUBLOTE,
                                   @V_NEW_CDPRODMOVI,   @V_NEW_IDTIPOMOVI,
                                   @V_NEW_QTTOTLANCTO,  @V_NEW_NRLANCORIGRP,
                                   @V_NEW_IDORDEMCLC,   @V_NEW_NRSEQUITEM;

   WHILE (@@FETCH_STATUS = 0) BEGIN

      SELECT  @V_NEW_DTLANCMOVI    = ISNULL(@V_NEW_DTLANCMOVI, MAX(DTLANCESTQ)),
              @V_NEW_IDALTCMEDMV   = MAX(IDALTCUSMED),
              @V_NEW_IDENTRSAIDOP  = MAX(IDENTRSAIDOP),
              @V_NEW_CDTIPOOPER    = MAX(CDTIPOOPER),
              @V_NEW_DSLANCESTIT   = MAX(DSLANCESTQ)
      FROM LANCTOESTOQ
      WHERE CDFILIAL  = @V_NEW_CDFILIAL AND
            NRLANCESTQ = @V_NEW_NRLANCESTQ;
      SET @V_TESTE =  @V_NEW_CDFILIMOVI+'-'+@V_NEW_NRLOTEESTQ+'-'+@V_NEW_NRSUBLOTE;

      SELECT @V_COUNT = COUNT(*)
      FROM LOTE
      WHERE  CDFILIAL   = @V_NEW_CDFILIMOVI  AND
             NRLOTEESTQ = @V_NEW_NRLOTEESTQ  AND
             NRSUBLOTE  = @V_NEW_NRSUBLOTE   AND
             CDPRODUTO  = @V_NEW_CDPRODMOVI;

      IF @V_COUNT = 0 BEGIN
         INSERT INTO LOTE
         (CDFILIAL, NRLOTEESTQ, NRSUBLOTE,   CDPRODUTO, IDSTATLOTE, DTENTRLOTE)
         VALUES
         (@V_NEW_CDFILIMOVI, @V_NEW_NRLOTEESTQ, @V_NEW_NRSUBLOTE, @V_NEW_CDPRODMOVI, 'L', GETDATE());
      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) OR (@V_NEW_CDTIPOOPER = '03') BEGIN

             set @V_TESTE =  @V_NEW_CDFILIAL +'_'+ @V_NEW_NRLANCESTQ+'_'+@V_NEW_CDFILIMOVI+'-'+@V_NEW_CDALMOXARIFE;


            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,
             CDTIPOOPER   = @V_NEW_CDTIPOOPER,
             IDENTRSAIDOP = @V_NEW_IDENTRSAIDOP
      WHERE CDFILIAL    = @V_NEW_CDFILIAL   AND
            NRLANCESTQ  = @V_NEW_NRLANCESTQ AND
            NRSEQUITEM  = @V_NEW_NRSEQUITEM;

      FETCH NEXT FROM C_INSLANCEST INTO @V_NEW_DTLANCMOVI, @V_NEW_IDALTCMEDMV,
                                      @V_NEW_IDENTRSAIDOP, @V_NEW_CDTIPOOPER,
                                      @V_NEW_DSLANCESTIT,  @V_NEW_CDFILIAL,
                                      @V_NEW_NRLANCESTQ,   @V_NEW_CDFILIMOVI,
                                      @V_NEW_CDALMOXARIFE, @V_NEW_CDLOCALESTOQ,
                                      @V_NEW_NRLOTEESTQ,   @V_NEW_NRSUBLOTE,
                                      @V_NEW_CDPRODMOVI,   @V_NEW_IDTIPOMOVI,
                                      @V_NEW_QTTOTLANCTO,  @V_NEW_NRLANCORIGRP,
                                      @V_NEW_IDORDEMCLC,   @V_NEW_NRSEQUITEM;
   END;

   CLOSE C_INSLANCEST;
   DEALLOCATE C_INSLANCEST;
END;
GO


-- VERSAO --
EXEC ATUVEROBJ 'ATUALIZA_INSERT_ITLANCTOEST','5.06.032';
GO

