
CREATE OR REPLACE FUNCTION public.search_payments(search text)
 RETURNS SETOF payments
 LANGUAGE plpgsql
 STABLE
AS $function$

BEGIN
  if search = '' then
    return query select * from payments ;
  else 
    return query SELECT
  p.*
FROM
  payments p, jobs j
WHERE
p.jobid = j.id AND
(
search <% p.paymentnum OR
search <% j.ownr_fn OR
search <% j.ownr_ln OR
search <% j.ownr_co_nm OR
search <% j.ro_number OR
  search <% (p.payer) OR
  search <% (p.transactionid) OR
  search <% (p.memo));
  end if;

	END
$function$;