CREATE OR REPLACE TRIGGER LOG_PLA_CALENCARD
AFTER INSERT OR UPDATE
ON CALENCARD FOR EACH ROW
DECLARE
   V_DSREMOTEADDR VARCHAR (50);
   V_DSREMOTEHOST VARCHAR (50);
   V_DSSERVERRDM  VARCHAR (50);
   V_DSSERVMETHOD VARCHAR (250);
   V_DSOPERADOR   VARCHAR2(20);
BEGIN
   WEBTUNNING(V_DSREMOTEADDR,
              V_DSREMOTEHOST,
              V_DSSERVERRDM,
              V_DSSERVMETHOD,
              V_DSOPERADOR);
   IF V_DSOPERADOR <> 'VIA BD' THEN

      IF UPDATING THEN
         INSERT INTO LOGCALENCARD
            (DTALTCALENCARD,
             DTMESCALENCARD,
             CDFILIAL,
             CDOPERADOR,
             DTPREVLIBECARDANT,
             DTLIMAPROVFILIANT,
             QTDIAPRORAPROVANT,
             DTLIMAPROVMATRANT,
             DTPREVLIBECARDDEP,
             DTLIMAPROVFILIDEP,
             QTDIAPRORAPROVDEP,
             DTLIMAPROVMATRDEP,
             QTDIABLOQCARD)
         VALUES
            (SYSDATE,
             :OLD.DTMESCALENCARD,
             :OLD.CDFILIAL,
             V_DSOPERADOR,
             :OLD.DTPREVLIBECARD,
             :OLD.DTLIMAPROVFILI,
             :OLD.QTDIAPRORAPROV,
             :OLD.DTLIMAPROVMATR,
             :NEW.DTPREVLIBECARD,
             :NEW.DTLIMAPROVFILI,
             :NEW.QTDIAPRORAPROV,
             :NEW.DTLIMAPROVMATR,
             :NEW.QTDIABLOQCARD);
      ELSE
         IF INSERTING THEN
            INSERT INTO LOGCALENCARD
               (DTALTCALENCARD,
                DTMESCALENCARD,
                CDFILIAL,
                CDOPERADOR,
                DTPREVLIBECARDANT,
                DTLIMAPROVFILIANT,
                QTDIAPRORAPROVANT,
                DTLIMAPROVMATRANT,
                DTPREVLIBECARDDEP,
                DTLIMAPROVFILIDEP,
                QTDIAPRORAPROVDEP,
                DTLIMAPROVMATRDEP,
                QTDIABLOQCARD)
            VALUES
               (SYSDATE,
                :NEW.DTMESCALENCARD,
                :NEW.CDFILIAL,
                V_DSOPERADOR,
                '',
                '',
                '',
                '',
                :NEW.DTPREVLIBECARD,
                :NEW.DTLIMAPROVFILI,
                :NEW.QTDIAPRORAPROV,
                :NEW.DTLIMAPROVMATR,
                :NEW.QTDIABLOQCARD);
         ELSE
            INSERT INTO LOGCALENCARD
               (DTALTCALENCARD,
                DTMESCALENCARD,
                CDFILIAL,
                CDOPERADOR,
                DTPREVLIBECARDANT,
                DTLIMAPROVFILIANT,
                QTDIAPRORAPROVANT,
                DTLIMAPROVMATRANT,
                DTPREVLIBECARDDEP,
                DTLIMAPROVFILIDEP,
                QTDIAPRORAPROVDEP,
                DTLIMAPROVMATRDEP,
                QTDIABLOQCARD)
            VALUES
               (SYSDATE,
                :OLD.DTMESCALENCARD,
                :OLD.CDFILIAL,
                V_DSOPERADOR,
                :OLD.DTPREVLIBECARD,
                :OLD.DTLIMAPROVFILI,
                :OLD.QTDIAPRORAPROV,
                :OLD.DTLIMAPROVMATR,
                '',
                '',
                '',
                '',
                :OLD.QTDIABLOQCARD);
         END IF;
      END IF;   
   END IF;
END;
/

