create or replace 
PROCEDURE IMPORTA_ALT_AS
IS
   V_CDFILLANC  VARCHAR2(4);
   V_CDFILDEST  VARCHAR2(4);
   V_NRAUTOFORN VARCHAR2(10);
   V_DATAPROC   DATE;
   V_IDSTATUS   VARCHAR2(1);
BEGIN
   FOR C_AUTOFORNE IN
       (SELECT U.CDSITESAP, U.CDSITETECFOOD, U.ORDEN,
               U.IDINTERFACE, U.IDSTATUSAS
          FROM USR_SAP_ALT_AUTOFORNE U
         WHERE U.DTINTERFACEDEST IS NULL
           AND U.STATUS <> 2
         ORDER BY U.DTINTERFACEORI)
   LOOP
      BEGIN
         SELECT F.CDFILIAL, NVL(P.DTPROCESSA, SYSDATE)
           INTO V_CDFILDEST, V_DATAPROC
           FROM FILIAL F, PARAMFILIAL P
          WHERE F.CDFILIAL = P.CDFILIAL
            AND P.CDEXFILIAL = C_AUTOFORNE.CDSITESAP;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDFILDEST := NULL;
            V_DATAPROC  := NULL;
            
            INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
            VALUES(C_AUTOFORNE.IDINTERFACE, 'A', 2, SYSDATE, 1, 'Site SAP no registrado.');
            
            UPDATE USR_SAP_ALT_AUTOFORNE
               SET STATUS = 2
             WHERE CDSITESAP = C_AUTOFORNE.CDSITESAP;
      END;
      
      IF (V_CDFILDEST IS NOT NULL) THEN
         V_CDFILLANC  := C_AUTOFORNE.CDSITETECFOOD;
         V_NRAUTOFORN := C_AUTOFORNE.ORDEN;
         
         IF (C_AUTOFORNE.IDSTATUSAS = 'B') THEN
            UPDATE AUTOFORNE
               SET IDBAIXAAF  = '3'
             WHERE CDFILDEST  = V_CDFILDEST
               AND CDFILLANC  = V_CDFILLANC
               AND NRAUTOFORN = V_NRAUTOFORN;

            UPDATE ITEMAUTF
               SET IDBAIXITMAF = '3',
                   DTEFETENTAF = SYSDATE,
                   DTBAIXITMAF = V_DATAPROC
             WHERE CDFILDEST   = V_CDFILDEST
               AND CDFILLANC   = V_CDFILLANC
               AND NRAUTOFORN  = V_NRAUTOFORN;
               
            IF SQL%ROWCOUNT > 0 THEN
               INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
               VALUES(C_AUTOFORNE.IDINTERFACE, 'A', 2, SYSDATE, 0, 'AS actualizada con suceso.');
               
               UPDATE USR_SAP_ALT_AUTOFORNE 
                  SET DTINTERFACEDEST = SYSDATE, 
                      STATUS = 0
                WHERE IDINTERFACE = C_AUTOFORNE.IDINTERFACE;
            ELSE
               INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
               VALUES(C_AUTOFORNE.IDINTERFACE, 'A', 2, SYSDATE, 1, 'AS no existe.');
               
               UPDATE USR_SAP_ALT_AUTOFORNE 
                  SET STATUS = 2
                WHERE IDINTERFACE = C_AUTOFORNE.IDINTERFACE;
            END IF;
         ELSE
            BEGIN
               SELECT NVL(IDBAIXAAF, 'O') IDBAIXAAF
                 INTO V_IDSTATUS
                 FROM AUTOFORNE
                WHERE CDFILDEST  = V_CDFILDEST
                  AND CDFILLANC  = V_CDFILLANC
                  AND NRAUTOFORN = V_NRAUTOFORN
                  AND USR_DTEXPORTA IS NOT NULL;
            EXCEPTION
               WHEN OTHERS THEN
                  V_IDSTATUS := 'A';
            END;
            
            IF (V_IDSTATUS <> 'A') THEN
               IF (V_IDSTATUS <> '3') THEN
                  IF (C_AUTOFORNE.IDSTATUSAS = 'L') THEN
                     UPDATE AUTOFORNE
                        SET IDBAIXAAF  = '6'
                      WHERE CDFILDEST  = V_CDFILDEST
                        AND CDFILLANC  = V_CDFILLANC
                        AND NRAUTOFORN = V_NRAUTOFORN;
                  ELSE
                     UPDATE AUTOFORNE
                        SET IDBAIXAAF  = NULL
                      WHERE CDFILDEST  = V_CDFILDEST
                        AND CDFILLANC  = V_CDFILLANC
                        AND NRAUTOFORN = V_NRAUTOFORN;
                  END IF;
                  UPDATE USR_SAP_ALT_AUTOFORNE 
                     SET DTINTERFACEDEST = SYSDATE, 
                         STATUS = 0
                   WHERE IDINTERFACE = C_AUTOFORNE.IDINTERFACE;
               ELSE
                  INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
                  VALUES(C_AUTOFORNE.IDINTERFACE, 'A', 2, SYSDATE, 0, 'AS ya bajada, no es permitido cambio.');

                  UPDATE USR_SAP_ALT_AUTOFORNE 
                     SET STATUS = 2
                   WHERE IDINTERFACE = C_AUTOFORNE.IDINTERFACE;
               END IF;         
            ELSE
               INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
               VALUES(C_AUTOFORNE.IDINTERFACE, 'A', 2, SYSDATE, 0, 'AS debe ser exportada nuevamente o no existe.');
            END IF;
         END IF;
      END IF;
   END LOOP;
   
   COMMIT;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('IMPORTA_ALT_AS','6.05.xxx'); END;
/