CREATE OR REPLACE PACKAGE PEDIDO_COLETA IS

    K_A   CONSTANT VARCHAR2(01) := 'A';
    K_C   CONSTANT VARCHAR2(01) := 'C';
    K_D   CONSTANT VARCHAR2(01) := 'D';
    K_M   CONSTANT VARCHAR2(01) := 'M';
    K_N   CONSTANT VARCHAR2(01) := 'N';
    K_O   CONSTANT VARCHAR2(01) := 'O';
    K_R   CONSTANT VARCHAR2(01) := 'R';
    K_S   CONSTANT VARCHAR2(01) := 'S';
    K_Z   CONSTANT VARCHAR2(01) := 'Z';
    K_CP  CONSTANT VARCHAR2(01) := '%';

    SUBTYPE TYPE_PEDIDOCOLETATMP IS PEDIDOCOLETA_TMP%ROWTYPE;
    TYPE TABLE_PEDIDOCOLETATMP   IS TABLE OF TYPE_PEDIDOCOLETATMP;

    SUBTYPE S_RECORD_PEDIDOCOLETA_TMP IS PEDIDOCOLETA_TMP%ROWTYPE;
    SUBTYPE S_RECORD_PARASUPRI        IS PARASUPRI%ROWTYPE;
    SUBTYPE S_RECORD_FILIAL           IS FILIAL%ROWTYPE;
    

    PROCEDURE GET_PARASUPRI(P_CDFILIAL   IN FILIAL.CDFILIAL%TYPE,
                            P_PARASUPRI OUT S_RECORD_PARASUPRI);
    PROCEDURE GET_DADOSFILIAL(P_CDFILIAL IN FILIAL.CDFILIAL%TYPE,
                              P_FILIAL  OUT S_RECORD_FILIAL);

    PROCEDURE INSERT_PEDIDOCOLETATMP(P_ITEMS IN S_RECORD_PEDIDOCOLETA_TMP);

    FUNCTION MONTA_TODOS_ACOMPRAR(P_PARASUPRI   IN S_RECORD_PARASUPRI,
                                  P_FILIAL      IN S_RECORD_FILIAL, 
                                  P_CDFILIPROC  IN FILIAL.CDFILIAL%TYPE,
                                  P_CDFILIAL    IN FILIAL.CDFILIAL%TYPE,
                                  P_CDMARCA     IN VARCHAR2,
                                  P_CDCOMPRADOR IN VARCHAR2,
                                  P_CDFORNECED  IN VARCHAR2,
                                  P_CDREGIAO    IN VARCHAR2,
                                  P_CDPRODINI   IN PRODUTO.CDARVPROD%TYPE,
                                  P_CDPRODFIN   IN PRODUTO.CDARVPROD%TYPE,
                                  P_DTINI       IN DATE,
                                  P_DTFIN       IN DATE,
                                  P_TIPOPEDIDO  IN VARCHAR2) RETURN VARCHAR2;

    FUNCTION MONTA_COTACAO(P_PARASUPRI   IN S_RECORD_PARASUPRI,
                           P_FILIAL      IN S_RECORD_FILIAL, 
                           P_CDFILIPROC  IN FILIAL.CDFILIAL%TYPE,
                           P_CDFILIAL    IN FILIAL.CDFILIAL%TYPE,
                           P_CDMARCA     IN VARCHAR2,
                           P_CDCOMPRADOR IN VARCHAR2,
                           P_CDFORNECED  IN VARCHAR2,
                           P_CDREGIAO    IN VARCHAR2,
                           P_CDPRODINI   IN PRODUTO.CDARVPROD%TYPE,
                           P_CDPRODFIN   IN PRODUTO.CDARVPROD%TYPE,
                           P_NRCOLEPRECO IN ITEMCOLE.NRCOLEPRECO%TYPE,
                           P_DESCFORNCOT IN VARCHAR2 DEFAULT 'N') RETURN VARCHAR2;

 
    FUNCTION MONTA_LISTPREC_IDSISDOS_3(P_PARASUPRI    IN S_RECORD_PARASUPRI,
                                       P_FILIAL       IN S_RECORD_FILIAL,
                                       P_CDFILIPROC   IN FILIAL.CDFILIAL%TYPE,
                                       P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
                                       P_CDMARCA      IN VARCHAR2,
                                       P_CDCOMPRADOR  IN VARCHAR2,
                                       P_CDFORNECED   IN VARCHAR2,
                                       P_CDREGIAO     IN VARCHAR2,
                                       P_CDPRODINI    IN PRODUTO.CDARVPROD%TYPE,
                                       P_CDPRODFIN    IN PRODUTO.CDARVPROD%TYPE,
                                       P_NRSEQPRELIST IN ITEMCOLE.NRSEQPRELIST%TYPE,
                                       P_DTINI        IN DATE,
                                       P_DTFIN        IN DATE) RETURN VARCHAR2;

    FUNCTION MONTA_LISTPREC_IDSISDOS_1(P_PARASUPRI    IN S_RECORD_PARASUPRI,
                                       P_FILIAL       IN S_RECORD_FILIAL,
                                       P_CDFILIPROC   IN FILIAL.CDFILIAL%TYPE,
                                       P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
                                       P_CDMARCA      IN VARCHAR2,
                                       P_CDCOMPRADOR  IN VARCHAR2,
                                       P_CDFORNECED   IN VARCHAR2,
                                       P_CDREGIAO     IN VARCHAR2,
                                       P_CDPRODINI    IN PRODUTO.CDARVPROD%TYPE,
                                       P_CDPRODFIN    IN PRODUTO.CDARVPROD%TYPE,
                                       P_NRSEQPRELIST IN ITEMCOLE.NRSEQPRELIST%TYPE) RETURN VARCHAR2;


    FUNCTION MONTA_NRCOMPRA(P_PARASUPRI   IN S_RECORD_PARASUPRI,
                            P_FILIAL      IN S_RECORD_FILIAL,
                            P_CDFILIPROC  IN FILIAL.CDFILIAL%TYPE,
                            P_CDFILIAL    IN FILIAL.CDFILIAL%TYPE,
                            P_CDMARCA     IN VARCHAR2,
                            P_CDCOMPRADOR IN VARCHAR2,
                            P_CDFORNECED  IN VARCHAR2,
                            P_CDREGIAO    IN VARCHAR2,
                            P_CDPRODINI   IN PRODUTO.CDARVPROD%TYPE,
                            P_CDPRODFIN   IN PRODUTO.CDARVPROD%TYPE,
                            P_NRCOMPRA    IN COMPRA.NRCOMPRA%TYPE,
                            P_DTINI       IN DATE,
                            P_DTFIN       IN DATE) RETURN VARCHAR2;


    PROCEDURE GETDATAPEDIDOCOLETA(P_CDFILIAL     IN FILIAL.CDFILIAL%TYPE,
                                  P_CDFILIPROC   IN FILIAL.CDFILIAL%TYPE,
                                  P_CDCOMPRADOR  IN VARCHAR2,
                                  P_CDPRODINI    IN VARCHAR2,
                                  P_CDPRODFIN    IN VARCHAR2,
                                  P_CDFORNECED   IN VARCHAR2,
                                  P_CDREGIAO     IN VARCHAR2,
                                  P_CDMARCA      IN VARCHAR2,
                                  P_DTINI        IN DATE,
                                  P_DTFIN        IN DATE,
                                  P_TIPOPEDIDO   IN VARCHAR2,
                                  P_NRCOLEPRECO  IN ITEMCOLE.NRCOLEPRECO%TYPE,
                                  P_DESCFORNCOT  IN VARCHAR2,
                                  P_NRCOMPRA     IN ITEMCOLE.NRCOLEPRECO%TYPE,
                                  P_CDLEILAO     IN PEDCOLETA.CDLEILAO%TYPE,
                                  P_NRSEQPRELIST IN ITEMCOLE.NRSEQPRELIST%TYPE,
                                  P_CDOPERLANC   IN OPERADOR.CDOPERADOR%TYPE,
                                  P_NRORG        IN FILIAL.NRORG%TYPE,
                                  P_NRPEDCOL     IN OUT PEDIDOCOLETA_TMP.NRPEDCOL%TYPE,
                                  P_ERROR       OUT VARCHAR2);


    PROCEDURE SAVEPEDCOL_PREVCONS(P_CDFILIAL    IN VARCHAR2,
                                  P_CDPRODINI   IN VARCHAR2,
                                  P_CDPRODFIN   IN VARCHAR2,
                                  P_CDMARCA     IN VARCHAR2,
                                  P_CDCOMPRADOR IN VARCHAR2,
                                  P_CDREGIAO    IN VARCHAR2,
                                  P_CDFORNECED  IN VARCHAR2,
                                  P_DTINI       IN DATE,
                                  P_DTFIN       IN DATE,
                                  P_NRORG       IN FILIAL.NRORG%TYPE,
                                  P_NRPEDCOL    IN PEDIDOCOLETA_TMP.NRPEDCOL%TYPE DEFAULT NULL);


    PROCEDURE SAVEPEDIDOCOLETA(P_NRPEDCOL       IN PEDIDOCOLETA_TMP.NRPEDCOL%TYPE, 
                               P_CDFILIAL       IN FILIAL.CDFILIAL%TYPE,
                               P_CDFILIPROC     IN FILIAL.CDFILIAL%TYPE,
                               P_QTMESES        IN FLOAT,
                               P_DTEXPIRAPEDCOL IN DATE,
                               P_IDPEDGERALREG  IN VARCHAR2,
                               P_IDPEDGERALFORN IN VARCHAR2,
                               P_IDCURVAABCAF   IN VARCHAR2,
                               P_IDCONSFORNAF   IN VARCHAR2,
                               P_ERROR         OUT VARCHAR2);

    FUNCTION GETPEDCOLETA(P_IDPEDCOL         IN VARCHAR2,
                          P_NRCOTACAO        IN VARCHAR2,
                          P_COMPRADOR        IN VARCHAR2,
                          P_MARCA            IN VARCHAR2,
                          P_FORNECED         IN VARCHAR2,
                          P_REGIAO           IN VARCHAR2,
                          P_PRODINI          IN VARCHAR2,
                          P_PRODFIN          IN VARCHAR2,
                          P_CDFILIAL         IN VARCHAR2,
                          P_CDFILIPROC       IN VARCHAR2,
                          P_NRCOMPRA         IN VARCHAR2,
                          P_CDLEILAO         IN VARCHAR2,
                          P_NRSEQPRELIST     IN VARCHAR2,
                          P_CDOPERLANC       IN VARCHAR2,
                          P_DESCFORNECEDOR   IN VARCHAR2,
                          P_LISTFORNECEDOR   IN VARCHAR2,
                          P_QTMESES          IN VARCHAR2,
                          P_DTINI            IN DATE,
                          P_DTFIN            IN DATE,
                          P_DTEXPIRAPEDCOL   IN DATE,
                          P_BOPEDEMAIL       IN VARCHAR2,
                          P_IDPEDGERALREG    IN VARCHAR2,
                          P_IDPEDGERALFORN   IN VARCHAR2,
                          P_IDCURVAABCAF     IN VARCHAR2,
                          P_IDENVMERCADUM    IN VARCHAR2,
                          P_BOACRESITEM      IN VARCHAR2,
                          P_IDCONSFORNAF     IN VARCHAR2,
                          P_NRORG            IN VARCHAR2 DEFAULT '1') RETURN GETPEDCOLETA_TABLE PIPELINED;
END PEDIDO_COLETA;
/
