CREATE OR REPLACE FUNCTION BUSCA_PRECOBASE( P_CDFILIAL       IN FILIAL.CDFILIAL%TYPE,
                                            P_CDCLIENTE      IN CLIENTE.CDCLIENTE%TYPE,
                                            P_CDVENDEDOR     IN VENDEDOR.CDVENDEDOR%TYPE,
                                            P_CDMODPGTO      IN MODPGTO.CDMODPGTO%TYPE )RETURN VARCHAR2
AS
   V_CTREGISTRO NUMBER;
   V_CDPRECOBASE PRECOBASE.CDPRECOBASE%TYPE;
BEGIN
   V_CDPRECOBASE := '';
   SELECT COUNT(CDPRECOBASE) INTO V_CTREGISTRO
      FROM PRECOBASECLI
      WHERE CDCLIENTE = P_CDCLIENTE
        AND IDPRBASEPRIN = 'S';

   IF V_CTREGISTRO > 0 THEN
      SELECT MAX(CDPRECOBASE) INTO V_CDPRECOBASE
      FROM PRECOBASECLI
      WHERE CDCLIENTE = P_CDCLIENTE
        AND IDPRBASEPRIN = 'S';
   ELSE
      SELECT COUNT(CDPRECOBASE) INTO V_CTREGISTRO
      FROM VENDEDOR
      WHERE CDVENDEDOR = P_CDVENDEDOR;

      IF V_CTREGISTRO > 0 THEN
         SELECT MAX(CDPRECOBASE) INTO V_CDPRECOBASE
         FROM VENDEDOR
         WHERE CDVENDEDOR = P_CDVENDEDOR;
      ELSE
         SELECT COUNT(CDPRECOBASE) INTO V_CTREGISTRO
         FROM PRECOUTI
         WHERE CDFILIAL  = P_CDFILIAL
           AND CDMODPGTO = P_CDMODPGTO;

         IF V_CTREGISTRO > 0 THEN
            SELECT MAX(CDPRECOBASE) INTO V_CDPRECOBASE
            FROM PRECOUTI
            WHERE CDFILIAL  = P_CDFILIAL
              AND CDMODPGTO = P_CDMODPGTO;
         END IF;
      END IF;
   END IF;
   RETURN(V_CDPRECOBASE);
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('BUSCA_PRECOBASE','4.05.131'); END;
/

