CREATE OR REPLACE TRIGGER GRAVA_LOGAUTOFORNE
AFTER INSERT OR UPDATE OR DELETE
ON AUTOFORNE FOR EACH ROW

DECLARE
   V_IDGRAVLOGAF VARCHAR2(03);
   V_DSRDMUSULAF  VARCHAR2(50);
   V_DSENDUSULAF  VARCHAR2(50);
   V_DSNMUSULAF   VARCHAR2(50);
   V_DSMETUSULAF  VARCHAR2(250);
   V_CDOPERLOGAF  VARCHAR2(12);

BEGIN
   SELECT NVL(MAX(IDGRAVLOGAF), 'SSS')
   INTO V_IDGRAVLOGAF
   FROM PARASUPRI
   WHERE (CDFILIAL = (SELECT NVL(CDFILCENTCMP, CDFILIAL)
                      FROM FILIAL
                      WHERE (CDFILIAL = NVL(:NEW.CDFILDEST, :OLD.CDFILDEST))));

   WEBTUNNING(V_DSENDUSULAF, V_DSNMUSULAF, V_DSRDMUSULAF, V_DSMETUSULAF, V_CDOPERLOGAF);

   IF INSERTING THEN
      IF SUBSTR(V_IDGRAVLOGAF, 1, 1) = 'S' THEN
         INSERT INTO LOGAUTOFORNE
         (CDFILDESLAF,  CDFILLANLAF,  NRAUTFORLAF,  VRTOTALAFANT, VRTOTALAFDEP, IDSITUAAFANT,
          IDSITUAAFDEP, IDAFFORLIANT, IDAFFORLIDEP, IDBAIXAAFANT, IDBAIXAAFDEP, CDFORMPGTANT,
          CDFORMPGTDEP, DSINCONAFANT, DSINCONAFDEP, IDOPERACLAF,  DSRDMUSULAF,  DSENDUSULAF,
          DSNMUSULAF,   DSMETUSULAF,  CDOPERLOGAF,  DTOPERALAF, NRSEQENVIOAFORN, IDCONFRECEBANT,
          IDCONFRECEBDEP, IDSITATENDANT, IDSITATENDDEP, DTENVIOAF, CDTPCTPAGAR, IDAFOPERLOG,
          CDFILIOPELOG, CDOPERADOR, IDSTATLEITURA, USR_DTEXPOAUTOFORN, USR_NRAUTOFORN,
          VRCONTAERRO, VRRESULTADO, VRREALORCAAF, VRORCAAF, CDCENTCUSTAF, CDCLASSFINAAF)
         VALUES
         (:NEW.CDFILDEST, :NEW.CDFILLANC, :NEW.NRAUTOFORN, 0, :NEW.VRTOTALAF, NULL,
          :NEW.IDSITUATUAAF, NULL, :NEW.IDAFFORALIM, NULL, :NEW.IDBAIXAAF, NULL,
          :NEW.CDFORMPGTO, NULL, :NEW.DSINCONSAF, 'I', V_DSRDMUSULAF, V_DSENDUSULAF,
          V_DSNMUSULAF, V_DSMETUSULAF, V_CDOPERLOGAF, SYSDATE, :NEW.NRSEQENVIOAFORN, NULL,
          :NEW.IDCONFRECEBIMENTO, NULL, :NEW.IDSITUATENDIMENTOAF, :NEW.DTENVIOAF, :NEW.CDTPCTPAGAR, :NEW.IDAFOPERLOG,
          :NEW.CDFILIOPELOG, :NEW.CDOPERADOR, :NEW.IDSTATLEITURA, :NEW.USR_DTEXPOAUTOFORN, :NEW.USR_NRAUTOFORN,
          :NEW.VRCONTAERRO, :NEW.VRRESULTADO, :NEW.VRREALORCAAF, :NEW.VRORCAAF, :NEW.CDCENTCUSTAF, :NEW.CDCLASSFINAAF);
      END IF;
   END IF;

   IF UPDATING THEN
      IF SUBSTR(V_IDGRAVLOGAF, 2, 1) = 'S' THEN
         INSERT INTO LOGAUTOFORNE
         (CDFILDESLAF,  CDFILLANLAF,  NRAUTFORLAF,  VRTOTALAFANT, VRTOTALAFDEP, IDSITUAAFANT,
          IDSITUAAFDEP, IDAFFORLIANT, IDAFFORLIDEP, IDBAIXAAFANT, IDBAIXAAFDEP, CDFORMPGTANT,
          CDFORMPGTDEP, DSINCONAFANT, DSINCONAFDEP, IDOPERACLAF,  DSRDMUSULAF,  DSENDUSULAF,
          DSNMUSULAF,   DSMETUSULAF,  CDOPERLOGAF,  DTOPERALAF, NRSEQENVIOAFORN, IDCONFRECEBANT,
          IDCONFRECEBDEP, IDSITATENDANT, IDSITATENDDEP, DTENVIOAF, CDTPCTPAGAR, IDAFOPERLOG,
          CDFILIOPELOG, CDOPERADOR, IDSTATLEITURA, USR_DTEXPOAUTOFORN, USR_NRAUTOFORN,
          VRCONTAERRO, VRRESULTADO, VRREALORCAAF, VRORCAAF, CDCENTCUSTAF, CDCLASSFINAAF)
         VALUES
         (:OLD.CDFILDEST, :OLD.CDFILLANC, :OLD.NRAUTOFORN, 0, :NEW.VRTOTALAF, :OLD.IDSITUATUAAF,
          :NEW.IDSITUATUAAF, :NEW.IDAFFORALIM, :NEW.IDAFFORALIM, :NEW.IDBAIXAAF, :NEW.IDBAIXAAF, :NEW.CDFORMPGTO,
          :NEW.CDFORMPGTO, :NEW.DSINCONSAF, :NEW.DSINCONSAF, 'A', V_DSRDMUSULAF, V_DSENDUSULAF,
          V_DSNMUSULAF, V_DSMETUSULAF, V_CDOPERLOGAF, SYSDATE, NVL(:OLD.NRSEQENVIOAFORN, :NEW.NRSEQENVIOAFORN),
          :NEW.IDCONFRECEBIMENTO, :NEW.IDCONFRECEBIMENTO, :NEW.IDSITUATENDIMENTOAF, :NEW.IDSITUATENDIMENTOAF,
          :NEW.DTENVIOAF, :NEW.CDTPCTPAGAR, :NEW.IDAFOPERLOG, :NEW.CDFILIOPELOG, :NEW.CDOPERADOR,
          :NEW.IDSTATLEITURA, :NEW.USR_DTEXPOAUTOFORN, :NEW.USR_NRAUTOFORN,
          :NEW.VRCONTAERRO, :NEW.VRRESULTADO, :NEW.VRREALORCAAF, :NEW.VRORCAAF, :NEW.CDCENTCUSTAF, :NEW.CDCLASSFINAAF);
      END IF;
   END IF;

   IF DELETING THEN
      IF SUBSTR(V_IDGRAVLOGAF, 3, 1) = 'S' THEN
         INSERT INTO LOGAUTOFORNE
         (CDFILDESLAF,  CDFILLANLAF,  NRAUTFORLAF,  VRTOTALAFANT, VRTOTALAFDEP, IDSITUAAFANT,
          IDSITUAAFDEP, IDAFFORLIANT, IDAFFORLIDEP, IDBAIXAAFANT, IDBAIXAAFDEP, CDFORMPGTANT,
          CDFORMPGTDEP, DSINCONAFANT, DSINCONAFDEP, IDOPERACLAF,  DSRDMUSULAF,  DSENDUSULAF,
          DSNMUSULAF,   DSMETUSULAF,  CDOPERLOGAF,  DTOPERALAF, NRSEQENVIOAFORN, IDCONFRECEBANT,
          IDCONFRECEBDEP, IDSITATENDANT, IDSITATENDDEP, DTENVIOAF, CDTPCTPAGAR, IDAFOPERLOG,
          CDFILIOPELOG, CDOPERADOR, IDSTATLEITURA, USR_DTEXPOAUTOFORN, USR_NRAUTOFORN,
          VRCONTAERRO, VRRESULTADO, VRREALORCAAF, VRORCAAF, CDCENTCUSTAF, CDCLASSFINAAF)
         VALUES
         (:OLD.CDFILDEST, :OLD.CDFILLANC, :OLD.NRAUTOFORN, 0, :OLD.VRTOTALAF, :OLD.IDSITUATUAAF,
          :OLD.IDSITUATUAAF, :OLD.IDAFFORALIM, :OLD.IDAFFORALIM, :OLD.IDBAIXAAF, :OLD.IDBAIXAAF, :OLD.CDFORMPGTO,
          :OLD.CDFORMPGTO, :OLD.DSINCONSAF, :OLD.DSINCONSAF, 'E', V_DSRDMUSULAF, V_DSENDUSULAF,
          V_DSNMUSULAF, V_DSMETUSULAF, V_CDOPERLOGAF, SYSDATE, :OLD.NRSEQENVIOAFORN,
          :OLD.IDCONFRECEBIMENTO, :OLD.IDCONFRECEBIMENTO, :OLD.IDSITUATENDIMENTOAF, :OLD.IDSITUATENDIMENTOAF,
          :OLD.DTENVIOAF, :OLD.CDTPCTPAGAR, :OLD.IDAFOPERLOG, :OLD.CDFILIOPELOG, :OLD.CDOPERADOR,
          :OLD.IDSTATLEITURA, :OLD.USR_DTEXPOAUTOFORN, :OLD.USR_NRAUTOFORN,
          :OLD.VRCONTAERRO, :OLD.VRRESULTADO, :OLD.VRREALORCAAF, :OLD.VRORCAAF, :OLD.CDCENTCUSTAF, :OLD.CDCLASSFINAAF);
      END IF;
   END IF;
END;
/