CREATE OR REPLACE TRIGGER USR_ALT_IDBAIXAAF_EDI
AFTER UPDATE OF QTPROGENTAF
ON ITEMAUTF FOR EACH ROW
DECLARE
   V_CDFORNECED   FORNECEDOR.CDFORNECED%TYPE;
   V_IDTPENVAFFOR FORNECEDOR.IDTPENVAFFOR%TYPE;
   V_IDBAIXAAF    AUTOFORNE.IDBAIXAAF%TYPE;
BEGIN
   IF :NEW.QTPROGENTAF > 0 THEN
      SELECT MAX(CDFORNECED), NVL(MAX(IDBAIXAAF), 'X')
        INTO V_CDFORNECED, V_IDBAIXAAF
        FROM AUTOFORNE
       WHERE AUTOFORNE.CDFILLANC  = :OLD.CDFILLANC
         AND AUTOFORNE.CDFILDEST  = :OLD.CDFILDEST
         AND AUTOFORNE.NRAUTOFORN = :OLD.NRAUTOFORN;
      IF V_IDBAIXAAF = 'X' THEN
         SELECT IDTPENVAFFOR
           INTO V_IDTPENVAFFOR
           FROM FORNECEDOR
          WHERE CDFORNECED = V_CDFORNECED;

         IF V_IDTPENVAFFOR = '2' THEN
            UPDATE AUTOFORNE
               SET IDBAIXAAF = '7'
             WHERE AUTOFORNE.CDFILLANC  = :OLD.CDFILLANC
               AND AUTOFORNE.CDFILDEST  = :OLD.CDFILDEST
               AND AUTOFORNE.NRAUTOFORN = :OLD.NRAUTOFORN;
         END IF;
      END IF;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_ALT_IDBAIXAAF_EDI','5.40.XXX'); END;
/

