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


  DROP TRIGGER ATUALIZA_DADOS_EFETCLIE
  GO


CREATE TRIGGER ATUALIZA_DADOS_EFETCLIE
ON EFETCLIE
FOR UPDATE, INSERT, DELETE
AS

   DECLARE @V_DTINICIAL       DATETIME;
   DECLARE @V_DTFINAL         DATETIME;
   DECLARE @V_IDFECHDIAROP    VARCHAR(1);
   DECLARE @V_NEW_CDFILIAL    VARCHAR(4);
   DECLARE @V_OLD_CDFILIAL    VARCHAR(4);
   DECLARE @V_NEW_QTCOMEFETCLI  DATETIME;
   DECLARE @V_NEW_QTFUNEFETCLI  DATETIME;
   DECLARE @V_NEW_QTDOAEFETCLI  DATETIME;
   DECLARE @V_OLD_QTFUNEFETCLI  DATETIME;
   DECLARE @V_OLD_QTCOMEFETCLI  DATETIME;
   DECLARE @V_OLD_QTDOAEFETCLI  DATETIME;
   DECLARE @V_ID_COMANDO      VARCHAR(1);
   DECLARE @V_IDUTPADRROP     VARCHAR(1);
   DECLARE @V_IDUTPADRROPFIL  VARCHAR(1);
   DECLARE @V_DTEFETCLIE      DATETIME;
   DECLARE @V_OLD_DTEFETCLIE  DATETIME;

BEGIN
   SET @V_ID_COMANDO = 'N';

   DECLARE C_CURSOR_DATA CURSOR FOR
      SELECT
        'U' IDCOMANDO,
        DELETED.CDFILIAL,
        DELETED.QTCOMEFETCLI,
        DELETED.QTFUNEFETCLI,
        DELETED.QTDOAEFETCLI,
        INSERTED.CDFILIAL,
        INSERTED.QTCOMEFETCLI,
        INSERTED.QTFUNEFETCLI,
        INSERTED.QTDOAEFETCLI,
        INSERTED.DTEFETCLIE,
        DELETED.DTEFETCLIE
      FROM
          DELETED,
          INSERTED
      WHERE
         INSERTED.CDFILIAL  = DELETED.CDFILIAL

      UNION ALL

      SELECT
        'I' IDCOMANDO,
        NULL,NULL,NULL,NULL,
        INSERTED.CDFILIAL,
        INSERTED.QTCOMEFETCLI,
        INSERTED.QTFUNEFETCLI,
        INSERTED.QTDOAEFETCLI,
        INSERTED.DTEFETCLIE,
        NULL
      FROM
        INSERTED
      WHERE
        0 = (SELECT COUNT(*) FROM DELETED)

      UNION ALL

      SELECT
        'D' IDCOMANDO,
        DELETED.CDFILIAL,
        DELETED.QTCOMEFETCLI,
        DELETED.QTFUNEFETCLI,
        DELETED.QTDOAEFETCLI,
        NULL, NULL, NULL, NULL, NULL,
        DELETED.DTEFETCLIE
      FROM
        DELETED
      WHERE
        0 = (SELECT COUNT(*) FROM INSERTED);

   OPEN C_CURSOR_DATA;
   FETCH NEXT FROM C_CURSOR_DATA INTO @V_ID_COMANDO,
                                      @V_OLD_CDFILIAL,
                                      @V_OLD_QTFUNEFETCLI,
                                      @V_OLD_QTCOMEFETCLI,
                                      @V_OLD_QTDOAEFETCLI,
                                      @V_NEW_CDFILIAL,
                                      @V_NEW_QTCOMEFETCLI,
                                      @V_NEW_QTFUNEFETCLI,
                                      @V_NEW_QTDOAEFETCLI,
                                      @V_DTEFETCLIE,
                                      @V_OLD_DTEFETCLIE

   WHILE (@@FETCH_STATUS = 0) BEGIN
      IF @V_ID_COMANDO = 'U' BEGIN
         SELECT @V_IDUTPADRROP = IDUTPADRROP
         FROM PARAMGERAL G;

         SELECT @V_IDFECHDIAROP   = ISNULL(MAX(IDFECHDIAROP),'N'),
                @V_IDUTPADRROPFIL = ISNULL(MAX(IDUTPADRROPFIL),'N')
         FROM  PARFILIALROP
         WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);

         IF @V_IDUTPADRROP = 'S' OR @V_IDUTPADRROPFIL = 'S' BEGIN
            SELECT @V_IDFECHDIAROP   = ISNULL(MAX(IDFECHDIAROP),'N'),
                   @V_IDUTPADRROPFIL = ISNULL(MAX(IDUTPADRROPFIL),'N')
            FROM  PARFILIALROP
            WHERE CDFILIAL = 'P';
         END;

         IF  (@V_NEW_QTCOMEFETCLI <> @V_OLD_QTCOMEFETCLI)
         AND (@V_NEW_QTFUNEFETCLI <> @V_OLD_QTFUNEFETCLI)
         AND (@V_NEW_QTDOAEFETCLI <> @V_OLD_QTDOAEFETCLI)
         BEGIN
            SELECT @V_DTINICIAL = MAX(DTINIAPURACROP),
                   @V_DTFINAL   = MAX(DTFINAPURACROP)
            FROM APURACAOROP
            WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);

            IF (ISNULL(@V_IDFECHDIAROP,'N') <> 'S') BEGIN

               IF (@V_DTEFETCLIE < @V_DTFINAL)  AND (@V_DTEFETCLIE < @V_DTINICIAL) BEGIN

                  UPDATE APURACAOROP
                  SET DTINIAPURACROP  = @V_DTEFETCLIE
                  WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);
               END;

            END
            ELSE BEGIN
               DELETE ROPFILIAL
               WHERE CDFILIAL     = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL)  AND
                     DTFILIALROP  >= @V_DTEFETCLIE;

               IF (@V_OLD_DTEFETCLIE< @V_DTINICIAL) BEGIN
                  UPDATE APURACAOROP
                  SET  DTINIAPURACROP  = @V_DTEFETCLIE
                  WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);
               END;

            END;
         END;
      END;

      IF @V_ID_COMANDO = 'I' BEGIN
         SELECT @V_IDUTPADRROP = IDUTPADRROP
         FROM PARAMGERAL G;

         SELECT @V_IDFECHDIAROP   = ISNULL(MAX(IDFECHDIAROP),'N'),
                @V_IDUTPADRROPFIL = ISNULL(MAX(IDUTPADRROPFIL),'N')
         FROM  PARFILIALROP
         WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);

         IF @V_IDUTPADRROP = 'S' OR @V_IDUTPADRROPFIL = 'S' BEGIN
            SELECT @V_IDFECHDIAROP   = ISNULL(MAX(IDFECHDIAROP),'N'),
                   @V_IDUTPADRROPFIL = ISNULL(MAX(IDUTPADRROPFIL),'N')
            FROM  PARFILIALROP
            WHERE CDFILIAL = 'P';
         END;

            SELECT @V_DTINICIAL = MAX(DTINIAPURACROP),
                   @V_DTFINAL   = MAX(DTFINAPURACROP)
            FROM APURACAOROP
            WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);

            IF (ISNULL(@V_IDFECHDIAROP,'N') <> 'S') BEGIN

               IF (@V_DTEFETCLIE < @V_DTFINAL)  AND (@V_DTEFETCLIE < @V_DTINICIAL) BEGIN

                  UPDATE APURACAOROP
                  SET DTINIAPURACROP  = @V_DTEFETCLIE
                  WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);
               END;

            END
            ELSE BEGIN
               DELETE ROPFILIAL
               WHERE CDFILIAL     = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL)  AND
                     DTFILIALROP  >= @V_DTEFETCLIE;

               IF (@V_DTEFETCLIE< @V_DTINICIAL)  BEGIN
                  UPDATE APURACAOROP
                  SET  DTINIAPURACROP  = @V_DTEFETCLIE
                  WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);
               END;
         END;
      END;

      IF @V_ID_COMANDO = 'D' BEGIN
         SELECT @V_IDUTPADRROP = IDUTPADRROP
         FROM PARAMGERAL G;

         SELECT @V_IDFECHDIAROP   = ISNULL(MAX(IDFECHDIAROP),'N'),
                @V_IDUTPADRROPFIL = ISNULL(MAX(IDUTPADRROPFIL),'N')
         FROM  PARFILIALROP
         WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);

         IF @V_IDUTPADRROP = 'S' OR @V_IDUTPADRROPFIL = 'S' BEGIN
            SELECT @V_IDFECHDIAROP   = ISNULL(MAX(IDFECHDIAROP),'N'),
                   @V_IDUTPADRROPFIL = ISNULL(MAX(IDUTPADRROPFIL),'N')
            FROM  PARFILIALROP
            WHERE CDFILIAL = 'P';
         END;

            SELECT @V_DTINICIAL = MAX(DTINIAPURACROP),
                   @V_DTFINAL   = MAX(DTFINAPURACROP)
            FROM APURACAOROP
            WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);

            IF (ISNULL(@V_IDFECHDIAROP,'N') <> 'S') BEGIN

               IF (@V_OLD_DTEFETCLIE < @V_DTFINAL)  AND (@V_OLD_DTEFETCLIE < @V_DTINICIAL) BEGIN

                  UPDATE APURACAOROP
                  SET DTINIAPURACROP  = @V_OLD_DTEFETCLIE
                  WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);
               END;

            END
            ELSE BEGIN
               DELETE ROPFILIAL
               WHERE CDFILIAL     = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL)  AND
                     DTFILIALROP  >= @V_DTEFETCLIE;


               IF (@V_OLD_DTEFETCLIE< @V_DTINICIAL) BEGIN
                  UPDATE APURACAOROP
                  SET  DTINIAPURACROP  = @V_OLD_DTEFETCLIE
                  WHERE CDFILIAL = ISNULL(@V_NEW_CDFILIAL, @V_OLD_CDFILIAL);
               END;
           END;
      END;

      FETCH NEXT FROM C_CURSOR_DATA INTO
                                      @V_ID_COMANDO,
                                      @V_OLD_CDFILIAL,
                                      @V_OLD_QTFUNEFETCLI,
                                      @V_OLD_QTCOMEFETCLI,
                                      @V_OLD_QTDOAEFETCLI,
                                      @V_NEW_CDFILIAL,
                                      @V_NEW_QTCOMEFETCLI,
                                      @V_NEW_QTFUNEFETCLI,
                                      @V_NEW_QTDOAEFETCLI,
                                      @V_DTEFETCLIE,
                                      @V_OLD_DTEFETCLIE

   END;
   CLOSE C_CURSOR_DATA;
   DEALLOCATE C_CURSOR_DATA;
END;

GO

-- VERSAO --
EXEC ATUVEROBJ 'ATUALIZA_DADOS_EFETCLIE','5.11.000';
GO