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


  DROP TRIGGER ATUALIZA_DADOS_EFETCLIEPME
  GO


CREATE TRIGGER ATUALIZA_DADOS_EFETCLIEPME
ON EFETCLIEPME
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_QTCOMEFCLPME  DATETIME;
   DECLARE @V_NEW_QTFUNEFCLPME  DATETIME;
   DECLARE @V_NEW_QTDOAEFCLPME  DATETIME;
   DECLARE @V_OLD_QTFUNEFCLPME  DATETIME;
   DECLARE @V_OLD_QTCOMEFCLPME  DATETIME;
   DECLARE @V_OLD_QTDOAEFCLPME  DATETIME;
   DECLARE @V_ID_COMANDO      VARCHAR(1);
   DECLARE @V_IDUTPADRROP     VARCHAR(1);
   DECLARE @V_IDUTPADRROPFIL  VARCHAR(1);
   DECLARE @V_DTEFETCLPME       DATETIME;
   DECLARE @V_CDFILICENTRAL   VARCHAR(4);
   DECLARE @V_OLD_DTEFETCLPME   DATETIME;

BEGIN
   SET @V_ID_COMANDO = 'N';

   DECLARE C_CURSOR_DATA CURSOR FOR
      SELECT
        'U' IDCOMANDO,
        DELETED.CDFILIAL,
        DELETED.QTCOMEFCLPME,
        DELETED.QTFUNEFCLPME,
        DELETED.QTDOAEFCLPME,
        INSERTED.CDFILIAL,
        INSERTED.QTCOMEFCLPME,
        INSERTED.QTFUNEFCLPME,
        INSERTED.QTDOAEFCLPME,
        INSERTED.DTEFETCLPME,
        DELETED.DTEFETCLPME
      FROM
          DELETED,
          INSERTED
      WHERE
         INSERTED.CDFILIAL  = DELETED.CDFILIAL

      UNION ALL

      SELECT
        'I' IDCOMANDO,
        NULL, NULL, NULL, NULL,
        INSERTED.CDFILIAL,
        INSERTED.QTCOMEFCLPME,
        INSERTED.QTFUNEFCLPME,
        INSERTED.QTDOAEFCLPME,
        INSERTED.DTEFETCLPME,
        NULL
      FROM
        INSERTED
      WHERE
        0 = (SELECT COUNT(*) FROM DELETED)

      UNION ALL

      SELECT
        'D' IDCOMANDO,
        DELETED.CDFILIAL,
        DELETED.QTCOMEFCLPME,
        DELETED.QTFUNEFCLPME,
        DELETED.QTDOAEFCLPME,
        NULL, NULL, NULL, NULL, NULL,
        DELETED.DTEFETCLPME
      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_QTFUNEFCLPME,
                                      @V_OLD_QTCOMEFCLPME,
                                      @V_OLD_QTDOAEFCLPME,
                                      @V_NEW_CDFILIAL,
                                      @V_NEW_QTCOMEFCLPME,
                                      @V_NEW_QTFUNEFCLPME,
                                      @V_NEW_QTDOAEFCLPME,
                                      @V_DTEFETCLPME,
                                      @V_OLD_DTEFETCLPME

   WHILE (@@FETCH_STATUS = 0) BEGIN
      SELECT @V_CDFILICENTRAL = MAX(CDFILCENTPME)
      FROM FILIAL
      WHERE CDFILIAL =  @V_NEW_CDFILIAL;

      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_QTCOMEFCLPME <> @V_OLD_QTCOMEFCLPME)
         AND (@V_NEW_QTFUNEFCLPME <> @V_OLD_QTFUNEFCLPME)
         AND (@V_NEW_QTDOAEFCLPME <> @V_OLD_QTDOAEFCLPME)
         BEGIN
            SELECT @V_DTINICIAL = MAX(DTINIAPURACROP),
                   @V_DTFINAL   = MAX(DTFINAPURACROP)
            FROM APURACAOROP
            WHERE CDFILIAL = @V_CDFILICENTRAL;

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

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

                  UPDATE APURACAOROP
                  SET DTINIAPURACROP  = @V_DTEFETCLPME
                  WHERE CDFILIAL = @V_CDFILICENTRAL;
               END;

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

               IF (@V_DTEFETCLPME< @V_DTINICIAL) BEGIN
                  UPDATE APURACAOROP
                  SET  DTINIAPURACROP = @V_DTEFETCLPME
                  WHERE CDFILIAL = @V_CDFILICENTRAL;
               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 = @V_CDFILICENTRAL;

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

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

                  UPDATE APURACAOROP
                  SET DTINIAPURACROP  = @V_DTEFETCLPME
                  WHERE CDFILIAL = @V_CDFILICENTRAL;
               END;

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

               IF (@V_DTEFETCLPME< @V_DTINICIAL) BEGIN
                  UPDATE APURACAOROP
                  SET  DTINIAPURACROP = @V_DTEFETCLPME
                  WHERE CDFILIAL = @V_CDFILICENTRAL;
               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 = @V_CDFILICENTRAL;

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

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

                  UPDATE APURACAOROP
                  SET DTINIAPURACROP  = @V_OLD_DTEFETCLPME
                  WHERE CDFILIAL = @V_CDFILICENTRAL;
               END;

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

               IF (@V_OLD_DTEFETCLPME< @V_DTINICIAL) BEGIN
                  UPDATE APURACAOROP
                  SET  DTINIAPURACROP = @V_OLD_DTEFETCLPME
                  WHERE CDFILIAL = @V_CDFILICENTRAL;
               END;

            END;
      END;


      FETCH NEXT FROM C_CURSOR_DATA INTO @V_ID_COMANDO,
                                      @V_OLD_CDFILIAL,
                                      @V_OLD_QTFUNEFCLPME,
                                      @V_OLD_QTCOMEFCLPME,
                                      @V_OLD_QTDOAEFCLPME,
                                      @V_NEW_CDFILIAL,
                                      @V_NEW_QTCOMEFCLPME,
                                      @V_NEW_QTFUNEFCLPME,
                                      @V_NEW_QTDOAEFCLPME,
                                      @V_DTEFETCLPME,
                                      @V_OLD_DTEFETCLPME

   END;
   CLOSE C_CURSOR_DATA;
   DEALLOCATE C_CURSOR_DATA;
END;

GO

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