summaryrefslogtreecommitdiff
path: root/sql/upgrade/1.2-1.3.sql
blob: 38d5b6baf2f0b1b8abf54ce042def345d5e1fbb3 (plain)
  1. BEGIN;
  2. CREATE TABLE location (
  3. id SERIAL PRIMARY KEY,
  4. companyname text,
  5. address1 text,
  6. address2 text,
  7. city text,
  8. state text,
  9. country text,
  10. zipcode text
  11. );
  12. CREATE SEQUENCE employee_id_seq;
  13. SELECT setval('employee_id_seq', (select max(id) + 1 FROM employee));
  14. ALTER TABLE employee ADD COLUMN locations_id integer;
  15. ALTER TABLE employee ADD FOREIGN KEY (locations_id) REFERENCES location(id);
  16. ALTER TABLE employee ALTER COLUMN id DROP DEFAULT;
  17. ALTER TABLE employee ALTER COLUMN id SET DEFAULT nextval('employee_id_seq');
  18. CREATE OR REPLACE FUNCTION location_save
  19. (in_id int, in_companyname text, in_address1 text, in_address2 text,
  20. in_city text, in_state text, in_zipcode text, in_country text)
  21. returns integer AS
  22. $$
  23. DECLARE
  24. location_id integer;
  25. BEGIN
  26. UPDATE location
  27. SET companyname = in_companyname,
  28. address1 = in_address1,
  29. address2 = in_address2,
  30. city = in_city,
  31. state = in_state,
  32. zipcode = in_zipcode,
  33. country = in_country
  34. WHERE id = in_id;
  35. IF FOUND THEN
  36. return in_id;
  37. END IF;
  38. INSERT INTO location
  39. (companyname, address1, address2, city, state, zipcode, country)
  40. VALUES
  41. (in_companyname, in_address1, in_address2, in_city, in_state,
  42. in_zipcode, in_country);
  43. SELECT lastval('location_id_seq') INTO location_id;
  44. return location_id;
  45. END;
  46. $$ LANGUAGE PLPGSQL;
  47. create or replace function employee_save
  48. (in_id integer, in_location_id integer, in_employeenumber varchar(32),
  49. in_name varchar(64), in_address1 varchar(32), in_address2 varchar(32),
  50. in_city varchar(32), in_state varchar(32), in_zipcode varchar(10),
  51. in_country varchar(32), in_workphone varchar(20),
  52. in_homephone varchar(20), in_startdate date, in_enddate date,
  53. in_notes text, in_role varchar(20), in_sales boolean, in_email text,
  54. in_ssn varchar(20), in_dob date, in_iban varchar(34),
  55. in_bic varchar(11), in_managerid integer) returns int
  56. AS
  57. $$
  58. BEGIN
  59. UPDATE employee
  60. SET location_id = in_location_id,
  61. employeenumber = in_employeenumber,
  62. name = in_name,
  63. address1 = in_address1,
  64. address2 = in_address2,
  65. city = in_city,
  66. state = in_state,
  67. zipcode = in_zipcode,
  68. country = in_country,
  69. workphone = in_workphone,
  70. homephone = in_homephone,
  71. startdate = in_startdate,
  72. enddate = in_enddate,
  73. notes = in_notes,
  74. role = in_role,
  75. sales = in_sales,
  76. email = in_email,
  77. ssn = in_ssn,
  78. dob=in_dob,
  79. iban = in_iban,
  80. bic = in_bic,
  81. manager_id = in_managerid
  82. WHERE id = in_id;
  83. IF FOUND THEN
  84. return in_id;
  85. END IF;
  86. INSERT INTO employee
  87. (location_id, employeenumber, name, address1, address2,
  88. city, state, zipcode, country, workphone, homephone,
  89. startdate, enddate, notes, role, sales, email, ssn,
  90. dob, iban, bic, managerid)
  91. VALUES
  92. (in_location_id, in_employeenumber, in_name, in_address1,
  93. in_address2, in_city, in_state, in_zipcode, in_country,
  94. in_workphone, in_homephone, in_startdate, in_enddate,
  95. in_notes, in_role, in_sales, in_email, in_ssn, in_dob,
  96. in_iban, in_bic, in_managerid);
  97. SELECT currval('employee_id_seq') INTO employee_id;
  98. return employee_id;
  99. END;
  100. $$ LANGUAGE PLPGSQL;
  101. CREATE OR REPLACE FUNCTION employee_get
  102. (in_id integer)
  103. returns employee as
  104. $$
  105. DECLARE
  106. emp employee%ROWTYPE;
  107. BEGIN
  108. SELECT * INTO emp FROM employee WHERE id = in_id;
  109. RETURN emp;
  110. END;
  111. $$ language plpgsql;
  112. CREATE OR REPLACE FUNCTION employee_list_managers
  113. (in_id integer)
  114. RETURNS SETOF employee as
  115. $$
  116. DECLARE
  117. emp employee%ROWTYPE;
  118. BEGIN
  119. FOR emp IN
  120. SELECT * FROM employee
  121. WHERE sales = '1' AND role='manager'
  122. AND id <> coalesce(in_id, -1)
  123. ORDER BY name
  124. LOOP
  125. RETURN NEXT emp;
  126. END LOOP;
  127. END;
  128. $$ language plpgsql;
  129. CREATE OR REPLACE FUNCTION employee_delete
  130. (in_id integer) returns void as
  131. $$
  132. BEGIN
  133. DELETE FROM employee WHERE id = in_id;
  134. RETURN;
  135. END;
  136. $$ language plpgsql;
  137. -- as long as we need the datatype, might as well get some other use out of it!
  138. CREATE OR REPLACE VIEW employee_search AS
  139. SELECT e.*, m.name AS manager
  140. FROM employee e JOIN employee m ON (e.managerid = m.id);
  141. CREATE OR REPLACE FUNCTION employee_search
  142. (in_startdatefrom date, in_startdateto date, in_name varchar, in_notes text,
  143. in_enddateto date, in_enddatefrom date)
  144. RETURNS SETOF employee_search AS
  145. $$
  146. DECLARE
  147. emp employee_search%ROWTYPE;
  148. BEGIN
  149. FOR emp IN
  150. SELECT * FROM employee_search
  151. WHERE coalesce(startdate, 'infinity'::timestamp)
  152. >= coalesce(in_startdateto, '-infinity'::timestamp)
  153. AND coalesce(startdate, '-infinity'::timestamp) <=
  154. coalesce(in_startdatefrom,
  155. 'infinity'::timestamp)
  156. AND coalesce(enddate, '-infinity'::timestamp) <=
  157. coalesce(in_enddateto, 'infinity'::timestamp)
  158. AND coalesce(enddate, 'infinity'::timestamp) >=
  159. coalesce(in_enddatefrom, '-infinity'::timestamp)
  160. AND lower(name) LIKE '%' || lower(in_name) || '%'
  161. AND lower(notes) LIKE '%' || lower(in_notes) || '%'
  162. AND (sales = 't' OR coalesce(in_sales, 'f') = 'f')
  163. LOOP
  164. RETURN NEXT emp;
  165. END LOOP;
  166. END;
  167. $$ language plpgsql;
  168. COMMIT;