CREATE OR REPLACE TRIGGER USR_EXPO_ITEMAUTF
BEFORE UPDATE ON ITEMAUTF
FOR EACH ROW

BEGIN
   IF (:OLD.QTPROGENTAF <> :NEW.QTPROGENTAF) OR
      (NVL(:OLD.QTEFETENTRAF,0) <> NVL(:NEW.QTEFETENTRAF,0)) OR
      ((NVL(:OLD.IDBAIXITMAF,'1') <> :NEW.IDBAIXITMAF) AND (:NEW.IDBAIXITMAF = '3')) THEN
      :NEW.USR_IDALTEITEM := 'S';
      :NEW.USR_DTEXITAUTOFORN := NULL;
      :NEW.USR_QTEXPORTACAO := (NVL(:OLD.QTPROGENTAF,0) - NVL(:OLD.QTEFETENTRAF,0));
   END IF;
END;
/

CREATE OR REPLACE TRIGGER USR_EXPO_AUTOFORNE
AFTER UPDATE ON AUTOFORNE
FOR EACH ROW

BEGIN
   IF ((NVL(:OLD.IDBAIXAAF,'1') <> :NEW.IDBAIXAAF) AND (:NEW.IDBAIXAAF = '5')) THEN
      UPDATE ITEMAUTF
      SET USR_IDALTEITEM = 'S',
          USR_DTEXITAUTOFORN = NULL
      WHERE CDFILDEST = :NEW.CDFILDEST
        AND CDFILLANC = :NEW.CDFILLANC
        AND NRAUTOFORN = :NEW.NRAUTOFORN
        AND IDBAIXITMAF <> '3';
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_EXPO_AUTOFORNE','5.01.003'); END;
/

