summaryrefslogtreecommitdiff
path: root/sql/modules/Employee.sql
blob: c407e244246dfa9a36d45def0409b00deca328a2 (plain)
  1. -- VERSION 1.3.0
  2. CREATE OR REPLACE FUNCTION employee_save
  3. (in_id integer, in_location_id integer, in_employeenumber varchar(32),
  4. in_name varchar(64), in_address1 varchar(32), in_address2 varchar(32),
  5. in_city varchar(32), in_state varchar(32), in_zipcode varchar(10),
  6. in_country varchar(32), in_workphone varchar(20),
  7. in_homephone varchar(20), in_startdate date, in_enddate date,
  8. in_notes text, in_role varchar(20), in_sales boolean, in_email text,
  9. in_ssn varchar(20), in_dob date, in_iban varchar(34),
  10. in_bic varchar(11), in_managerid integer) returns int
  11. AS
  12. $$
  13. BEGIN
  14. UPDATE employees
  15. SET location_id = in_location_id,
  16. employeenumber = in_employeenumber,
  17. name = in_name,
  18. address1 = in_address1,
  19. address2 = in_address2,
  20. city = in_city,
  21. state = in_state,
  22. zipcode = in_zipcode,
  23. country = in_country,
  24. workphone = in_workphone,
  25. homephone = in_homephone,
  26. startdate = in_startdate,
  27. enddate = in_enddate,
  28. notes = in_notes,
  29. role = in_role,
  30. sales = in_sales,
  31. email = in_email,
  32. ssn = in_ssn,
  33. dob=in_dob,
  34. iban = in_iban,
  35. bic = in_bic,
  36. manager_id = in_managerid
  37. WHERE id = in_id;
  38. IF FOUND THEN
  39. return in_id;
  40. END IF;
  41. INSERT INTO employees
  42. (location_id, employeenumber, name, address1, address2,
  43. city, state, zipcode, country, workphone, homephone,
  44. startdate, enddate, notes, role, sales, email, ssn,
  45. dob, iban, bic, managerid)
  46. VALUES
  47. (in_location_id, in_employeenumber, in_name, in_address1,
  48. in_address2, in_city, in_state, in_zipcode, in_country,
  49. in_workphone, in_homephone, in_startdate, in_enddate,
  50. in_notes, in_role, in_sales, in_email, in_ssn, in_dob,
  51. in_iban, in_bic, in_managerid);
  52. SELECT currval('employee_id_seq') INTO employee_id;
  53. return employee_id;
  54. END;
  55. $$ LANGUAGE PLPGSQL;
  56. CREATE OR REPLACE FUNCTION employee_get
  57. (in_id integer)
  58. returns employees as
  59. $$
  60. DECLARE
  61. emp employees%ROWTYPE;
  62. BEGIN
  63. SELECT * INTO emp FROM employees WHERE id = in_id;
  64. RETURN emp;
  65. END;
  66. $$ language plpgsql;
  67. CREATE OR REPLACE FUNCTION employee_list_managers
  68. (in_id integer)
  69. RETURNS SETOF employees as
  70. $$
  71. DECLARE
  72. emp employees%ROWTYPE;
  73. BEGIN
  74. FOR emp IN
  75. SELECT * FROM employees
  76. WHERE sales = '1' AND role='manager'
  77. AND id <> coalesce(in_id, -1)
  78. ORDER BY name
  79. LOOP
  80. RETURN NEXT emp;
  81. END LOOP;
  82. END;
  83. $$ language plpgsql;
  84. CREATE OR REPLACE FUNCTION employee_delete
  85. (in_id integer) returns void as
  86. $$
  87. BEGIN
  88. DELETE FROM employees WHERE id = in_id;
  89. RETURN;
  90. END;
  91. $$ language plpgsql;
  92. -- as long as we need the datatype, might as well get some other use out of it!
  93. CREATE OR REPLACE VIEW employee_search AS
  94. SELECT e.*, m.name AS manager
  95. FROM employees e LEFT JOIN employees m ON (e.managerid = m.id);
  96. CREATE OR REPLACE FUNCTION employee_search
  97. (in_startdatefrom date, in_startdateto date, in_name varchar, in_notes text,
  98. in_enddateto date, in_enddatefrom date, in_sales boolean)
  99. RETURNS SETOF employee_search AS
  100. $$
  101. DECLARE
  102. emp employee_search%ROWTYPE;
  103. BEGIN
  104. FOR emp IN
  105. SELECT * FROM employee_search
  106. WHERE coalesce(startdate, 'infinity'::timestamp)
  107. >= coalesce(in_startdateto, '-infinity'::timestamp)
  108. AND coalesce(startdate, '-infinity'::timestamp) <=
  109. coalesce(in_startdatefrom,
  110. 'infinity'::timestamp)
  111. AND coalesce(enddate, '-infinity'::timestamp) <=
  112. coalesce(in_enddateto, 'infinity'::timestamp)
  113. AND coalesce(enddate, 'infinity'::timestamp) >=
  114. coalesce(in_enddatefrom, '-infinity'::timestamp)
  115. AND lower(name) LIKE '%' || lower(in_name) || '%'
  116. AND lower(notes) LIKE '%' || lower(in_notes) || '%'
  117. AND (sales = 't' OR coalesce(in_sales, 'f') = 'f')
  118. LOOP
  119. RETURN NEXT emp;
  120. END LOOP;
  121. END;
  122. $$ language plpgsql;