create or replace PACKAGE SUP_COTACAO AS
   /*
    * Teknisa Service
    * Package com procedimentos das rotinas relacionadas a cotação de preços
    */

   /*Constants*/
   K_S                     CONSTANT VARCHAR2(01) := 'S';
   K_N                     CONSTANT VARCHAR2(01) := 'N';
   K_T                     CONSTANT VARCHAR2(01) := 'T';
   K_A                     CONSTANT VARCHAR2(01) := 'A';
   K_C                     CONSTANT VARCHAR2(01) := 'C';
   K_0                     CONSTANT VARCHAR2(01) := '0';
   K_1                     CONSTANT VARCHAR2(01) := '1';
   K_WW                    CONSTANT VARCHAR2(02) := 'WW';
   K_SUCCESS               CONSTANT VARCHAR2(07) := 'SUCCESS';
   K_ERROR                 CONSTANT VARCHAR2(05) := 'ERROR';
   K_PZOPGTPRFO            CONSTANT VARCHAR2(30) := 'PZOPGTPRFO';
   K_PRAZOPGTFORN          CONSTANT VARCHAR2(30) := 'PRAZOPGTFORN';
   K_COTACAO_CONCLUIDA     CONSTANT VARCHAR2(30) := 'COTACAO_CONCLUIDA_IDCOTCONCLUI';
   K_DSMET_DUPLICA_COTACAO CONSTANT VARCHAR2(30) := 'SUP_COTACAO.DUPLICA_COTACAO';
   K_NRORG                 CONSTANT NUMBER       := NVL(SYS_CONTEXT('nrorg_ctx','nrorg'), 1);

   /*Subtype*/
   SUBTYPE S_RECORD_COLETAPR     IS COLETAPR%ROWTYPE;
   SUBTYPE S_RECORD_ITEMCOLE     IS ITEMCOLE%ROWTYPE;
   SUBTYPE S_RECORD_LOGCOLETAPR  IS LOGCOLETAPR%ROWTYPE;
   SUBTYPE S_RECORD_LOGITEMCOLE  IS LOGITEMCOLE%ROWTYPE;
   SUBTYPE S_RECORD_PZOPGTITMCOL IS PZOPGTITMCOL%ROWTYPE;

   PROCEDURE INSERT_COLETAPR(P_REGISTRO      IN OUT S_RECORD_COLETAPR,
                             P_PARASUPRI     IN PARASUPRI%ROWTYPE,
                             P_ERROR        OUT VARCHAR2);

   PROCEDURE INSERT_ITEMCOLE(P_REGISTRO     IN OUT S_RECORD_ITEMCOLE,
                             P_PARASUPRI    IN PARASUPRI%ROWTYPE,
                             P_FORNECEDORES IN FORNECEDOR%ROWTYPE,
                             P_ERROR       OUT VARCHAR2);

   PROCEDURE DELETE_PZOPGTITMCOL(P_REGISTRO IN S_RECORD_ITEMCOLE,
                                 P_ERROR   OUT VARCHAR2);

   PROCEDURE INSERT_PZOPGTITMCOL(P_REGISTRO   IN S_RECORD_ITEMCOLE,
                                 P_CDPRODFORN IN PRODFORN.CDPRODUTO%TYPE DEFAULT NULL,
                                 P_ERROR     OUT VARCHAR2);

   PROCEDURE INSERT_LOGCOLETAPR(P_COLETAPR      IN S_RECORD_COLETAPR,
                                P_DSENDUSULOG   IN LOGCOLETAPR.DSENDUSULOG%TYPE,
                                P_DSNMUSULOG    IN LOGCOLETAPR.DSNMUSULOG%TYPE,
                                P_DSRDMUSULOG   IN LOGCOLETAPR.DSRDMUSULOG%TYPE,
                                P_DSMETUSULOG   IN LOGCOLETAPR.DSMETUSULOG%TYPE,
                                P_CDOPERINCLOG  IN LOGCOLETAPR.CDOPERINCLOG%TYPE,
                                P_IDOPERCOTLOG  IN LOGCOLETAPR.IDOPERCOTLOG%TYPE,
                                P_DSMESSAGERROR IN LOGCOLETAPR.DSMESSAGERROR%TYPE); 

   PROCEDURE INSERT_LOGITEMCOLE(P_ITEMCOLE      IN S_RECORD_ITEMCOLE,
                                P_DSENDUSULIC   IN LOGITEMCOLE.DSENDUSULIC%TYPE,
                                P_DSNMUSULIC    IN LOGITEMCOLE.DSNMUSULIC%TYPE,
                                P_DSRDMUSULIC   IN LOGITEMCOLE.DSRDMUSULIC%TYPE,
                                P_DSMETUSULIC   IN LOGITEMCOLE.DSMETUSULIC%TYPE,
                                P_CDOPERLIC     IN LOGITEMCOLE.CDOPERLIC%TYPE,
                                P_IDOPERALIC    IN LOGITEMCOLE.IDOPERALIC%TYPE,
                                P_DSMESSAGERROR IN LOGITEMCOLE.DSMESSAGERROR%TYPE);

   PROCEDURE COTACAO_OPL(P_CDFILIAL    IN COLETAPR.CDFILIAL%TYPE,
                         P_NRCOLEORIG  IN COLETAPR.NRCOLEPRECO%TYPE,
                         P_NRCOLEPRECO IN COLETAPR.NRCOLEPRECO%TYPE,
                         P_FORNOL      IN VARCHAR2,
                         P_CDREGIAOL   IN VARCHAR2,
                         P_REAJUSTE    IN NUMBER,
                         P_DTNOVINIVAL IN COLETAPR.DTINIVALCOL%TYPE,
                         P_DTNOVFINVAL IN COLETAPR.DTFINVALCOL%TYPE,
                         P_CDOPERINCL  IN OPERADOR.CDOPERADOR%TYPE,
                         P_ERROR      OUT VARCHAR2);

   PROCEDURE DUPLICA_COTACAO(P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
                             P_CDFILIPROC   IN FILIAL.CDFILIAL%TYPE,
                             P_NRCOLEPRECO  IN COLETAPR.NRCOLEPRECO%TYPE,
                             P_NRSEMAINIC   IN COLETAPR.NRSEMAINIC%TYPE,
                             P_NRSEMAFINAL  IN COLETAPR.NRSEMAFINAL%TYPE,
                             P_DTNOVINIVAL  IN COLETAPR.DTINIVALCOL%TYPE,
                             P_DTNOVFINVAL  IN COLETAPR.DTFINVALCOL%TYPE,
                             P_DTINIVALCOL  IN COLETAPR.DTINIVALCOL%TYPE,
                             P_DTFINVALCOL  IN COLETAPR.DTFINVALCOL%TYPE,
                             P_DTCOLEPREC   IN COLETAPR.DTCOLEPREC%TYPE,
                             P_REAJUSTE     IN NUMBER,
                             P_NRCOLEDUP    IN COLETAPR.NRCOLEPRECO%TYPE,
                             P_IDDUPLICA    IN VARCHAR2,
                             P_CDOPERADOR   IN VARCHAR2 DEFAULT NULL,
                             P_TCDOPERADOR  IN VARCHAR2,
                             P_CDOPERINCL   IN VARCHAR2,
                             P_CDREGIAO     IN VARCHAR2 DEFAULT NULL,
                             P_DUPLICAOL    IN VARCHAR2 DEFAULT 'N',
                             P_CDFORNOL     IN VARCHAR2 DEFAULT NULL,
                             P_CDREGIAOL    IN VARCHAR2 DEFAULT NULL,
                             P_COTA_N_DUP   OUT VARCHAR2);

   PROCEDURE GRAVA_COTWEB(P_CDFILIAL     IN PEDCOLETA.CDFILIAL%TYPE,
                          P_NRPEDCOLETA  IN PEDCOLETA.NRPEDCOLETA%TYPE,
                          P_CDREGIAO     IN PEDCOLETA.CDREGIAO%TYPE,
                          P_CDFORNECED   IN PEDCOLETA.CDFORNECED%TYPE,
                          P_NRSEMINI     IN COLETAPR.NRSEMAINIC%TYPE,
                          P_NRSEMFIN     IN COLETAPR.NRSEMAFINAL%TYPE,
                          P_ENTRALISTA   IN VARCHAR2,
                          P_COTA_N_GER  OUT VARCHAR2,
                          P_CDOPERINCL   IN VARCHAR2 DEFAULT NULL,
                          P_TRANSACAO    IN VARCHAR2 DEFAULT 'S',
                          P_CDCOMPRADOR  IN OPERADOR.CDOPERADOR%TYPE DEFAULT NULL,
                          P_DTINIVALITM  IN ITEMCOLE.DTINIVALITM%TYPE DEFAULT NULL,
                          P_DTFINVALITM  IN ITEMCOLE.DTFINVALITM%TYPE DEFAULT NULL);
END SUP_COTACAO;
/

CREATE OR REPLACE PACKAGE BODY SUP_COTACAO AS

   PROCEDURE INSERT_COLETAPR(P_REGISTRO      IN OUT S_RECORD_COLETAPR,
                             P_PARASUPRI     IN PARASUPRI%ROWTYPE,
                             P_ERROR        OUT VARCHAR2) IS
   BEGIN        
      P_REGISTRO.IDSITUCOLETA := CASE NVL(P_PARASUPRI.IDUTISTATCOT, K_N) WHEN K_S THEN K_A ELSE K_C END;
      P_REGISTRO.DTINCLCOLET  := SYSDATE;  

      SELECT TO_CHAR(TO_DATE(P_REGISTRO.DTINIVALCOL, 'DD/MM/YYYY HH24:MI:SS'), K_WW), 
             TO_CHAR(TO_DATE(P_REGISTRO.DTFINVALCOL, 'DD/MM/YYYY HH24:MI:SS'), K_WW)
        INTO P_REGISTRO.NRSEMAINIC, 
             P_REGISTRO.NRSEMAFINAL
        FROM DUAL;

      BEGIN
         INSERT INTO COLETAPR
              VALUES P_REGISTRO;
      EXCEPTION
            WHEN OTHERS THEN
               P_ERROR := SQLERRM;
      END;
   END INSERT_COLETAPR;

   PROCEDURE INSERT_ITEMCOLE(P_REGISTRO     IN OUT S_RECORD_ITEMCOLE,
                             P_PARASUPRI    IN PARASUPRI%ROWTYPE,
                             P_FORNECEDORES IN FORNECEDOR%ROWTYPE,
                             P_ERROR       OUT VARCHAR2) IS
      V_PRODFORN PRODFORN%ROWTYPE;
      V_ERROR    VARCHAR2(4000);
   BEGIN
      /*Fornecedor atende região*/
      V_ERROR := SUP_VALIDATIONS.FORNECEDOR_ATENDE_REGIAO(P_CDFORNECED => P_REGISTRO.CDFORNECED,
                                                          P_CDREGIAO   => P_REGISTRO.CDREGIAO);
      IF V_ERROR IS NULL THEN
         /*Qualificação produto por fornecedor*/
         V_ERROR := SUP_VALIDATIONS.QUALIFICACAO_PROD_FORN(P_CDPRODUTO  => P_REGISTRO.CDPRODUTO,
                                                           P_CDFORNECED => P_REGISTRO.CDFORNECED,
                                                           P_DTPEDIDO   => P_REGISTRO.DTFINVALITM,
                                                           P_PARASUPRI  => P_PARASUPRI);
      END IF;

      IF V_ERROR IS NULL THEN
         /*Qualificação marca por produto*/
         V_ERROR := SUP_VALIDATIONS.QUALIFICACAO_MARCA_PRODUTO(P_CDPRODUTO => P_REGISTRO.CDPRODUTO,
                                                               P_CDMARCA   => P_REGISTRO.CDMARCA,
                                                               P_DTPEDIDO  => P_REGISTRO.DTFINVALITM,
                                                               P_PARASUPRI => P_PARASUPRI);

      END IF;

      IF V_ERROR IS NULL THEN
         /*Produto não cadastrado para o fornecedor*/
         V_ERROR := SUP_VALIDATIONS.CHECK_PRODUTO_FORNECEDOR(P_CDPRODUTO  => P_REGISTRO.CDPRODUTO,
                                                             P_CDFORNECED => P_REGISTRO.CDFORNECED);
      END IF;

      IF V_ERROR IS NULL THEN
         BEGIN
               SELECT *
                 INTO V_PRODFORN
                 FROM PRODFORN
                WHERE CDFORNECED = P_REGISTRO.CDFORNECED
                  AND EXISTS (SELECT 1
                                FROM PRODFORN PF
                                JOIN PRODUTO PI ON PI.CDPRODUTO = PF.CDPRODUTO
                                JOIN PRODUTO PR ON PR.CDARVPROD LIKE PI.CDARVPROD || '%'
                               WHERE PF.CDFORNECED = PRODFORN.CDFORNECED
                                 AND PI.CDPRODUTO  = PRODFORN.CDPRODUTO
                                 AND PR.CDPRODUTO  = P_REGISTRO.CDPRODUTO);

               P_REGISTRO.CDFORMPGTO   := V_PRODFORN.CDFORMPGTO; 
               P_REGISTRO.IDTPFOPGTCOL := V_PRODFORN.IDTPFORPGPRF;
               P_REGISTRO.NRPARCPGTCOL := V_PRODFORN.NRPARCPGTPRF;
               P_REGISTRO.NRDIAPGFXCOL := V_PRODFORN.NRDIAPGFIPRF;
               P_REGISTRO.IDUTDIAPGCOL := V_PRODFORN.IDUTDIAPGPRF;

               BEGIN
                  INSERT INTO ITEMCOLE
                       VALUES P_REGISTRO;
               EXCEPTION
                  WHEN OTHERS THEN
                     V_ERROR := SQLERRM;
               END;

               IF V_ERROR IS NULL THEN
                  DELETE_PZOPGTITMCOL(P_REGISTRO => P_REGISTRO,
                                      P_ERROR    => V_ERROR);

                  INSERT_PZOPGTITMCOL(P_REGISTRO   => P_REGISTRO,
                                      P_CDPRODFORN => V_PRODFORN.CDPRODUTO,
                                      P_ERROR      => V_ERROR);
               END IF;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               P_REGISTRO.CDFORMPGTO   := P_FORNECEDORES.CDFORMPGTO; 
               P_REGISTRO.IDTPFOPGTCOL := P_FORNECEDORES.IDTPFORPGTFO;
               P_REGISTRO.NRPARCPGTCOL := P_FORNECEDORES.NRPARCPGTOFO;
               P_REGISTRO.NRDIAPGFXCOL := P_FORNECEDORES.NRDIAPGFIXFO;
               P_REGISTRO.IDUTDIAPGCOL := P_FORNECEDORES.IDUTDIAPGTFO;

               BEGIN
                  INSERT INTO ITEMCOLE
                       VALUES P_REGISTRO;
               EXCEPTION
                  WHEN OTHERS THEN
                        V_ERROR := SQLERRM;
               END;

               IF V_ERROR IS NULL THEN
                  DELETE_PZOPGTITMCOL(P_REGISTRO => P_REGISTRO,
                                      P_ERROR    => V_ERROR);

                  INSERT_PZOPGTITMCOL(P_REGISTRO => P_REGISTRO,
                                      P_ERROR    => V_ERROR);
               END IF;
         END;
      END IF;

      P_ERROR := V_ERROR;
   END INSERT_ITEMCOLE;

   PROCEDURE DELETE_PZOPGTITMCOL(P_REGISTRO IN S_RECORD_ITEMCOLE,
                                 P_ERROR   OUT VARCHAR2) IS
   BEGIN
      DELETE PZOPGTITMCOL
       WHERE NRCOLEPRECO  = P_REGISTRO.NRCOLEPRECO
         AND CDFORNECED   = P_REGISTRO.CDFORNECED
         AND CDFILIAL     = P_REGISTRO.CDFILIAL
         AND CDREGIAO     = P_REGISTRO.CDREGIAO
         AND CDPRODUTO    = P_REGISTRO.CDPRODUTO
         AND CDMARCA      = P_REGISTRO.CDMARCA;
   EXCEPTION
      WHEN OTHERS THEN
         P_ERROR := SQLERRM;
   END DELETE_PZOPGTITMCOL;

   PROCEDURE INSERT_PZOPGTITMCOL(P_REGISTRO   IN S_RECORD_ITEMCOLE,
                                 P_CDPRODFORN IN PRODFORN.CDPRODUTO%TYPE DEFAULT NULL,
                                 P_ERROR     OUT VARCHAR2) IS
      V_RECORD S_RECORD_PZOPGTITMCOL;
   BEGIN
      V_RECORD.CDFILIAL    := P_REGISTRO.CDFILIAL;
      V_RECORD.NRCOLEPRECO := P_REGISTRO.NRCOLEPRECO;
      V_RECORD.CDPRODUTO   := P_REGISTRO.CDPRODUTO;
      V_RECORD.CDMARCA     := P_REGISTRO.CDMARCA;
      V_RECORD.CDREGIAO    := P_REGISTRO.CDREGIAO;

      FOR C_PRAZO IN (SELECT CDFORNECED,
                             NRDIASPGPRFO NRDIASPGTCOL,
                             VRPERCPGPRFO VRPERCPGTCOL,
                             NRORG,
                             IDATIVO
                        FROM PZOPGTPRFO
                       WHERE CDPRODUTO  = P_CDPRODFORN
                         AND CDFORNECED = P_REGISTRO.CDFORNECED
                       UNION ALL   
                      SELECT CDFORNECED,
                             NRDIASPGTOFO NRDIASPGTCOL,
                             VRPERCPGTOFO VRPERCPGTCOL,
                             NRORG,
                             IDATIVO
                        FROM PRAZOPGTFORN
                       WHERE CDFORNECED = P_REGISTRO.CDFORNECED
                         AND NOT EXISTS (SELECT 1
                                           FROM PZOPGTPRFO PZ
                                          WHERE PZ.CDPRODUTO    = P_CDPRODFORN
                                            AND PZ.CDFORNECED   = PRAZOPGTFORN.CDFORNECED))
      LOOP
         V_RECORD.CDFORNECED   := C_PRAZO.CDFORNECED; 
         V_RECORD.NRDIASPGTCOL := C_PRAZO.NRDIASPGTCOL;
         V_RECORD.VRPERCPGTCOL := C_PRAZO.VRPERCPGTCOL;
         V_RECORD.NRORG        := C_PRAZO.NRORG;
         V_RECORD.IDATIVO      := C_PRAZO.IDATIVO;

         BEGIN
            INSERT INTO PZOPGTITMCOL
                 VALUES V_RECORD;
         EXCEPTION
               WHEN OTHERS THEN
                  P_ERROR := SQLERRM;
         END;
      END LOOP;
   END INSERT_PZOPGTITMCOL;

   PROCEDURE INSERT_LOGCOLETAPR(P_COLETAPR      IN S_RECORD_COLETAPR,
                               P_DSENDUSULOG   IN LOGCOLETAPR.DSENDUSULOG%TYPE,
                               P_DSNMUSULOG    IN LOGCOLETAPR.DSNMUSULOG%TYPE,
                               P_DSRDMUSULOG   IN LOGCOLETAPR.DSRDMUSULOG%TYPE,
                               P_DSMETUSULOG   IN LOGCOLETAPR.DSMETUSULOG%TYPE,
                               P_CDOPERINCLOG  IN LOGCOLETAPR.CDOPERINCLOG%TYPE,
                               P_IDOPERCOTLOG  IN LOGCOLETAPR.IDOPERCOTLOG%TYPE,
                               P_DSMESSAGERROR IN LOGCOLETAPR.DSMESSAGERROR%TYPE) IS
      V_REC_LOGCOLETAPR S_RECORD_LOGCOLETAPR;
   BEGIN
      V_REC_LOGCOLETAPR.CDFILIALLOG   := P_COLETAPR.CDFILIAL; 
      V_REC_LOGCOLETAPR.NRCOLEPRLOG   := P_COLETAPR.NRCOLEPRECO; 
      V_REC_LOGCOLETAPR.CDCOMPLOG     := P_COLETAPR.CDOPERADOR; 
      V_REC_LOGCOLETAPR.DTCOLEPRLOG   := P_COLETAPR.DTCOLEPREC; 
      V_REC_LOGCOLETAPR.NRSEMAINLOG   := P_COLETAPR.NRSEMAINIC; 
      V_REC_LOGCOLETAPR.NRSEMAFILOG   := P_COLETAPR.NRSEMAFINAL; 
      V_REC_LOGCOLETAPR.DTINIVALLOG   := P_COLETAPR.DTINIVALCOL; 
      V_REC_LOGCOLETAPR.DTFINVALLOG   := P_COLETAPR.DTFINVALCOL; 
      V_REC_LOGCOLETAPR.CDOPECOLLOG   := P_COLETAPR.CDOPERINCL; 
      V_REC_LOGCOLETAPR.DTULTCOLLOG   := SYSDATE;                 
      V_REC_LOGCOLETAPR.IDOPERCOTLOG  := P_IDOPERCOTLOG; 
      V_REC_LOGCOLETAPR.NRCOLEORILOG  := P_COLETAPR.NRCOLEORIGCO; 
      V_REC_LOGCOLETAPR.DTINCLITMLOG  := SYSDATE;
      V_REC_LOGCOLETAPR.DSMESSAGERROR := P_DSMESSAGERROR;

      V_REC_LOGCOLETAPR.DSENDUSULOG   := P_DSENDUSULOG;
      V_REC_LOGCOLETAPR.DSNMUSULOG    := P_DSNMUSULOG;
      V_REC_LOGCOLETAPR.DSRDMUSULOG   := P_DSRDMUSULOG;
      V_REC_LOGCOLETAPR.DSMETUSULOG   := SUBSTR(K_DSMET_DUPLICA_COTACAO||P_DSMETUSULOG, 0, 250);
      V_REC_LOGCOLETAPR.CDOPERINCLOG  := P_CDOPERINCLOG;
      V_REC_LOGCOLETAPR.NRORG         := K_NRORG;

      INSERT INTO LOGCOLETAPR
           VALUES V_REC_LOGCOLETAPR;

   END INSERT_LOGCOLETAPR;

   PROCEDURE INSERT_LOGITEMCOLE(P_ITEMCOLE      IN S_RECORD_ITEMCOLE,
                                P_DSENDUSULIC   IN LOGITEMCOLE.DSENDUSULIC%TYPE,
                                P_DSNMUSULIC    IN LOGITEMCOLE.DSNMUSULIC%TYPE,
                                P_DSRDMUSULIC   IN LOGITEMCOLE.DSRDMUSULIC%TYPE,
                                P_DSMETUSULIC   IN LOGITEMCOLE.DSMETUSULIC%TYPE,
                                P_CDOPERLIC     IN LOGITEMCOLE.CDOPERLIC%TYPE,
                                P_IDOPERALIC    IN LOGITEMCOLE.IDOPERALIC%TYPE,
                                P_DSMESSAGERROR IN LOGITEMCOLE.DSMESSAGERROR%TYPE) IS
      V_REC_LOGITEMCOLE S_RECORD_LOGITEMCOLE;
   BEGIN
      V_REC_LOGITEMCOLE.CDFILIALLIC   := P_ITEMCOLE.CDFILIAL; 
      V_REC_LOGITEMCOLE.NRCOLEPRELIC  := P_ITEMCOLE.NRCOLEPRECO; 
      V_REC_LOGITEMCOLE.CDMARCALIC    := P_ITEMCOLE.CDMARCA;
      V_REC_LOGITEMCOLE.CDREGIAOLIC   := P_ITEMCOLE.CDREGIAO;
      V_REC_LOGITEMCOLE.CDFORNECELIC  := P_ITEMCOLE.CDFORNECED;
      V_REC_LOGITEMCOLE.CDPRODUTOLIC  := P_ITEMCOLE.CDPRODUTO;
      V_REC_LOGITEMCOLE.DTINIVALLIC   := P_ITEMCOLE.DTINIVALITM;
      V_REC_LOGITEMCOLE.DTFINVALLIC   := P_ITEMCOLE.DTFINVALITM;
      V_REC_LOGITEMCOLE.DTOPERACLIC   := SYSDATE; 
      V_REC_LOGITEMCOLE.IDOPERALIC    := P_IDOPERALIC;
      V_REC_LOGITEMCOLE.DSRDMUSULIC   := P_DSRDMUSULIC;
      V_REC_LOGITEMCOLE.DSENDUSULIC   := P_DSENDUSULIC;
      V_REC_LOGITEMCOLE.DSNMUSULIC    := P_DSNMUSULIC;
      V_REC_LOGITEMCOLE.DSMETUSULIC   := SUBSTR(K_DSMET_DUPLICA_COTACAO||P_DSMETUSULIC, 0, 250);
      V_REC_LOGITEMCOLE.CDOPERLIC     := P_CDOPERLIC;
      V_REC_LOGITEMCOLE.VRPRECANTLIC  := 0;
      V_REC_LOGITEMCOLE.VRPRECDEPLIC  := P_ITEMCOLE.VRPRECUNIITM;
      V_REC_LOGITEMCOLE.IDITMLISPLIC  := P_ITEMCOLE.IDITMLISPREC;
      V_REC_LOGITEMCOLE.NRCOLEORILIC  := P_ITEMCOLE.NRCOLEORIGIC;
      V_REC_LOGITEMCOLE.DTINCLITMLIC  := SYSDATE;
      V_REC_LOGITEMCOLE.CDOPERINCLIC  := P_ITEMCOLE.CDOPERINCL;
      V_REC_LOGITEMCOLE.VRPRECECOLIC  := P_ITEMCOLE.VRPRECECOITM;
      V_REC_LOGITEMCOLE.VRPRECOPILIC  := P_ITEMCOLE.VRPRECOITMPI;
      V_REC_LOGITEMCOLE.DSMESSAGERROR := P_DSMESSAGERROR;
      V_REC_LOGITEMCOLE.NRORG         := K_NRORG;
      V_REC_LOGITEMCOLE.IDATIVO       := K_S;

      INSERT INTO LOGITEMCOLE
           VALUES V_REC_LOGITEMCOLE;

   END INSERT_LOGITEMCOLE;

   PROCEDURE COTACAO_OPL(P_CDFILIAL    IN COLETAPR.CDFILIAL%TYPE,
                         P_NRCOLEORIG  IN COLETAPR.NRCOLEPRECO%TYPE,
                         P_NRCOLEPRECO IN COLETAPR.NRCOLEPRECO%TYPE,
                         P_FORNOL      IN VARCHAR2,
                         P_CDREGIAOL   IN VARCHAR2,
                         P_REAJUSTE    IN NUMBER,
                         P_DTNOVINIVAL IN COLETAPR.DTINIVALCOL%TYPE,
                         P_DTNOVFINVAL IN COLETAPR.DTFINVALCOL%TYPE,
                         P_CDOPERINCL  IN OPERADOR.CDOPERADOR%TYPE,
                         P_ERROR      OUT VARCHAR2) IS
      V_REC_ITEMCOLE S_RECORD_ITEMCOLE;
      C_PARASUPRI    PARASUPRI%ROWTYPE;
      C_FORNECEDORES FORNECEDOR%ROWTYPE;
      V_ERROR        VARCHAR2(4000);
      V_DSIP         VARCHAR2(50);
      V_DSUSUARIO    VARCHAR2(50);
      V_DSRDM        VARCHAR2(50);
      V_DSMETODO     VARCHAR2(250);
      V_DSOPERADOR   VARCHAR2(12);

   BEGIN
      WEBTUNNING(V_DSIP,
                  V_DSUSUARIO,
                  V_DSRDM,
                  V_DSMETODO,
                  V_DSOPERADOR);

      SUP_VALIDATIONS.CHECK_PARASUPRI(P_CDFILIAL  => P_CDFILIAL,
                                      P_PARASUPRI => C_PARASUPRI,
                                      P_ERROR     => V_ERROR);
      IF V_ERROR IS NOT NULL THEN
         RETURN;
      END IF;
        
      FOR C_REGIAO IN (SELECT FILDEST.CDREGIAO, FILIAL.CDFORNOPELOG
                         FROM TIPOOPEROPL INNER JOIN FILIAL ON TIPOOPEROPL.CDFILIOPELOG = FILIAL.CDFILIAL
                        INNER JOIN FILIAL FILDEST ON TIPOOPEROPL.CDFILDEST = FILDEST.CDFILIAL
                        WHERE INSTR(NVL(P_FORNOL, FILIAL.CDFORNOPELOG), FILIAL.CDFORNOPELOG) > 0
                          AND INSTR(NVL(P_CDREGIAOL, FILDEST.CDREGIAO), FILDEST.CDREGIAO) > 0
                          AND FILDEST.CDREGIAO IS NOT NULL
                          AND FILIAL.CDFORNOPELOG IS NOT NULL
                        GROUP BY FILDEST.CDREGIAO, FILIAL.CDFORNOPELOG)
      LOOP                          
         SUP_VALIDATIONS.CHECK_FORNECEDOR(P_CDFORNECED   => C_REGIAO.CDFORNOPELOG,
                                          P_DTPEDIDO     => P_DTNOVFINVAL,
                                          P_PARASUPRI    => C_PARASUPRI,
                                          P_FORNECEDORES => C_FORNECEDORES,
                                          P_MESSAGE      => V_ERROR);
         IF V_ERROR IS NULL THEN
            FOR C_ITENS IN (SELECT ITEMCOLE.CDPRODUTO, RO.CDMARCA, MIN(ITEMCOLE.VRPRECUNIITM) VRPRECUNIITM, 
                               MIN(ITEMCOLE.VRPRECECOITM) VRPRECECOITM, MIN(ITEMCOLE.VRPERDESITM) VRPERDESITM,
                               MIN(ITEMCOLE.VRPRECOITMPI) VRPRECOITMPI
                              FROM ITEMCOLE
                              JOIN RELACPRODOL RO ON ITEMCOLE.CDPRODUTO = RO.CDPRARVORIG
                             WHERE ITEMCOLE.CDFILIAL    = P_CDFILIAL
                               AND ITEMCOLE.NRCOLEPRECO = P_NRCOLEORIG
                               AND ITEMCOLE.VRPRECUNIITM > 0
                             GROUP BY ITEMCOLE.CDPRODUTO, RO.CDMARCA)
            LOOP
               V_REC_ITEMCOLE.CDFILIAL        := P_CDFILIAL;
               V_REC_ITEMCOLE.NRCOLEPRECO     := P_NRCOLEPRECO;
               V_REC_ITEMCOLE.CDREGIAO        := C_REGIAO.CDREGIAO;
               V_REC_ITEMCOLE.CDFORNECED      := C_REGIAO.CDFORNOPELOG;
               V_REC_ITEMCOLE.CDPRODUTO       := C_ITENS.CDPRODUTO;
               V_REC_ITEMCOLE.CDMARCA         := C_ITENS.CDMARCA;

               V_REC_ITEMCOLE.VRPRECUNIITM    := C_ITENS.VRPRECUNIITM + ((C_ITENS.VRPRECUNIITM * NVL(P_REAJUSTE, 1)) / 100);
               V_REC_ITEMCOLE.VRPRECECOITM    := C_ITENS.VRPRECECOITM;
               V_REC_ITEMCOLE.VRPERDESITM     := C_ITENS.VRPERDESITM;
               V_REC_ITEMCOLE.VRPRECOITMPI    := C_ITENS.VRPRECOITMPI;
               V_REC_ITEMCOLE.DTINIVALITM     := P_DTNOVINIVAL;
               V_REC_ITEMCOLE.DTFINVALITM     := P_DTNOVFINVAL;
               V_REC_ITEMCOLE.NRCOLEORIGIC    := P_NRCOLEORIG;
               V_REC_ITEMCOLE.CDOPERINCL      := P_CDOPERINCL;
               V_REC_ITEMCOLE.IDITMLISPREC    := K_N;
               V_REC_ITEMCOLE.IDPRECALT       := K_N;
               V_REC_ITEMCOLE.IDITVALDIFLISTA := K_S;
               V_REC_ITEMCOLE.DTINCLITMCOL    := SYSDATE;
               V_REC_ITEMCOLE.NRORG           := K_NRORG;
               V_REC_ITEMCOLE.IDATIVO         := K_S;

               INSERT_ITEMCOLE(P_REGISTRO     => V_REC_ITEMCOLE,
                               P_PARASUPRI    => C_PARASUPRI,
                               P_FORNECEDORES => C_FORNECEDORES,
                               P_ERROR        => V_ERROR);

               IF V_ERROR IS NOT NULL THEN
                  INSERT_LOGITEMCOLE(P_ITEMCOLE      => V_REC_ITEMCOLE,
                                     P_DSENDUSULIC   => V_DSIP,
                                     P_DSNMUSULIC    => V_DSUSUARIO,
                                     P_DSRDMUSULIC   => V_DSRDM,
                                     P_DSMETUSULIC   => V_DSMETODO,
                                     P_CDOPERLIC     => V_DSOPERADOR,
                                     P_IDOPERALIC    => 'N',
                                     P_DSMESSAGERROR => V_ERROR);
               END IF;                    
               
               P_ERROR := V_ERROR;
            END LOOP;
         END IF;
      END LOOP;
   END COTACAO_OPL;

   PROCEDURE DUPLICA_COTACAO(P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
                             P_CDFILIPROC   IN FILIAL.CDFILIAL%TYPE,
                             P_NRCOLEPRECO  IN COLETAPR.NRCOLEPRECO%TYPE,
                             P_NRSEMAINIC   IN COLETAPR.NRSEMAINIC%TYPE,
                             P_NRSEMAFINAL  IN COLETAPR.NRSEMAFINAL%TYPE,
                             P_DTNOVINIVAL  IN COLETAPR.DTINIVALCOL%TYPE,
                             P_DTNOVFINVAL  IN COLETAPR.DTFINVALCOL%TYPE,
                             P_DTINIVALCOL  IN COLETAPR.DTINIVALCOL%TYPE,
                             P_DTFINVALCOL  IN COLETAPR.DTFINVALCOL%TYPE,
                             P_DTCOLEPREC   IN COLETAPR.DTCOLEPREC%TYPE,
                             P_REAJUSTE     IN NUMBER,
                             P_NRCOLEDUP    IN COLETAPR.NRCOLEPRECO%TYPE,
                             P_IDDUPLICA    IN VARCHAR2,
                             P_CDOPERADOR   IN VARCHAR2 DEFAULT NULL,
                             P_TCDOPERADOR  IN VARCHAR2,
                             P_CDOPERINCL   IN VARCHAR2,
                             P_CDREGIAO     IN VARCHAR2 DEFAULT NULL,
                             P_DUPLICAOL    IN VARCHAR2 DEFAULT 'N',
                             P_CDFORNOL     IN VARCHAR2 DEFAULT NULL,
                             P_CDREGIAOL    IN VARCHAR2 DEFAULT NULL,
                             P_COTA_N_DUP   OUT VARCHAR2)
   IS
      V_REC_COLETAPR S_RECORD_COLETAPR;
      V_REC_ITEMCOLE S_RECORD_ITEMCOLE;
      TYPE CURTYPE IS REF CURSOR;
      C_COTACAO      CURTYPE;
      C_PARASUPRI    PARASUPRI%ROWTYPE;
      C_FORNECEDOR   FORNECEDOR%ROWTYPE;
      
      V_NRCOLEPRECO  COLETAPR.NRCOLEPRECO%TYPE;
      V_NRCOLEORIG   COLETAPR.NRCOLEPRECO%TYPE;
      V_CDOPERADOR   OPERADOR.CDOPERADOR%TYPE;
      V_IDDISMONTLIS COLETAPR.IDDISMONTLIS%TYPE;

      V_COUNT             INT;
      V_COUNTAUX          INT;

      V_PGTO_CDFORMPGTO   PRODFORN.CDFORMPGTO%TYPE;
      V_PGTO_IDTPFORPGPRF PRODFORN.IDTPFORPGPRF%TYPE;
      V_PGTO_NRPARCPGTPRF PRODFORN.NRPARCPGTPRF%TYPE;
      V_PGTO_NRDIAPGFIPRF PRODFORN.NRDIAPGFIPRF%TYPE;
      V_PGTO_IDUTDIAPGPRF PRODFORN.IDUTDIAPGPRF%TYPE;

      V_PGTO_IDTPFORPGTFO FORNECEDOR.IDTPFORPGTFO%TYPE;
      V_PGTO_NRPARCPGTOFO FORNECEDOR.NRPARCPGTOFO%TYPE;
      V_PGTO_NRDIAPGFIXFO FORNECEDOR.NRDIAPGFIXFO%TYPE;
      V_PGTO_IDUTDIAPGTFO FORNECEDOR.IDUTDIAPGTFO%TYPE;
      V_PGTO_IDTPDESCFORN FORNECEDOR.IDTPDESCFORN%TYPE;
      V_PGTO_VRPEDESCFORN FORNECEDOR.VRPEDESCFORN%TYPE;
      V_PGTO_IDTPFRETFORN FORNECEDOR.IDTPFRETFORN%TYPE;

      V_IDSITUCOLETA VARCHAR2(1);
      
      V_DSMETUSULIC VARCHAR2(50);

      V_PRXCOLETA   VARCHAR2(20);

      V_CDEMPRESA  EMPRESA.CDEMPRESA%TYPE;
      V_CDINSCESTA INSCRESTAD.CDINSCESTA%TYPE;

      V_CONTADOR FLOAT := 0;

      V_DSIP         VARCHAR2(50);
      V_DSUSUARIO    VARCHAR2(50);
      V_DSRDM        VARCHAR2(50);
      V_DSMETODO     VARCHAR2(250);
      V_DSOPERADOR   VARCHAR2(12);
      V_LISTACOTACAO VARCHAR2(2000) := '_';

      V_NRCOLEPRECO_AUX  COLETAPR.NRCOLEPRECO%TYPE;
      V_ERROR VARCHAR2(4000);

      V_SQLCOTACAO VARCHAR2(32000);

            
   BEGIN
      V_DSMETUSULIC := '99LJ_SUPPKGO_SUP_COTACAO';
      WEBTUNNING(V_DSIP, V_DSUSUARIO, V_DSRDM, V_DSMETODO, V_DSOPERADOR);

      SUP_VALIDATIONS.CHECK_PARASUPRI(P_CDFILIAL  => P_CDFILIAL,
                                      P_PARASUPRI => C_PARASUPRI,
                                      P_ERROR     => V_ERROR);
      IF V_ERROR IS NOT NULL THEN
         RETURN;
      END IF;

      V_SQLCOTACAO := 
         ' SELECT NRCOLEPRECO, COLETAPR.CDOPERADOR, IDDISMONTLIS' ||
         '   FROM COLETAPR'||
         '  WHERE CDFILIAL = '''|| P_CDFILIAL || '''' ||
         '    AND EXISTS (SELECT 1'||
         '                  FROM ITEMCOLE'||
         '                 WHERE ITEMCOLE.CDFILIAL = COLETAPR.CDFILIAL'||
         '                   AND ITEMCOLE.NRCOLEPRECO = COLETAPR.NRCOLEPRECO)';
      
      IF P_TCDOPERADOR = K_T THEN
         V_SQLCOTACAO := V_SQLCOTACAO ||
            ' AND EXISTS (SELECT 1'||
            '                FROM COMPROPER CO, COMPRADOR C, OPERADOR O'||
            '               WHERE CO.IDCOTCMPOPER = '''|| K_S ||''''||
            '                 AND CO.CDCOMPRADOR  = C.CDOPERADOR'||
            '                 AND CO.CDOPERADOR   = ''' || P_CDOPERINCL || '''' ||
            '                 AND O.CDOPERADOR    = C.CDOPERADOR'||
            '                 AND C.CDOPERADOR = COLETAPR.CDOPERADOR'||
            '               UNION'||
            '              SELECT 1'||
            '                FROM COMPRADOR C, OPERADOR O'||
            '               WHERE C.CDOPERADOR = O.CDOPERADOR'||
            '                 AND C.CDOPERADOR = ''' || P_CDOPERINCL || '''' ||
            '                 AND C.CDOPERADOR = COLETAPR.CDOPERADOR)';
      ELSE
         V_SQLCOTACAO := V_SQLCOTACAO ||
            ' AND INSTR(''' || P_CDOPERADOR || ''', COLETAPR.CDOPERADOR) > 0';
      END IF;

      IF P_IDDUPLICA = K_1 THEN
         V_SQLCOTACAO := V_SQLCOTACAO ||
            ' AND DTINIVALCOL >= ''' || P_DTINIVALCOL || ''' AND DTFINVALCOL <= ''' || P_DTFINVALCOL || '''';
      ELSE
         V_SQLCOTACAO := V_SQLCOTACAO ||
            ' AND NRCOLEPRECO = ''' || P_NRCOLEDUP || '''';
      END IF;
     
      IF C_PARASUPRI.IDUTISTATCOT = 'S' THEN
         V_IDSITUCOLETA := 'A';
      ELSE
         V_IDSITUCOLETA := '';
      END IF;

      P_COTA_N_DUP := 'N';

      OPEN C_COTACAO FOR V_SQLCOTACAO;
      LOOP
         FETCH C_COTACAO INTO V_NRCOLEORIG, V_CDOPERADOR, V_IDDISMONTLIS;
         EXIT WHEN C_COTACAO%NOTFOUND;
         
         NOVO_CODIGO('COLETA'||P_CDFILIAL, V_PRXCOLETA);
         V_NRCOLEPRECO := SUBSTR(V_PRXCOLETA,11,10);

         V_REC_COLETAPR.CDFILIAL     := P_CDFILIAL;
         V_REC_COLETAPR.NRCOLEPRECO  := V_NRCOLEPRECO;
         V_REC_COLETAPR.NRSEMAINIC   := P_NRSEMAINIC;
         V_REC_COLETAPR.NRSEMAFINAL  := P_NRSEMAFINAL;
         V_REC_COLETAPR.DTINIVALCOL  := P_DTNOVINIVAL;
         V_REC_COLETAPR.DTFINVALCOL  := P_DTNOVFINVAL;
         V_REC_COLETAPR.CDOPERADOR   := V_CDOPERADOR;
         V_REC_COLETAPR.DTCOLEPREC   := P_DTCOLEPREC;
         V_REC_COLETAPR.NRCOLEORIGCO := V_NRCOLEORIG;
         V_REC_COLETAPR.CDOPERINCL   := P_CDOPERINCL;
         V_REC_COLETAPR.DTINCLCOLET  := SYSDATE;
         V_REC_COLETAPR.IDDISMONTLIS := V_IDDISMONTLIS;
         V_REC_COLETAPR.IDSITUCOLETA := V_IDSITUCOLETA;
         V_REC_COLETAPR.NRORG        := K_NRORG;
         V_REC_COLETAPR.IDATIVO      := K_S;

         INSERT_COLETAPR(P_REGISTRO     => V_REC_COLETAPR,
                         P_PARASUPRI    => C_PARASUPRI,
                         P_ERROR        => V_ERROR);        

         IF P_DUPLICAOL = K_N THEN
            FOR C_REGIAO IN (SELECT CDREGIAO
                              FROM REGIAO
                              WHERE INSTR(NVL(P_CDREGIAO, REGIAO.CDREGIAO), REGIAO.CDREGIAO) <> 0
                              AND EXISTS (SELECT 1
                                            FROM ITEMCOLE
                                           WHERE ITEMCOLE.CDFILIAL    = P_CDFILIAL
                                             AND ITEMCOLE.NRCOLEPRECO = V_NRCOLEORIG
                                             AND ITEMCOLE.CDREGIAO    = REGIAO.CDREGIAO))
            LOOP
               FOR C_ITEMCOLE IN (SELECT I.CDFILIAL, I.CDFORNECED, I.CDREGIAO,
                                         I.CDPRODUTO, I.CDMARCA, I.CDTIPCOB, I.CDSEGMARCCOL,
                                         I.VRPRECUNIITM + ((I.VRPRECUNIITM * P_REAJUSTE) / 100) AS VRPRECUNIITM,
                                         I.VRPRECECOITM, I.VRPRECOITMPI, I.VRPERDESITM, I.IDTIPFREITM, I.NRPZOENTCOL,
                                         I.CDFORMPGTO, I.IDTPFOPGTCOL, I.NRPARCPGTCOL, I.NRDIAPGFXCOL,
                                         I.IDUTDIAPGCOL, I.DSPZOPGTCONC, I.IDITVALDIFLISTA
                                    FROM ITEMCOLE I
                                   WHERE I.CDFILIAL     = P_CDFILIAL
                                     AND I.NRCOLEPRECO  = V_NRCOLEORIG
                                     AND I.CDREGIAO     = C_REGIAO.CDREGIAO)
               LOOP
                  V_REC_ITEMCOLE.CDFILIAL        := P_CDFILIAL;
                  V_REC_ITEMCOLE.NRCOLEPRECO     := V_NRCOLEPRECO;
                  V_REC_ITEMCOLE.CDREGIAO        := C_ITEMCOLE.CDREGIAO;
                  V_REC_ITEMCOLE.CDFORNECED      := C_ITEMCOLE.CDFORNECED;
                  V_REC_ITEMCOLE.CDPRODUTO       := C_ITEMCOLE.CDPRODUTO;
                  V_REC_ITEMCOLE.CDMARCA         := C_ITEMCOLE.CDMARCA;

                  V_REC_ITEMCOLE.VRPRECUNIITM    := C_ITEMCOLE.VRPRECUNIITM + ((C_ITEMCOLE.VRPRECUNIITM * NVL(P_REAJUSTE, 1)) / 100);
                  V_REC_ITEMCOLE.VRPRECECOITM    := C_ITEMCOLE.VRPRECECOITM;
                  V_REC_ITEMCOLE.VRPERDESITM     := C_ITEMCOLE.VRPERDESITM;
                  V_REC_ITEMCOLE.VRPRECOITMPI    := C_ITEMCOLE.VRPRECOITMPI;
                  V_REC_ITEMCOLE.DTINIVALITM     := P_DTNOVINIVAL;
                  V_REC_ITEMCOLE.DTFINVALITM     := P_DTNOVFINVAL;
                  V_REC_ITEMCOLE.NRCOLEORIGIC    := V_NRCOLEORIG;
                  V_REC_ITEMCOLE.CDOPERINCL      := P_CDOPERINCL;
                  V_REC_ITEMCOLE.IDITMLISPREC    := K_N;
                  V_REC_ITEMCOLE.IDPRECALT       := K_N;
                  V_REC_ITEMCOLE.IDITVALDIFLISTA := C_ITEMCOLE.IDITVALDIFLISTA;
                  V_REC_ITEMCOLE.DTINCLITMCOL    := SYSDATE;
                  V_REC_ITEMCOLE.NRORG           := K_NRORG;
                  V_REC_ITEMCOLE.IDATIVO         := K_S;

                  IF (C_PARASUPRI.IDUTISTATCOT = 'S' AND C_PARASUPRI.IDCOTCONCLUI = 'S') THEN
                     SELECT MAX(I.NRCOLEPRECO)
                        INTO V_NRCOLEPRECO_AUX
                        FROM ITEMCOLE I INNER JOIN COLETAPR C ON C.CDFILIAL    = I.CDFILIAL
                                                             AND C.NRCOLEPRECO = I.NRCOLEPRECO
                       WHERE I.CDFILIAL    = P_CDFILIAL
                         AND I.NRCOLEPRECO <> V_NRCOLEPRECO
                         AND I.CDMARCA     = C_ITEMCOLE.CDMARCA
                         AND I.CDREGIAO    = C_ITEMCOLE.CDREGIAO
                         AND I.CDFORNECED  = C_ITEMCOLE.CDFORNECED
                         AND I.CDPRODUTO   = C_ITEMCOLE.CDPRODUTO
                         AND C.DTINIVALCOL = P_DTNOVINIVAL
                         AND C.DTFINVALCOL = P_DTNOVFINVAL
                         AND NVL(C.IDSITUCOLETA, 'C') = 'C';

                     IF V_NRCOLEPRECO_AUX IS NOT NULL THEN
                        P_COTA_N_DUP := 'S';
                        IF (NVL(INSTR(V_LISTACOTACAO, V_NRCOLEPRECO), 0) = 0) THEN
                           V_LISTACOTACAO := V_LISTACOTACAO||V_NRCOLEPRECO||'_';
                        END IF;

                        INSERT_LOGITEMCOLE(P_ITEMCOLE      => V_REC_ITEMCOLE,
                                           P_DSENDUSULIC   => V_DSIP,
                                           P_DSNMUSULIC    => V_DSUSUARIO,
                                           P_DSRDMUSULIC   => V_DSRDM,
                                           P_DSMETUSULIC   => V_DSMETODO,
                                           P_CDOPERLIC     => V_DSOPERADOR,
                                           P_IDOPERALIC    => 'N',
                                           P_DSMESSAGERROR => SUP_COTACAO.K_COTACAO_CONCLUIDA);

                     END IF;
                  END IF;

                  IF V_NRCOLEPRECO_AUX IS NULL THEN
                     INSERT_ITEMCOLE(P_REGISTRO     => V_REC_ITEMCOLE,
                                     P_PARASUPRI    => C_PARASUPRI,
                                     P_FORNECEDORES => C_FORNECEDOR,
                                     P_ERROR        => V_ERROR);
                     
                     IF V_ERROR IS NOT NULL THEN
                        INSERT_LOGITEMCOLE(P_ITEMCOLE      => V_REC_ITEMCOLE,
                                          P_DSENDUSULIC   => V_DSIP,
                                          P_DSNMUSULIC    => V_DSUSUARIO,
                                          P_DSRDMUSULIC   => V_DSRDM,
                                          P_DSMETUSULIC   => V_DSMETODO,
                                          P_CDOPERLIC     => V_DSOPERADOR,
                                          P_IDOPERALIC    => 'N',
                                          P_DSMESSAGERROR => V_ERROR);
                     ELSE
                        SELECT SUBSTR(EMITENTE(P_CDFILIAL, C_ITEMCOLE.CDPRODUTO, C_ITEMCOLE.CDFORNECED), 3, 2) AS CDEMPRESA
                          INTO V_CDEMPRESA
                          FROM DUAL;

                        SELECT MAX(EMPRESA.CDEMPRESA), MIN(INSCRESTAD.CDINSCESTA) AS CDINSCESTA
                          INTO V_CDEMPRESA, V_CDINSCESTA
                          FROM EMPRESA, INSCRESTAD
                         WHERE EMPRESA.CDEMPRESA = V_CDEMPRESA
                           AND EMPRESA.CDEMPRESA = INSCRESTAD.CDEMPRESA;

                        IMPOSTO_ITEMCOL(V_CDEMPRESA,
                                        V_CDINSCESTA,
                                        C_ITEMCOLE.CDFORNECED, 
                                        P_CDFILIAL,
                                        V_NRCOLEPRECO,
                                        C_ITEMCOLE.CDFORNECED,
                                        C_ITEMCOLE.CDREGIAO,
                                        C_ITEMCOLE.CDPRODUTO,
                                        C_ITEMCOLE.CDMARCA, 
                                        P_DTCOLEPREC);

                        DELETE ITEMCOLE
                         WHERE ITEMCOLE.NRCOLEPRECO <> V_NRCOLEPRECO
                           AND ITEMCOLE.CDFILIAL    =  C_ITEMCOLE.CDFILIAL
                           AND ITEMCOLE.CDPRODUTO   =  C_ITEMCOLE.CDPRODUTO   
                           AND ITEMCOLE.CDMARCA     =  C_ITEMCOLE.CDMARCA     
                           AND ITEMCOLE.CDFORNECED  =  C_ITEMCOLE.CDFORNECED  
                           AND ITEMCOLE.CDREGIAO    =  C_ITEMCOLE.CDREGIAO    
                           AND ITEMCOLE.DTINIVALITM =  P_DTNOVINIVAL 
                           AND ITEMCOLE.DTFINVALITM =  P_DTNOVFINVAL;                        
                        
                        UPDATE ITEMCOLE
                           SET IDTPFOPGTCOL = IDTPFOPGTCOL
                         WHERE CDFILIAL    = P_CDFILIAL
                           AND NRCOLEPRECO = V_NRCOLEPRECO
                           AND CDFORNECED  = C_ITEMCOLE.CDFORNECED
                           AND CDREGIAO    = C_ITEMCOLE.CDREGIAO
                           AND CDPRODUTO   = C_ITEMCOLE.CDPRODUTO
                           AND CDMARCA     = C_ITEMCOLE.CDMARCA;
                     END IF;
                  END IF;
                  IF V_ERROR IS NOT NULL THEN
                     P_COTA_N_DUP := 'S';
                     IF (NVL(INSTR(V_LISTACOTACAO, V_NRCOLEPRECO), 0) = 0) THEN
                        V_LISTACOTACAO := V_LISTACOTACAO||V_NRCOLEPRECO||'_';
                     END IF;
                  END IF;
               END LOOP;
            END LOOP;
         END IF;

         IF P_DUPLICAOL  = 'S' THEN      
            V_ERROR := NULL;
            SUP_COTACAO.COTACAO_OPL(P_CDFILIAL    => P_CDFILIAL,
                                    P_NRCOLEORIG  => V_NRCOLEORIG,
                                    P_NRCOLEPRECO => V_NRCOLEPRECO,
                                    P_FORNOL      => P_CDFORNOL,
                                    P_CDREGIAOL   => P_CDREGIAOL,
                                    P_REAJUSTE    => P_REAJUSTE,
                                    P_DTNOVINIVAL => P_DTNOVINIVAL,
                                    P_DTNOVFINVAL => P_DTNOVFINVAL,
                                    P_CDOPERINCL  => P_CDOPERINCL,
                                    P_ERROR       => V_ERROR);
            IF V_ERROR IS NOT NULL THEN
               P_COTA_N_DUP := 'S';
               IF (NVL(INSTR(V_LISTACOTACAO, V_NRCOLEPRECO), 0) = 0) THEN
                  V_LISTACOTACAO := V_LISTACOTACAO||V_NRCOLEPRECO||'_';
               END IF;
            END IF;
         END IF;
         COMMIT;
      END LOOP;
      CLOSE C_COTACAO;

      IF P_COTA_N_DUP = 'S' THEN
         P_COTA_N_DUP := 'S'||V_LISTACOTACAO;
      END IF;

      FOR C_LIMPEZA IN (SELECT CDFILIAL, NRCOLEPRECO
                          FROM COLETAPR
                         WHERE NOT EXISTS (SELECT 1
                                             FROM ITEMCOLE
                                            WHERE ITEMCOLE.CDFILIAL    = COLETAPR.CDFILIAL
                                              AND ITEMCOLE.NRCOLEPRECO = COLETAPR.NRCOLEPRECO))
      LOOP
         DELETE
           FROM COLETAPR
          WHERE CDFILIAL    = C_LIMPEZA.CDFILIAL
            AND NRCOLEPRECO = C_LIMPEZA.NRCOLEPRECO;

         V_CONTADOR := V_CONTADOR + 1;

         IF V_CONTADOR >= 500 THEN
            V_CONTADOR := 0;
            COMMIT;
         END IF;
      END LOOP;
      COMMIT;      
   END DUPLICA_COTACAO;


    PROCEDURE GRAVA_COTWEB(P_CDFILIAL     IN PEDCOLETA.CDFILIAL%TYPE,
                           P_NRPEDCOLETA  IN PEDCOLETA.NRPEDCOLETA%TYPE,
                           P_CDREGIAO     IN PEDCOLETA.CDREGIAO%TYPE,
                           P_CDFORNECED   IN PEDCOLETA.CDFORNECED%TYPE,
                           P_NRSEMINI     IN COLETAPR.NRSEMAINIC%TYPE,
                           P_NRSEMFIN     IN COLETAPR.NRSEMAFINAL%TYPE,
                           P_ENTRALISTA   IN VARCHAR2,
                           P_COTA_N_GER  OUT VARCHAR2,
                           P_CDOPERINCL   IN VARCHAR2 DEFAULT NULL,
                           P_TRANSACAO    IN VARCHAR2 DEFAULT 'S',
                           P_CDCOMPRADOR  IN OPERADOR.CDOPERADOR%TYPE DEFAULT NULL,
                           P_DTINIVALITM  IN ITEMCOLE.DTINIVALITM%TYPE DEFAULT NULL,
                           P_DTFINVALITM  IN ITEMCOLE.DTFINVALITM%TYPE DEFAULT NULL)
    IS
        V_NRCOLEPRECO     VARCHAR2(20);
        V_CDFORMPGTO      FORNECEDOR.CDFORMPGTO%TYPE;
        V_IDTPFORPGTO     FORNECEDOR.IDTPFORPGTFO%TYPE;
        V_NRPARCPGTO      FORNECEDOR.NRPARCPGTOFO%TYPE;
        V_NRDIAPGTO       FORNECEDOR.NRDIAPGFIXFO%TYPE;
        V_IDUTDIAPGTO     FORNECEDOR.IDUTDIAPGTFO%TYPE;
        V_IDTIPFRETE      FORNECEDOR.IDTPFRETFORN%TYPE;
        V_CDTIPCOB        FORNECEDOR.CDTIPCOB%TYPE;
        V_VRPERDESC       FORNECEDOR.VRPEDESCFORN%TYPE;
        V_CDPRODFORN      PRODUTO.CDARVPROD%TYPE;
        V_CDEMPRESA       EMPRESA.CDEMPRESA%TYPE;
        V_INSCESTA        INSCRESTAD.CDINSCESTA%TYPE;
        V_CTITMCOL        INT;
        V_APAGACOL        VARCHAR2(1);
        V_NRPEDIDOCOL     VARCHAR2(10);
        V_CDOPERADOR      VARCHAR2(12);
        V_IDUTISTATCOT    VARCHAR2(1);
        V_IDSITUCOLETA    VARCHAR2(1);
        V_IDMOTNGERPEDCOL LOGNGERAPEDCOL.IDMOTNGERPEDCOL%TYPE;
        V_GEROUREGISTRO   VARCHAR2(1);
        V_COUNT           INT;
        V_COUNTAUX        INT;
        V_IDGRAVAITEMZERADO PARAMGERALECOMMERCE.IDGRAVAITEMZERADO%TYPE;
        V_IDCOTUNICAPEDCOL  PARAMGERALECOMMERCE.IDCOTUNICAPEDCOL%TYPE;
        V_DTINIPERCOT       PEDCOLETA.DTINIPERCOT%TYPE;
        V_DTFINPERCOT       PEDCOLETA.DTFINPERCOT%TYPE;
    BEGIN
        P_COTA_N_GER := 'N';

        SELECT MAX(IDTPFRETFORN), MAX(CDTIPCOB), MAX(DECODE(IDTPDESCFORN, 'C', VRPEDESCFORN, 0))
          INTO V_IDTIPFRETE, V_CDTIPCOB, V_VRPERDESC
          FROM FORNECEDOR
         WHERE CDFORNECED = P_CDFORNECED;

        SELECT NVL(MAX(IDGRAVAITEMZERADO), 'N'), NVL(MAX(IDCOTUNICAPEDCOL), 'N')
          INTO V_IDGRAVAITEMZERADO, V_IDCOTUNICAPEDCOL
          FROM PARAMGERALECOMMERCE
         WHERE CDFILIAL = P_CDFILIAL;

        IF V_IDCOTUNICAPEDCOL = 'S' THEN
            SELECT MAX(NRPEDIDOCOL)
              INTO V_NRPEDIDOCOL
              FROM PEDCOLETA
             WHERE CDFILIAL    = P_CDFILIAL
               AND NRPEDCOLETA = P_NRPEDCOLETA
               AND CDREGIAO    = P_CDREGIAO
               AND CDFORNECED  = P_CDFORNECED;

            SELECT MAX(ITEMCOLE.NRCOLEPRECO)
              INTO V_NRCOLEPRECO
              FROM ITEMCOLE INNER JOIN COLETAPR ON ITEMCOLE.CDFILIAL = COLETAPR.CDFILIAL
                                               AND ITEMCOLE.NRCOLEPRECO = COLETAPR.NRCOLEPRECO
             WHERE ITEMCOLE.CDFILIAL    = P_CDFILIAL
               AND ITEMCOLE.DTINIVALITM = P_DTINIVALITM
               AND ITEMCOLE.DTFINVALITM = P_DTFINVALITM
               AND COLETAPR.CDOPERADOR  = P_CDCOMPRADOR;
        ELSE
            SELECT MAX(NRPEDIDOCOL), MAX(CDOPERADOR)
              INTO V_NRPEDIDOCOL, V_CDOPERADOR
              FROM PEDCOLETA
             WHERE CDFILIAL    = P_CDFILIAL
               AND NRPEDCOLETA = P_NRPEDCOLETA
               AND CDREGIAO    = P_CDREGIAO
               AND CDFORNECED  = P_CDFORNECED;

            SELECT MAX(ITEMCOLE.NRCOLEPRECO)
              INTO V_NRCOLEPRECO
              FROM ITEMCOLE INNER JOIN COLETAPR ON ITEMCOLE.CDFILIAL = COLETAPR.CDFILIAL
                                               AND ITEMCOLE.NRCOLEPRECO = COLETAPR.NRCOLEPRECO
             WHERE ITEMCOLE.CDFILIAL    = P_CDFILIAL
               AND ITEMCOLE.NRPEDIDOCOL = V_NRPEDIDOCOL
               AND COLETAPR.CDOPERADOR  = V_CDOPERADOR;
        END IF;

        IF V_NRCOLEPRECO IS NULL THEN
            NOVO_CODIGO('COLETA'|| P_CDFILIAL, V_NRCOLEPRECO, 1);
            V_NRCOLEPRECO := SUBSTR(V_NRCOLEPRECO, 11, 10);

            V_IDSITUCOLETA := 'C';

            SELECT NVL(MAX(IDUTISTATCOT), 'N')
              INTO V_IDUTISTATCOT
              FROM PARASUPRI
             WHERE PARASUPRI.CDFILIAL = P_CDFILIAL;

            IF V_IDUTISTATCOT = 'S' THEN
                V_IDSITUCOLETA := 'A';
            END IF;

            INSERT INTO COLETAPR(
                CDFILIAL,
                NRCOLEPRECO,
                DTCOLEPREC,
                DTINIVALCOL,
                DTFINVALCOL,
                CDOPERADOR,
                IDTIPCOLREG,
                NRSEMAINIC,
                NRSEMAFINAL,
                IDSITUCOLETA,
                IDDISMONTLIS,
                CDOPERINCL,
                DTINCLCOLET)
            SELECT CDFILIAL,
                   V_NRCOLEPRECO,
                   TO_DATE(SYSDATE),
                   DTINIPERCOT,
                   DTFINPERCOT,
                   CDOPERADOR,
                   'W',
                   P_NRSEMINI,
                   P_NRSEMFIN,
                   V_IDSITUCOLETA,
                   P_ENTRALISTA,
                   P_CDOPERINCL,
                   TO_DATE(SYSDATE)
              FROM PEDCOLETA
             WHERE CDFILIAL    = P_CDFILIAL    
               AND CDREGIAO    = P_CDREGIAO
               AND CDFORNECED  = P_CDFORNECED
               AND NRPEDCOLETA = P_NRPEDCOLETA;

            V_APAGACOL := 'S';
        ELSE
            V_APAGACOL := 'N';
        END IF;

        V_GEROUREGISTRO := 'N';

        DELETE FROM LOGNGERAPEDCOL
         WHERE CDFILIAL    = P_CDFILIAL
           AND NRPEDCOLETA = P_NRPEDCOLETA
           AND CDREGIAO    = P_CDREGIAO
           AND CDFORNECED  = P_CDFORNECED;

        FOR C_PEDIDO IN (SELECT ITEMPEDCOL.CDPRODUTO,
                                PRODUTO.CDARVPROD,
                                ITEMPEDCOL.CDMARCA,
                                ITEMPEDCOL.QTPRODPEDCOL,
                                CASE NVL(ITEMPEDCOL.VRPRECREN, 0)
                                  WHEN 0 THEN NVL(ITEMPEDCOL.VRITEMPEDCOL, 0)
                                  ELSE ITEMPEDCOL.VRPRECREN
                                END VRITEMPEDCOL,
                                ITEMPEDCOL.VRPERDESPEDCOL,
                                PEDCOLETA.DTINIPERCOT,
                                PEDCOLETA.DTFINPERCOT,
                                PEDCOLETA.CDFORNECED,
                                PEDCOLETA.CDREGIAO,
                                PEDCOLETA.DTPEDCOLETA,
                                FORNECEDOR.IDSITUCADA,
                                FORNECEDOR.CDQUALIF,
                                FORNECEDOR.IDFORNAUTO,
                                FORNECEDOR.DTVALQUALIF
                           FROM ITEMPEDCOL INNER JOIN PEDCOLETA ON ITEMPEDCOL.CDFILIAL    = PEDCOLETA.CDFILIAL
                                                               AND ITEMPEDCOL.NRPEDCOLETA = PEDCOLETA.NRPEDCOLETA 
                                                               AND ITEMPEDCOL.CDFORNECED  = PEDCOLETA.CDFORNECED  
                                                               AND ITEMPEDCOL.CDREGIAO    = PEDCOLETA.CDREGIAO
                                           INNER JOIN PRODUTO ON ITEMPEDCOL.CDPRODUTO = PRODUTO.CDPRODUTO
                                           INNER JOIN FORNECEDOR ON ITEMPEDCOL.CDFORNECED = FORNECEDOR.CDFORNECED
                                            LEFT JOIN LEILAO ON PEDCOLETA.CDLEILAO = LEILAO.CDLEILAO
                          WHERE ((PEDCOLETA.CDLEILAO IS NOT NULL AND ITEMPEDCOL.IDSELELEILAO = 'S') OR (PEDCOLETA.CDLEILAO IS NULL))  
                            AND PEDCOLETA.CDFILIAL     = P_CDFILIAL    
                            AND PEDCOLETA.NRPEDCOLETA  = P_NRPEDCOLETA 
                            AND PEDCOLETA.CDFORNECED   = P_CDFORNECED  
                            AND PEDCOLETA.CDREGIAO     = P_CDREGIAO    
                            AND (NVL(ITEMPEDCOL.VRITEMPEDCOL,0) > 0  OR V_IDGRAVAITEMZERADO = 'S'))
        LOOP
            SELECT COUNT(1)
              INTO V_COUNT
              FROM ITEMCOLE
             WHERE ITEMCOLE.CDFILIAL    = P_CDFILIAL
               AND ITEMCOLE.NRCOLEPRECO = V_NRCOLEPRECO
               AND ITEMCOLE.CDMARCA     = C_PEDIDO.CDMARCA
               AND ITEMCOLE.CDREGIAO    = C_PEDIDO.CDREGIAO
               AND ITEMCOLE.CDFORNECED  = C_PEDIDO.CDFORNECED
               AND ITEMCOLE.CDPRODUTO   = C_PEDIDO.CDPRODUTO;

            IF V_COUNT = 0 THEN
                V_IDMOTNGERPEDCOL := '';

                /* IdMotivNDupl [1] = '1' Fornecedor inativo */
                IF C_PEDIDO.IDSITUCADA = 'I' THEN
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'1';
                ELSE
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'0';
                END IF;

                /* IdMotivNDupl [2] = '1' Fornecedor nao atende a regiao */
                SELECT COUNT(1)
                  INTO V_COUNT
                  FROM REFOREG
                 WHERE REFOREG.CDREGIAO   = C_PEDIDO.CDREGIAO
                   AND REFOREG.CDFORNECED = C_PEDIDO.CDFORNECED;

                IF V_COUNT <> 0 THEN
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'1';
                ELSE
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'0';
                END IF;

                /* IdMotivNDupl [3] = '1' Qualificacao de produto por fornecedor */
                SELECT COUNT(1)
                  INTO V_COUNT
                  FROM QFOPRODFORN, PARASUPRI
                 WHERE PARASUPRI.CDFILIAL     = P_CDFILIAL
                   AND QFOPRODFORN.CDPRODUTO  = C_PEDIDO.CDPRODUTO
                   AND QFOPRODFORN.CDFORNECED = C_PEDIDO.CDFORNECED
                   AND ((QFOPRODFORN.IDFORNAPROV = 'N' AND QFOPRODFORN.IDFORNAUTOR = 'N') OR 
                        (QFOPRODFORN.IDFORNAPROV = 'S' AND ((PARASUPRI.IDULTIDTVALQ = 'M' AND C_PEDIDO.DTPEDCOLETA > LAST_DAY(QFOPRODFORN.DTVALPRFORN)) OR 
                                                            (PARASUPRI.IDULTIDTVALQ = 'D' AND C_PEDIDO.DTPEDCOLETA > QFOPRODFORN.DTVALPRFORN))));

                IF V_COUNT <> 0 THEN
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'1';
                ELSE
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'0';
                END IF;

                /* IdMotivNDupl [4] = '1' Qualificacao de marca por produto */
                SELECT COUNT(1)
                  INTO V_COUNT
                  FROM QFOMARPROD, PARASUPRI
                 WHERE PARASUPRI.CDFILIAL   = P_CDFILIAL
                   AND QFOMARPROD.CDPRODUTO = C_PEDIDO.CDPRODUTO
                   AND QFOMARPROD.CDMARCA   = C_PEDIDO.CDMARCA
                   AND ((QFOMARPROD.IDAPRMARPRO = 'N' AND QFOMARPROD.IDAUTMARPRO = 'N') OR 
                        (QFOMARPROD.IDAPRMARPRO = 'S' AND ((PARASUPRI.IDULTIDTVALQ = 'M' AND C_PEDIDO.DTPEDCOLETA > LAST_DAY(QFOMARPROD.DTVALAPROVA)) OR
                                                           (PARASUPRI.IDULTIDTVALQ = 'D' AND C_PEDIDO.DTPEDCOLETA > QFOMARPROD.DTVALAPROVA))));

                SELECT COUNT(1)
                  INTO V_COUNTAUX
                  FROM (SELECT QFOMARPROD.CDMARCA
                          FROM QFOMARPROD
                         WHERE QFOMARPROD.CDPRODUTO = C_PEDIDO.CDPRODUTO 
                           AND QFOMARPROD.CDMARCA   = C_PEDIDO.CDMARCA
                         UNION
                        SELECT PARASUPRI.CDMARCA
                          FROM PARASUPRI
                         WHERE PARASUPRI.CDFILIAL     = P_CDFILIAL
                           AND (PARASUPRI.IDMARSEMQUA = 'N' OR PARASUPRI.IDMARSEMQUA IS NULL));

                IF (V_COUNT = 0) AND (V_COUNTAUX <> 0) THEN
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'0';
                ELSE
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'1';
                END IF;

                /* IdMotivNDupl [5] = '1' Qualificacao de fornecedor */
                SELECT COUNT(1)
                  INTO V_COUNT
                  FROM QUALIFICACAO, PARASUPRI
                 WHERE QUALIFICACAO.CDQUALIF = C_PEDIDO.CDQUALIF
                   AND PARASUPRI.CDFILIAL    = P_CDFILIAL
                   AND C_PEDIDO.IDFORNAUTO   = 'N'
                   AND (QUALIFICACAO.IDPERCOTCMP = '2' OR ((PARASUPRI.IDULTIDTVALQ = 'D' AND C_PEDIDO.DTFINPERCOT > C_PEDIDO.DTVALQUALIF) OR
                                                           (PARASUPRI.IDULTIDTVALQ = 'M' AND C_PEDIDO.DTFINPERCOT > LAST_DAY(C_PEDIDO.DTVALQUALIF))));

                IF V_COUNT <> 0 THEN
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'1';
                ELSE
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'0';
                END IF;

                /* IdMotivNDupl [6] = '1' Produto nao cadastrado para o fornecedor */
                SELECT COUNT(1)
                  INTO V_COUNT
                  FROM PRODFORNCOMP
                 WHERE PRODFORNCOMP.CDPRODCOMPL = C_PEDIDO.CDPRODUTO
                   AND PRODFORNCOMP.CDFORNECED  = C_PEDIDO.CDFORNECED;

                IF V_COUNT = 0 THEN
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'1';
                ELSE
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'0';
                END IF;

                /* IdMotivNDupl [7] = '1' Marca Inativa */
                SELECT COUNT(1)
                  INTO V_COUNT
                  FROM MARCA
                 WHERE MARCA.CDMARCA      = C_PEDIDO.CDMARCA
                   AND MARCA.IDMARCAATIVA = 'I';

                IF V_COUNT <> 0 THEN
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'1';
                ELSE
                    V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'0';
                END IF;

                V_IDMOTNGERPEDCOL := V_IDMOTNGERPEDCOL||'000';

                IF V_IDMOTNGERPEDCOL = '0000000000' THEN
                    V_GEROUREGISTRO := 'S';
                    V_APAGACOL := 'N';

                    SELECT MAX(PRODUTO.CDARVPROD),
                           MAX(PRODFORN.CDFORMPGTO),
                           MAX(PRODFORN.IDTPFORPGPRF),
                           MAX(PRODFORN.NRPARCPGTPRF),
                           MAX(PRODFORN.NRDIAPGFIPRF),
                           MAX(PRODFORN.IDUTDIAPGPRF)
                      INTO V_CDPRODFORN,
                           V_CDFORMPGTO,
                           V_IDTPFORPGTO,
                           V_NRPARCPGTO,
                           V_NRDIAPGTO,
                           V_IDUTDIAPGTO
                      FROM PRODFORN INNER JOIN PRODUTO ON PRODFORN.CDPRODUTO = PRODUTO.CDPRODUTO
                     WHERE PRODFORN.CDFORNECED = P_CDFORNECED 
                       AND C_PEDIDO.CDARVPROD LIKE PRODUTO.CDARVPROD || '%';

                    IF V_IDTPFORPGTO IS NULL THEN
                        SELECT MAX(CDFORMPGTO),
                               MAX(IDTPFORPGTFO),
                               MAX(NRPARCPGTOFO),
                               MAX(NRDIAPGFIXFO),
                               MAX(IDUTDIAPGTFO)
                          INTO V_CDFORMPGTO,
                               V_IDTPFORPGTO,
                               V_NRPARCPGTO,
                               V_NRDIAPGTO,
                               V_IDUTDIAPGTO
                          FROM FORNECEDOR
                         WHERE CDFORNECED = P_CDFORNECED;

                        INSERT INTO ITEMCOLE(
                            CDFILIAL,     
                            NRCOLEPRECO,  
                            CDFORNECED,    
                            CDREGIAO,
                            CDPRODUTO,    
                            CDMARCA,      
                            QTACMPITMCOL,  
                            VRPRECUNIITM,
                            DTINIVALITM,  
                            DTFINVALITM,   
                            IDTIPFREITM,
                            CDTIPCOB,     
                            VRPERDESITM,   
                            CDFORMPGTO,
                            IDTPFOPGTCOL, 
                            NRPARCPGTCOL, 
                            NRDIAPGFXCOL,  
                            IDUTDIAPGCOL, 
                            NRPEDIDOCOL, 
                            CDOPERINCL, 
                            DTINCLITMCOL)
                        VALUES(
                            P_CDFILIAL, 
                            V_NRCOLEPRECO,  
                            P_CDFORNECED,
                            P_CDREGIAO,
                            C_PEDIDO.CDPRODUTO,    
                            C_PEDIDO.CDMARCA, 
                            C_PEDIDO.QTPRODPEDCOL,
                            C_PEDIDO.VRITEMPEDCOL, 
                            C_PEDIDO.DTINIPERCOT, 
                            C_PEDIDO.DTFINPERCOT,
                            V_IDTIPFRETE, 
                            V_CDTIPCOB, 
                            NVL(C_PEDIDO.VRPERDESPEDCOL, V_VRPERDESC), 
                            V_CDFORMPGTO, 
                            V_IDTPFORPGTO,
                            V_NRPARCPGTO, 
                            V_NRDIAPGTO, 
                            V_IDUTDIAPGTO, 
                            V_NRPEDIDOCOL, 
                            P_CDOPERINCL, 
                            TO_DATE(SYSDATE));

                        DELETE PZOPGTITMCOL
                         WHERE NRCOLEPRECO = V_NRCOLEPRECO
                           AND CDFORNECED  = P_CDFORNECED
                           AND CDFILIAL    = P_CDFILIAL
                           AND CDREGIAO    = P_CDREGIAO
                           AND CDPRODUTO   = C_PEDIDO.CDPRODUTO
                           AND CDMARCA     = C_PEDIDO.CDMARCA;

                        INSERT INTO PZOPGTITMCOL(
                            CDFILIAL, 
                            NRCOLEPRECO, 
                            CDFORNECED, 
                            CDPRODUTO,
                            CDMARCA, 
                            CDREGIAO, 
                            NRDIASPGTCOL, 
                            VRPERCPGTCOL)
                        SELECT P_CDFILIAL,
                               V_NRCOLEPRECO,
                               P_CDFORNECED,
                               C_PEDIDO.CDPRODUTO,
                               C_PEDIDO.CDMARCA,
                               P_CDREGIAO,
                               PRAZOPGTFORN.NRDIASPGTOFO,
                               PRAZOPGTFORN.VRPERCPGTOFO
                          FROM PRAZOPGTFORN
                         WHERE PRAZOPGTFORN.CDFORNECED = P_CDFORNECED;
                    ELSE
                        INSERT INTO ITEMCOLE(
                            CDFILIAL,
                            NRCOLEPRECO,  
                            CDFORNECED,    
                            CDREGIAO,
                            CDPRODUTO,    
                            CDMARCA,      
                            QTACMPITMCOL,  
                            VRPRECUNIITM,
                            DTINIVALITM,  
                            DTFINVALITM,   
                            IDTIPFREITM,
                            CDTIPCOB,     
                            VRPERDESITM,   
                            CDFORMPGTO,
                            IDTPFOPGTCOL, 
                            NRPARCPGTCOL, 
                            NRDIAPGFXCOL,  
                            IDUTDIAPGCOL, 
                            NRPEDIDOCOL, 
                            CDOPERINCL, 
                            DTINCLITMCOL)
                        VALUES(
                            P_CDFILIAL, 
                            V_NRCOLEPRECO,  
                            P_CDFORNECED,     
                            P_CDREGIAO,
                            C_PEDIDO.CDPRODUTO,    
                            C_PEDIDO.CDMARCA, 
                            C_PEDIDO.QTPRODPEDCOL,
                            C_PEDIDO.VRITEMPEDCOL, 
                            C_PEDIDO.DTINIPERCOT, 
                            C_PEDIDO.DTFINPERCOT,
                            V_IDTIPFRETE, 
                            V_CDTIPCOB, 
                            NVL(C_PEDIDO.VRPERDESPEDCOL, V_VRPERDESC), 
                            V_CDFORMPGTO, 
                            V_IDTPFORPGTO,
                            V_NRPARCPGTO, 
                            V_NRDIAPGTO, 
                            V_IDUTDIAPGTO, 
                            V_NRPEDIDOCOL, 
                            P_CDOPERINCL, 
                            TO_DATE(SYSDATE));

                        DELETE PZOPGTITMCOL
                         WHERE NRCOLEPRECO = V_NRCOLEPRECO
                           AND CDFORNECED  = P_CDFORNECED
                           AND CDFILIAL    = P_CDFILIAL
                           AND CDREGIAO    = P_CDREGIAO
                           AND CDPRODUTO   = C_PEDIDO.CDPRODUTO
                           AND CDMARCA     = C_PEDIDO.CDMARCA;

                        INSERT INTO PZOPGTITMCOL(
                            CDFILIAL, 
                            NRCOLEPRECO, 
                            CDFORNECED, 
                            CDPRODUTO,
                            CDMARCA, 
                            CDREGIAO, 
                            NRDIASPGTCOL, 
                            VRPERCPGTCOL)
                        SELECT P_CDFILIAL,
                               V_NRCOLEPRECO,
                               P_CDFORNECED,
                               C_PEDIDO.CDPRODUTO,
                               C_PEDIDO.CDMARCA,
                               P_CDREGIAO,
                               PZOPGTPRFO.NRDIASPGPRFO,
                               PZOPGTPRFO.VRPERCPGPRFO
                          FROM PZOPGTPRFO INNER JOIN PRODUTO ON PZOPGTPRFO.CDPRODUTO = PRODUTO.CDPRODUTO
                         WHERE CDFORNECED = P_CDFORNECED 
                           AND CDARVPROD  = V_CDPRODFORN;
                    END IF;

                    SELECT SUBSTR(EMITENTE(P_CDFILIAL, C_PEDIDO.CDPRODUTO, P_CDFORNECED), 3, 2) CDEMPRESA
                      INTO V_CDEMPRESA
                      FROM PARAMGERAL;

                    SELECT MIN(INSCRESTAD.CDINSCESTA) INTO V_INSCESTA
                      FROM EMPRESA INNER JOIN INSCRMUNICIP ON EMPRESA.CDEMPRESA = INSCRMUNICIP.CDEMPRESA
                                   INNER JOIN INSCRESTAD ON EMPRESA.CDEMPRESA = INSCRESTAD.CDEMPRESA
                     WHERE EMPRESA.CDEMPRESA = V_CDEMPRESA;

                    IMPOSTO_ITEMCOL(P_EMPRESA    => V_CDEMPRESA,
                                    P_INSCESTA   => V_INSCESTA,
                                    P_FORNECEDOR => P_CDFORNECED,
                                    P_FILCENT    => P_CDFILIAL,
                                    P_LANCCOLP   => V_NRCOLEPRECO,
                                    P_CDFORNECED => P_CDFORNECED,
                                    P_CDREGIAO   => P_CDREGIAO,
                                    P_CDPRODUTO  => C_PEDIDO.CDPRODUTO,
                                    P_CDMARCA    => C_PEDIDO.CDMARCA,
                                    P_INIVALID   => TO_DATE(SYSDATE),
                                    P_TRANSACAO  => P_TRANSACAO);

                ELSE
                    P_COTA_N_GER := 'S';
                    INSERT INTO LOGNGERAPEDCOL(
                        CDFILIAL,
                        NRPEDCOLETA,
                        CDFORNECED,
                        CDREGIAO,
                        CDPRODUTO,
                        CDMARCA,
                        IDMOTNGERPEDCOL)
                    VALUES(
                        P_CDFILIAL,
                        P_NRPEDCOLETA,
                        C_PEDIDO.CDFORNECED,
                        C_PEDIDO.CDREGIAO,
                        C_PEDIDO.CDPRODUTO,
                        C_PEDIDO.CDMARCA,
                        V_IDMOTNGERPEDCOL);
                END IF;
            END IF;
        END LOOP;

        /* Forcar execucao da trigger - solucao temporaria */
        IF (V_GEROUREGISTRO = 'S') AND (V_APAGACOL = 'N') THEN
            UPDATE ITEMCOLE
               SET IDTPFOPGTCOL = IDTPFOPGTCOL
             WHERE CDFILIAL    = P_CDFILIAL
               AND NRCOLEPRECO = V_NRCOLEPRECO;
        END IF;

        IF V_APAGACOL = 'S' THEN
            DELETE COLETAPR
             WHERE CDFILIAL    = P_CDFILIAL
               AND NRCOLEPRECO = V_NRCOLEPRECO;
        END IF;
    END GRAVA_COTWEB;    

END SUP_COTACAO;
/