/* repetia cliente si contactos igual al nombre */
SET TERM ^ ;

CREATE OR ALTER procedure BUSCAR_CLIENTES_MOVIL (
    BUSCAR varchar(20),
    VEND integer)
returns (
    TERC_NIT varchar(20),
    TERC_NOM varchar(60),
    TERC_CONTACTO varchar(255),
    TERC_DIR varchar(60))
AS
declare variable CLIXVEND CHAR(2);
begin
EXECUTE PROCEDURE lee_configuracion('FACTURACION', 'PEDIDOS', 'MOSTRAR SOLO CLIENTES DEL VENDEDOR EN PEDIDOS WEB') returning_values (:CLIXVEND);
--BUSQUEDA POR NOMBRE
for select t.terc_nit, terc_nom, terc_contacto, terc_dir from terceros t, clientes c
  where t.terc_nit = c.terc_nit and terc_clie = 'S' and ((vend_cod = :vend) or (:CLIXVEND = 'NO'))
  AND (upper(terc_nom) like upper('%' || :buscar || '%')) and clie_estado = 'A' order by terc_nom
  into terc_nit, terc_nom, terc_contacto, terc_dir
  do
  BEGIN
  if (terc_contacto is null) then
    terc_contacto = '';
  if (terc_dir is null) then
    terc_dir = '';
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_NOM) returning_values (TERC_NOM);
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_CONTACTO) returning_values (TERC_CONTACTO);
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_DIR) returning_values (TERC_DIR);
  suspend;
  END
--BUSQUEDA POR NIT
for select t.terc_nit, terc_nom, terc_contacto, terc_dir from terceros t, clientes c
  where t.terc_nit = c.terc_nit and terc_clie = 'S' and ((vend_cod = :vend) or (:CLIXVEND = 'NO'))
  AND (upper(c.terc_nit) like upper('%' || :buscar || '%')) and clie_estado = 'A' order by terc_nom
  into terc_nit, terc_nom, terc_contacto, terc_dir
  do
  BEGIN
  if (terc_contacto is null) then
    terc_contacto = '';
  if (terc_dir is null) then
    terc_dir = '';
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_NOM) returning_values (TERC_NOM);
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_CONTACTO) returning_values (TERC_CONTACTO);
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_DIR) returning_values (TERC_DIR);
  suspend;
  END
--BUSQUEDA POR CONTACTO
for select t.terc_nit, terc_nom, terc_contacto, terc_dir from terceros t, clientes c
  where t.terc_nit = c.terc_nit and terc_clie = 'S' and ((vend_cod = :vend) or (:CLIXVEND = 'NO'))
  AND (upper(terc_contacto) like upper('%' || :buscar || '%')) and clie_estado = 'A'
  AND NOT EXISTS (select t2.terc_nit from terceros t2, clientes c2
  where t2.terc_nit = c2.terc_nit and terc_clie = 'S' and ((C2.vend_cod = :vend) or (:CLIXVEND = 'NO'))
  AND (upper(t2.terc_nom) like upper('%' || :buscar || '%')) and c2.clie_estado = 'A' AND t2.TERC_NIT = t.TERC_NIT) order by terc_nom
  into terc_nit, terc_nom, terc_contacto, terc_dir
  do
  BEGIN
  if (terc_contacto is null) then
    terc_contacto = '';
  if (terc_dir is null) then
    terc_dir = '';
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_NOM) returning_values (TERC_NOM);
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_CONTACTO) returning_values (TERC_CONTACTO);
  EXECUTE PROCEDURE NOMBRE_ASCII(TERC_DIR) returning_values (TERC_DIR);
  suspend;
  END
end^

SET TERM ; ^



