summaryrefslogtreecommitdiff
path: root/sql/modules/Customer.sql
blob: a024b9fcf2455bf31c88f0bdc004a68ef2ac6015 (plain)
  1. BEGIN;
  2. CREATE OR REPLACE FUNCTION customer_location_save (
  3. in_entity_id int,
  4. in_location_class int, in_line_one text, in_line_two text,
  5. in_line_three text,
  6. in_city TEXT, in_state text, in_mail_code text, in_country_id int
  7. ) returns int AS $$
  8. BEGIN
  9. return _entity_location_save(
  10. in_entity_id, NULL,
  11. in_location_class, in_line_one, in_line_two, in_line_three,
  12. in_city, in_state, in_mail_code, in_country_id);
  13. END;
  14. $$ language 'plpgsql';
  15. CREATE OR REPLACE FUNCTION customer_search(in_pattern TEXT) returns setof customer_search_return as $$
  16. -- searches customer name, account number, street address, city, state,
  17. -- other location-based stuff
  18. declare
  19. v_row customer_search_return;
  20. query text;
  21. begin
  22. for v_row in select c.legal_name, v.* from customer v
  23. join company c on c.entity_id = v.entity_id
  24. join entity e on e.id = v.entity_id
  25. join company_to_location ctl on c.id = ctl.company_id
  26. join location l on l.id = ctl.location_id
  27. where l.line_one % in_pattern
  28. OR l.line_two % in_pattern
  29. OR l.line_three % in_pattern
  30. OR l.city_province % in_pattern
  31. OR c.legal_name % in_pattern
  32. OR e.name % in_pattern
  33. LOOP
  34. RETURN NEXT v_row;
  35. END LOOP;
  36. RETURN;
  37. end;
  38. $$ language 'plpgsql';
  39. COMMIT;