summaryrefslogtreecommitdiff
path: root/sql/modules/Vendor.sql
blob: 1675d5eed839b177263c6fdc31351923dcee1d25 (plain)
  1. BEGIN;
  2. -- TODO: Move indexes to Pg-database
  3. CREATE OR REPLACE FUNCTION vendor_search(in_name TEXT, in_address TEXT,
  4. in_city_prov TEXT)
  5. RETURNS SETOF vendor_search_return AS $$
  6. -- searches vendor name, account number, street address, city, state,
  7. -- other location-based stuff
  8. declare
  9. v_row vendor_search_return;
  10. query text;
  11. begin
  12. for v_row in select c.legal_name, v.* from vendor v
  13. join company c on c.entity_id = v.entity_id
  14. join entity e on e.id = v.entity_id
  15. join company_to_location ctl on c.id = ctl.company_id
  16. join location l on l.id = ctl.location_id
  17. where (
  18. l.line_one % in_address
  19. OR l.line_two % in_address
  20. OR l.line_three % in_address
  21. )
  22. OR l.city_province % in_city_prov
  23. OR (
  24. c.legal_name % in_name
  25. OR e.name % in_name
  26. )
  27. LOOP
  28. RETURN NEXT v_row;
  29. END LOOP;
  30. RETURN;
  31. end;
  32. $$ language 'plpgsql';
  33. COMMIT;