create or replace 
PROCEDURE EXPORTA_LISTA_PRECIOS(P_CDFILIAL  IN VARCHAR2,
                                P_DTINICIAL IN DATE,
                                P_DTFINAL   IN DATE,
                                P_TCDFILIAL IN CHAR)
IS
   V_CDCHAVE    VARCHAR2(60);
   V_VERFILIAL  INTEGER;
   V_FILIAL     VARCHAR2(4);
   V_VDLISTAPR  BOOLEAN;
   V_SEQUENCIAL INTEGER;
   V_VALIDA     VARCHAR2(60);
   V_MENSSITE   VARCHAR2(50);
   V_MENSPROV   VARCHAR2(50);
   V_MENSPROD   VARCHAR2(50);
   --BOSITESAP    BOOLEAN;
   BOPROVSAP    BOOLEAN;
   --BOPRODSAP    BOOLEAN;
   V_FILATUA    VARCHAR2(4);
   V_NRCOLATUA  VARCHAR2(10);
   V_PARTICULA1 INTEGER;
   V_PARTICULA2 INTEGER;
   V_PARTICULA3 INTEGER;
   V_PARTICULA4 INTEGER;
   V_PARTICULA5 INTEGER;
   V_PARTICULA6 INTEGER;
   
   PROCEDURE VALIDA(P_CHAVE IN VARCHAR2, P_VALIDA IN VARCHAR2, P_BOPROVSAP IN BOOLEAN, 
                    /*P_BOSITESAP IN BOOLEAN, P_BOPRODSAP IN BOOLEAN,*/ P_SEQUENCIAL IN INTEGER, 
                    P_MENSSITE IN VARCHAR2, P_MENSPROV IN VARCHAR2, P_MENSPROD IN VARCHAR2,
                    P_BORETORNO IN OUT BOOLEAN, P_CDMARCA IN VARCHAR2)
   IS
      BORETORNO BOOLEAN;
      V_MARCA VARCHAR2(30);
   BEGIN
      IF (P_VALIDA = P_CHAVE) AND NOT(P_BORETORNO) THEN
         UPDATE COLETAPR
            SET USR_DTEXPORTA = NULL
          WHERE CDFILIAL    = SUBSTR(P_VALIDA, 1, 4)
            AND NRCOLEPRECO = SUBSTR(P_VALIDA, 5, 10);
      ELSE
         IF ((TRIM(P_VALIDA) IS NOT NULL) AND (P_BORETORNO)) THEN
            UPDATE COLETAPR
               SET USR_DTEXPORTA = SYSDATE
             WHERE CDFILIAL    = SUBSTR(P_VALIDA, 1, 4)
               AND NRCOLEPRECO = SUBSTR(P_VALIDA, 5, 10);
         END IF;
      END IF;
      
      IF (P_BOPROVSAP) /*(P_BOSITESAP) OR (P_BOPRODSAP)*/ THEN
         P_BORETORNO := FALSE;
         
         IF SQL%ROWCOUNT > 0 THEN
            ROLLBACK;
         END IF;
      ELSE
         P_BORETORNO := TRUE;
      END IF;
      
      IF (P_BOPROVSAP) THEN
         INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
         VALUES(P_SEQUENCIAL, 'I', 7, SYSDATE, 1, 'Proveedor '||P_MENSPROV||' no posee c'||CHR(243)||'digo SAP');
      END IF;

      /*IF (P_BOSITESAP) THEN
         INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
         VALUES(P_SEQUENCIAL, 'I', 7, SYSDATE, 1, 'Site SAP no registrado para el site TecFood '|| P_MENSSITE);
      END IF;
      
      IF (P_BOPRODSAP) THEN
         SELECT NMMARCA
           INTO V_MARCA
           FROM MARCA
          WHERE CDMARCA = P_CDMARCA;
         
         INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
         VALUES(P_SEQUENCIAL, 'I', 7, SYSDATE, 1, 'Producto '||P_MENSPROD||' no posee c'||CHR(243)||'digo SAP para la Marca '||P_CDMARCA||' - '||V_MARCA);
      END IF;*/
   END;
   
BEGIN
   V_VERFILIAL := LENGTH(REGEXP_REPLACE(TRIM(P_CDFILIAL), '\d'));
   V_VDLISTAPR := TRUE;
   V_VALIDA    := '   ';
   
   IF ((V_VERFILIAL > 2) OR (P_TCDFILIAL = 'T')) THEN     
      FOR C_LISTAPRECO IN
          (SELECT I.CDFILIAL, I.NRCOLEPRECO, I.CDMARCA, I.CDREGIAO, F.NRINSJURFORN NIF_PROVEE, P.CDPRODUTO,
                  I.VRPRECUNIITM, I.DTINIVALITM, I.DTFINVALITM, M.CDPRODSAP, F.CDEXFORNECED CDPROVEESAP,
                  PF.CDEXFILIAL CDSITESAP, P.SGUNIDADE UOMSAP, P.NMPRODUTO, F.CDFORNECED, 
                  M.UOMSAPALT, P.CDARVPROD
             FROM ITEMCOLE I, PRODUTO P, MARCPROD M, FORNECEDOR F, PARAMFILIAL PF, COLETAPR C
            WHERE I.CDPRODUTO    = P.CDPRODUTO
              AND M.CDPRODUTO    = P.CDPRODUTO
              AND I.CDMARCA      = M.CDMARCA
              AND I.CDFORNECED   = F.CDFORNECED
              AND I.CDFILIAL     = PF.CDFILIAL
              AND C.CDFILIAL     = I.CDFILIAL
              AND C.NRCOLEPRECO  = I.NRCOLEPRECO
              AND M.CDPRODSAP IS NOT NULL
              AND I.IDITMLISPREC = 'S'
              AND ((INSTR(P_CDFILIAL, '_'||I.CDFILIAL||'_') > 0) OR (P_TCDFILIAL = 'T'))
              AND I.DTINIVALITM BETWEEN P_DTINICIAL AND P_DTFINAL
              AND C.USR_DTEXPORTA IS NULL
            ORDER BY C.CDFILIAL, C.NRCOLEPRECO)
      LOOP
         V_CDCHAVE := C_LISTAPRECO.CDFILIAL||C_LISTAPRECO.NRCOLEPRECO||C_LISTAPRECO.CDMARCA||C_LISTAPRECO.CDREGIAO||C_LISTAPRECO.CDFORNECED||C_LISTAPRECO.CDPRODUTO;
         --BOSITESAP := FALSE;
         BOPROVSAP := FALSE;
         --BOPRODSAP := FALSE;
         
         SELECT USR_SAP_IDINTERFACE.NEXTVAL
           INTO V_SEQUENCIAL
           FROM DUAL;
         
         IF ((TRIM(V_VALIDA) IS NOT NULL) AND (SUBSTR(V_CDCHAVE, 1, 14) <> V_VALIDA)) THEN
            V_VDLISTAPR := TRUE;
            COMMIT;
         END IF;
         
         IF (C_LISTAPRECO.CDPROVEESAP IS NULL) THEN
            BOPROVSAP  := TRUE;
            V_MENSPROV := C_LISTAPRECO.NIF_PROVEE;
         END IF;
         
         /*IF (C_LISTAPRECO.CDSITESAP IS NULL) THEN
            BOSITESAP  := TRUE;
            V_MENSSITE := C_LISTAPRECO.CDFILIAL;
         END IF;
         
         IF (C_LISTAPRECO.CDPRODSAP IS NULL) THEN
            BOPRODSAP  := TRUE;
            V_MENSPROD := C_LISTAPRECO.NMPRODUTO;
         END IF;*/
         
         VALIDA(V_CDCHAVE, V_VALIDA, BOPROVSAP, /*BOSITESAP, BOPRODSAP,*/ V_SEQUENCIAL, V_MENSSITE, V_MENSPROV, V_MENSPROD,
                V_VDLISTAPR, C_LISTAPRECO.CDMARCA);
         
         IF (V_VDLISTAPR) THEN   
            FOR C_REGIAO_FILIAL IN
               (SELECT PF.CDEXFILIAL CDSITESAP, F.CDFILIAL, F.NMFILIAL
                  FROM FILIAL F, PARAMFILIAL PF
                 WHERE F.CDFILIAL = PF.CDFILIAL
                   AND F.CDREGIAO = C_LISTAPRECO.CDREGIAO)
            LOOP
               IF (C_REGIAO_FILIAL.CDSITESAP IS NOT NULL) THEN
                  V_PARTICULA1 := 0;
                  V_PARTICULA2 := 0;
                  V_PARTICULA3 := 0;
                  V_PARTICULA4 := 0;
                  V_PARTICULA5 := 0;
                  V_PARTICULA6 := 0;
                  SELECT COUNT(1)
                    INTO V_PARTICULA1
                    FROM REFORFIL
                   WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDFORNECED = C_LISTAPRECO.CDFORNECED)
                     AND (CDPERIENTR IS NULL);
                  
                  SELECT COUNT(1)
                    INTO V_PARTICULA2
                    FROM REFOREG
                   WHERE (REFOREG.CDREGIAO   = C_LISTAPRECO.CDREGIAO)
                     AND (REFOREG.CDFORNECED = C_LISTAPRECO.CDFORNECED);
                  
                  SELECT COUNT(1)
                    INTO V_PARTICULA3
                    FROM (SELECT CDFILIAL
                              FROM PARTFILI
                             WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                               AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                               AND (CDTIPPART IN ('1', '2'))
                               AND (CDMARCA   = C_LISTAPRECO.CDMARCA)
                               AND (NOT EXISTS (SELECT 1
                                                  FROM PARTFILI
                                                 WHERE (CDFILIAL    = C_REGIAO_FILIAL.CDFILIAL)
                                                   AND (CDPRODUTO   = C_LISTAPRECO.CDPRODUTO)
                                                   AND (CDTIPPART   IN ('4', '5'))
                                                   AND (NRCOLEPRECO IS NULL)
                                                   AND (CDFORNECED  <> C_LISTAPRECO.CDFORNECED)))
                                OR (EXISTS (SELECT 1
                                              FROM PARTFILI
                                             WHERE (CDFILIAL    = C_REGIAO_FILIAL.CDFILIAL)
                                               AND (CDPRODUTO   = C_LISTAPRECO.CDPRODUTO)
                                               AND (CDTIPPART   IN ('4', '5'))
                                               AND (NRCOLEPRECO IS NULL)
                                               AND (CDFORNECED  = C_LISTAPRECO.CDFORNECED)
                                               AND (NOT EXISTS (SELECT 1
                                                                  FROM PARTFILI
                                                                 WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                                                   AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                                                                   AND (CDTIPPART IN ('1', '2'))
                                                                   AND (CDMARCA   <> C_LISTAPRECO.CDMARCA))
                                                    )
                                            )
                                    )
                                OR (EXISTS (SELECT 1
                                              FROM PARTFILI
                                             WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                                               AND (CDPRODUTO  = C_LISTAPRECO.CDPRODUTO)
                                               AND (CDTIPPART  IN ('7', '8'))
                                               AND (CDMARCA    = C_LISTAPRECO.CDMARCA)
                                               AND (CDFORNECED = C_LISTAPRECO.CDFORNECED))
                                    )
                                OR (EXISTS (SELECT 1
                                              FROM PARTFILI
                                             WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                               AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                                               AND (CDTIPPART  IN ('3','6','9')))
                                               AND NOT EXISTS (SELECT 1
                                                                 FROM PARTFILI
                                                                WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                                                  AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                                                                  AND (NRCOLEPRECO IS NULL)
                                                                  AND (CDTIPPART  IN ('1','2','4','5','7','8')))
                                    )
                                OR (NOT EXISTS (SELECT 1
                                                  FROM PARTFILI
                                                 WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                                   AND (NRCOLEPRECO IS NULL)
                                                   AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO))
                                    )
                              );

                  SELECT COUNT(1)
                    INTO V_PARTICULA4
                    FROM PARTFILI
                   WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                     AND (CDTIPPART = '3')
                     AND (CDMARCA   = C_LISTAPRECO.CDMARCA);

                  SELECT COUNT(1)
                    INTO V_PARTICULA5
                    FROM PARTFILI
                   WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDPRODUTO  = C_LISTAPRECO.CDPRODUTO)
                     AND (CDTIPPART  = '6')
                     AND (CDFORNECED = C_LISTAPRECO.CDMARCA);
                  
                  SELECT COUNT(1)
                    INTO V_PARTICULA6
                    FROM PARTFILI
                   WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDPRODUTO  = C_LISTAPRECO.CDPRODUTO)
                     AND (CDTIPPART  = '9')
                     AND (CDMARCA    = C_LISTAPRECO.CDMARCA)
                     AND (CDFORNECED = C_LISTAPRECO.CDFORNECED);
                  
                  IF (V_PARTICULA1+V_PARTICULA2+V_PARTICULA4+V_PARTICULA5+V_PARTICULA6) = 0 THEN 
                     IF (V_PARTICULA3 > 0) THEN
                         INSERT INTO USR_SAP_LISTPRECO(CDCHAVE, CDSITESAP, CDPRODSAP, UOMSAP, NIF_PROVEE, 
                                                       CDPROVEESAP, VRUNITARIO, DTINIVIG, DTFINVIG,
                                                       IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO,
                                                       UOMSAPALT, CDARVPROD)
                         VALUES(V_CDCHAVE, C_REGIAO_FILIAL.CDSITESAP, C_LISTAPRECO.CDPRODSAP, C_LISTAPRECO.UOMSAP, C_LISTAPRECO.NIF_PROVEE,
                                C_LISTAPRECO.CDPROVEESAP, C_LISTAPRECO.VRPRECUNIITM, C_LISTAPRECO.DTINIVALITM, C_LISTAPRECO.DTFINVALITM,
                                V_SEQUENCIAL, 'I', SYSDATE, NULL, 1, 1, 2,
                                C_LISTAPRECO.UOMSAPALT, C_LISTAPRECO.CDARVPROD);
                                
                         IF V_VALIDA <> SUBSTR(V_CDCHAVE, 1, 14) THEN
                            V_VALIDA := SUBSTR(V_CDCHAVE, 1, 14);
                         END IF;
                         
                         V_FILATUA   := SUBSTR(V_VALIDA, 1, 4);
                         V_NRCOLATUA := SUBSTR(V_VALIDA, 5, 10);
                         
                         INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
                         VALUES(V_SEQUENCIAL, 'I', 7, SYSDATE, 0, 'Lista de Precios exportada con '||CHR(233)||'xito');
                      END IF;
                  END IF;
               ELSE
                  INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
                  VALUES(V_SEQUENCIAL, 'I', 7, SYSDATE, 1, 'Site SAP no registrado para el site TecFood '||C_REGIAO_FILIAL.CDFILIAL||' - '||C_REGIAO_FILIAL.NMFILIAL);
               END IF;
               
               SELECT USR_SAP_IDINTERFACE.NEXTVAL
                 INTO V_SEQUENCIAL
                 FROM DUAL;
            END LOOP;
         END IF;
      END LOOP;
   ELSE
      V_FILIAL := REGEXP_REPLACE(P_CDFILIAL, '\D');
      
      FOR C_LISTAPRECO IN
          (SELECT I.CDFILIAL, I.NRCOLEPRECO, I.CDMARCA, I.CDREGIAO, F.NRINSJURFORN NIF_PROVEE, P.CDPRODUTO,
                  I.VRPRECUNIITM, I.DTINIVALITM, I.DTFINVALITM, M.CDPRODSAP, F.CDEXFORNECED CDPROVEESAP,
                  PF.CDEXFILIAL CDSITESAP, P.SGUNIDADE UOMSAP, P.NMPRODUTO, F.CDFORNECED,
                  M.UOMSAPALT, P.CDARVPROD
             FROM ITEMCOLE I, PRODUTO P, MARCPROD M, FORNECEDOR F, PARAMFILIAL PF, COLETAPR C
            WHERE I.CDPRODUTO    = P.CDPRODUTO
              AND M.CDPRODUTO    = P.CDPRODUTO
              AND I.CDMARCA      = M.CDMARCA
              AND I.CDFORNECED   = F.CDFORNECED
              AND I.CDFILIAL     = PF.CDFILIAL
              AND C.CDFILIAL     = I.CDFILIAL
              AND C.NRCOLEPRECO  = I.NRCOLEPRECO
              AND M.CDPRODSAP IS NOT NULL
              AND I.IDITMLISPREC = 'S'
              AND I.CDFILIAL     = V_FILIAL
              AND I.DTINIVALITM BETWEEN P_DTINICIAL AND P_DTFINAL
              AND C.USR_DTEXPORTA IS NULL
            ORDER BY C.CDFILIAL, C.NRCOLEPRECO)
      LOOP
         V_CDCHAVE := C_LISTAPRECO.CDFILIAL||C_LISTAPRECO.NRCOLEPRECO||C_LISTAPRECO.CDMARCA||C_LISTAPRECO.CDREGIAO||C_LISTAPRECO.CDFORNECED||C_LISTAPRECO.CDPRODUTO;
         --BOSITESAP := FALSE;
         BOPROVSAP := FALSE;
         --BOPRODSAP := FALSE;
         
         SELECT USR_SAP_IDINTERFACE.NEXTVAL
           INTO V_SEQUENCIAL
           FROM DUAL;
         
         IF ((TRIM(V_VALIDA) IS NOT NULL) AND (SUBSTR(V_CDCHAVE, 1, 14) <> V_VALIDA)) THEN
            V_VDLISTAPR := TRUE;
            COMMIT;
         END IF;
         
         IF (C_LISTAPRECO.CDPROVEESAP IS NULL) THEN
            BOPROVSAP  := TRUE;
            V_MENSPROV := C_LISTAPRECO.NIF_PROVEE;
         END IF;
         
         /*IF (C_LISTAPRECO.CDSITESAP IS NULL) THEN
            BOSITESAP  := TRUE;
            V_MENSSITE := C_LISTAPRECO.CDFILIAL;
         END IF;
         
         IF (C_LISTAPRECO.CDPRODSAP IS NULL) THEN
            BOPRODSAP  := TRUE;
            V_MENSPROD := C_LISTAPRECO.NMPRODUTO;
         END IF;*/
         
         VALIDA(V_CDCHAVE, V_VALIDA, BOPROVSAP, /*BOSITESAP, BOPRODSAP,*/V_SEQUENCIAL, V_MENSSITE, V_MENSPROV, V_MENSPROD,
                V_VDLISTAPR, C_LISTAPRECO.CDMARCA);
         
         IF (V_VDLISTAPR) THEN   
            FOR C_REGIAO_FILIAL IN
               (SELECT PF.CDEXFILIAL CDSITESAP, F.CDFILIAL, F.NMFILIAL
                  FROM FILIAL F, PARAMFILIAL PF
                 WHERE F.CDFILIAL = PF.CDFILIAL
                   AND F.CDREGIAO = C_LISTAPRECO.CDREGIAO)
            LOOP
               IF (C_REGIAO_FILIAL.CDSITESAP IS NOT NULL) THEN
                  V_PARTICULA1 := 0;
                  V_PARTICULA2 := 0;
                  V_PARTICULA3 := 0;
                  V_PARTICULA4 := 0;
                  V_PARTICULA5 := 0;
                  V_PARTICULA6 := 0;
                  SELECT COUNT(1)
                    INTO V_PARTICULA1
                    FROM REFORFIL
                   WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDFORNECED = C_LISTAPRECO.CDFORNECED)
                     AND (CDPERIENTR IS NULL);
                  
                  SELECT COUNT(1)
                    INTO V_PARTICULA2
                    FROM REFOREG
                   WHERE (REFOREG.CDREGIAO   = C_LISTAPRECO.CDREGIAO)
                     AND (REFOREG.CDFORNECED = C_LISTAPRECO.CDFORNECED);
                  
                  SELECT COUNT(1)
                    INTO V_PARTICULA3
                    FROM (SELECT CDFILIAL
                              FROM PARTFILI
                             WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                               AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                               AND (CDTIPPART IN ('1', '2'))
                               AND (CDMARCA   = C_LISTAPRECO.CDMARCA)
                               AND (NOT EXISTS (SELECT 1
                                                  FROM PARTFILI
                                                 WHERE (CDFILIAL    = C_REGIAO_FILIAL.CDFILIAL)
                                                   AND (CDPRODUTO   = C_LISTAPRECO.CDPRODUTO)
                                                   AND (CDTIPPART   IN ('4', '5'))
                                                   AND (NRCOLEPRECO IS NULL)
                                                   AND (CDFORNECED  <> C_LISTAPRECO.CDFORNECED)))
                                OR (EXISTS (SELECT 1
                                              FROM PARTFILI
                                             WHERE (CDFILIAL    = C_REGIAO_FILIAL.CDFILIAL)
                                               AND (CDPRODUTO   = C_LISTAPRECO.CDPRODUTO)
                                               AND (CDTIPPART   IN ('4', '5'))
                                               AND (NRCOLEPRECO IS NULL)
                                               AND (CDFORNECED  = C_LISTAPRECO.CDFORNECED)
                                               AND (NOT EXISTS (SELECT 1
                                                                  FROM PARTFILI
                                                                 WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                                                   AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                                                                   AND (CDTIPPART IN ('1', '2'))
                                                                   AND (CDMARCA   <> C_LISTAPRECO.CDMARCA))
                                                    )
                                            )
                                    )
                                OR (EXISTS (SELECT 1
                                              FROM PARTFILI
                                             WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                                               AND (CDPRODUTO  = C_LISTAPRECO.CDPRODUTO)
                                               AND (CDTIPPART  IN ('7', '8'))
                                               AND (CDMARCA    = C_LISTAPRECO.CDMARCA)
                                               AND (CDFORNECED = C_LISTAPRECO.CDFORNECED))
                                    )
                                OR (EXISTS (SELECT 1
                                              FROM PARTFILI
                                             WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                               AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                                               AND (CDTIPPART  IN ('3','6','9')))
                                               AND NOT EXISTS (SELECT 1
                                                                 FROM PARTFILI
                                                                WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                                                  AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                                                                  AND (NRCOLEPRECO IS NULL)
                                                                  AND (CDTIPPART  IN ('1','2','4','5','7','8')))
                                    )
                                OR (NOT EXISTS (SELECT 1
                                                  FROM PARTFILI
                                                 WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                                                   AND (NRCOLEPRECO IS NULL)
                                                   AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO))
                                    )
                              );

                  SELECT COUNT(1)
                    INTO V_PARTICULA4
                    FROM PARTFILI
                   WHERE (CDFILIAL  = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDPRODUTO = C_LISTAPRECO.CDPRODUTO)
                     AND (CDTIPPART = '3')
                     AND (CDMARCA   = C_LISTAPRECO.CDMARCA);

                  SELECT COUNT(1)
                    INTO V_PARTICULA5
                    FROM PARTFILI
                   WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDPRODUTO  = C_LISTAPRECO.CDPRODUTO)
                     AND (CDTIPPART  = '6')
                     AND (CDFORNECED = C_LISTAPRECO.CDMARCA);
                  
                  SELECT COUNT(1)
                    INTO V_PARTICULA6
                    FROM PARTFILI
                   WHERE (CDFILIAL   = C_REGIAO_FILIAL.CDFILIAL)
                     AND (CDPRODUTO  = C_LISTAPRECO.CDPRODUTO)
                     AND (CDTIPPART  = '9')
                     AND (CDMARCA    = C_LISTAPRECO.CDMARCA)
                     AND (CDFORNECED = C_LISTAPRECO.CDFORNECED);
                  
                  IF (V_PARTICULA1+V_PARTICULA2+V_PARTICULA4+V_PARTICULA5+V_PARTICULA6) = 0 THEN 
                     IF (V_PARTICULA3 > 0) THEN
                         INSERT INTO USR_SAP_LISTPRECO(CDCHAVE, CDSITESAP, CDPRODSAP, UOMSAP, NIF_PROVEE, 
                                                       CDPROVEESAP, VRUNITARIO, DTINIVIG, DTFINVIG,
                                                       IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO,
                                                       UOMSAPALT, CDARVPROD)
                         VALUES(V_CDCHAVE, C_LISTAPRECO.CDSITESAP, C_LISTAPRECO.CDPRODSAP, C_LISTAPRECO.UOMSAP, C_LISTAPRECO.NIF_PROVEE,
                                C_LISTAPRECO.CDPROVEESAP, C_LISTAPRECO.VRPRECUNIITM, C_LISTAPRECO.DTINIVALITM, C_LISTAPRECO.DTFINVALITM,
                                V_SEQUENCIAL, 'I', SYSDATE, NULL, 1, 1, 2,
                                C_LISTAPRECO.UOMSAPALT, C_LISTAPRECO.CDARVPROD);
                                
                         IF V_VALIDA <> SUBSTR(V_CDCHAVE, 1, 14) THEN
                            V_VALIDA := SUBSTR(V_CDCHAVE, 1, 14);
                         END IF;
                         V_FILATUA   := SUBSTR(V_VALIDA, 1, 4);
                         V_NRCOLATUA := SUBSTR(V_VALIDA, 5, 10);
                         
                         INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
                         VALUES(V_SEQUENCIAL, 'I', 7, SYSDATE, 0, 'Lista de Precios exportada con '||CHR(233)||'xito');
                      END IF;
                  END IF;
               ELSE
                  INSERT INTO USR_SAP_LOG(IDINTERFACE, IDSEQLOG, IDPROCESSO, DTLOG, STATUS, OBSSTATUS)
                  VALUES(V_SEQUENCIAL, 'I', 7, SYSDATE, 1, 'Site SAP no registrado para el site TecFood '||C_REGIAO_FILIAL.CDFILIAL||' - '||C_REGIAO_FILIAL.NMFILIAL);
               END IF;
               
               SELECT USR_SAP_IDINTERFACE.NEXTVAL
                 INTO V_SEQUENCIAL
                 FROM DUAL;
            END LOOP;
         END IF;
      END LOOP;
   END IF;
   
   UPDATE COLETAPR
      SET USR_DTEXPORTA = SYSDATE
    WHERE CDFILIAL    = V_FILATUA
      AND NRCOLEPRECO = V_NRCOLATUA;
   
   COMMIT;
END;
/

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