summaryrefslogtreecommitdiff
path: root/sql/modules/Person.sql
blob: f942b1371684affc2c573217f1e53b2ec3e1c19d (plain)
  1. begin;
  2. CREATE OR REPLACE FUNCTION person__save
  3. (in_entity_id integer, in_salutation_id int,
  4. in_first_name text, in_middle_name text, in_last_name text
  5. )
  6. RETURNS INT AS $$
  7. DECLARE
  8. e_id int;
  9. e entity;
  10. loc location;
  11. l_id int;
  12. p_id int;
  13. BEGIN
  14. select * into e from entity where id = in_entity_id and entity_class = 3;
  15. e_id := in_entity_id;
  16. IF NOT FOUND THEN
  17. INSERT INTO entity (name, entity_class)
  18. values (in_first_name || ' ' || in_last_name, 3);
  19. e_id := currval('entity_id_seq');
  20. END IF;
  21. UPDATE person SET
  22. salutation_id = in_salutation_id,
  23. first_name = in_first_name,
  24. last_name = in_last_name,
  25. middle_name = in_middle_name
  26. WHERE
  27. entity_id = in_entity_id;
  28. IF FOUND THEN
  29. RETURN in_entity_id;
  30. ELSE
  31. -- Do an insert
  32. INSERT INTO person (salutation_id, first_name, last_name, entity_id)
  33. VALUES (in_salutation_id, in_first_name, in_last_name, e_id);
  34. RETURN e_id;
  35. END IF;
  36. END;
  37. $$ language plpgsql;
  38. CREATE OR REPLACE FUNCTION employee__save
  39. (in_entity_id int, in_start_date date, in_end_date date, in_dob date,
  40. in_role text, in_ssn text, in_sales bool, in_manager_id int, in_employee_number text)
  41. RETURNS int AS $$
  42. DECLARE out_id INT;
  43. BEGIN
  44. UPDATE entity_employee
  45. SET startdate = in_start_date,
  46. enddate = in_end_date,
  47. dob = in_dob,
  48. role = in_role,
  49. ssn = in_ssn,
  50. manager_id = in_manager_id,
  51. employeenumber = in_employee_number,
  52. person_id = (select id FROM person
  53. WHERE entity_id = in_entity_id)
  54. WHERE entity_id = in_entity_id;
  55. out_id = in_entity_id;
  56. IF NOT FOUND THEN
  57. INSERT INTO entity_employee
  58. (startdate, enddate, dob, role, ssn, manager_id,
  59. employeenumber, entity_id, person_id)
  60. VALUES
  61. (in_start_date, in_end_date, in_dob, in_role, in_ssn,
  62. in_manager_id, in_employee_number, in_entity_id,
  63. (SELECT id FROM person
  64. WHERE entity_id = in_entity_id));
  65. RETURN in_entity_id;
  66. END IF;
  67. END;
  68. $$ LANGUAGE PLPGSQL;
  69. CREATE OR REPLACE FUNCTION person__list_locations(in_entity_id int)
  70. RETURNS SETOF location_result AS
  71. $$
  72. DECLARE out_row RECORD;
  73. BEGIN
  74. FOR out_row IN
  75. SELECT l.id, l.line_one, l.line_two, l.line_three, l.city,
  76. l.state, l.mail_code, c.name, lc.class
  77. FROM location l
  78. JOIN person_to_location ctl ON (ctl.location_id = l.id)
  79. JOIN person p ON (ctl.person_id = p.id)
  80. JOIN location_class lc ON (ctl.location_class = lc.id)
  81. JOIN country c ON (c.id = l.country_id)
  82. WHERE p.entity_id = in_entity_id
  83. ORDER BY lc.id, l.id, c.name
  84. LOOP
  85. RETURN NEXT out_row;
  86. END LOOP;
  87. END;
  88. $$ LANGUAGE PLPGSQL;
  89. CREATE OR REPLACE FUNCTION person__list_contacts(in_entity_id int)
  90. RETURNS SETOF contact_list AS
  91. $$
  92. DECLARE out_row RECORD;
  93. BEGIN
  94. FOR out_row IN
  95. SELECT cc.class, cc.id, c.contact
  96. FROM person_to_contact c
  97. JOIN contact_class cc ON (c.contact_class_id = cc.id)
  98. JOIN person p ON (c.person_id = p.id)
  99. WHERE p.entity_id = in_entity_id
  100. LOOP
  101. RETURN NEXT out_row;
  102. END LOOP;
  103. END;
  104. $$ LANGUAGE plpgsql;
  105. CREATE OR REPLACE FUNCTION person__save_contact
  106. (in_entity_id int, in_contact_class int, in_contact text)
  107. RETURNS INT AS
  108. $$
  109. DECLARE out_id int;
  110. BEGIN
  111. INSERT INTO person_to_contact(person_id, contact_class_id, contact)
  112. SELECT id, in_contact_class, in_contact FROM person
  113. WHERE entity_id = in_entity_id;
  114. RETURN 1;
  115. END;
  116. $$ LANGUAGE PLPGSQL;
  117. /*(
  118. unknown,
  119. unknown,
  120. unknown,
  121. unknown,
  122. unknown,
  123. unknown,
  124. unknown,
  125. unknown)
  126. */
  127. create or replace function person__save_location(
  128. in_entity_id int,
  129. in_location_id int,
  130. in_line_one text,
  131. in_line_two text,
  132. in_line_three text,
  133. in_city TEXT,
  134. in_state TEXT,
  135. in_mail_code text,
  136. in_country_code int
  137. ) returns int AS $$
  138. DECLARE
  139. l_row location;
  140. l_id INT;
  141. t_person_id int;
  142. BEGIN
  143. SELECT id INTO t_person_id
  144. FROM person WHERE entity_id = in_entity_id;
  145. -- why does it delete?
  146. select * into l_row FROM location
  147. WHERE id = in_location_id;
  148. IF NOT FOUND THEN
  149. -- Create a new one.
  150. l_id := location_save(
  151. in_location_id,
  152. in_line_one,
  153. in_line_two,
  154. in_line_three,
  155. in_city,
  156. in_state,
  157. in_mail_code,
  158. in_country_code);
  159. INSERT INTO person_to_location
  160. (person_id, location_id)
  161. VALUES (t_person_id, l_id);
  162. ELSE
  163. l_id := location_save(
  164. in_location_id,
  165. in_line_one,
  166. in_line_two,
  167. in_line_three,
  168. in_city,
  169. in_state,
  170. in_mail_code,
  171. in_country_code);
  172. -- Update the old one.
  173. END IF;
  174. return l_id;
  175. END;
  176. $$ language 'plpgsql';
  177. CREATE OR REPLACE FUNCTION person__delete_location (
  178. in_entity_id INT, in_location_id INT
  179. ) returns int AS $$
  180. DECLARE
  181. v_loc location;
  182. BEGIN
  183. select loc.* into v_loc FROM location loc
  184. JOIN person_to_location ptl ON loc.id = ptl.location_id
  185. JOIN person p ON p.id = ptl.person_id
  186. WHERE p.entity_id = in_entity_id
  187. AND loc.id = in_location_id;
  188. IF NOT FOUND THEN
  189. RAISE EXCEPTION "Cannot find records to delete for entity % and location %", in_entity_id, in_location_id;
  190. ELSE
  191. DELETE FROM people_to_location WHERE location_id = in_location_id;
  192. DELETE FROM location WHERE location_id = in_location_id;
  193. END IF;
  194. END;
  195. $$ language plpgsql;
  196. CREATE OR REPLACE FUNCTION person__all_locations (
  197. in_entity_id int
  198. ) returns setof location AS $$
  199. SELECT l.* FROM location l
  200. JOIN person_to_location ptl ON ptl.location_id = l.id
  201. JOIN person p on ptl.person_id = p.id
  202. WHERE p.id = $1;
  203. $$ language sql;
  204. commit;