CREATE OR REPLACE
PROCEDURE EXCLUI_AF_POR_PREVISAO_COMPRA (P_CDFILCENTCMP   IN ITMAPURPREC.CDCENTITMAP%TYPE,         /*Central de Compras da Unidade de Processamento*/
                                         P_NRAPURPREC     IN ITMAPURPREC.NRSEQITMAP%TYPE,          /*N�mero sequencial da Apura��o*/
                                         P_CDFORNECED     IN VARCHAR2,                             /*Fornecedor Filtrado*/
                                         P_CDFILIAL       IN VARCHAR2,                             /*Unidade Filtrada*/
                                         P_CDCOMPRADOR    IN VARCHAR2,                             /*Comprador Filrado*/
                                         P_CDPRODINI      IN PRODUTO.CDARVPROD%TYPE,               /*Produto Inicial filtrado*/
                                         P_CDPRODFIN      IN PRODUTO.CDARVPROD%TYPE,               /*Produto Final Filtrado*/
                                         P_CDMARCA        IN VARCHAR2,                             /*Marca Filtrada*/
                                         P_DTENTREGA      IN ITMAPURPREC.DTENTRAPUT%TYPE,          /*Data de entrega filtrada*/
                                         P_ROWSAFFECT     OUT VARCHAR2)
IS
   V_SEM_ERRO VARCHAR2(100);
BEGIN
   P_ROWSAFFECT := 'N';
   FOR C_ITEM IN (SELECT ITMAPURPREC.CDCENTITMAP, ITMAPURPREC.NRSEQITMAP, ITMAPURPREC.CDREGITMAP, ITMAPURPREC.CDFORNITMAP,
                         ITMAPURPREC.NRCOLEITMAP, ITMAPURPREC.DTENTRAPUT, ITMAPURPREC.CDFILIAP, ITMAPURPREC.CDPRODITMAP,
                         ITMAPURPREC.CDMARCITMAP, APURPRECO.CDFILLANC
                    FROM ITMAPURPREC, PRODUTO, APURPRECO
                   WHERE ITMAPURPREC.CDCENTITMAP = APURPRECO.CDFILCENTCMP
                     AND ITMAPURPREC.NRSEQITMAP  = APURPRECO.NRAPURPREC
                     AND ITMAPURPREC.CDCENTITMAP = P_CDFILCENTCMP
                     AND ITMAPURPREC.NRSEQITMAP = P_NRAPURPREC
                     AND NVL(ITMAPURPREC.IDITMSELPREVCMP, 'N') = 'S'
                     AND APURPRECO.CDCOMPRADOR =  P_CDCOMPRADOR
                     AND APURPRECO.IDSITAPUPREC  = '1'
                     AND ITMAPURPREC.CDPRODITMAP = PRODUTO.CDPRODUTO
                     /*AND ITMAPURPREC.DTENTRAPUT  =  P_DTENTREGA*/ 
                     AND ( ( INSTR(P_CDMARCA, ITMAPURPREC.CDMARCITMAP) > 0 ) OR ( P_CDMARCA = 'T' ) )
                     AND ( ( INSTR(P_CDFILIAL, ITMAPURPREC.CDFILIAP) > 0 ) OR ( P_CDFILIAL = 'T' ) )
                     AND ( ( INSTR(P_CDFORNECED, ITMAPURPREC.CDFORNITMAP) > 0 ) OR ( P_CDFORNECED = 'T' ) )
                     AND ( PRODUTO.CDARVPROD BETWEEN P_CDPRODINI AND RPAD(P_CDPRODFIN, 20, 'Z') ))
   LOOP
      UPDATE ITMAPURPREC
         SET IDITMSELPREVCMP = 'N',
             IDITMSELAPUR    = 'N'/*,
             QTSOLACOMP      = 0,
             QTACOMP         = 0*/
       WHERE CDCENTITMAP     = P_CDFILCENTCMP
         AND NRSEQITMAP      = P_NRAPURPREC
         AND CDFILIAP        = C_ITEM.CDFILIAP
         AND DTENTRAPUT      = C_ITEM.DTENTRAPUT
         AND CDPRODITMAP     = C_ITEM.CDPRODITMAP
         AND NRCOLEITMAP     = C_ITEM.NRCOLEITMAP
         AND CDFORNITMAP     = C_ITEM.CDFORNITMAP
         AND CDREGITMAP      = C_ITEM.CDREGITMAP
         AND CDMARCITMAP     = C_ITEM.CDMARCITMAP;
      FOR C_ITEMAUTF IN (SELECT ITEMAUTF.CDFILDEST, ITEMAUTF.CDFILLANC, ITEMAUTF.NRAUTOFORN,
                                ITEMAUTF.CDMARCA, ITEMAUTF.CDPRODUTO, ITEMAUTF.DTPROGENTAF,
                                ITEMAUTF.ROWID
                           FROM ITEMAUTF
                          WHERE ITEMAUTF.CDFILDEST   = C_ITEM.CDFILIAP
                            AND ITEMAUTF.CDFILLANC   = C_ITEM.CDFILLANC
                            AND ITEMAUTF.CDMARCA     = C_ITEM.CDMARCITMAP
                            AND ITEMAUTF.CDPRODUTO   = C_ITEM.CDPRODITMAP
                            AND ITEMAUTF.DTPROGENTAF = C_ITEM.DTENTRAPUT
                            AND NVL(ITEMAUTF.IDBAIXITMAF, '0') = '0'
                            AND EXISTS (SELECT 1
                                          FROM AUTOFORNE
                                         WHERE AUTOFORNE.CDFILDEST  = C_ITEM.CDFILIAP
                                           AND AUTOFORNE.CDFILLANC  = C_ITEM.CDFILLANC
                                           AND AUTOFORNE.NRAUTOFORN = ITEMAUTF.NRAUTOFORN
                                           AND AUTOFORNE.NRSEQAPURAF = P_NRAPURPREC
                                           AND AUTOFORNE.CDFORNECED  = C_ITEM.CDFORNITMAP
                                           AND NVL(AUTOFORNE.IDBAIXAAF, '0') = '0'))
      LOOP
         DELETE
           FROM ITEMAUTF
          WHERE ITEMAUTF.ROWID = C_ITEMAUTF.ROWID;

         DELETE
           FROM AUTOFORNE
          WHERE AUTOFORNE.CDFILDEST  = C_ITEMAUTF.CDFILDEST
            AND AUTOFORNE.CDFILLANC  = C_ITEMAUTF.CDFILLANC
            AND AUTOFORNE.NRAUTOFORN = C_ITEMAUTF.NRAUTOFORN
            AND AUTOFORNE.NRSEQAPURAF = P_NRAPURPREC
            AND NOT EXISTS (SELECT 1
                              FROM ITEMAUTF
                             WHERE ITEMAUTF.CDFILDEST  = C_ITEMAUTF.CDFILDEST
                               AND ITEMAUTF.CDFILLANC  = C_ITEMAUTF.CDFILLANC
                               AND ITEMAUTF.NRAUTOFORN = C_ITEMAUTF.NRAUTOFORN);

         FECHA_AUTOFORNE_MOBILE(C_ITEMAUTF.CDFILDEST, C_ITEMAUTF.CDFILLANC, C_ITEMAUTF.NRAUTOFORN, V_SEM_ERRO);

         P_ROWSAFFECT := 'S';
      END LOOP;
   END LOOP;
END;
/