CREATE OR REPLACE TRIGGER LOG_NFCE
AFTER INSERT OR UPDATE
ON VENDA FOR EACH ROW
DECLARE
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
  V_EXISTE       VARCHAR2(1);
  V_NRSEQLOGNFCE VARCHAR2(3);
BEGIN
   BEGIN   
     IF INSERTING THEN
        IF :NEW.IDTPEMISVEND = 'N' THEN
          WEBTUNNING(V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);              
          INSERT INTO LOGNFCE (CDFILIAL,CDCAIXA,NRSEQVENDA,NRSEQLOGNFCE,NRSEQVENDAMSDE,DTABERTUR,IDSITUVENDA,CDSERIENFCE,NRNOTAFISCALCE,IDSTATUSNFCE,NRACESSONFCE,DSOBSSTATUSNFCE,DTEMISSAONFCE,NRLANCTONFCE,IDOPERACAO,DTHRLOGNFCE,
                               DSREMOTEADDR, DSREMOTEHOST, DSSERVERRDM, DSSERVMETHOD, DSREMOTEOPER)
            VALUES (:NEW.CDFILIAL,:NEW.CDCAIXA,:NEW.NRSEQVENDA,NULL,:NEW.NRSEQVENDAMSDE,:NEW.DTABERTUR, :NEW.IDSITUVENDA,:NEW.CDSERIENFCE,:NEW.NRNOTAFISCALCE,:NEW.IDSTATUSNFCE,:NEW.NRACESSONFCE,:NEW.DSOBSSTATUSNFCE,:NEW.DTEMISSAONFCE,:NEW.NRLANCTONFCE,'E',SYSDATE,
                    V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);

        END IF;
     END IF;
     IF UPDATING THEN
        IF :OLD.IDTPEMISVEND = 'N' THEN
          IF (NVL(:OLD.IDSITUVENDA,'!')      <> NVL(:NEW.IDSITUVENDA,'!'))     OR
             (NVL(:OLD.CDSERIENFCE,'!')      <> NVL(:NEW.CDSERIENFCE,'!'))     OR
             (NVL(:OLD.NRNOTAFISCALCE,'!')   <> NVL(:NEW.NRNOTAFISCALCE,'!'))  OR
             (NVL(:OLD.IDSTATUSNFCE,'!')     <> NVL(:NEW.IDSTATUSNFCE,'!'))    OR
             (NVL(:OLD.NRACESSONFCE,'!')     <> NVL(:NEW.NRACESSONFCE,'!'))    OR
             (NVL(:OLD.DSOBSSTATUSNFCE,'!')  <> NVL(:NEW.DSOBSSTATUSNFCE,'!')) OR
             (NVL(:OLD.DTEMISSAONFCE,TO_DATE('01/01/0001','DD/MM/YYYY')) <> NVL(:NEW.DTEMISSAONFCE,TO_DATE('01/01/0001','DD/MM/YYYY'))) OR
             (NVL(:OLD.NRLANCTONFCE,'!')   <> NVL(:NEW.NRLANCTONFCE,'!')) OR
             (NVL(:OLD.NRENVIOSEFAZ,0)     <> NVL(:NEW.NRENVIOSEFAZ,0)) THEN

            WEBTUNNING(V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);              
            
            BEGIN
              SELECT MAX('S')
                INTO V_EXISTE
                FROM LOGNFCE
               WHERE CDFILIAL   = :NEW.CDFILIAL
                 AND CDCAIXA    = :NEW.CDCAIXA
                 AND NRSEQVENDA = :NEW.NRSEQVENDA;
            EXCEPTION
              WHEN NO_DATA_FOUND THEN
                V_EXISTE := 'N';
            END;                  
            
            IF V_EXISTE = 'S' THEN                              
              
              INSERT INTO LOGNFCE (CDFILIAL,CDCAIXA,NRSEQVENDA,NRSEQLOGNFCE,NRSEQVENDAMSDE,DTABERTUR,IDSITUVENDA,CDSERIENFCE,NRNOTAFISCALCE,IDSTATUSNFCE,NRACESSONFCE,DSOBSSTATUSNFCE,DTEMISSAONFCE,NRLANCTONFCE,IDOPERACAO,DTHRLOGNFCE,
                                 DSREMOTEADDR, DSREMOTEHOST, DSSERVERRDM, DSSERVMETHOD, DSREMOTEOPER)
              VALUES (:NEW.CDFILIAL,:NEW.CDCAIXA,:NEW.NRSEQVENDA,NULL,:NEW.NRSEQVENDAMSDE,:NEW.DTABERTUR,:NEW.IDSITUVENDA,:NEW.CDSERIENFCE,:NEW.NRNOTAFISCALCE,:NEW.IDSTATUSNFCE,:NEW.NRACESSONFCE,:NEW.DSOBSSTATUSNFCE,:NEW.DTEMISSAONFCE,:NEW.NRLANCTONFCE,'U',SYSDATE,
                      V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER);
            END IF;
          END IF;          
        END IF;
     END IF;
   EXCEPTION
    WHEN OTHERS THEN
      V_DSREMOTEOPER := ''; 
   END;
END;

/