CREATE OR REPLACE TRIGGER GRAVA_LOGITEMAUTF
AFTER INSERT OR UPDATE OR DELETE
ON ITEMAUTF FOR EACH ROW

DECLARE
   V_IDGRAVLOGAF  VARCHAR2(03);
   V_DSRDMUSLITAF VARCHAR2(50);
   V_DSENDUSLITAF VARCHAR2(50);
   V_DSNMUSULITAF VARCHAR2(50);
   V_DSMETUSLITAF VARCHAR2(250);
   V_CDOPELOGITAF 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_DSENDUSLITAF, V_DSNMUSULITAF, V_DSRDMUSLITAF, V_DSMETUSLITAF, V_CDOPELOGITAF);

   IF INSERTING THEN
      IF SUBSTR(V_IDGRAVLOGAF, 1, 1) = 'S' THEN
         INSERT INTO LOGITEMAUTF
         (CDFIDESLITAF, CDFILANLITAF, NRAUTOFLITAF, CDMARCALITAF, CDPRODLITAF,  DTPROGLITAF,
          QTPROGENTANT, QTPROGENTDEP, VRPREUNIANT,  VRPREUNIDEP,  IDBAIXITANT,  IDBAIXITDEP,
          DSINCITAFANT, DSINCITAFDEP, DTEITSOLITAF, VRPERDEAFANT, VRPERDEAFDEP, DTEFETEAFANT,
          DTEFETEAFDEP, QTEFETEAFANT, QTEFETEAFDEP, IDOPERALITAF, DSRDMUSLITAF, DSENDUSLITAF,
          DSNMUSULITAF, DSMETUSLITAF, CDOPELOGITAF, DTOPERAITAF, IDTPGERARUP, CDPRODRUP,
          CDMARCARUP, QTNOVAPROGAF, QTNOVAPROGAFORIG)
         VALUES
         (:NEW.CDFILDEST, :NEW.CDFILLANC, :NEW.NRAUTOFORN, :NEW.CDMARCA, :NEW.CDPRODUTO, :NEW.DTPROGENTAF,
          0, :NEW.QTPROGENTAF, 0, :NEW.VRPREUNIAF, NULL, :NEW.IDBAIXITMAF,
          NULL, :NEW.DSINCONSITAF, :NEW.DTENTRITMSOL, 0, :NEW.VRPERDESAF, NULL,
          :NEW.DTEFETENTAF, 0, :NEW.QTEFETENTRAF, 'I', V_DSRDMUSLITAF, V_DSENDUSLITAF,
          V_DSNMUSULITAF, V_DSMETUSLITAF, V_CDOPELOGITAF, SYSDATE, :NEW.IDTPGERARUP, :NEW.CDPRODRUP,
          :NEW.CDMARCARUP, :NEW.QTNOVAPROGAF, :NEW.QTNOVAPROGAFORIG);
      END IF;
   END IF;

   IF UPDATING THEN
      IF SUBSTR(V_IDGRAVLOGAF, 2, 1) = 'S' THEN
         INSERT INTO LOGITEMAUTF
         (CDFIDESLITAF, CDFILANLITAF, NRAUTOFLITAF, CDMARCALITAF, CDPRODLITAF,  DTPROGLITAF,
          QTPROGENTANT, QTPROGENTDEP, VRPREUNIANT,  VRPREUNIDEP,  IDBAIXITANT,  IDBAIXITDEP,
          DSINCITAFANT, DSINCITAFDEP, DTEITSOLITAF, VRPERDEAFANT, VRPERDEAFDEP, DTEFETEAFANT,
          DTEFETEAFDEP, QTEFETEAFANT, QTEFETEAFDEP, IDOPERALITAF, DSRDMUSLITAF, DSENDUSLITAF,
          DSNMUSULITAF, DSMETUSLITAF, CDOPELOGITAF, DTOPERAITAF, IDTPGERARUP, CDPRODRUP,
          CDMARCARUP, QTNOVAPROGAF, QTNOVAPROGAFORIG)
         VALUES
         (:OLD.CDFILDEST, :OLD.CDFILLANC, :OLD.NRAUTOFORN, :OLD.CDMARCA, :OLD.CDPRODUTO, :OLD.DTPROGENTAF,
          :OLD.QTPROGENTAF, :NEW.QTPROGENTAF, :OLD.VRPREUNIAF, :NEW.VRPREUNIAF, :OLD.IDBAIXITMAF, :NEW.IDBAIXITMAF,
          :OLD.DSINCONSITAF, :NEW.DSINCONSITAF, :NEW.DTENTRITMSOL, :OLD.VRPERDESAF, :NEW.VRPERDESAF, :OLD.DTEFETENTAF,
          :NEW.DTEFETENTAF, :OLD.QTEFETENTRAF, :NEW.QTEFETENTRAF, 'A', V_DSRDMUSLITAF, V_DSENDUSLITAF,
          V_DSNMUSULITAF, V_DSMETUSLITAF, V_CDOPELOGITAF, SYSDATE, :NEW.IDTPGERARUP, :NEW.CDPRODRUP,
          :NEW.CDMARCARUP, :NEW.QTNOVAPROGAF, :NEW.QTNOVAPROGAFORIG);
      END IF;
   END IF;

   IF DELETING THEN
      IF SUBSTR(V_IDGRAVLOGAF, 3, 1) = 'S' THEN
         INSERT INTO LOGITEMAUTF
         (CDFIDESLITAF, CDFILANLITAF, NRAUTOFLITAF, CDMARCALITAF, CDPRODLITAF,  DTPROGLITAF,
          QTPROGENTANT, QTPROGENTDEP, VRPREUNIANT,  VRPREUNIDEP,  IDBAIXITANT,  IDBAIXITDEP,
          DSINCITAFANT, DSINCITAFDEP, DTEITSOLITAF, VRPERDEAFANT, VRPERDEAFDEP, DTEFETEAFANT,
          DTEFETEAFDEP, QTEFETEAFANT, QTEFETEAFDEP, IDOPERALITAF, DSRDMUSLITAF, DSENDUSLITAF,
          DSNMUSULITAF, DSMETUSLITAF, CDOPELOGITAF, DTOPERAITAF, IDTPGERARUP, CDPRODRUP,
          CDMARCARUP, QTNOVAPROGAF, QTNOVAPROGAFORIG)
         VALUES
         (:OLD.CDFILDEST, :OLD.CDFILLANC, :OLD.NRAUTOFORN, :OLD.CDMARCA, :OLD.CDPRODUTO, :OLD.DTPROGENTAF,
          :OLD.QTPROGENTAF, :OLD.QTPROGENTAF, :OLD.VRPREUNIAF, :OLD.VRPREUNIAF, :OLD.IDBAIXITMAF, :OLD.IDBAIXITMAF,
          :OLD.DSINCONSITAF, :OLD.DSINCONSITAF, :OLD.DTENTRITMSOL, :OLD.VRPERDESAF, :OLD.VRPERDESAF, :OLD.DTEFETENTAF,
          :OLD.DTEFETENTAF, :OLD.QTEFETENTRAF, :OLD.QTEFETENTRAF, 'E', V_DSRDMUSLITAF, V_DSENDUSLITAF,
          V_DSNMUSULITAF, V_DSMETUSLITAF, V_CDOPELOGITAF, SYSDATE, :OLD.IDTPGERARUP,  :OLD.CDPRODRUP,
          :OLD.CDMARCARUP, :OLD.QTNOVAPROGAF, :OLD.QTNOVAPROGAFORIG);
      END IF;
   END IF;
END;
/