Guest User

Untitled

a guest
Jun 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE PACKAGE BODY SHIFT_PACKAGE AS
  2.  
  3. PROCEDURE update_staff_shift
  4.     (p_first_name IN VARCHAR2, p_last_name IN VARCHAR2, p_code IN NUMBER)
  5. IS
  6.     employee_id F_STAFFS.id%TYPE;
  7.     shift_code F_SHIFTS.code%TYPE;
  8.     no_assigned_shift EXCEPTION;
  9.     PRAGMA EXCEPTION_INIT (no_assigned_shift, -20001);
  10. BEGIN
  11.     SELECT id INTO employee_id FROM f_staffs WHERE first_name = p_first_name AND last_name = p_last_name;
  12.     SELECT code INTO shift_code FROM f_shifts WHERE code = p_code;
  13.     UPDATE f_shift_assignments SET code = p_code, SHIFT_ASSIGN_DATE = SYSDATE WHERE id = employee_id;
  14.     IF sql%rowcount = 0 THEN
  15.        RAISE no_assigned_shift;
  16.     END IF;
  17. EXCEPTION
  18.     WHEN no_assigned_shift THEN
  19.         log_error(SQLCODE, 'Cannot update shift. No assigned shift found.');
  20.         dbms_output.put_line('Employee has no assigned shift. You must first assign employee to shift
  21.            by calling procedure assign_staff_to_shift(first_name, last_name, description) before you can
  22.            change it.');
  23.     WHEN no_data_found THEN
  24.         IF employee_id IS NULL THEN
  25.             log_error(SQLCODE, 'Employee with given name does not exist.');
  26.             dbms_output.put_line('Employee with given name does not exist.');
  27.         ELSE
  28.             log_error(SQLCODE, 'Shift with given code does not exist.');
  29.             dbms_output.put_line('Shift with given code does not exist.');
  30.         END IF;
  31.     WHEN OTHERS THEN
  32.         dbms_output.put_line('Shift could not be changed.');
  33. END;        
  34.  
  35.  
  36. PROCEDURE add_new_shift (
  37.     p_description IN VARCHAR2 ) IS
  38. shifts_count IN NUMBER;
  39. new_code IN NUMBER;  
  40. BEGIN
  41.     SELECT COUNT(*) INTO shifts_count FROM f_shifts;
  42.     IF shifts_count > 0 THEN
  43.         SELECT MAX(code) INTO new_code FROM f_shifts;
  44.         new_code := new_code + 1;
  45.     ELSE
  46.         new_code := 1;
  47.     END IF;
  48.     INSERT INTO f_shifts (code, description) VALUES (new_code, p_description);
  49.    
  50. EXCEPTION
  51.     WHEN OTHERS THEN
  52.         log_error(SQLCODE, 'Shift could not be added. ' || SQLERRM);
  53.         dbms_output.put_line('Shift could not be added.');      
  54. END;
  55.  
  56. PROCEDURE assign_staff_to_shift (
  57.     p_first_name IN VARCHAR2,
  58.     p_last_name IN VARCHAR2,
  59.     p_description IN VARCHAR2)
  60. IS
  61.     employee_id F_STAFFS.id%TYPE;
  62.     shift_code F_SHIFTS.code%TYPE;
  63.     assigned_shifts NUMBER;
  64.     already_assigned EXCEPTION;
  65.     PRAGMA exception_init (already_assigned, -20002);
  66. BEGIN
  67.     SELECT id INTO employee_id FROM f_staffs WHERE first_name = p_first_name AND last_name = p_last_name;
  68.     SELECT code INTO shift_code FROM f_shifts WHERE description = p_description;
  69.     SELECT COUNT(*) INTO assigned_shifts FROM f_shift_assignments WHERE id = employee_id;
  70.     IF assigned_shifts > 0 THEN
  71.         RAISE already_assigned;
  72.     ELSE
  73.         INSERT INTO f_shift_assignments (id,code,shift_assign_date) VALUES(employee_id,shift_code,SYSDATE);
  74.     END IF;
  75. EXCEPTION
  76.     WHEN already_assigned THEN
  77.         log_error(SQLCODE, 'Employee with given name has already been assigned to shift.');
  78.         dbms_output.put_line('Employee with given name has already been assigned to shift.');
  79.         dbms_output.put_line('You can now only change shift using function update_staff_shift(first_name, last_name, code).');
  80.     WHEN no_data_found THEN
  81.         IF employee_id IS NULL THEN
  82.             log_error(SQLCODE, 'Employee with given name does not exist.');
  83.             dbms_output.put_line('Employee with given name does not exist.');
  84.         ELSE
  85.             log_error(SQLCODE, 'Shift with given description does not exist.');
  86.             dbms_output.put_line('Shift with given description does not exist.');
  87.         END IF;
  88.     WHEN OTHERS THEN
  89.         log_error(SQLCODE, 'Shift could not be assigned to employee.' || SQLERRM);
  90.         dbms_output.put_line('Shift could not be assigned to employee.');
  91. END ;
  92.  
  93. FUNCTION count_shift (
  94.     p_code IN NUMBER )
  95.     RETURN NUMBER
  96. IS
  97.     shift_count NUMBER;  
  98. BEGIN
  99.      SELECT COUNT(*) INTO shift_count FROM staff_shift_changes WHERE new_code = p_code;  
  100.      RETURN shift_count;
  101. END;
  102.  
  103. FUNCTION count_shift (
  104.     p_code IN NUMBER,
  105.     p_id IN NUMBER,
  106.     p_start_date IN DATE,
  107.     p_end_date IN DATE
  108.     )
  109.     RETURN NUMBER
  110. IS
  111. shift_count NUMBER;  
  112. BEGIN
  113.      SELECT COUNT(*) INTO shift_count FROM staff_shift_changes WHERE id = p_id AND time_stamp >= p_start_date
  114.         AND time_stamp <= p_end_date AND new_code = p_code;  
  115.      RETURN shift_count;
  116. END;
  117.  
  118. PROCEDURE find_shifts (
  119.     p_id IN NUMBER,
  120.     p_start_date IN DATE := NULL,
  121.     p_end_date IN DATE := NULL)
  122. IS
  123.     employee f_staffs%ROWTYPE;
  124.     first_day DATE;
  125.     LAST_DAY DATE;
  126.     CURSOR shifts IS SELECT DISTINCT new_code FROM staff_shift_changes WHERE id = p_id;
  127.     shift_desc f_shifts.description%TYPE;
  128. BEGIN
  129.     SELECT * INTO employee FROM f_staffs WHERE id = p_id;
  130.     IF p_start_date IS NOT NULL AND p_end_date IS NOT NULL THEN
  131.          dbms_output.put_line('Employee ' || employee.first_name || ' ' || employee.last_name || ' worked on these shifts during ' || p_start_date || ' - ' || p_end_date || ': ');
  132.         dbms_output.put_line('');
  133.        
  134.         FOR shift IN shifts LOOP
  135.             SELECT description INTO shift_desc FROM f_shifts WHERE code = shift.new_code;
  136.            
  137.             dbms_output.put_line('Shift: ' || shift_desc || ' - ' || count_shift(shift.new_code, employee.id, '1.1.1900', p_end_date) || ' time(s).');
  138.  
  139.             END LOOP;
  140.        
  141.     ELSIF p_end_date IS NOT NULL THEN
  142.         dbms_output.put_line('Employee ' || employee.first_name || ' ' || employee.last_name || ' worked on these shifts till ' || p_end_date || ': ');
  143.         dbms_output.put_line('');
  144.        
  145.         FOR shift IN shifts LOOP
  146.             SELECT description INTO shift_desc FROM f_shifts WHERE code = shift.new_code;
  147.            
  148.             dbms_output.put_line('Shift: ' || shift_desc || ' - ' || count_shift(shift.new_code, employee.id, '1.1.1900', p_end_date) || ' time(s).');
  149.  
  150.             END LOOP;
  151.     ELSIF p_start_date IS NOT NULL THEN
  152.         dbms_output.put_line('Employee ' || employee.first_name || ' ' || employee.last_name || ' has worked on these shifts since '
  153.             || p_start_date || ': ');
  154.         dbms_output.put_line('');
  155.        
  156.         FOR shift IN shifts LOOP
  157.             SELECT description INTO shift_desc FROM f_shifts WHERE code = shift.new_code;
  158.            
  159.             dbms_output.put_line('Shift: ' || shift_desc || ' - ' || count_shift(shift.new_code, employee.id, p_start_date, SYSDATE) || ' time(s).');
  160.  
  161.             END LOOP;
  162.     ELSE
  163.         SELECT TRUNC(SYSDATE,'Year') INTO first_day FROM dual;
  164.         SELECT ADD_MONTHS(TRUNC(SYSDATE,'Year'),12)-1 INTO LAST_DAY FROM dual;
  165.         dbms_output.put_line('Employee ' || employee.first_name || ' ' || employee.last_name || ' worked on these shifts this year: ');
  166.         dbms_output.put_line('');
  167.        
  168.         FOR shift IN shifts LOOP
  169.             SELECT description INTO shift_desc FROM f_shifts WHERE code = shift.new_code;
  170.            
  171.             dbms_output.put_line('Shift: ' || shift_desc || ' - ' || count_shift(shift.new_code, employee.id, first_day, LAST_DAY) || ' time(s).');
  172.  
  173.             END LOOP;
  174.     END IF;
  175.    
  176.    
  177. EXCEPTION
  178.     WHEN no_data_found THEN
  179.         log_error(SQLCODE, 'Employee with given name does not exist.');
  180.         dbms_output.put_line('Employee with given name does not exist.');
  181.          
  182. END;
  183.  
  184. PROCEDURE log_error (
  185.     p_code IN NUMBER,
  186.     p_message IN VARCHAR2
  187.     ) IS
  188. BEGIN
  189.     INSERT INTO error_log (e_user, e_date, error_code, error_message) VALUES (SYS_CONTEXT('USERENV', 'CURRENT_USER'), SYSDATE, p_code, p_message);
  190. END;
  191.  
  192.  
  193. END;
Add Comment
Please, Sign In to add comment