IF EXISTS (SELECT * FROM SYSOBJECTS O, SYSUSERS U
            WHERE O.NAME = 'LOG_FAI_MINEFETMES'
              AND TYPE   = 'TR'
   	      AND U.UID  = O.UID
     	      AND U.NAME = USER)
DROP TRIGGER LOG_FAI_MINEFETMES
GO


CREATE  TRIGGER LOG_FAI_MINEFETMES
ON MINEFETMES 
FOR INSERT, UPDATE, DELETE
AS

   DECLARE @V_USUARIO      VARCHAR(40);
   DECLARE @V_TIPO         VARCHAR(1);
   DECLARE @V_ITPESQ       VARCHAR(40);
   DECLARE @V_DESC         VARCHAR(300);
   DECLARE @V_DTOPER       DATETIME;
   DECLARE @V_DSREMOTEADDR VARCHAR(15);
   DECLARE @V_DSREMOTEHOST VARCHAR(15);
   DECLARE @V_DSSERVERRDM  VARCHAR(20);
   DECLARE @V_DSSERVMETHOD VARCHAR(30);
   DECLARE @V_DSREMOTEOPER VARCHAR(12);
   
  DECLARE @V_OLD_CDFILIAL     VARCHAR(04);
  DECLARE @V_OLD_CDSERVFILI   VARCHAR(5);
  DECLARE @V_OLD_NRDIABASE    FLOAT;
  DECLARE @V_OLD_QTMINEFET    FLOAT;
  DECLARE @V_OLD_DSMINEFET    VARCHAR(400);
  DECLARE @V_OLD_IDTERCEIROS  VARCHAR(1);


  DECLARE @V_NEW_CDFILIAL     VARCHAR(04);
  DECLARE @V_NEW_CDSERVFILI   VARCHAR(5);
  DECLARE @V_NEW_NRDIABASE    FLOAT;
  DECLARE @V_NEW_QTMINEFET    FLOAT;
  DECLARE @V_NEW_DSMINEFET    VARCHAR(400);
  DECLARE @V_NEW_IDTERCEIROS  VARCHAR(1);
  DECLARE @V_IDCOMANDO        VARCHAR(2);
BEGIN
   SET @V_TIPO = '0';
   SET @V_DESC = '';

   DECLARE C_CUR_MINEFETMES CURSOR FOR
      SELECT 
         'D' IDCOMANDO,
         NULL,NULL,NULL,
         NULL,NULL,NULL,
         CDFILIAL,
         CDSERVFILI,
         NRDIABASE,
         QTMINEFET,
         DSMINEFET,
         IDTERCEIROS
      FROM
         DELETED
      WHERE
         0 = (SELECT COUNT(*) FROM INSERTED)

      UNION ALL

      SELECT
         'U' IDCOMANDO,
         INSERTED.CDFILIAL,
         INSERTED.CDSERVFILI,
         INSERTED.NRDIABASE,
         INSERTED.QTMINEFET,
         INSERTED.DSMINEFET,
         INSERTED.IDTERCEIROS,
         DELETED.CDFILIAL,
         DELETED.CDSERVFILI,
         DELETED.NRDIABASE,
         DELETED.QTMINEFET,
         DELETED.DSMINEFET,
         DELETED.IDTERCEIROS
      FROM
         INSERTED,
         DELETED
      WHERE
         INSERTED.CDFILIAL    = DELETED.CDFILIAL    AND
         INSERTED.CDSERVFILI  = DELETED.CDSERVFILI

      UNION ALL

      SELECT
         'I' IDCOMANDO,
         INSERTED.CDFILIAL,
         INSERTED.CDSERVFILI,
         INSERTED.NRDIABASE,
         INSERTED.QTMINEFET,
         INSERTED.DSMINEFET,
         INSERTED.IDTERCEIROS,
         NULL,NULL,NULL,
         NULL,NULL,NULL
      FROM
         INSERTED
      WHERE
         0 = (SELECT COUNT(*) FROM DELETED)

   OPEN C_CUR_MINEFETMES;
   FETCH NEXT FROM C_CUR_MINEFETMES INTO @V_IDCOMANDO,
                                   @V_NEW_CDFILIAL,
                                   @V_NEW_CDSERVFILI,
                                   @V_NEW_NRDIABASE,
                                   @V_NEW_QTMINEFET,
                                   @V_NEW_DSMINEFET,
                                   @V_NEW_IDTERCEIROS,
                                   @V_OLD_CDFILIAL,
                                   @V_OLD_CDSERVFILI,
                                   @V_OLD_NRDIABASE,
                                   @V_OLD_QTMINEFET,
                                   @V_OLD_DSMINEFET,
                                   @V_OLD_IDTERCEIROS;
   WHILE (@@FETCH_STATUS = 0) BEGIN
      IF (@V_IDCOMANDO = 'D') BEGIN
         
         SET @V_DESC = @V_DESC + 'Filial='  + @V_OLD_CDFILIAL;
         SET @V_DESC = @V_DESC + 'Servico=' + @V_OLD_CDSERVFILI;

         IF @V_OLD_NRDIABASE IS NOT NULL BEGIN
            SET @V_DESC = @V_DESC + 'DiaBase=' + CONVERT(VARCHAR,@V_OLD_NRDIABASE);
         END;

         IF @V_OLD_QTMINEFET IS NOT NULL BEGIN
            SET @V_DESC = @V_DESC + 'QtMinEfetivosMes=' + CONVERT(VARCHAR, @V_OLD_QTMINEFET);
         END;

         IF @V_OLD_DSMINEFET IS NOT NULL BEGIN
            SET @V_DESC = @V_DESC + 'DescMinEfet=' + @V_OLD_DSMINEFET;
         END;

         IF @V_OLD_IDTERCEIROS IS NOT NULL BEGIN
            SET @V_DESC = @V_DESC + 'ConsideraTerceiro=' + @V_OLD_IDTERCEIROS;
         END;

         SET @V_TIPO     = '3';
         SET @V_ITPESQ   = @V_OLD_CDFILIAL+ @V_OLD_CDSERVFILI;
      END
      ELSE BEGIN
         IF (@V_IDCOMANDO = 'U') BEGIN
            IF (ISNULL(@V_OLD_NRDIABASE,-1)    <> ISNULL(@V_NEW_NRDIABASE, -1))   OR
               (ISNULL(@V_OLD_QTMINEFET,-1)    <> ISNULL(@V_NEW_QTMINEFET, -1))   OR
               (ISNULL(@V_OLD_DSMINEFET,'#')   <> ISNULL(@V_NEW_DSMINEFET,'#'))   OR
               (ISNULL(@V_OLD_IDTERCEIROS,'#') <> ISNULL(@V_NEW_IDTERCEIROS,'#')) BEGIN
               SET @V_TIPO         = '2';
               SET @V_ITPESQ       = @V_OLD_CDFILIAL+ @V_OLD_CDSERVFILI;
            END;
         END
        ELSE BEGIN
            SET @V_TIPO         = '1';
            SET @V_ITPESQ       = @V_NEW_CDFILIAL+@V_NEW_CDSERVFILI;
         END;

         IF (ISNULL(@V_OLD_NRDIABASE,-1)    <> ISNULL(@V_NEW_NRDIABASE, -1)) BEGIN
            SET @V_DESC = @V_DESC + 'DiaBase='     + CONVERT(VARCHAR,@V_NEW_NRDIABASE);
         END;
         IF (ISNULL(@V_OLD_QTMINEFET,-1)    <> ISNULL(@V_NEW_QTMINEFET, -1)) BEGIN
            SET @V_DESC = @V_DESC + 'QtMinEfetivosMes='     + CONVERT(VARCHAR,@V_NEW_QTMINEFET);
         END;
         IF (ISNULL(@V_OLD_DSMINEFET,'#')   <> ISNULL(@V_NEW_DSMINEFET,'#')) BEGIN
            SET @V_DESC = @V_DESC + 'DescMinEfet='     + @V_NEW_DSMINEFET;
         END;
         IF (ISNULL(@V_OLD_IDTERCEIROS,'#')   <> ISNULL(@V_NEW_IDTERCEIROS,'#')) BEGIN
            SET @V_DESC = @V_DESC + 'ConsideraTerceiro='      + @V_NEW_IDTERCEIROS;
         END;
      END;
      FETCH NEXT FROM C_CUR_MINEFETMES  INTO @V_IDCOMANDO,
                                   @V_NEW_CDFILIAL,
                                   @V_NEW_CDSERVFILI,
                                   @V_NEW_NRDIABASE,
                                   @V_NEW_QTMINEFET,
                                   @V_NEW_DSMINEFET,
                                   @V_NEW_IDTERCEIROS,
                                   @V_OLD_CDFILIAL,
                                   @V_OLD_CDSERVFILI,
                                   @V_OLD_NRDIABASE,
                                   @V_OLD_QTMINEFET,
                                   @V_OLD_DSMINEFET,
                                   @V_OLD_IDTERCEIROS;

   END;
   CLOSE C_CUR_MINEFETMES;
   DEALLOCATE C_CUR_MINEFETMES;
   
   IF @V_TIPO <> '0' BEGIN
      EXEC TEKNISA.NMUSUARIO @V_USUARIO;

      EXEC WEBTUNNING @V_DSREMOTEADDR,
                      @V_DSREMOTEHOST,
                      @V_DSSERVERRDM,
                      @V_DSSERVMETHOD,
                      @V_DSREMOTEOPER;

      IF NOT @V_DESC IS NULL BEGIN
         SET @V_DTOPER = GETDATE();
         INSERT INTO LOGOPERA
              (NMTABELA,     DTOPERACAO,   NMRESPOPER, IDTIPOOPER,    DSOPERACAO, CDPESQITEM,
               DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, DSMETUSUOPER, CDORESPOPER)
           VALUES
              ('MINEFETMES', @V_DTOPER, ISNULL(@V_USUARIO,' '), @V_TIPO, ISNULL(@V_DESC, 'VAZIO'),
               @V_ITPESQ, @V_DSSERVERRDM, @V_DSREMOTEADDR, @V_DSREMOTEHOST, @V_DSSERVMETHOD,
               @V_DSREMOTEOPER);
      END;
   END;
END;
GO
-- VERSAO --
EXEC ATUVEROBJ 'LOG_FAI_MINEFETMES','5.65.000';
GO
