summaryrefslogtreecommitdiff
path: root/sql/modules/Employee.sql
blob: a934a3e5136cff921f64f68cd1535cbee7c6f197 (plain)
  1. -- VERSION 1.3.0
  2. BEGIN;
  3. CREATE OR REPLACE FUNCTION employee_save
  4. (in_id integer, in_employeenumber varchar(32),
  5. in_salutation int, in_first_name varchar(64), in_last_name varchar(64),
  6. in_address1 varchar(32), in_address2 varchar(32),
  7. in_city varchar(32), in_state varchar(32), in_zipcode varchar(10),
  8. in_country int, in_workphone varchar(20),
  9. in_homephone varchar(20), in_startdate date, in_enddate date,
  10. in_notes text, in_role varchar(20), in_sales boolean, in_email text,
  11. in_ssn varchar(20), in_dob date, in_iban varchar(34),
  12. in_bic varchar(11), in_managerid integer)
  13. returns int AS $$
  14. DECLARE
  15. e_id int;
  16. e entity;
  17. loc location;
  18. l_id int;
  19. per person;
  20. p_id int;
  21. BEGIN
  22. select * into e from entity where id = in_id and entity_class = 3;
  23. if found then
  24. select l.* into loc from location l
  25. left join person_to_location ptl on ptl.location_id = l.id
  26. left join person p on p.id = ptl.person_id
  27. where p.entity_id = in_id;
  28. select * into per from person p where p.entity_id = in_id;
  29. update location
  30. set
  31. line_one = in_address1,
  32. line_two = in_address2,
  33. city_province = in_city,
  34. mail_code = in_zipcode,
  35. country_id = in_country
  36. where id = loc.id;
  37. UPDATE employee
  38. SET
  39. employeenumber = in_employeenumber,
  40. startdate = in_startdate,
  41. enddate = in_enddate,
  42. role = in_role,
  43. sales = in_sales,
  44. ssn = in_ssn,
  45. dob = in_dob,
  46. managerid = in_managerid
  47. WHERE entity_id = in_id;
  48. update entity_note
  49. set
  50. note = in_note
  51. where entity_id = in_id;
  52. UPDATE entity_bank_account
  53. SET
  54. bic = in_bic,
  55. iban = in_iban
  56. WHERE entity_id = in_id;
  57. UPDATE person
  58. SET
  59. salutation_id = in_salutation,
  60. first_name = in_first_name,
  61. last_name = in_last_name
  62. WHERE entity_id = in_id;
  63. UPDATE person_to_contact
  64. set
  65. contact = in_homephone
  66. WHERE person_id = per.id
  67. AND contact_class_id = 11;
  68. UPDATE person_to_contact
  69. set
  70. contact = in_workphone
  71. WHERE person_id = per.id
  72. AND contact_class_id = 1;
  73. UPDATE person_to_contact
  74. set
  75. contact = in_email
  76. WHERE person_id = per.id
  77. AND contact_class_id = 12;
  78. return in_id;
  79. ELSIF NOT FOUND THEN
  80. -- first, create a new entity
  81. -- Then, create an employee.
  82. e_id := in_id; -- expect nextval entity_id to have been called.
  83. INSERT INTO entity (id, entity_class, name) VALUES (e_id, 3, in_first_name||' '||in_last_name);
  84. INSERT INTO entity_bank_account (entity_id, iban, bic)
  85. VALUES (e_id, in_iban, in_bic);
  86. p_id := nextval('person_id_seq');
  87. insert into person (id, salutation_id, first_name, last_name, entity_id)
  88. VALUES
  89. (p_id, in_salutation, in_first_name, in_last_name, e_id);
  90. if in_notes is not null then
  91. insert into entity_note (note_class, note, ref_key, vector)
  92. values (1, in_notes, e_id, '');
  93. END IF;
  94. insert into person_to_contact (person_id, contact_class_id, contact)
  95. VALUES (p_id, 1, in_workphone); -- work phone #
  96. insert into person_to_contact (person_id, contact_class_id, contact)
  97. VALUES (p_id, 11, in_homephone); -- Home phone #
  98. insert into person_to_contact (person_id, contact_class_id, contact)
  99. VALUES (p_id, 12, in_email); -- email address.
  100. INSERT INTO employee
  101. (employeenumber, startdate, enddate,
  102. role, sales, ssn,
  103. dob, managerid, entity_id, entity_class_id)
  104. VALUES
  105. (in_employeenumber, in_startdate, in_enddate,
  106. in_role, in_sales, in_ssn,
  107. in_dob, in_managerid, e_id, 3);
  108. l_id := nextval('location_id_seq');
  109. insert into location (id, location_class, line_one, line_two, city_province, country_id, mail_code)
  110. VALUES (
  111. l_id,
  112. 1,
  113. in_address1,
  114. in_address2,
  115. in_city,
  116. in_country,
  117. in_zipcode
  118. );
  119. insert into person_to_location (person_id, location_id)
  120. VALUES (p_id, l_id);
  121. return e_id;
  122. END IF;
  123. END;
  124. $$ LANGUAGE 'plpgsql';
  125. -- why is this like this?
  126. CREATE OR REPLACE FUNCTION employee_get
  127. (in_id integer)
  128. returns employee as
  129. $$
  130. DECLARE
  131. emp employee%ROWTYPE;
  132. BEGIN
  133. SELECT * INTO emp FROM employees WHERE id = in_id;
  134. RETURN emp;
  135. END;
  136. $$ language plpgsql;
  137. CREATE OR REPLACE FUNCTION employee_list_managers
  138. (in_id integer)
  139. RETURNS SETOF employee as
  140. $$
  141. DECLARE
  142. emp employee%ROWTYPE;
  143. BEGIN
  144. FOR emp IN
  145. SELECT * FROM employee
  146. WHERE sales = '1' AND role='manager'
  147. AND entity_id <> coalesce(in_id, -1)
  148. ORDER BY name
  149. LOOP
  150. RETURN NEXT emp;
  151. END LOOP;
  152. END;
  153. $$ language plpgsql;
  154. CREATE OR REPLACE FUNCTION employee_delete
  155. (in_id integer) returns void as
  156. $$
  157. BEGIN
  158. DELETE FROM employee WHERE entity_id = in_id;
  159. RETURN;
  160. END;
  161. $$ language plpgsql;
  162. -- as long as we need the datatype, might as well get some other use out of it!
  163. --
  164. -- % type is pg_trgm comparison.
  165. CREATE INDEX name_idx ON employee USING gist(name gist_trgm_ops);
  166. CREATE INDEX notes_idx ON entity_note USING gist(note gist_trgm_ops);
  167. CREATE OR REPLACE VIEW employee_search AS
  168. SELECT e.*, em.name AS manager, emn.note, en.name as name
  169. FROM employee e
  170. LEFT JOIN entity en on (e.entity_id = en.id)
  171. LEFT JOIN employee m ON (e.managerid = m.entity_id)
  172. LEFT JOIN entity em on (em.id = m.entity_id)
  173. LEFT JOIN entity_note emn on (emn.ref_key = em.id);
  174. CREATE OR REPLACE FUNCTION employee_search
  175. (in_startdatefrom date, in_startdateto date, in_name varchar, in_notes text,
  176. in_enddateto date, in_enddatefrom date, in_sales boolean)
  177. RETURNS SETOF employee_search AS
  178. $$
  179. DECLARE
  180. emp employee_search%ROWTYPE;
  181. BEGIN
  182. FOR emp IN
  183. SELECT * FROM employee_search
  184. WHERE coalesce(startdate, 'infinity'::timestamp)
  185. >= coalesce(in_startdateto, '-infinity'::timestamp)
  186. AND coalesce(startdate, '-infinity'::timestamp) <=
  187. coalesce(in_startdatefrom,
  188. 'infinity'::timestamp)
  189. AND coalesce(enddate, '-infinity'::timestamp) <=
  190. coalesce(in_enddateto, 'infinity'::timestamp)
  191. AND coalesce(enddate, 'infinity'::timestamp) >=
  192. coalesce(in_enddatefrom, '-infinity'::timestamp)
  193. AND (name % in_name
  194. OR note % in_notes)
  195. AND (sales = 't' OR coalesce(in_sales, 'f') = 'f')
  196. LOOP
  197. RETURN NEXT emp;
  198. END LOOP;
  199. return;
  200. END;
  201. $$ language plpgsql;
  202. COMMIT;