summaryrefslogtreecommitdiff
path: root/sql/modules/Employee.sql
blob: bd3445f635a670667103474ce317664a41bc3ce0 (plain)
  1. -- VERSION 1.3.0
  2. BEGIN;
  3. CREATE OR REPLACE FUNCTION employee_save(
  4. in_person int, in_entity int, in_startdate date, in_enddate date,
  5. in_role text, in_sales boolean, in_dob date,
  6. in_managerid integer, in_employeenumber text
  7. )
  8. returns int AS $$
  9. DECLARE
  10. e_ent entity_employee;
  11. e entity;
  12. p person;
  13. BEGIN
  14. select * into e from entity where id = in_entity and entity_class = 3;
  15. IF NOT FOUND THEN
  16. RAISE EXCEPTION 'No entity found for ID %', in_id;
  17. END IF;
  18. select * into p from person where id = in_person;
  19. IF NOT FOUND THEN
  20. RAISE EXCEPTION 'No person found for ID %', in_id;
  21. END IF;
  22. -- Okay, we're good. Check to see if we update or insert.
  23. select * into e_ent from entity_employee where person_id = in_person
  24. and entity_id = in_entity;
  25. IF NOT FOUND THEN
  26. -- insert.
  27. INSERT INTO entity_employee (person_id, entity_id, startdate,
  28. enddate, role, sales, manager_id, employeenumber, dob)
  29. VALUES (in_person, in_entity, in_startdate, in_enddate, in_role,
  30. in_sales, in_managerid, in_employeenumber, in_dob);
  31. return in_entity;
  32. ELSE
  33. -- update
  34. UPDATE entity_employee
  35. SET
  36. startdate = in_startdate,
  37. enddate = in_enddate,
  38. role = in_role,
  39. sales = in_sales,
  40. manager_id = in_managerid,
  41. employeenumber = in_employeenumber,
  42. dob = in_dob
  43. WHERE
  44. entity_id = in_entity
  45. AND
  46. person_id = in_person;
  47. return in_entity;
  48. END IF;
  49. END;
  50. $$ language 'plpgsql';
  51. create view employees as
  52. select
  53. s.salutation,
  54. p.first_name,
  55. p.last_name,
  56. ee.*
  57. FROM person p
  58. JOIN entity_employee ee USING (entity_id)
  59. JOIN salutation s ON (p.salutation_id = s.id);
  60. CREATE OR REPLACE FUNCTION employee_get
  61. (in_id integer)
  62. returns employees as
  63. $$
  64. DECLARE
  65. emp employees%ROWTYPE;
  66. BEGIN
  67. SELECT
  68. s.salutation,
  69. p.first_name,
  70. p.last_name,
  71. ee.*
  72. INTO emp
  73. FROM employees ee
  74. join person p USING (entity_id)
  75. JOIN salutation s ON (p.salutation_id = s.id)
  76. WHERE ee.entity_id = in_id;
  77. RETURN emp;
  78. END;
  79. $$ language plpgsql;
  80. CREATE OR REPLACE FUNCTION employee_list_managers
  81. (in_id integer)
  82. RETURNS SETOF employees as
  83. $$
  84. DECLARE
  85. emp employees%ROWTYPE;
  86. BEGIN
  87. FOR emp IN
  88. SELECT
  89. e.salutation,
  90. e.first_name,
  91. e.last_name,
  92. ee.*
  93. FROM entity_employee ee
  94. JOIN entity e on e.id = ee.entity_id
  95. WHERE ee.sales = 't'::bool AND ee.role='manager'
  96. AND ee.entity_id <> coalesce(in_id, -1)
  97. ORDER BY name
  98. LOOP
  99. RETURN NEXT emp;
  100. END LOOP;
  101. END;
  102. $$ language plpgsql;
  103. CREATE OR REPLACE FUNCTION employee_delete
  104. (in_id integer) returns void as
  105. $$
  106. BEGIN
  107. DELETE FROM employee WHERE entity_id = in_id;
  108. RETURN;
  109. END;
  110. $$ language plpgsql;
  111. -- as long as we need the datatype, might as well get some other use out of it!
  112. --
  113. -- % type is pg_trgm comparison.
  114. CREATE INDEX notes_idx ON entity_note USING gist(note gist_trgm_ops);
  115. CREATE OR REPLACE VIEW employee_search AS
  116. SELECT e.*, em.name AS manager, emn.note, en.name as name
  117. FROM entity_employee e
  118. LEFT JOIN entity en on (e.entity_id = en.id)
  119. LEFT JOIN entity_employee m ON (e.manager_id = m.entity_id)
  120. LEFT JOIN entity em on (em.id = m.entity_id)
  121. LEFT JOIN entity_note emn on (emn.ref_key = em.id);
  122. CREATE OR REPLACE FUNCTION employee_search
  123. (in_startdatefrom date, in_startdateto date, in_name varchar, in_notes text,
  124. in_enddateto date, in_enddatefrom date, in_sales boolean)
  125. RETURNS SETOF employee_search AS
  126. $$
  127. DECLARE
  128. emp employee_search%ROWTYPE;
  129. BEGIN
  130. FOR emp IN
  131. SELECT * FROM employee_search
  132. WHERE coalesce(startdate, 'infinity'::timestamp)
  133. >= coalesce(in_startdateto, '-infinity'::timestamp)
  134. AND coalesce(startdate, '-infinity'::timestamp) <=
  135. coalesce(in_startdatefrom,
  136. 'infinity'::timestamp)
  137. AND coalesce(enddate, '-infinity'::timestamp) <=
  138. coalesce(in_enddateto, 'infinity'::timestamp)
  139. AND coalesce(enddate, 'infinity'::timestamp) >=
  140. coalesce(in_enddatefrom, '-infinity'::timestamp)
  141. AND (name % in_name
  142. OR note % in_notes)
  143. AND (sales = 't' OR coalesce(in_sales, 'f') = 'f')
  144. LOOP
  145. RETURN NEXT emp;
  146. END LOOP;
  147. return;
  148. END;
  149. $$ language plpgsql;
  150. create or replace function employee_set_location
  151. (in_employee int, in_location int)
  152. returns void as $$
  153. INSERT INTO person_to_location (person_id,location_id)
  154. VALUES ($1, $2);
  155. $$ language 'sql';
  156. COMMIT;