CREATE OR REPLACE
PROCEDURE LISPREC_UPPRODFILI
  (P_CDFILIAL     IN ITMPREVLIST.CDFILIAL%TYPE,
   P_NRSEQPRELIST IN ITMPREVLIST.NRSEQPRELIST%TYPE,
   P_ATUCUSTO     IN VARCHAR2 DEFAULT 'N',
   P_CDFILCUSTO   IN FILIAL.CDFILIAL%TYPE DEFAULT NULL,
   P_CDLISTAREG   IN VARCHAR2 DEFAULT NULL,
   P_CDPRODUTO    IN VARCHAR2 DEFAULT NULL) IS
                                                                                                   
   V_IDUTCUSTOM1  VARCHAR(1);
   V_IDUTCUSTOM2  VARCHAR(1);
   V_IDUTCUSLIPR  VARCHAR(1);

   V_VRCUSPRLISM1 FLOAT;
   V_VRCUSPRLISM2 FLOAT;

   TYPE T_INDICE IS TABLE OF FLOAT INDEX BY VARCHAR(8);

   V_VRINDICEM1   T_INDICE;
   V_VRINDICEM2   T_INDICE;

   V_POSMINPRECO  INT;
   V_POSMINPREPI  INT;

   V_MINPRECO     FLOAT;
   V_VRIMPITM     FLOAT;
   V_NRCOLEPRECO  ITEMCOLE.NRCOLEPRECO%TYPE;
   V_CDREGIAO     ITEMCOLE.CDREGIAO%TYPE;
   V_CDFORNECED   ITEMCOLE.CDFORNECED%TYPE;
   V_CDPRODUTO    ITEMCOLE.CDPRODUTO%TYPE;
   V_CDMARCA      ITEMCOLE.CDMARCA%TYPE;

   V_IDTIPOAPUR   PARASUPRI.IDTIPOAPUR%TYPE;

   V_DTINIVALLIST PREVLISTA.DTINIVALLIST%TYPE;
   V_DTFINVALLIST PREVLISTA.DTFINVALLIST%TYPE;
   V_CDIMPOSTRIB  VARCHAR2(2);
   V_IDVRIMPSTRIB CHAR(1);

   V_MULTIPLO     INT;
   V_MASCARA      VARCHAR2(255);

   V_VRFATOCONV   FLOAT;

   V_LISTA_FILIAL VARCHAR2(20); /* NUMERO DA PRXLISTA SOBRE FILIAL */


   V_ULTIMAREGIAO  REGIAO.CDREGIAO%TYPE;
   V_ULTIMAFILIAL  FILIAL.CDFILIAL%TYPE;
   V_ULTIMOPRODUTO PRODUTO.CDPRODUTO%TYPE;

   V_QTFILIALLISPREC FLOAT;
   V_QTREGIAOLISPREC FLOAT;
   V_QTPRODLISPREC   FLOAT;

   V_AUX_REGIAOGERAL FLOAT;
   V_AUX_FILIAL      FLOAT;
   V_AUX_CUSTO       FLOAT;

   V_LISTATEMPORARIA ITEMCOLE.NRSEQPRELIST%TYPE;

   TYPE CURTYPE               IS REF CURSOR;
   C_REGIAOGERAL              CURTYPE;
   C_FILIAL                   CURTYPE;
   C_CUSTO                    CURTYPE;
   V_REGIAOGERAL_CDREGIAO     REGIAO.CDREGIAO%TYPE;
   V_FILIAL_CDFILIAL          FILIAL.CDFILIAL%TYPE;
   V_CUSTOSUP_TMP_CDFILIAL    FILIAL.CDFILIAL%TYPE;
   V_CUSTOSUP_TMP_DTINIVALITM ITEMCOLE.DTINIVALITM%TYPE;
   V_CUSTOSUP_TMP_DTFINVALITM ITEMCOLE.DTFINVALITM%TYPE;
   V_CUSTOSUP_TMP_CDPRODESTO  PRODUTO.CDPRODESTO%TYPE;
   V_CUSTOSUP_TMP_MINPRECO    VARCHAR2(200);

   V_PRODUTO_ANTERIOR         PRODUTO.CDPRODUTO%TYPE;
   V_COTACAO_ANTERIOR         VARCHAR2(200);
   V_FILIAL_ANTERIOR          FILIAL.CDFILIAL%TYPE;
   V_COUNT                    FLOAT;

   V_VRPERAJFORNFIL           AJUSTEPRFORNFIL.VRPERAJFORNFIL%TYPE;
   V_IDCONSPERPIAJPR          AJUSTEPRFORNFIL.IDCONSPERPIAJPR%TYPE;
   V_PIDSV                    FORNECEDOR.VRPERPIFORN%TYPE;

   V_CDOPERADOR               OPERADOR.CDOPERADOR%TYPE;
   V_IDCUSSUPFILIVEN          PARAMGERALVEN.IDCUSSUPFILIVEN%TYPE;
BEGIN
   V_MASCARA    := '00000000000000000000000';
   V_MULTIPLO   := 1000000;
   V_VRFATOCONV := 1;

   SELECT
      MAX(IDTIPOAPUR) INTO V_IDTIPOAPUR
   FROM
      PARASUPRI
   WHERE
      CDFILIAL = P_CDFILIAL;

   SELECT
      NVL(MAX(IDUTTPCUSTO), 'N')
   INTO
      V_IDUTCUSTOM1
   FROM
      TIPOCUSTO
   WHERE
      CDTIPOCUSTO = '30';

   SELECT
      NVL(MAX(IDUTTPCUSTO), 'N')
   INTO
      V_IDUTCUSTOM2
   FROM
      TIPOCUSTO
   WHERE
      CDTIPOCUSTO = '31';

   SELECT
      NVL(MAX(IDUTTPCUSTO), 'N')
   INTO
      V_IDUTCUSLIPR
   FROM
      TIPOCUSTO
   WHERE
      CDTIPOCUSTO = '10';

   SELECT MAX(IDCUSSUPFILIVEN)
     INTO V_IDCUSSUPFILIVEN
     FROM PARAMGERALVEN;
   
   V_IDCUSSUPFILIVEN := NVL(V_IDCUSSUPFILIVEN, 'N');

   SELECT
      MAX(DTINIVALLIST),
      MAX(DTFINVALLIST),
      MAX(CDOPERADOR)
   INTO
       V_DTINIVALLIST,
       V_DTFINVALLIST,
       V_CDOPERADOR
   FROM PREVLISTA
   WHERE CDFILIAL = P_CDFILIAL
     AND NRSEQPRELIST = P_NRSEQPRELIST;

   IF (V_IDUTCUSLIPR = 'N') THEN
      SELECT
         NVL(MAX(IDUTTPCUSTO), 'N')
      INTO
         V_IDUTCUSLIPR
      FROM
         TIPOCUSTO
      WHERE
         CDTIPOCUSTO = '48';
   END IF;

   NOVO_CODIGO('LISTA', V_LISTA_FILIAL, 1);
   V_LISTA_FILIAL := SUBSTR(V_LISTA_FILIAL, 11, 10);

   IF P_CDFILCUSTO IS NULL THEN
     IF P_CDLISTAREG IS NULL THEN
        FOR R1 IN (WITH FILIAIS AS
          (SELECT
              DISTINCT C.CDFILIAL, C.IDFUNCAOFILI
           FROM
              FILIAL C
           WHERE
              P_CDFILIAL = NVL(C.CDFILCENTCMP, C.CDFILIAL) AND
              NVL(C.IDCNTRRESC, 'N') = 'N')
           SELECT
              V_LISTA_FILIAL,
              F.CDFILIAL
           FROM
              FILIAIS F
           WHERE
              (V_IDCUSSUPFILIVEN = 'S' OR 
              EXISTS (SELECT
                         1
                       FROM
                         CARDADIA C
                      WHERE
                         C.CDFILIAL = F.CDFILIAL
                      UNION ALL
                      SELECT
                         1
                       FROM
                         CONSSERV CO
                      WHERE
                         CO.CDFILIAL = F.CDFILIAL
                      UNION ALL
                      SELECT
                         1
                       FROM
                         FILIAL FI
                      WHERE
                         FI.CDFILIAL     = F.CDFILIAL
                         AND 'O' = FI.IDFUNCAOFILI
                      UNION ALL
                      SELECT
                         1
                       FROM
                         CARDIAPME CPME
                      WHERE
                         CPME.CDFILIAL = F.CDFILIAL
                      UNION ALL
                      SELECT
                         1
                       FROM
                         CONSSERVPME COPME
                      WHERE
                         COPME.CDFILIAL = F.CDFILIAL))
           ) LOOP
           INSERT INTO LISTA
              (NRLISTA, CDLISTA)
           VALUES
              (V_LISTA_FILIAL, R1.CDFILIAL);
        END LOOP;
     ELSE
        FOR R1 IN (WITH FILIAIS AS
          (SELECT
              DISTINCT C.CDFILIAL, C.IDFUNCAOFILI
           FROM
              FILIAL C,
              LISTA LISTAREG
           WHERE
              P_CDFILIAL = NVL(C.CDFILCENTCMP, C.CDFILIAL) AND
              NVL(C.IDCNTRRESC, 'N') = 'N' AND
              LISTAREG.NRLISTA = P_CDLISTAREG AND
              LISTAREG.CDLISTA = C.CDREGIAO )
           SELECT
              V_LISTA_FILIAL,
              F.CDFILIAL
           FROM
              FILIAIS F
           WHERE
              EXISTS (SELECT
                         1
                      FROM
                         CARDADIA C
                      WHERE
                         C.CDFILIAL = F.CDFILIAL
                      UNION ALL
                      SELECT
                         1
                       FROM
                         CONSSERV CO
                      WHERE
                         CO.CDFILIAL = F.CDFILIAL
                      UNION ALL 
                      SELECT 
                         1 
                       FROM 
                         FILIAL FI 
                      WHERE 
                         FI.CDFILIAL     = F.CDFILIAL
                         AND 'O' = FI.IDFUNCAOFILI
                      UNION ALL
                      SELECT
                         1
                      FROM
                         CARDIAPME CPME
                      WHERE
                         CPME.CDFILIAL = F.CDFILIAL
                      UNION ALL
                      SELECT
                         1
                       FROM
                         CONSSERVPME COPME
                      WHERE
                         COPME.CDFILIAL = F.CDFILIAL)
           ) LOOP
           INSERT INTO LISTA
              (NRLISTA, CDLISTA)
           VALUES
              (V_LISTA_FILIAL, R1.CDFILIAL);
        END LOOP;
     END IF;
   ELSE
      INSERT INTO LISTA
         (NRLISTA,
          CDLISTA)
      VALUES
         (V_LISTA_FILIAL,
          P_CDFILCUSTO);
   END IF;
   IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
      SELECT
         MAX(NRSEQULTLISPREC),
         MAX(CDREGULTLISPREC),
         MAX(CDFILULTLISPREC),
         MAX(CDPRODULLISPREC)
      INTO
         V_LISTATEMPORARIA,
         V_ULTIMAREGIAO,
         V_ULTIMAFILIAL,
         V_ULTIMOPRODUTO
      FROM TEMPULTATULISPR
      WHERE CDCENULTLISPREC = P_CDFILIAL
        AND NRSEQULTLISPREC = P_NRSEQPRELIST;

      IF V_LISTATEMPORARIA IS NULL THEN
         INSERT INTO TEMPULTATULISPR (CDCENULTLISPREC,NRSEQULTLISPREC)
         VALUES (P_CDFILIAL, P_NRSEQPRELIST);
         COMMIT;
      END IF;
   ELSE
      V_LISTATEMPORARIA := NULL;
      V_ULTIMAREGIAO    := NULL;
      V_ULTIMAFILIAL    := NULL;
      V_ULTIMOPRODUTO   := NULL;
   END IF;

   V_QTREGIAOLISPREC := NULL;
   OPEN C_REGIAOGERAL FOR
      SELECT A.CDREGIAO, ROWNUM
      FROM
      (SELECT CDREGIAO
      FROM REGIAO
      WHERE ((REGIAO.CDREGIAO < V_ULTIMAREGIAO) OR (V_ULTIMAREGIAO IS NULL))
        AND EXISTS (SELECT 1
                    FROM FILIAL,
                         LISTA
                          WHERE NRLISTA = V_LISTA_FILIAL
                            AND FILIAL.CDFILIAL = LISTA.CDLISTA
                            AND FILIAL.CDREGIAO = REGIAO.CDREGIAO
                          )
      ORDER BY CDREGIAO
      ) A
      ORDER BY ROWNUM DESC;
   LOOP
      FETCH C_REGIAOGERAL INTO V_REGIAOGERAL_CDREGIAO, V_AUX_REGIAOGERAL;
      EXIT WHEN C_REGIAOGERAL%NOTFOUND;
      V_QTFILIALLISPREC := NULL;
      IF V_QTREGIAOLISPREC IS NULL THEN
         V_QTREGIAOLISPREC := V_AUX_REGIAOGERAL;
         IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
            UPDATE TEMPULTATULISPR
            SET QTREGIAOLISPREC = V_QTREGIAOLISPREC
            WHERE CDCENULTLISPREC = P_CDFILIAL
              AND NRSEQULTLISPREC = P_NRSEQPRELIST;
            COMMIT;
         END IF;
      END IF;
      OPEN C_FILIAL FOR
         SELECT A.CDFILIAL, ROWNUM
         FROM
         (SELECT FILIAL.CDFILIAL
          FROM FILIAL,
              LISTA
          WHERE NRLISTA = V_LISTA_FILIAL
           AND FILIAL.CDFILIAL = LISTA.CDLISTA
           AND NVL(FILIAL.IDCNTRRESC, 'N') = 'N'           
           AND FILIAL.CDREGIAO = V_REGIAOGERAL_CDREGIAO
           AND ((FILIAL.CDFILIAL < V_ULTIMAFILIAL) OR (V_ULTIMAFILIAL IS NULL))
          ORDER BY CDFILIAL) A
         ORDER BY ROWNUM DESC;
      LOOP
         FETCH C_FILIAL INTO V_FILIAL_CDFILIAL, V_AUX_FILIAL;
         EXIT WHEN C_FILIAL%NOTFOUND;
         V_QTPRODLISPREC := NULL;
         IF V_QTFILIALLISPREC IS NULL THEN
            V_QTFILIALLISPREC := V_AUX_FILIAL;
            IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
               UPDATE TEMPULTATULISPR
               SET QTFILIALLISPREC = V_QTFILIALLISPREC
               WHERE CDCENULTLISPREC = P_CDFILIAL
                 AND NRSEQULTLISPREC = P_NRSEQPRELIST;
               COMMIT;
            END IF;
         END IF;

         INSERT INTO CUSTOSUP_TMP
           (CDFILIAL,
            CDFILCENTCMP,
            NRSEQPRELIST,
            DTINIVALITM,
            DTFINVALITM,
            CDPRODESTO,
            MINPRECO
            )
         SELECT
            FILIAL.CDFILIAL,
            ITEMCOLE.CDFILIAL,
            P_NRSEQPRELIST,
            DECODE(V_DTINIVALLIST - ITEMCOLE.DTINIVALITM, ABS(V_DTINIVALLIST - ITEMCOLE.DTINIVALITM), V_DTINIVALLIST, ITEMCOLE.DTINIVALITM),
            DECODE(V_DTFINVALLIST - ITEMCOLE.DTFINVALITM, ABS(V_DTFINVALLIST - ITEMCOLE.DTFINVALITM), ITEMCOLE.DTFINVALITM, V_DTFINVALLIST),
            PRODUTO.CDPRODESTO,
            MIN(TO_CHAR(DECODE(V_IDTIPOAPUR, NULL, (ITEMCOLE.VRPRECUNIITM/PRODUTO.VRFATOCONV),
                                              '1', (ITEMCOLE.VRPRECECOITM/PRODUTO.VRFATOCONV),
                                                   (ITEMCOLE.VRPRECOITMPI/PRODUTO.VRFATOCONV)) * V_MULTIPLO, V_MASCARA) ||'#'||
                TO_CHAR((ITEMCOLE.VRPRECUNIITM/PRODUTO.VRFATOCONV) * V_MULTIPLO, V_MASCARA)  ||'%'||
                ITEMCOLE.NRCOLEPRECO ||
                ITEMCOLE.CDREGIAO    ||
                ITEMCOLE.CDPRODUTO   ||
                ITEMCOLE.CDMARCA     ||
                ITEMCOLE.CDFORNECED)
         FROM
            FORNECEDOR,
            COLETAPR,
            PRODUTO,
            FILIAL,
            ITEMCOLE
         WHERE
            ((ITEMCOLE.CDPRODUTO IN (SELECT PRODPREFMED.CDPRODPREF
                                     FROM PRODPREFMED
                                     WHERE PRODPREFMED.CDFILIAL  = FILIAL.CDFILIAL AND
                                           PRODPREFMED.CDPRODUTO = PRODUTO.CDPRODESTO)) OR
              (NOT EXISTS (SELECT 1
                           FROM PRODPREFMED P
                           WHERE
                              P.CDFILIAL  = FILIAL.CDFILIAL    AND
                              P.CDPRODUTO = PRODUTO.CDPRODESTO AND
                              P.CDPRODPREF IS NOT NULL))
             ) AND
            (FILIAL.CDFILIAL       = NVL(P_CDFILCUSTO, FILIAL.CDFILIAL)) AND
            (ITEMCOLE.CDREGIAO     = V_REGIAOGERAL_CDREGIAO) AND
            (FILIAL.CDFILIAL       = V_FILIAL_CDFILIAL)    AND
            (ITEMCOLE.CDFILIAL     = P_CDFILIAL)           AND
            (ITEMCOLE.NRSEQPRELIST = DECODE(IDITVALDIFLISTA, 'S', ITEMCOLE.NRSEQPRELIST, P_NRSEQPRELIST) ) AND
            (ITEMCOLE.DTINIVALITM <= V_DTFINVALLIST)       AND
            (ITEMCOLE.DTFINVALITM >= V_DTINIVALLIST)       AND
            (ITEMCOLE.VRPRECUNIITM > 0)                    AND
            (ITEMCOLE.IDITMLISPREC = 'S')                  AND
            (ITEMCOLE.CDREGIAO     = FILIAL.CDREGIAO)      AND
            (ITEMCOLE.CDPRODUTO    = PRODUTO.CDPRODUTO)    AND
            (FORNECEDOR.CDFORNECED = ITEMCOLE.CDFORNECED)  AND
            (COLETAPR.CDFILIAL     = ITEMCOLE.CDFILIAL)    AND
            (COLETAPR.NRCOLEPRECO  = ITEMCOLE.NRCOLEPRECO) AND
            (COLETAPR.CDOPERADOR   = V_CDOPERADOR)         AND
            (FORNECEDOR.IDSITUCADA = 'A')                  AND
            (NVL(FORNECEDOR.IDTIPOFORN, 'N') <> 'A')       AND
             PRODUTO.CDPRODESTO     = NVL(P_CDPRODUTO, PRODUTO.CDPRODESTO)
             AND NOT EXISTS (SELECT
                                1
                             FROM
                                REFORFIL
                             WHERE
                                (CDFILIAL   = FILIAL.CDFILIAL)   AND
                                (CDFORNECED = ITEMCOLE.CDFORNECED) AND
                                (CDPERIENTR IS NULL))
             AND NOT EXISTS (SELECT
                                1
                             FROM
                                REFOREG
                             WHERE
                                (REFOREG.CDREGIAO   = ITEMCOLE.CDREGIAO) AND
                                (REFOREG.CDFORNECED = ITEMCOLE.CDFORNECED))
             AND NOT EXISTS (SELECT
                                1
                             FROM
                                QFOPRODFORN,
                                PARASUPRI
                             WHERE
                                (PARASUPRI.CDFILIAL     = P_CDFILIAL)              AND
                                (QFOPRODFORN.CDPRODUTO  = ITEMCOLE.CDPRODUTO)  AND
                                (QFOPRODFORN.CDFORNECED = ITEMCOLE.CDFORNECED) AND
                                ((QFOPRODFORN.IDFORNAPROV = 'N' AND QFOPRODFORN.IDFORNAUTOR = 'N') OR ((QFOPRODFORN.IDFORNAPROV = 'S') AND ((PARASUPRI.IDULTIDTVALQ = 'M' AND COLETAPR.DTCOLEPREC > LAST_DAY(QFOPRODFORN.DTVALPRFORN)) OR (PARASUPRI.IDULTIDTVALQ = 'D' AND COLETAPR.DTCOLEPREC > QFOPRODFORN.DTVALPRFORN)))))
             AND NOT EXISTS (SELECT
                                1
                             FROM
                                QFOMARPROD,
                                PARASUPRI
                             WHERE
                                (PARASUPRI.CDFILIAL   = P_CDFILIAL)             AND
                                (QFOMARPROD.CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                (QFOMARPROD.CDMARCA   = ITEMCOLE.CDMARCA)   AND
                                ((QFOMARPROD.IDAPRMARPRO = 'N' AND QFOMARPROD.IDAUTMARPRO = 'N') OR ((QFOMARPROD.IDAPRMARPRO = 'S') AND ((PARASUPRI.IDULTIDTVALQ = 'M' AND COLETAPR.DTCOLEPREC > LAST_DAY(QFOMARPROD.DTVALAPROVA)) OR (PARASUPRI.IDULTIDTVALQ = 'D' AND COLETAPR.DTCOLEPREC > QFOMARPROD.DTVALAPROVA)))))
             AND EXISTS (SELECT
                            1
                         FROM
                            QFOMARPROD
                         WHERE
                            (QFOMARPROD.CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                            (QFOMARPROD.CDMARCA   = ITEMCOLE.CDMARCA)

                         UNION

                         SELECT
                            1
                         FROM
                            PARASUPRI
                         WHERE
                            (PARASUPRI.CDFILIAL    = P_CDFILIAL) AND
                            (PARASUPRI.IDMARSEMQUA = 'N' OR PARASUPRI.IDMARSEMQUA IS NULL))
             AND NOT EXISTS (SELECT
                                1
                             FROM
                                QUALIFICACAO,
                                PARASUPRI
                             WHERE
                                (QUALIFICACAO.CDQUALIF     = FORNECEDOR.CDQUALIF) AND
                                (PARASUPRI.CDFILIAL        = P_CDFILIAL)          AND
                                (FORNECEDOR.IDFORNAUTO     = 'N')                 AND
                                ((QUALIFICACAO.IDPERCOTCMP = '2') OR ((PARASUPRI.IDULTIDTVALQ = 'D' AND COLETAPR.DTCOLEPREC > FORNECEDOR.DTVALQUALIF) OR (PARASUPRI.IDULTIDTVALQ = 'M' AND COLETAPR.DTCOLEPREC > LAST_DAY(FORNECEDOR.DTVALQUALIF)))))
             AND (

                  (EXISTS (SELECT
                              1
                           FROM
                              PARTFILI
                           WHERE
                              (CDFILIAL  = FILIAL.CDFILIAL)    AND
                              (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                              (CDTIPPART IN ('1', '2'))        AND
                              (CDMARCA   = ITEMCOLE.CDMARCA)   AND
                              (NOT EXISTS (SELECT
                                              1
                                           FROM
                                              PARTFILI
                                           WHERE
                                              (CDFILIAL   = FILIAL.CDFILIAL )       AND
                                              (CDPRODUTO  = ITEMCOLE.CDPRODUTO)     AND
                                              (CDTIPPART  IN ('4', '5'))            AND
                                              (NRCOLEPRECO IS NULL)                 AND
                                              (CDFORNECED <> ITEMCOLE.CDFORNECED))

                               )
                            )



                   )

                  OR (EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL )    AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO)  AND
                                 (CDTIPPART  IN ('4', '5'))         AND
                                 (NRCOLEPRECO IS NULL)              AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED) AND
                                 (NOT EXISTS (SELECT
                                                 1
                                              FROM
                                                 PARTFILI
                                              WHERE
                                                (CDFILIAL  = FILIAL.CDFILIAL)    AND
                                                (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                                (CDTIPPART IN ('1', '2'))        AND
                                                (CDMARCA   <> ITEMCOLE.CDMARCA))
                                   )
                                )

                      )

                  OR (EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  IN ('7', '8'))            AND
                                 (CDMARCA    = ITEMCOLE.CDMARCA)   AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED)))

                  OR (EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL  = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  IN ('3','6','9')))   AND
                      NOT EXISTS (SELECT
                                     1
                                  FROM
                                     PARTFILI
                                  WHERE
                                     (CDFILIAL  = FILIAL.CDFILIAL)    AND
                                     (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                     (NRCOLEPRECO IS NULL)            AND
                                     (CDTIPPART  IN ('1','2','4','5','7','8')))
                      )

                  OR (NOT EXISTS (SELECT
                                     1
                                  FROM
                                     PARTFILI
                                  WHERE
                                     (CDFILIAL  = FILIAL.CDFILIAL) AND
                                     (NRCOLEPRECO IS NULL)         AND
                                     (CDPRODUTO = ITEMCOLE.CDPRODUTO)))
                  )
             AND (NOT EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL  = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART = '3')                    AND
                                 (CDMARCA   = ITEMCOLE.CDMARCA)))
             AND (NOT EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  = '6')                    AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED)))
             AND (NOT EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  = '9')                    AND
                                 (CDMARCA    = ITEMCOLE.CDMARCA)   AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED)))
         GROUP BY
            FILIAL.CDFILIAL,
            ITEMCOLE.CDFILIAL,
            P_NRSEQPRELIST,
            DECODE(V_DTINIVALLIST - ITEMCOLE.DTINIVALITM, ABS(V_DTINIVALLIST - ITEMCOLE.DTINIVALITM), V_DTINIVALLIST, ITEMCOLE.DTINIVALITM),
            DECODE(V_DTFINVALLIST - ITEMCOLE.DTFINVALITM, ABS(V_DTFINVALLIST - ITEMCOLE.DTFINVALITM), ITEMCOLE.DTFINVALITM, V_DTFINVALLIST),
            PRODUTO.CDPRODESTO;
         V_AUX_CUSTO := SQL%ROWCOUNT;
         COMMIT;

         /* Alteracao proposta pelo Jalmor - populando tabelas em memoria (array)
           com os valores dos indices (moedas) parametrizados */

         IF (V_IDUTCUSTOM1 = 'S') OR (V_IDUTCUSTOM2 = 'S') THEN
            FOR C_DATAS IN
              (SELECT
                  DISTINCT
                  DTINIVALITM
               FROM
                  CUSTOSUP_TMP
               WHERE
                  CUSTOSUP_TMP.CDFILCENTCMP = P_CDFILIAL AND
                  CUSTOSUP_TMP.NRSEQPRELIST = P_NRSEQPRELIST)
            LOOP
               IF (V_IDUTCUSTOM1 = 'S') THEN
                  SELECT
                     NVL(MAX(MOVINDICE.VRINDICE), 0)
                  INTO
                     V_VRINDICEM1(TO_CHAR(C_DATAS.DTINIVALITM, 'DDMMYYYY'))
                  FROM
                     PARAMGERAL,
                     MOVINDICE
                  WHERE
                     (PARAMGERAL.CDINDICEMF = MOVINDICE.CDINDICE)  AND
                     (MOVINDICE.DTREFIND    = C_DATAS.DTINIVALITM) AND
                     (MOVINDICE.CDTPINDICE  = '1')                 AND
                     (MOVINDICE.IDPERVALOR  = '2');
               END IF;

               IF (V_IDUTCUSTOM2 = 'S') THEN
                  SELECT
                     NVL(MAX(MOVINDICE.VRINDICE), 0)
                  INTO
                     V_VRINDICEM2(TO_CHAR(C_DATAS.DTINIVALITM, 'DDMMYYYY'))
                  FROM
                     PARAMGERAL,
                     MOVINDICE
                  WHERE
                     (PARAMGERAL.CDINDICEMF2 = MOVINDICE.CDINDICE)  AND
                     (MOVINDICE.DTREFIND     = C_DATAS.DTINIVALITM) AND
                     (MOVINDICE.CDTPINDICE   = '1')                 AND
                     (MOVINDICE.IDPERVALOR   = '2');
               END IF;
            END LOOP;
         END IF;

         V_COUNT := 0;
         FOR C_VERIFICA_VALIDADE IN
            (SELECT CUSTOSUP_TMP.ROWID, CUSTOSUP_TMP.CDPRODESTO, CUSTOSUP_TMP.DTINIVALITM, CUSTOSUP_TMP.DTFINVALITM
             FROM CUSTOSUP_TMP
             WHERE CDFILIAL = V_FILIAL_CDFILIAL)
         LOOP
            IF V_COUNT = 0 THEN
               V_COUNT := 0;
               SELECT COUNT(1)
               INTO V_COUNT
               FROM CUSTOSUP_TMP
               WHERE CUSTOSUP_TMP.CDFILIAL = V_FILIAL_CDFILIAL
                 AND CUSTOSUP_TMP.CDPRODESTO = C_VERIFICA_VALIDADE.CDPRODESTO
                 AND CUSTOSUP_TMP.ROWID <> C_VERIFICA_VALIDADE.ROWID
                 AND ((CUSTOSUP_TMP.DTINIVALITM <> C_VERIFICA_VALIDADE.DTINIVALITM) OR (CUSTOSUP_TMP.DTFINVALITM <> C_VERIFICA_VALIDADE.DTFINVALITM))
                 AND CUSTOSUP_TMP.DTINIVALITM <= C_VERIFICA_VALIDADE.DTFINVALITM
                 AND CUSTOSUP_TMP.DTFINVALITM >= C_VERIFICA_VALIDADE.DTINIVALITM;
            ELSE
               EXIT;
            END IF;
         END LOOP;

         IF V_COUNT > 0 THEN
            DELETE FROM CUSTOSUPACOR_TMP
            WHERE CUSTOSUPACOR_TMP.CDFILCENTCMP = P_CDFILIAL
              AND CUSTOSUPACOR_TMP.NRSEQPRELIST = P_NRSEQPRELIST;
            FOR C_DATA IN
               (SELECT DTDATA
                FROM DATA
                WHERE DTDATA BETWEEN V_DTINIVALLIST AND V_DTFINVALLIST
                ORDER BY DTDATA)
            LOOP
               FOR C_PRODUTO IN
                  (SELECT DISTINCT CUSTOSUP_TMP.CDFILIAL, CUSTOSUP_TMP.CDPRODESTO
                   FROM CUSTOSUP_TMP
                   ORDER BY CUSTOSUP_TMP.CDFILIAL, CUSTOSUP_TMP.CDPRODESTO)
               LOOP
                  FOR C_MENOR_PRECO_DATA IN
                     (SELECT MIN(CUSTOSUP_TMP.MINPRECO) MINPRECO
                      FROM CUSTOSUP_TMP
                      WHERE C_DATA.DTDATA BETWEEN CUSTOSUP_TMP.DTINIVALITM AND CUSTOSUP_TMP.DTFINVALITM
                        AND C_PRODUTO.CDFILIAL = CUSTOSUP_TMP.CDFILIAL
                        AND C_PRODUTO.CDPRODESTO = CUSTOSUP_TMP.CDPRODESTO)
                  LOOP
                     INSERT INTO CUSTOSUPACOR_TMP(CDFILIAL, CDFILCENTCMP, NRSEQPRELIST, DTINIVALITM,
                                                  DTFINVALITM, CDPRODESTO, MINPRECO)
                     VALUES(C_PRODUTO.CDFILIAL, P_CDFILIAL, P_NRSEQPRELIST, C_DATA.DTDATA,
                            C_DATA.DTDATA, C_PRODUTO.CDPRODESTO, C_MENOR_PRECO_DATA.MINPRECO);
                  END LOOP;

                  DELETE FROM CUSTOSUP
                  WHERE CDFILIAL = C_PRODUTO.CDFILIAL
                    AND CDPRODUTO = C_PRODUTO.CDPRODESTO
                    AND DTCUSTOSUP <= V_DTFINVALLIST
                    AND DTFINCUSTSUP >= V_DTINIVALLIST;
               END LOOP;
            END LOOP;

            V_PRODUTO_ANTERIOR := NULL;
            V_COTACAO_ANTERIOR := NULL;
            V_FILIAL_ANTERIOR  := NULL;
            FOR C_AJUSTE IN
               (SELECT CDFILIAL, CDPRODESTO, DTINIVALITM, MINPRECO
                FROM CUSTOSUPACOR_TMP
                ORDER BY CDFILIAL, CDPRODESTO, DTINIVALITM)
            LOOP
               IF (V_PRODUTO_ANTERIOR IS NULL) AND (V_COTACAO_ANTERIOR IS NULL) AND (V_FILIAL_ANTERIOR IS NULL) THEN
                  V_PRODUTO_ANTERIOR := C_AJUSTE.CDPRODESTO;
                  V_COTACAO_ANTERIOR := C_AJUSTE.MINPRECO;
                  V_FILIAL_ANTERIOR  := C_AJUSTE.CDFILIAL;
                  UPDATE CUSTOSUP_TMP
                  SET IDAJUSTEATUCUST = 'S'
                  WHERE CDPRODESTO = C_AJUSTE.CDPRODESTO
                    AND MINPRECO = C_AJUSTE.MINPRECO
                    AND CDFILIAL = C_AJUSTE.CDFILIAL;
               ELSE
                  IF   (((V_PRODUTO_ANTERIOR <> C_AJUSTE.CDPRODESTO)
                    OR (NVL(V_COTACAO_ANTERIOR, 'X') <> NVL(C_AJUSTE.MINPRECO, 'Y'))
                    OR (V_FILIAL_ANTERIOR  <> C_AJUSTE.CDFILIAL)) AND (NVL(C_AJUSTE.MINPRECO, 'Y') <> 'Y'))  THEN
                     INSERT INTO CUSTOSUP_TMP (CDFILIAL, CDFILCENTCMP, NRSEQPRELIST, DTINIVALITM,
                                               DTFINVALITM, CDPRODESTO, MINPRECO, IDAJUSTEATUCUST)
                     SELECT
                        C_AJUSTE.CDFILIAL,
                        P_CDFILIAL,
                        P_NRSEQPRELIST,
                        C_AJUSTE.DTINIVALITM,
                        C.DTFINVALITM,
                        C_AJUSTE.CDPRODESTO,
                        C.MINPRECO,
                        'S'
                     FROM CUSTOSUP_TMP C
                     WHERE C.MINPRECO = C_AJUSTE.MINPRECO
                      AND  C.CDPRODESTO = C_AJUSTE.CDPRODESTO
                      AND  C.CDFILIAL = C_AJUSTE.CDFILIAL;

                     DELETE FROM CUSTOSUP_TMP
                     WHERE NVL(IDAJUSTEATUCUST, 'N') = 'N'
                       AND CUSTOSUP_TMP.MINPRECO = C_AJUSTE.MINPRECO
                       AND CUSTOSUP_TMP.CDPRODESTO = C_AJUSTE.CDPRODESTO
                       AND CUSTOSUP_TMP.CDFILIAL = C_AJUSTE.CDFILIAL;

                    V_PRODUTO_ANTERIOR := C_AJUSTE.CDPRODESTO;
                    V_COTACAO_ANTERIOR := C_AJUSTE.MINPRECO;
                    V_FILIAL_ANTERIOR  := C_AJUSTE.CDFILIAL;
                  END IF;
               END IF;
            END LOOP;
            DELETE FROM CUSTOSUPACOR_TMP
            WHERE CUSTOSUPACOR_TMP.CDFILCENTCMP = P_CDFILIAL
              AND CUSTOSUPACOR_TMP.NRSEQPRELIST = P_NRSEQPRELIST;
            DELETE FROM CUSTOSUP_TMP
            WHERE NVL(IDAJUSTEATUCUST, 'N') = 'N'
              AND CUSTOSUP_TMP.CDFILIAL = V_FILIAL_CDFILIAL;
         END IF;
         COMMIT;

         OPEN C_CUSTO FOR
            SELECT
               CUSTOSUP_TMP.CDFILIAL,
               CUSTOSUP_TMP.DTINIVALITM,
               CUSTOSUP_TMP.DTFINVALITM,
               CUSTOSUP_TMP.CDPRODESTO,
               CUSTOSUP_TMP.MINPRECO
            FROM
               CUSTOSUP_TMP
            WHERE CUSTOSUP_TMP.CDFILCENTCMP = P_CDFILIAL
              AND CUSTOSUP_TMP.NRSEQPRELIST = P_NRSEQPRELIST;
         LOOP
            FETCH C_CUSTO INTO V_CUSTOSUP_TMP_CDFILIAL,
                               V_CUSTOSUP_TMP_DTINIVALITM,
                               V_CUSTOSUP_TMP_DTFINVALITM,
                               V_CUSTOSUP_TMP_CDPRODESTO,
                               V_CUSTOSUP_TMP_MINPRECO;
            EXIT WHEN C_CUSTO%NOTFOUND;
            IF V_QTPRODLISPREC IS NULL THEN
               V_QTPRODLISPREC := V_AUX_CUSTO;
               IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
                  UPDATE TEMPULTATULISPR
                  SET QTPRODLISPREC = V_QTPRODLISPREC
                  WHERE CDCENULTLISPREC = P_CDFILIAL
                    AND NRSEQULTLISPREC = P_NRSEQPRELIST;
                  COMMIT;
               END IF;
            END IF;

            V_POSMINPREPI := INSTR(V_CUSTOSUP_TMP_MINPRECO, '#');
            V_POSMINPRECO := INSTR(V_CUSTOSUP_TMP_MINPRECO, '%');

            V_MINPRECO    := (SUBSTR(V_CUSTOSUP_TMP_MINPRECO, V_POSMINPREPI + 1, V_POSMINPRECO - V_POSMINPREPI - 1))/V_MULTIPLO;
            V_NRCOLEPRECO := SUBSTR(V_CUSTOSUP_TMP_MINPRECO, V_POSMINPRECO+1, 10);
            V_CDREGIAO    := SUBSTR(V_CUSTOSUP_TMP_MINPRECO, V_POSMINPRECO+11, 3);
            V_CDPRODUTO   := SUBSTR(V_CUSTOSUP_TMP_MINPRECO, V_POSMINPRECO+14, 10);
            V_CDMARCA     := SUBSTR(V_CUSTOSUP_TMP_MINPRECO, V_POSMINPRECO+24, 5);
            V_CDFORNECED  := SUBSTR(V_CUSTOSUP_TMP_MINPRECO, V_POSMINPRECO+29, 21);

            V_VRPERAJFORNFIL := NULL;
            V_IDCONSPERPIAJPR := 'N';
            SELECT
               MAX(VRPERAJFORNFIL),
               MAX(IDCONSPERPIAJPR)
            INTO V_VRPERAJFORNFIL, V_IDCONSPERPIAJPR
            FROM AJUSTEPRFORNFIL
            WHERE AJUSTEPRFORNFIL.CDFORNECED = V_CDFORNECED
              AND AJUSTEPRFORNFIL.CDFILIAL = V_CUSTOSUP_TMP_CDFILIAL
              AND AJUSTEPRFORNFIL.DTINIAJPRFORFIL = (SELECT MAX(A.DTINIAJPRFORFIL)
                                                     FROM AJUSTEPRFORNFIL A
                                                     WHERE A.CDFORNECED = V_CDFORNECED
                                                       AND A.CDFILIAL = V_CUSTOSUP_TMP_CDFILIAL
                                                       AND TRUNC(SYSDATE) BETWEEN A.DTINIAJPRFORFIL AND NVL(A.DTFINAJPRFORFIL, TRUNC(SYSDATE)));

            V_PIDSV := NULL;
            IF V_IDCONSPERPIAJPR = 'S' THEN
               SELECT
                  MAX(VRPERPIPRFO)
               INTO V_PIDSV
               FROM PRODFORNCOMP
               WHERE PRODFORNCOMP.CDFORNECED  = V_CDFORNECED
                 AND PRODFORNCOMP.CDPRODCOMPL = V_CDPRODUTO;

               IF V_PIDSV IS NULL THEN
                  SELECT MAX(VRPERPIFORN)
                  INTO V_PIDSV
                  FROM FORNECEDOR
                  WHERE FORNECEDOR.CDFORNECED = V_CDFORNECED;
               END IF;
            END IF;

            IF V_PIDSV IS NULL THEN
               V_PIDSV := 0;
            END IF;
            IF V_VRPERAJFORNFIL IS NULL THEN
               V_VRPERAJFORNFIL := 0;
            END IF;

            V_MINPRECO := V_MINPRECO - (V_MINPRECO * V_PIDSV/100) + (V_MINPRECO * V_VRPERAJFORNFIL/100);

            IF V_IDUTCUSLIPR = 'S' THEN
               V_VRIMPITM := 0;
               FOR C_IMPOSTO IN
                  (SELECT
                      IMPOSTO.CDIMPOSTO,
                      IMPOSTO.IDTRATIMPO,
                      IMPOSTO.IDIMPORECUP,
                      IMPOSTO.CDCCTBLATIV,
                      IMPOSTO.CDCCTBLPASS,
                      IMPCOLET.VRADIMPCOL,
                      IMPCOLET.VRALIQIMPCOL,
                      IMPCOLET.VRIMPCOLETA,
                      IMPCOLET.CDFISCITMCOL
                   FROM
                      IMPCOLET,
                      IMPOSTO
                   WHERE
                      IMPCOLET.CDFILIAL    = P_CDFILIAL     AND
                      IMPCOLET.NRCOLEPRECO = V_NRCOLEPRECO  AND
                      IMPCOLET.CDFORNECED  = V_CDFORNECED   AND
                      IMPCOLET.CDREGIAO    = V_CDREGIAO     AND
                      IMPCOLET.CDPRODUTO   = V_CDPRODUTO    AND
                      IMPCOLET.CDMARCA     = V_CDMARCA      AND
                      IMPCOLET.CDIMPOSTO   = IMPOSTO.CDIMPOSTO
                   )
               LOOP
                  SELECT
                     MAX(CDIMPOSTO),
                     MAX(IDVRIMPSTRIB)
                  INTO
                     V_CDIMPOSTRIB,
                     V_IDVRIMPSTRIB
                  FROM
                     IMPOSTO
                  WHERE
                     CDIMPOSTRIB = C_IMPOSTO.CDIMPOSTO;

                  IF (C_IMPOSTO.IDIMPORECUP = 'S') THEN
                     IF (C_IMPOSTO.CDFISCITMCOL = '1') THEN
                        IF (C_IMPOSTO.CDCCTBLATIV IS NOT NULL) THEN
                           IF C_IMPOSTO.IDTRATIMPO = 'V' THEN
                              V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                           END IF;
                        END IF;
                     ELSE
                        IF C_IMPOSTO.IDTRATIMPO = 'A' THEN
                           IF V_CDIMPOSTRIB IS NULL THEN
                              V_VRIMPITM := V_VRIMPITM + C_IMPOSTO.VRIMPCOLETA;
                           END IF;
                        ELSIF C_IMPOSTO.IDTRATIMPO IN ('D','R') THEN
                           V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                        END IF;
                     END IF;
                  ELSE
                     IF C_IMPOSTO.CDCCTBLPASS IS NOT NULL THEN
                        IF C_IMPOSTO.IDTRATIMPO = 'V' THEN
                           V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                        END IF;
                     ELSE
                        IF C_IMPOSTO.IDTRATIMPO = 'A' THEN
                           IF V_CDIMPOSTRIB IS NULL THEN
                              V_VRIMPITM := V_VRIMPITM + C_IMPOSTO.VRIMPCOLETA;
                           END IF;
                        ELSIF C_IMPOSTO.IDTRATIMPO IN ('D','R') THEN
                           V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                        END IF;
                     END IF;
                  END IF;
               END LOOP;
               BEGIN
                  SELECT
                     NVL(VRFATOCONV, 1)
                  INTO
                     V_VRFATOCONV
                  FROM
                     PRODUTO
                  WHERE
                     PRODUTO.CDPRODUTO = V_CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_VRFATOCONV := 1;
               END;
               IF V_VRFATOCONV IS NULL THEN
                  V_VRFATOCONV := 1;
               END IF;
            END IF;

            IF (V_IDUTCUSTOM1 = 'N') OR (V_VRINDICEM1(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM,'DDMMYYYY')) = 0) THEN
               V_VRCUSPRLISM1 := 0;
            ELSE
               V_VRCUSPRLISM1 := NVL(V_MINPRECO, 0) / V_VRINDICEM1(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM, 'DDMMYYYY'));
            END IF;
            IF (V_IDUTCUSTOM2 = 'N') OR (V_VRINDICEM2(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM,'DDMMYYYY')) = 0) THEN
               V_VRCUSPRLISM2 := 0;
            ELSE
               V_VRCUSPRLISM2 := NVL(V_MINPRECO, 0) / V_VRINDICEM2(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM,'DDMMYYYY'));
            END IF;

            /* O campo VRHISPRLISTP, no update abaixo, tem a funcao de guardar o custo da lista,
               para o caso dele ser substituido pelo preco de algum acordo existente, no processo
               que e executado mais abaixo. */

            UPDATE CUSTOSUP SET
               VRCUSPRLISTP = V_MINPRECO,
               VRHISPRLISTP = V_MINPRECO,
               CDFILCOTLIST = P_CDFILIAL,
               NRCOLEPRLIST = V_NRCOLEPRECO,
               CDREGIAOLIST = V_CDREGIAO,
               CDFORNECLIST = V_CDFORNECED,
               CDPRODLIST   = V_CDPRODUTO,
               CDMARCALIST  = V_CDMARCA,
               DTFINCUSTSUP = V_CUSTOSUP_TMP_DTFINVALITM,
               VRCUSPRLISM1 = V_VRCUSPRLISM1,
               VRCUSPRLISM2 = V_VRCUSPRLISM2,
               CDFILCOTACOR = NULL,
               NRCOLEPRACOR = NULL,
               CDMARCAACOR  = NULL,
               CDREGIAOACOR = NULL,
               CDFORNECACOR = NULL,
               CDPRODACOR   = NULL,
               VRCUSLIPRIMP = DECODE(V_IDUTCUSLIPR, 'N', NULL, ((V_MINPRECO*V_VRFATOCONV) + V_VRIMPITM)/V_VRFATOCONV)
            WHERE
               (CDFILIAL   = V_CUSTOSUP_TMP_CDFILIAL)   AND
               (CDPRODUTO  = V_CUSTOSUP_TMP_CDPRODESTO) AND
               (DTCUSTOSUP = V_CUSTOSUP_TMP_DTINIVALITM);

            IF SQL%NOTFOUND THEN

               IF (V_IDUTCUSTOM1 = 'N') OR (V_VRINDICEM1(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM,'DDMMYYYY')) = 0) THEN
                  V_VRCUSPRLISM1 := 0;
               ELSE
                  V_VRCUSPRLISM1 := NVL(V_MINPRECO, 0) / V_VRINDICEM1(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM,'DDMMYYYY'));
               END IF;
               IF (V_IDUTCUSTOM2 = 'N') OR (V_VRINDICEM2(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM,'DDMMYYYY')) = 0) THEN
                  V_VRCUSPRLISM2 := 0;
               ELSE
                  V_VRCUSPRLISM2 := NVL(V_MINPRECO, 0) / V_VRINDICEM2(TO_CHAR(V_CUSTOSUP_TMP_DTINIVALITM,'DDMMYYYY'));
               END IF;

               BEGIN
                  INSERT INTO CUSTOSUP
                    (CDFILIAL,
                     CDPRODUTO,
                     DTCUSTOSUP,
                     VRCUSPRLISTP,
                     CDFILCOTLIST,
                     NRCOLEPRLIST,
                     CDREGIAOLIST,
                     CDFORNECLIST,
                     CDPRODLIST,
                     CDMARCALIST,
                     VRCUSPRLISM1,
                     VRCUSPRLISM2,
                     VRHISPRLISTP,
                     DTFINCUSTSUP,
                     VRCUSLIPRIMP)
                  VALUES
                    (V_CUSTOSUP_TMP_CDFILIAL,
                     V_CUSTOSUP_TMP_CDPRODESTO,
                     V_CUSTOSUP_TMP_DTINIVALITM,
                     V_MINPRECO,
                     P_CDFILIAL,
                     V_NRCOLEPRECO,
                     V_CDREGIAO,
                     V_CDFORNECED,
                     V_CDPRODUTO,
                     V_CDMARCA,
                     V_VRCUSPRLISM1,
                     V_VRCUSPRLISM2,
                     V_MINPRECO,
                     V_CUSTOSUP_TMP_DTFINVALITM,
                     DECODE(V_IDUTCUSLIPR, 'N', NULL, ((V_MINPRECO*V_VRFATOCONV)+ V_VRIMPITM)/V_VRFATOCONV));
               EXCEPTION
                  WHEN OTHERS THEN NULL;
               END;
            END IF;

            V_AUX_CUSTO := V_AUX_CUSTO - 1;
            IF V_AUX_CUSTO < 0 THEN
               V_AUX_CUSTO := 0;
            END IF;
            IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
               UPDATE TEMPULTATULISPR
               SET QTPRODLISPREC = V_AUX_CUSTO,
                   CDPRODULLISPREC = V_CUSTOSUP_TMP_CDPRODESTO
               WHERE CDCENULTLISPREC = P_CDFILIAL
                 AND NRSEQULTLISPREC = P_NRSEQPRELIST;
               COMMIT;
            END IF;
         END LOOP;
         CLOSE C_CUSTO;

         INSERT INTO CUSTOSUPACOR_TMP
           (CDFILIAL,
            CDFILCENTCMP,
            NRSEQPRELIST,
            DTINIVALITM,
            DTFINVALITM,
            CDPRODESTO,
            MINPRECO
            )
         SELECT
            FILIAL.CDFILIAL,
            ITEMCOLE.CDFILIAL,
            P_NRSEQPRELIST,
            ITEMCOLE.DTINIVALITM,
            ITEMCOLE.DTFINVALITM,
            PRODUTO.CDPRODESTO,
            MIN(TO_CHAR(DECODE(V_IDTIPOAPUR, NULL, (ITEMCOLE.VRPRECUNIITM/PRODUTO.VRFATOCONV),
                                                 '1', (ITEMCOLE.VRPRECECOITM/PRODUTO.VRFATOCONV),
                                                      (ITEMCOLE.VRPRECOITMPI/PRODUTO.VRFATOCONV)) * V_MULTIPLO, V_MASCARA) ||'#'||
                   TO_CHAR((ITEMCOLE.VRPRECUNIITM/PRODUTO.VRFATOCONV) * V_MULTIPLO, V_MASCARA)  ||'%'||
                   ITEMCOLE.NRCOLEPRECO ||
                   ITEMCOLE.CDREGIAO    ||
                   ITEMCOLE.CDPRODUTO   ||
                   ITEMCOLE.CDMARCA     ||
                   ITEMCOLE.CDFORNECED)
         FROM
            FORNECEDOR,
            PRODUTO,
            FILIAL,
            COLETAPR,
            ITEMCOLE,
            PARTFILI
         WHERE
            ((ITEMCOLE.CDPRODUTO IN (SELECT PRODPREFMED.CDPRODPREF
                                     FROM PRODPREFMED
                                     WHERE PRODPREFMED.CDFILIAL  = FILIAL.CDFILIAL AND
                                           PRODPREFMED.CDPRODUTO = PRODUTO.CDPRODESTO)) OR
              (NOT EXISTS (SELECT 1
                           FROM PRODPREFMED P
                           WHERE
                              P.CDFILIAL  = FILIAL.CDFILIAL    AND
                              P.CDPRODUTO = PRODUTO.CDPRODESTO AND
                              P.CDPRODPREF IS NOT NULL))
             ) AND
            (FILIAL.CDFILIAL       = NVL(P_CDFILCUSTO, FILIAL.CDFILIAL)) AND
            (ITEMCOLE.CDREGIAO     = V_REGIAOGERAL_CDREGIAO)                  AND
            (FILIAL.CDFILIAL       = V_FILIAL_CDFILIAL)                  AND
            (PARTFILI.CDFILIAL     = FILIAL.CDFILIAL)                    AND
            (PARTFILI.CDTIPPART    = '4')                                AND
            (ITEMCOLE.VRPRECUNIITM > 0)                                  AND
            (PARTFILI.CDFILCOTA    = P_CDFILIAL)                         AND
            (ITEMCOLE.CDFILIAL     = P_CDFILIAL)                         AND
            (ITEMCOLE.NRCOLEPRECO  = PARTFILI.NRCOLEPRECO)               AND
            (ITEMCOLE.CDPRODUTO    = PRODUTO.CDPRODUTO)                  AND
            (ITEMCOLE.CDPRODUTO    = PARTFILI.CDPRODUTO)                 AND
            (ITEMCOLE.CDFORNECED   = PARTFILI.CDFORNECED)                AND
            (ITEMCOLE.CDFORNECED   = FORNECEDOR.CDFORNECED)              AND
            (ITEMCOLE.CDREGIAO     = FILIAL.CDREGIAO)                    AND
            (ITEMCOLE.DTFINVALITM  >= TRUNC(SYSDATE))                    AND
            (COLETAPR.CDFILIAL     = ITEMCOLE.CDFILIAL)                  AND
            (COLETAPR.NRCOLEPRECO  = ITEMCOLE.NRCOLEPRECO)               AND
            (COLETAPR.CDOPERADOR   = V_CDOPERADOR)
            AND (FORNECEDOR.IDSITUCADA     = 'A')
            AND (NVL(FORNECEDOR.IDTIPOFORN, 'N') <> 'A')
            AND PRODUTO.CDPRODESTO     = NVL(P_CDPRODUTO, PRODUTO.CDPRODESTO)
            AND NOT EXISTS (SELECT
                               1
                            FROM
                               REFORFIL
                            WHERE
                               (CDFILIAL   = FILIAL.CDFILIAL)   AND
                               (CDFORNECED = ITEMCOLE.CDFORNECED) AND
                               (CDPERIENTR IS NULL))

            AND NOT EXISTS (SELECT
                               1
                            FROM
                               REFOREG
                            WHERE
                               (REFOREG.CDREGIAO   = ITEMCOLE.CDREGIAO) AND
                               (REFOREG.CDFORNECED = ITEMCOLE.CDFORNECED))

            AND NOT EXISTS (SELECT
                               1
                            FROM
                               QFOPRODFORN,
                               PARASUPRI
                            WHERE
                               (PARASUPRI.CDFILIAL     = P_CDFILIAL)       AND
                               (QFOPRODFORN.CDPRODUTO  = ITEMCOLE.CDPRODUTO)  AND
                               (QFOPRODFORN.CDFORNECED = ITEMCOLE.CDFORNECED) AND
                               ((QFOPRODFORN.IDFORNAPROV = 'N' AND QFOPRODFORN.IDFORNAUTOR = 'N') OR ((QFOPRODFORN.IDFORNAPROV = 'S') AND ((PARASUPRI.IDULTIDTVALQ = 'M' AND COLETAPR.DTCOLEPREC > LAST_DAY(QFOPRODFORN.DTVALPRFORN)) OR (PARASUPRI.IDULTIDTVALQ = 'D' AND COLETAPR.DTCOLEPREC > QFOPRODFORN.DTVALPRFORN)))))

            AND NOT EXISTS (SELECT
                               1
                            FROM
                               QFOMARPROD,
                               PARASUPRI
                            WHERE
                               (PARASUPRI.CDFILIAL   = P_CDFILIAL)      AND
                               (QFOMARPROD.CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                               (QFOMARPROD.CDMARCA   = ITEMCOLE.CDMARCA)   AND
                               ((QFOMARPROD.IDAPRMARPRO = 'N' AND QFOMARPROD.IDAUTMARPRO = 'N') OR ((QFOMARPROD.IDAPRMARPRO = 'S') AND ((PARASUPRI.IDULTIDTVALQ = 'M' AND COLETAPR.DTCOLEPREC > LAST_DAY(QFOMARPROD.DTVALAPROVA)) OR (PARASUPRI.IDULTIDTVALQ = 'D' AND COLETAPR.DTCOLEPREC > QFOMARPROD.DTVALAPROVA)))))

            AND EXISTS (SELECT
                           1
                        FROM
                           QFOMARPROD
                        WHERE
                           (QFOMARPROD.CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                           (QFOMARPROD.CDMARCA   = ITEMCOLE.CDMARCA)

                        UNION

                        SELECT
                           1
                        FROM
                           PARASUPRI
                        WHERE
                           (PARASUPRI.CDFILIAL    = P_CDFILIAL) AND
                           (PARASUPRI.IDMARSEMQUA = 'N' OR PARASUPRI.IDMARSEMQUA IS NULL))

            AND NOT EXISTS (SELECT
                               1
                            FROM
                               QUALIFICACAO,
                               PARASUPRI
                            WHERE
                               (QUALIFICACAO.CDQUALIF     = FORNECEDOR.CDQUALIF) AND
                               (PARASUPRI.CDFILIAL        = P_CDFILIAL)          AND
                               (FORNECEDOR.IDFORNAUTO     = 'N')                 AND
                               ((QUALIFICACAO.IDPERCOTCMP = '2') OR ((PARASUPRI.IDULTIDTVALQ  = 'D' AND COLETAPR.DTCOLEPREC > FORNECEDOR.DTVALQUALIF) OR (PARASUPRI.IDULTIDTVALQ = 'M' AND COLETAPR.DTCOLEPREC > LAST_DAY(FORNECEDOR.DTVALQUALIF)))))

            AND (

                  (EXISTS (SELECT
                              1
                           FROM
                              PARTFILI
                           WHERE
                              (CDFILIAL  = FILIAL.CDFILIAL)    AND
                              (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                              (CDTIPPART IN ('1', '2'))        AND
                              (CDMARCA   = ITEMCOLE.CDMARCA)   AND
                              (NOT EXISTS (SELECT
                                              1
                                           FROM
                                              PARTFILI
                                           WHERE
                                              (CDFILIAL   = FILIAL.CDFILIAL )       AND
                                              (CDPRODUTO  = ITEMCOLE.CDPRODUTO)     AND
                                              (CDTIPPART  IN ('4', '5'))            AND
                                              (CDFORNECED <> ITEMCOLE.CDFORNECED))

                               )
                            )



                   )

                  OR (EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL )    AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO)  AND
                                 (CDTIPPART  IN ('4', '5'))         AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED) AND
                                 (NOT EXISTS (SELECT
                                                 1
                                              FROM
                                                 PARTFILI
                                              WHERE
                                                (CDFILIAL  = FILIAL.CDFILIAL)    AND
                                                (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                                (CDTIPPART IN ('1', '2'))        AND
                                                (CDMARCA   <> ITEMCOLE.CDMARCA))
                                   )
                                )

                      )

                  OR (EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  IN ('7', '8'))            AND
                                 (CDMARCA    = ITEMCOLE.CDMARCA)   AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED)))

                  OR (EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL  = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  IN ('3','6','9')))   AND
                      NOT EXISTS (SELECT
                                     1
                                  FROM
                                     PARTFILI
                                  WHERE
                                     (CDFILIAL  = FILIAL.CDFILIAL)    AND
                                     (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                     (CDTIPPART  IN ('1','2','4','5','7','8')))
                      )

                  OR (NOT EXISTS (SELECT
                                     1
                                  FROM
                                     PARTFILI
                                  WHERE
                                     (CDFILIAL  = FILIAL.CDFILIAL) AND
                                     (CDPRODUTO = ITEMCOLE.CDPRODUTO)))
                 )
             AND (NOT EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL  = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART = '3')                    AND
                                 (CDMARCA   = ITEMCOLE.CDMARCA)))
             AND (NOT EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  = '6')                    AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED)))
             AND (NOT EXISTS (SELECT
                                 1
                              FROM
                                 PARTFILI
                              WHERE
                                 (CDFILIAL   = FILIAL.CDFILIAL)  AND
                                 (CDPRODUTO  = ITEMCOLE.CDPRODUTO) AND
                                 (CDTIPPART  = '9')                    AND
                                 (CDMARCA    = ITEMCOLE.CDMARCA)   AND
                                 (CDFORNECED = ITEMCOLE.CDFORNECED)))
            GROUP BY
               FILIAL.CDFILIAL,
               ITEMCOLE.CDFILIAL,
               P_NRSEQPRELIST,
               ITEMCOLE.DTINIVALITM,
               ITEMCOLE.DTFINVALITM,
               PRODUTO.CDPRODESTO;
         COMMIT;


         IF (V_IDUTCUSTOM1 = 'S') OR (V_IDUTCUSTOM2 = 'S') THEN
            FOR C_DATAS IN
              (SELECT
                  DISTINCT
                  DTINIVALITM
               FROM
                  CUSTOSUPACOR_TMP
               WHERE
                  CUSTOSUPACOR_TMP.CDFILCENTCMP = P_CDFILIAL AND
                  CUSTOSUPACOR_TMP.NRSEQPRELIST = P_NRSEQPRELIST)
            LOOP
               IF (V_IDUTCUSTOM1 = 'S') THEN
                  SELECT
                     NVL(MAX(MOVINDICE.VRINDICE), 0)
                  INTO
                     V_VRINDICEM1(TO_CHAR(C_DATAS.DTINIVALITM, 'DDMMYYYY'))
                  FROM
                     PARAMGERAL,
                     MOVINDICE
                  WHERE
                     (PARAMGERAL.CDINDICEMF = MOVINDICE.CDINDICE)  AND
                     (MOVINDICE.DTREFIND    = C_DATAS.DTINIVALITM) AND
                     (MOVINDICE.CDTPINDICE  = '1')                 AND
                     (MOVINDICE.IDPERVALOR  = '2');
               END IF;

               IF (V_IDUTCUSTOM2 = 'S') THEN
                  SELECT
                     NVL(MAX(MOVINDICE.VRINDICE), 0)
                  INTO
                     V_VRINDICEM2(TO_CHAR(C_DATAS.DTINIVALITM, 'DDMMYYYY'))
                  FROM
                     PARAMGERAL,
                     MOVINDICE
                  WHERE
                     (PARAMGERAL.CDINDICEMF2 = MOVINDICE.CDINDICE)  AND
                     (MOVINDICE.DTREFIND     = C_DATAS.DTINIVALITM) AND
                     (MOVINDICE.CDTPINDICE   = '1')                 AND
                     (MOVINDICE.IDPERVALOR   = '2');
               END IF;
            END LOOP;
         END IF;

         FOR C_ACORDO IN
           (SELECT
               CUSTO.CDFILIAL,
               CUSTO.DTINIVALITM,
               CUSTO.DTFINVALITM,
               CUSTO.CDPRODESTO,
               MINPRECO
            FROM
               CUSTOSUPACOR_TMP CUSTO
            WHERE
               CUSTO.CDFILCENTCMP = P_CDFILIAL AND
               CUSTO.NRSEQPRELIST = P_NRSEQPRELIST)
         LOOP
            V_POSMINPREPI := INSTR(C_ACORDO.MINPRECO, '#');
            V_POSMINPRECO := INSTR(C_ACORDO.MINPRECO, '%');

            V_MINPRECO    := (SUBSTR(C_ACORDO.MINPRECO, V_POSMINPREPI + 1, V_POSMINPRECO - V_POSMINPREPI - 1))/V_MULTIPLO;
            V_NRCOLEPRECO := SUBSTR(C_ACORDO.MINPRECO, V_POSMINPRECO+1, 10);
            V_CDREGIAO    := SUBSTR(C_ACORDO.MINPRECO, V_POSMINPRECO+11, 3);
            V_CDPRODUTO   := SUBSTR(C_ACORDO.MINPRECO, V_POSMINPRECO+14, 10);
            V_CDMARCA     := SUBSTR(C_ACORDO.MINPRECO, V_POSMINPRECO+24, 5);
            V_CDFORNECED  := SUBSTR(C_ACORDO.MINPRECO, V_POSMINPRECO+29, 21);

            V_VRPERAJFORNFIL := NULL;
            V_IDCONSPERPIAJPR := 'N';
            SELECT
               MAX(VRPERAJFORNFIL),
               MAX(IDCONSPERPIAJPR)
            INTO V_VRPERAJFORNFIL, V_IDCONSPERPIAJPR
            FROM AJUSTEPRFORNFIL
            WHERE AJUSTEPRFORNFIL.CDFORNECED = V_CDFORNECED
              AND AJUSTEPRFORNFIL.CDFILIAL = C_ACORDO.CDFILIAL
              AND AJUSTEPRFORNFIL.DTINIAJPRFORFIL = (SELECT MAX(A.DTINIAJPRFORFIL)
                                                     FROM AJUSTEPRFORNFIL A
                                                     WHERE A.CDFORNECED = V_CDFORNECED
                                                       AND A.CDFILIAL = C_ACORDO.CDFILIAL
                                                       AND TRUNC(SYSDATE) BETWEEN A.DTINIAJPRFORFIL AND NVL(A.DTFINAJPRFORFIL, TRUNC(SYSDATE)));

            V_PIDSV := NULL;
            IF V_IDCONSPERPIAJPR = 'S' THEN
               SELECT
                  MAX(VRPERPIPRFO)
               INTO V_PIDSV
               FROM PRODFORNCOMP
               WHERE PRODFORNCOMP.CDFORNECED  = V_CDFORNECED
                 AND PRODFORNCOMP.CDPRODCOMPL = V_CDPRODUTO;

               IF V_PIDSV IS NULL THEN
                  SELECT MAX(VRPERPIFORN)
                  INTO V_PIDSV
                  FROM FORNECEDOR
                  WHERE FORNECEDOR.CDFORNECED = V_CDFORNECED;
               END IF;
            END IF;

            IF V_PIDSV IS NULL THEN
               V_PIDSV := 0;
            END IF;
            IF V_VRPERAJFORNFIL IS NULL THEN
               V_VRPERAJFORNFIL := 0;
            END IF;

            V_MINPRECO := V_MINPRECO - (V_MINPRECO * V_PIDSV/100) + (V_MINPRECO * V_VRPERAJFORNFIL/100);

            IF V_IDUTCUSLIPR = 'S' THEN
               V_VRIMPITM := 0;
               FOR C_IMPOSTO IN
                  (SELECT
                      IMPOSTO.CDIMPOSTO,
                      IMPOSTO.IDTRATIMPO,
                      IMPOSTO.IDIMPORECUP,
                      IMPOSTO.CDCCTBLATIV,
                      IMPOSTO.CDCCTBLPASS,
                      IMPCOLET.VRADIMPCOL,
                      IMPCOLET.VRALIQIMPCOL,
                      IMPCOLET.VRIMPCOLETA,
                      IMPCOLET.CDFISCITMCOL
                   FROM
                      IMPCOLET,
                      IMPOSTO
                   WHERE
                      IMPCOLET.CDFILIAL    = P_CDFILIAL    AND
                      IMPCOLET.NRCOLEPRECO = V_NRCOLEPRECO AND
                      IMPCOLET.CDFORNECED  = V_CDFORNECED  AND
                      IMPCOLET.CDREGIAO    = V_CDREGIAO    AND
                      IMPCOLET.CDPRODUTO   = V_CDPRODUTO   AND
                      IMPCOLET.CDMARCA     = V_CDMARCA     AND
                      IMPCOLET.CDIMPOSTO   = IMPOSTO.CDIMPOSTO
                   )
               LOOP
                  SELECT
                     MAX(CDIMPOSTO),
                     MAX(IDVRIMPSTRIB)
                  INTO
                     V_CDIMPOSTRIB,
                     V_IDVRIMPSTRIB
                  FROM
                     IMPOSTO
                  WHERE
                     CDIMPOSTRIB = C_IMPOSTO.CDIMPOSTO;
                  IF (C_IMPOSTO.IDIMPORECUP = 'S') THEN
                     IF (C_IMPOSTO.CDFISCITMCOL = '1') THEN
                        IF (C_IMPOSTO.CDCCTBLATIV IS NOT NULL) THEN
                           IF C_IMPOSTO.IDTRATIMPO = 'V' THEN
                              V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                           END IF;
                        END IF;
                     ELSE
                        IF C_IMPOSTO.IDTRATIMPO = 'A' THEN
                           IF V_CDIMPOSTRIB IS NULL THEN
                              V_VRIMPITM := V_VRIMPITM + C_IMPOSTO.VRIMPCOLETA;
                           END IF;
                        ELSIF C_IMPOSTO.IDTRATIMPO IN ('D','R') THEN
                           V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                        END IF;
                     END IF;
                  ELSE
                     IF C_IMPOSTO.CDCCTBLPASS IS NOT NULL THEN
                        IF C_IMPOSTO.IDTRATIMPO = 'V' THEN
                           V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                        END IF;
                     ELSE
                        IF C_IMPOSTO.IDTRATIMPO = 'A' THEN
                           IF V_CDIMPOSTRIB IS NULL THEN
                              V_VRIMPITM := V_VRIMPITM + C_IMPOSTO.VRIMPCOLETA;
                           END IF;
                        ELSIF C_IMPOSTO.IDTRATIMPO IN ('D','R') THEN
                           V_VRIMPITM := V_VRIMPITM - C_IMPOSTO.VRIMPCOLETA;
                        END IF;
                     END IF;
                  END IF;
               END LOOP;
               BEGIN
                  SELECT
                     NVL(VRFATOCONV, 1)
                  INTO
                     V_VRFATOCONV
                  FROM
                     PRODUTO
                  WHERE
                     PRODUTO.CDPRODUTO = V_CDPRODUTO;
               EXCEPTION
                  WHEN NO_DATA_FOUND THEN
                     V_VRFATOCONV := 1;
               END;
               IF V_VRFATOCONV IS NULL THEN
                  V_VRFATOCONV := 1;
               END IF;
            END IF;

            BEGIN
               IF (V_IDUTCUSTOM1 = 'N') OR (V_VRINDICEM1(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY')) = 0) THEN
                  V_VRCUSPRLISM1 := 0;
               ELSE
                  V_VRCUSPRLISM1 := NVL(V_MINPRECO, 0) / V_VRINDICEM1(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY'));
               END IF;
               IF (V_IDUTCUSTOM2 = 'N') OR (V_VRINDICEM2(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY')) = 0) THEN
                  V_VRCUSPRLISM2 := 0;
               ELSE
                  V_VRCUSPRLISM2 := NVL(V_MINPRECO, 0) / V_VRINDICEM2(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY'));
               END IF;

               /* Insere um novo registro de custo de lista de precos - custo de acordo */

               INSERT INTO CUSTOSUP
                 (CDFILIAL,
                  CDPRODUTO,
                  DTCUSTOSUP,
                  VRCUSPRLISTP,
                  CDFILCOTACOR,
                  NRCOLEPRACOR,
                  CDREGIAOACOR,
                  CDFORNECACOR,
                  CDPRODACOR,
                  CDMARCAACOR,
                  VRCUSPRLISM1,
                  VRCUSPRLISM2,
                  DTFINCUSTSUP,
                  VRCUSLIPRIMP)
               VALUES
                 (C_ACORDO.CDFILIAL,
                  C_ACORDO.CDPRODESTO,
                  C_ACORDO.DTINIVALITM,
                  V_MINPRECO,
                  P_CDFILIAL,
                  V_NRCOLEPRECO,
                  V_CDREGIAO,
                  V_CDFORNECED,
                  V_CDPRODUTO,
                  V_CDMARCA,
                  V_VRCUSPRLISM1,
                  V_VRCUSPRLISM2,
                  C_ACORDO.DTFINVALITM,
                  DECODE(V_IDUTCUSLIPR, 'N', NULL, ((V_MINPRECO*V_VRFATOCONV) + V_VRIMPITM)/V_VRFATOCONV));

               COMMIT;
            EXCEPTION
               WHEN DUP_VAL_ON_INDEX THEN BEGIN

                  /* Caso ja exista um custo de lista de precos com data de
                     inicio igual ao custo de acordo e com a data final maior,
                     ou seja, se o custo de acordo estiver dentro do perodo de
                     uma lista, entao o custo de lista passara a ter a data
                     de inicio um dia depois da data final do custo de acordo */

                  IF V_IDUTCUSLIPR = 'S' THEN
                     SELECT
                        NVL(MAX(VRFATOCONV),1)
                     INTO
                        V_VRFATOCONV
                     FROM
                        PRODUTO
                     WHERE
                        PRODUTO.CDPRODUTO = C_ACORDO.CDPRODESTO;
                  END IF;

                  BEGIN
                     INSERT INTO CUSTOSUP
                       (CDFILIAL,
                        CDPRODUTO,
                        DTCUSTOSUP,
                        VRCUSPRLISTP,
                        CDFILCOTLIST,
                        NRCOLEPRLIST,
                        CDREGIAOLIST,
                        CDFORNECLIST,
                        CDPRODLIST,
                        CDMARCALIST,
                        VRCUSPRLISM1,
                        VRCUSPRLISM2,
                        VRHISPRLISTP,
                        DTFINCUSTSUP,
                        VRCUSLIPRIMP)
                     SELECT
                        CUSTOSUP.CDFILIAL,
                        CUSTOSUP.CDPRODUTO,
                        C_ACORDO.DTFINVALITM + 1,
                        CUSTOSUP.VRCUSPRLISTP,
                        CUSTOSUP.CDFILCOTLIST,
                        CUSTOSUP.NRCOLEPRLIST,
                        CUSTOSUP.CDREGIAOLIST,
                        CUSTOSUP.CDFORNECLIST,
                        CUSTOSUP.CDPRODLIST,
                        CUSTOSUP.CDMARCALIST,
                        CUSTOSUP.VRCUSPRLISM1,
                        CUSTOSUP.VRCUSPRLISM2,
                        CUSTOSUP.VRHISPRLISTP,
                        PREVLISTA.DTFINVALLIST,
                        CUSTOSUP.VRCUSLIPRIMP
                     FROM
                        CUSTOSUP,
                        PREVLISTA
                     WHERE
                        (CUSTOSUP.CDFILIAL      = C_ACORDO.CDFILIAL)    AND
                        (CUSTOSUP.CDPRODUTO     = C_ACORDO.CDPRODESTO)  AND
                        (CUSTOSUP.DTCUSTOSUP    = C_ACORDO.DTINIVALITM) AND
                        (PREVLISTA.CDFILIAL     = P_CDFILIAL)           AND
                        (PREVLISTA.NRSEQPRELIST = P_NRSEQPRELIST)       AND
                        (PREVLISTA.DTINIVALLIST = C_ACORDO.DTINIVALITM) AND
                        (PREVLISTA.DTFINVALLIST > C_ACORDO.DTFINVALITM);
                  EXCEPTION
                     WHEN DUP_VAL_ON_INDEX THEN BEGIN
                        /* Deletando custo imediatamente posterior ao do acordo */
                        DELETE FROM CUSTOSUP
                        WHERE
                           (CDFILIAL   = C_ACORDO.CDFILIAL)   AND
                           (CDPRODUTO  = C_ACORDO.CDPRODESTO) AND
                           (DTCUSTOSUP = C_ACORDO.DTFINVALITM + 1);

                        INSERT INTO CUSTOSUP
                          (CDFILIAL,
                           CDPRODUTO,
                           DTCUSTOSUP,
                           VRCUSPRLISTP,
                           CDFILCOTLIST,
                           NRCOLEPRLIST,
                           CDREGIAOLIST,
                           CDFORNECLIST,
                           CDPRODLIST,
                           CDMARCALIST,
                           VRCUSPRLISM1,
                           VRCUSPRLISM2,
                           VRHISPRLISTP,
                           DTFINCUSTSUP,
                           VRCUSLIPRIMP)
                        SELECT
                           CUSTOSUP.CDFILIAL,
                           CUSTOSUP.CDPRODUTO,
                           C_ACORDO.DTFINVALITM + 1,
                           CUSTOSUP.VRCUSPRLISTP,
                           CUSTOSUP.CDFILCOTLIST,
                           CUSTOSUP.NRCOLEPRLIST,
                           CUSTOSUP.CDREGIAOLIST,
                           CUSTOSUP.CDFORNECLIST,
                           CUSTOSUP.CDPRODLIST,
                           CUSTOSUP.CDMARCALIST,
                           CUSTOSUP.VRCUSPRLISM1,
                           CUSTOSUP.VRCUSPRLISM2,
                           CUSTOSUP.VRHISPRLISTP,
                           PREVLISTA.DTFINVALLIST,
                           CUSTOSUP.VRCUSLIPRIMP
                        FROM
                           CUSTOSUP,
                           PREVLISTA
                        WHERE
                           (CUSTOSUP.CDFILIAL      = C_ACORDO.CDFILIAL)    AND
                           (CUSTOSUP.CDPRODUTO     = C_ACORDO.CDPRODESTO)  AND
                           (CUSTOSUP.DTCUSTOSUP    = C_ACORDO.DTINIVALITM) AND
                           (PREVLISTA.CDFILIAL     = P_CDFILIAL)           AND
                           (PREVLISTA.NRSEQPRELIST = P_NRSEQPRELIST)       AND
                           (PREVLISTA.DTINIVALLIST = C_ACORDO.DTINIVALITM) AND
                           (PREVLISTA.DTFINVALLIST > C_ACORDO.DTFINVALITM);
                     END;
                  END;
                  COMMIT;

                  IF (V_IDUTCUSTOM1 = 'N') OR (V_VRINDICEM1(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY')) = 0) THEN
                     V_VRCUSPRLISM1 := 0;
                  ELSE
                     V_VRCUSPRLISM1 := NVL(V_MINPRECO, 0) / V_VRINDICEM1(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY'));
                  END IF;
                  IF (V_IDUTCUSTOM2 = 'N') OR (V_VRINDICEM2(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY')) = 0) THEN
                     V_VRCUSPRLISM2 := 0;
                  ELSE
                     V_VRCUSPRLISM2 := NVL(V_MINPRECO, 0) / V_VRINDICEM2(TO_CHAR(C_ACORDO.DTINIVALITM,'DDMMYYYY'));
                  END IF;

                  /* Atualizando custo de acordo */

                  UPDATE CUSTOSUP SET
                     VRCUSPRLISTP = V_MINPRECO,
                     CDFILCOTACOR = P_CDFILIAL,
                     NRCOLEPRACOR = V_NRCOLEPRECO,
                     CDREGIAOACOR = V_CDREGIAO,
                     CDFORNECACOR = V_CDFORNECED,
                     CDPRODACOR   = V_CDPRODUTO,
                     CDMARCAACOR  = V_CDMARCA,
                     DTFINCUSTSUP = C_ACORDO.DTFINVALITM,
                     VRCUSPRLISM1 = V_VRCUSPRLISM1,
                     VRCUSPRLISM2 = V_VRCUSPRLISM2,
                     VRCUSLIPRIMP = DECODE(V_IDUTCUSLIPR, 'N', NULL, ((V_MINPRECO*V_VRFATOCONV) + V_VRIMPITM)/V_VRFATOCONV)
                  WHERE
                     (CDFILIAL   = C_ACORDO.CDFILIAL)   AND
                     (CDPRODUTO  = C_ACORDO.CDPRODESTO) AND
                     (DTCUSTOSUP = C_ACORDO.DTINIVALITM);
                  COMMIT;
               END;
            END;

            /* Update e Insert a seguir:
               Faz o tratamento dos custos de lista de precos que estao dentro
               do periodo de validade da cotacao de acordo: esses custos sao
               excluidos pois o custo valido deve ser o custo do acordo.
            */

            UPDATE CUSTOSUP
            SET
               VRHISPRLISTP = NULL,
               VRCUSPRLISTP = NULL,
               VRCUSPRLISM1 = NULL,
               VRCUSPRLISM2 = NULL,
               CDFILCOTLIST = NULL,
               NRCOLEPRLIST = NULL,
               CDMARCALIST  = NULL,
               CDREGIAOLIST = NULL,
               CDFORNECLIST = NULL,
               CDPRODLIST   = NULL
            WHERE
               (CDFILIAL   = C_ACORDO.CDFILIAL)    AND
               (CDPRODUTO  = C_ACORDO.CDPRODESTO)  AND
               (DTCUSTOSUP > C_ACORDO.DTINIVALITM) AND
               (DTCUSTOSUP < C_ACORDO.DTFINVALITM) AND
               (VRPRCUSTOAF IS NOT NULL);

            DELETE CUSTOSUP
            WHERE
               (CDFILIAL   = C_ACORDO.CDFILIAL)    AND
               (CDPRODUTO  = C_ACORDO.CDPRODESTO)  AND
               (DTCUSTOSUP > C_ACORDO.DTINIVALITM) AND
               (DTCUSTOSUP < C_ACORDO.DTFINVALITM) AND
               (VRPRCUSTOAF IS NULL);
            COMMIT;
         END LOOP;

         DELETE FROM CUSTOSUP_TMP
         WHERE CUSTOSUP_TMP.CDFILCENTCMP = P_CDFILIAL AND
               CUSTOSUP_TMP.NRSEQPRELIST = P_NRSEQPRELIST;

         DELETE FROM CUSTOSUPACOR_TMP
         WHERE CUSTOSUPACOR_TMP.CDFILCENTCMP = P_CDFILIAL AND
               CUSTOSUPACOR_TMP.NRSEQPRELIST = P_NRSEQPRELIST;

         IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
            UPDATE TEMPULTATULISPR
            SET QTFILIALLISPREC = V_AUX_FILIAL - 1,
                CDFILULTLISPREC = V_FILIAL_CDFILIAL
            WHERE CDCENULTLISPREC = P_CDFILIAL
              AND NRSEQULTLISPREC = P_NRSEQPRELIST;
            COMMIT;
         END IF;
      END LOOP;
      CLOSE C_FILIAL;
      IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
         UPDATE TEMPULTATULISPR
         SET QTREGIAOLISPREC = V_AUX_REGIAOGERAL - 1,
             CDREGULTLISPREC = V_REGIAOGERAL_CDREGIAO
         WHERE CDCENULTLISPREC = P_CDFILIAL
           AND NRSEQULTLISPREC = P_NRSEQPRELIST;
         COMMIT;
      END IF;
   END LOOP;
   CLOSE C_REGIAOGERAL;

   FOR C_ATUALIZA IN
      (SELECT DISTINCT
          CDREGIAO
       FROM
          FILIAL,
          LISTA
       WHERE
          NRLISTA = V_LISTA_FILIAL        AND
          FILIAL.CDFILIAL = LISTA.CDLISTA AND
          FILIAL.CDREGIAO IS NOT NULL)
   LOOP
      UPDATE ITEMCOLE
         SET ITEMCOLE.IDITECOLEALT = 'N'
      WHERE
         ITEMCOLE.CDFILIAL     = P_CDFILIAL          AND
         (ITEMCOLE.NRSEQPRELIST = DECODE(ITEMCOLE.IDITVALDIFLISTA, 'S', ITEMCOLE.NRSEQPRELIST, P_NRSEQPRELIST)) AND
         (ITEMCOLE.DTINIVALITM <= V_DTFINVALLIST)    AND
         (ITEMCOLE.DTFINVALITM >= V_DTINIVALLIST)    AND
         ITEMCOLE.CDREGIAO     = C_ATUALIZA.CDREGIAO AND
         NVL(ITEMCOLE.IDITECOLEALT, 'S') = 'S';

      DELETE FROM
         ITEXCLISPREC
      WHERE
         ITEXCLISPREC.CDFILIAL     = P_CDFILIAL          AND
         ITEXCLISPREC.NRSEQPRELIST = P_NRSEQPRELIST      AND
         ITEXCLISPREC.CDREGIAO     = C_ATUALIZA.CDREGIAO;
      COMMIT;
   END LOOP;
   IF (P_CDFILCUSTO IS NULL) AND (P_CDLISTAREG IS NULL) THEN
      DELETE FROM TEMPULTATULISPR
      WHERE CDCENULTLISPREC = P_CDFILIAL
        AND NRSEQULTLISPREC = P_NRSEQPRELIST;
   END IF;

   DELETE FROM LISTA WHERE NRLISTA = V_LISTA_FILIAL;
   COMMIT;
END;
/