Repository navigation
Expand file tree
/
Copy pathfinal_implementation.sql
More file actions
2222 lines (1863 loc) · 63.2 KB
/
Copy pathfinal_implementation.sql
File metadata and controls
2222 lines (1863 loc) · 63.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
create DATABASE University_HR_ManagementSystem_128;
GO
USE University_HR_ManagementSystem_128;
GO
CREATE FUNCTION HRSalary_calculation
(@employee_ID int)
Returns decimal(10,2)
AS
Begin
Declare @salary decimal(10,2)
Declare @base_salary decimal(10,2)
Declare @percentage_YOE decimal(10,2)
Declare @YOE int
SELECT @base_salary = base_salary, @percentage_YOE = percentage_YOE ,@YOE= E.years_of_experience
FROM Employee_Role ER
INNER JOIN Role R
ON ER.role_name = R.role_name
INNER JOIN Employee E
ON E.employee_ID = ER.emp_ID
WHERE employee_ID = @employee_ID
Order by R.rank ASC
SET @salary = @base_salary + (@percentage_YOE/100) * @YOE * @base_salary
Return @salary
END
GO
CREATE PROC createAllTables
AS
CREATE TABLE Department(
name varchar(50) primary key,
building_location varchar(50)
);
CREATE TABLE Employee(
employee_id int primary key identity,
first_name varchar(50),
last_name varchar(50),
email varchar(50),
password varchar(50),
address varchar(50),
gender CHAR(1),
official_day_off varchar(50),
years_of_experience int,
national_ID char(16),
employment_status varchar(50) CHECK(employment_status IN ('active', 'onleave', 'notice_period','resigned')),
type_of_contract varchar(50),CHECK(type_of_contract IN ('full_time','part_time')),
emergency_contact_name varchar(50),
emergency_contact_phone char(11),
annual_balance int,
accidental_balance int,
salary AS dbo.HRSalary_calculation(employee_id),
hire_date date,
last_working_date date,
dept_name varchar(50) foreign key references Department
);
CREATE TABLE Employee_Phone(
emp_id int foreign key references Employee,
phone_num char(11),
primary key(emp_id, phone_num)
);
CREATE TABLE Role(
role_name varchar (50) PRIMARY KEY,
title varchar (50),
description varchar (50),
rank int,
base_salary decimal (10,2),
percentage_YOE decimal (4,2),
percentage_overtime decimal (4,2),
annual_balance int,
accidental_balance int
);
CREATE TABLE Employee_Role(
emp_ID int foreign key references Employee,
role_name varchar(50) foreign key references Role,
primary key(emp_ID, role_name)
);
CREATE TABLE Role_existsIn_Department(
department_name varchar(50) FOREIGN KEY references Department,
Role_name varchar(50) FOREIGN KEY references Role,
PRIMARY KEY(department_name, role_name)
);
CREATE TABLE Leave (
request_ID int PRIMARY KEY IDENTITY,
date_of_request date,
start_date date,
end_date date,
num_days AS DATEDIFF(day, start_date, end_date)+1,
final_approval_status varchar(50) CHECK(final_approval_status IN('Approved', 'Pending', 'Rejected')) DEFAULT('Pending')
);
CREATE TABLE Annual_Leave (
request_ID int PRIMARY KEY FOREIGN KEY REFERENCES LEAVE,
emp_ID int FOREIGN KEY REFERENCES Employee,
replacement_emp int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Accidental_Leave (
request_ID int PRIMARY KEY FOREIGN KEY REFERENCES LEAVE,
emp_ID int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Medical_Leave(
request_ID int PRIMARY KEY FOREIGN KEY REFERENCES LEAVE,
insurance_status BIT,
disability_details varchar(50),
type varchar(50) CHECK(type IN ('sick','maternity')),
Emp_ID int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Unpaid_Leave(
request_ID int PRIMARY KEY FOREIGN KEY REFERENCES LEAVE,
Emp_ID int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Compensation_Leave(
request_ID int PRIMARY KEY FOREIGN KEY REFERENCES LEAVE,
reason varchar (50),
date_of_original_workday date,
emp_ID int FOREIGN KEY REFERENCES Employee,
replacement_emp int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Document (
document_id int primary key IDENTITY,
type varchar (50),
description varchar (50),
file_name varchar (50),
creation_date date,
expiry_date date,
status varchar (50) CHECK(status in ('valid', 'expired')),
emp_ID int FOREIGN KEY REFERENCES Employee,
medical_ID int FOREIGN KEY REFERENCES MEDICAL_LEAVE,
unpaid_ID INT FOREIGN KEY REFERENCES UNPAID_LEAVE
);
CREATE TABLE Payroll (
ID int PRIMARY KEY IDENTITY,
payment_date date,
final_salary_amount decimal (10,1),
from_date date,
to_date date,
comments varchar(150),
bonus_amount decimal(10,2),
deductions_amount decimal(10,2),
emp_ID int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Attendance (
attendance_ID int PRIMARY KEY IDENTITY,
date date,
check_in_time time,
check_out_time time,
total_duration as DATEDIFF(Minute, check_in_time,check_out_time),
status varchar(50) CHECK(status IN ('Absent', 'Attended')) DEFAULT('Absent'),
emp_ID int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Deduction (
deduction_ID int IDENTITY,
emp_ID INT FOREIGN KEY REFERENCES Employee,
date date,
amount decimal (10,2),
type varchar (50) CHECK(type IN ('unpaid', 'missing_hours', 'missing_days')),
status varchar (50) CHECK(status IN ('pending', 'finalized')) DEFAULT('pending'),
unpaid_ID int FOREIGN KEY REFERENCES Unpaid_leave,
attendance_ID int FOREIGN KEY REFERENCES Attendance,
PRIMARY KEY(deduction_id, emp_ID)
);
CREATE TABLE Performance (
performance_ID int PRIMARY KEY IDENTITY,
rating int CHECK(rating>=1 AND rating<=5),
comments varchar (50),
semester CHAR(3),
emp_ID int FOREIGN KEY REFERENCES Employee
);
CREATE TABLE Employee_Replace_Employee (
table_id INT IDENTITY,
Emp1_ID INT FOREIGN KEY REFERENCES EMPLOYEE
,
Emp2_ID INT FOREIGN KEY REFERENCES EMPLOYEE
,
from_date date,
to_date date,
PRIMARY KEY(table_id, Emp1_ID, Emp2_ID)
);
CREATE TABLE Employee_Approve_Leave(
Emp1_ID INT FOREIGN KEY REFERENCES EMPLOYEE,
leave_ID INT FOREIGN KEY REFERENCES Leave,
status varchar(50) CHECK(status IN('Approved', 'Pending', 'Rejected')) DEFAULT('Pending'),
PRIMARY KEY(Emp1_ID, leave_ID)
);
GO
exec createAllTables
go
CREATE PROC dropAllTables
AS
DROP TABLE Employee_Phone;
DROP TABLE Employee_Role;
DROP TABLE Annual_Leave;
DROP TABLE Accidental_Leave;
DROP TABLE Document;
DROP TABLE Compensation_Leave;
DROP TABLE Role_existsIn_Department;
DROP TABLE Role;
DROP TABLE Performance;
DROP TABLE Employee_Replace_Employee;
DROP TABLE Employee_Approve_Leave;
DROP TABLE Medical_Leave;
DROP TABLE Deduction;
DROP TABLE Payroll;
DROP TABLE Unpaid_Leave;
DROP TABLE Attendance;
DROP TABLE Leave;
DROP TABLE Employee;
DROP TABLE Department;
GO
CREATE PROC clearAllTables
AS
TRUNCATE TABLE Employee_Phone;
TRUNCATE TABLE Employee_Role;
TRUNCATE TABLE Annual_Leave;
TRUNCATE TABLE Accidental_Leave;
TRUNCATE TABLE Compensation_Leave;
TRUNCATE TABLE Role_existsIn_Department;
TRUNCATE TABLE Performance;
TRUNCATE TABLE Employee_Replace_Employee;
TRUNCATE TABLE Employee_Approve_Leave;
TRUNCATE TABLE Deduction;
TRUNCATE TABLE Payroll;
TRUNCATE TABLE Document;
DELETE FROM Attendance;
DELETE FROM Medical_Leave;
DELETE FROM Unpaid_Leave;
DELETE FROM Leave;
DELETE FROM Employee;
DELETE FROM Department;
DELETE FROM Role;
GO
--
CREATE OR ALTER PROC dropAllProceduresFunctionsViews
AS
DROP PROC createAllTables;
DROP PROC dropAllTables;
DROP PROC clearAllTables;
DROP VIEW allEmployeeProfiles;
DROP VIEW allValidContracts;
DROP VIEW NoEmployeeDept;
DROP VIEW EmployeePayroll;
DROP VIEW allPerformance;
DROP VIEW allApprovedLeaves;
DROP VIEW allRejectedMedicals;
DROP VIEW allEmployeeAttendance;
DROP VIEW allDeductionUnpaid;
DROP VIEW allEmployeeRank;
DROP PROC New_Department;
DROP PROC New_Role;
DROP PROC Update_Status_Doc;
DROP PROC Remove_Deductions;
DROP PROC Delete_Doc_Employee;
DROP PROC Create_Holiday;
DROP PROC Add_holiday;
DROP PROC Intitiate_Attendance;
DROP PROC Update_attendance;
DROP PROC Remove_holiday;
DROP PROC Remove_dayOff;
DROP PROC Remove_Approved_Leaves;
DROP PROC Replace_employee;
DROP FUNCTION HRLoginValidation;
DROP PROC HR_approval_an_acc;
DROP PROC HR_approval_unpaid;
DROP PROC HR_approval_comp;
DROP PROC Deduction_hours;
DROP PROC Deduction_unpaid;
DROP FUNCTION Bonus_amount;
DROP PROC Add_payroll;
DROP FUNCTION HRSalary_calculation;
DROP FUNCTION EmployeeLoginValidation;
DROP FUNCTION Personal_info;
DROP FUNCTION MyPerformance;
DROP FUNCTION MyAttendance;
DROP FUNCTION Last_month_payroll;
DROP FUNCTION Deductions_Attendance;
DROP PROC MyResignation;
DROP FUNCTION Is_on_leave;
DROP PROC Submit_annual;
DROP FUNCTION status_leaves;
DROP PROC Dean_approve_annual;
DROP PROC Submit_Accidental;
DROP PROC Submit_Medical;
DROP PROC Submit_Unpaid;
DROP PROC Upperboard_approve_unpaids;
DROP PROC Submit_compensation;
DROP PROC DeanandHR_Evaluation;
DROP PROC Medical_approval;
GO
---------------------------------- 2.2 ---------------------------------------
-- a)
CREATE VIEW allEmployeeProfiles
AS
SELECT employee_ID, first_name, last_name, gender, email, address,years_of_experience, official_day_off,type_of_contract,employment_status,
annual_balance, accidental_balance
FROM Employee;
GO
-- b)
Create View NoEmployeeDept
AS
select dept_name as 'Department',count(employee_id)
as 'Number of Employees'from Employee
WHERE dept_name IS NOT NULL
group by dept_name
GO
-- c)
CREATE VIEW allPerformance
AS
SELECT P.*
FROM Performance P
INNER JOIN Employee E
ON p.emp_id = E.employee_ID
WHERE P.SEMESTER LIKE 'W%';
GO
-- d)
CREATE VIEW allRejectedMedicals
AS
select M.*
FROM medical_Leave M INNER JOIN leave
ON M.request_ID = leave.request_ID
where leave.final_approval_status='rejected';
GO
-- e)
CREATE OR ALTER VIEW allEmployeeAttendance
AS
SELECT *
FROM Attendance
WHERE Date=CAST(DATEADD(day, -1, GETDATE()) AS date);
GO
---------------------------- 2.3 ------------------------------
--------------2.3 a ----------
CREATE PROC Update_Status_Doc
AS
UPDATE Document
SET status='expired'
WHERE expiry_date IS NOT NULL AND CURRENT_TIMESTAMP> expiry_date;
GO
--------------2.3 b ----------
CREATE PROC Remove_Deductions
AS
DELETE FROM Deduction
WHERE emp_ID = ANY(
SELECT employee_id
FROM Employee
WHERE last_working_date< CURRENT_TIMESTAMP AND employment_status= 'resigned');
GO
-- 2.3 c) Update the employee’s employment_status daily based on whether the employee is on leave or active.
CREATE or alter PROC Update_Employment_Status @Employee_ID int
AS
declare @onleave bit =dbo.Is_On_Leave ( @Employee_ID,
CAST(CURRENT_TIMESTAMP AS DATE), CAST(CURRENT_TIMESTAMP AS DATE))
IF @onleave =1
begin
update Employee
set employment_status='onleave'
where employee_id=@Employee_ID
end
else IF @onleave =0
begin
update Employee
set employment_status='active'
where employee_id=@Employee_ID
end
GO
-- 2.3 d)
CREATE PROC Create_Holiday
AS
CREATE TABLE Holiday(
holiday_ID int IDENTITY,
holiday_name varchar(50),
from_date date,
to_date date
);
GO
CREATE OR ALTER PROCEDURE Add_Holiday
@holiday_name VARCHAR(50),
@from_date DATE,
@to_date DATE
AS
BEGIN
-- Added Basic Input validation
IF @holiday_name IS NULL OR @holiday_name= '' OR @from_date IS NULL OR @to_date IS NULL OR @from_date > @to_date
BEGIN
PRINT 'holiday_data must not be NULL or empty and the date_range entered must be correct.'
END
IF EXISTS (
SELECT 1 FROM Holiday h
WHERE holiday_name = @holiday_name
AND NOT (h.to_date < @from_date OR h.from_date > @to_date)
)
BEGIN
PRINT 'Holiday date range overlaps an existing holiday for the same name.'
END
ELSE
BEGIN
INSERT INTO Holiday (holiday_name, from_date, to_date)
VALUES (@holiday_name, @from_date, @to_date);
END
END;
GO
CREATE OR ALTER PROCEDURE Initiate_Attendance
AS
BEGIN
DECLARE @today DATE = CAST(GETDATE() AS DATE);
-- Insert attendance records for all employees who don't already have a record for today
INSERT INTO Attendance ([date], emp_ID)
SELECT @today, e.employee_id
FROM dbo.Employee AS e
WHERE NOT EXISTS (
SELECT 1
FROM dbo.Attendance AS a
WHERE a.emp_ID = e.employee_id
AND a.[date] = @today
);
END;
GO
--------------2.3 g ----------
CREATE OR ALTER PROCEDURE Update_Attendance
@Employee_id INT,
@check_in_time TIME = NULL,
@check_out_time TIME = NULL
AS
BEGIN
DECLARE @today DATE = CAST(GETDATE() AS DATE);
DECLARE @status VARCHAR(10);
-- Determine status based on presence of check-in/check-out times
IF @check_in_time IS NULL OR @check_out_time IS NULL
SET @status = 'Absent';
ELSE
SET @status = 'Attended';
-- Ensure the attendance record for today exists
IF NOT EXISTS (
SELECT 1
FROM Attendance
WHERE emp_ID = @Employee_id
AND [date] = @today
)
BEGIN
PRINT 'No attendance record found for this employee today.'
END
ELSE
BEGIN
UPDATE Attendance
SET check_in_time = @check_in_time,
check_out_time = @check_out_time,
status = @status
WHERE emp_ID = @Employee_id
AND [date] = @today;
END
END;
GO
----------- 2.3 h -------------
CREATE OR ALTER PROCEDURE Remove_Holiday
AS
BEGIN
-- Delete any attendance record whose date falls within any holiday range
DELETE A
FROM Attendance AS A
WHERE EXISTS (
SELECT 1
FROM dbo.Holiday AS H
WHERE A.[date] BETWEEN H.from_date AND H.to_date
);
END;
GO
----------- 2.3 i -------------
CREATE OR ALTER PROCEDURE dbo.Remove_DayOff
@employee_ID INT
AS
BEGIN
DECLARE @official_day_off VARCHAR(50);
DECLARE @current_year INT = YEAR(GETDATE());
DECLARE @current_month INT = MONTH(GETDATE());
-- Validate employee and read official day off
SELECT @official_day_off = official_day_off
FROM dbo.Employee
WHERE employee_id = @employee_ID;
IF @official_day_off IS NULL
BEGIN
PRINT 'Employee not found or official_day_off is NULL for the specified employee.';
END
-- Perform delete:
-- remove rows for the employee in the current month/year
-- where weekday matches the employee's official day off (case-insensitive)
-- AND (status = 'Absent' OR (check_in_time IS NULL AND check_out_time IS NULL))
DELETE A
FROM dbo.Attendance AS A
WHERE A.emp_ID = @employee_ID
AND YEAR(A.[date]) = @current_year
AND MONTH(A.[date]) = @current_month
AND UPPER(DATENAME(WEEKDAY, A.[date])) = UPPER(@official_day_off)
AND (
-- prefer explicit status check if column exists / used
(A.status IS NOT NULL AND UPPER(A.status) = 'ABSENT')
OR
(A.check_in_time IS NULL AND A.check_out_time IS NULL)
);
END;
GO
----------- 2.3 j -------------
CREATE OR ALTER PROCEDURE dbo.Remove_Approved_Leaves
@employee_id INT
AS
BEGIN
IF @employee_id IS NULL
BEGIN
PRINT 'employee_id must not be NULL.';
END;
ELSE
BEGIN
/*
Delete any attendance row for the given employee where:
- the attendance date falls BETWEEN start_date AND end_date of any Approved leave for that employee
- AND (status = 'Absent' OR (check_in_time IS NULL AND check_out_time IS NULL))
The logic uses EXISTS against each leave-subtype table so any matching approved leave will qualify the row for deletion.
*/
DELETE A
FROM dbo.Attendance AS A
WHERE A.emp_ID = @employee_id
AND (
-- row date falls within an approved Annual_Leave for this employee
EXISTS (
SELECT 1
FROM dbo.Leave AS L
INNER JOIN dbo.Annual_Leave AS AL ON AL.request_ID = L.request_ID
WHERE AL.emp_ID = @employee_id
AND L.final_approval_status = 'Approved'
AND A.[date] BETWEEN L.start_date AND L.end_date
)
OR
-- accidental leave
EXISTS (
SELECT 1
FROM dbo.Leave AS L
INNER JOIN dbo.Accidental_Leave AS ACL ON ACL.request_ID = L.request_ID
WHERE ACL.emp_ID = @employee_id
AND L.final_approval_status = 'Approved'
AND A.[date] BETWEEN L.start_date AND L.end_date
)
OR
-- compensation leave
EXISTS (
SELECT 1
FROM dbo.Leave AS L
INNER JOIN dbo.Compensation_Leave AS CL ON CL.request_ID = L.request_ID
WHERE CL.emp_ID = @employee_id
AND L.final_approval_status = 'Approved'
AND A.[date] BETWEEN L.start_date AND L.end_date
)
OR
-- medical leave
EXISTS (
SELECT 1
FROM dbo.Leave AS L
INNER JOIN dbo.Medical_Leave AS ML ON ML.request_ID = L.request_ID
WHERE ML.Emp_ID = @employee_id
AND L.final_approval_status = 'Approved'
AND A.[date] BETWEEN L.start_date AND L.end_date
)
OR
-- unpaid leave
EXISTS (
SELECT 1
FROM dbo.Leave AS L
INNER JOIN dbo.Unpaid_Leave AS UL ON UL.request_ID = L.request_ID
WHERE UL.Emp_ID = @employee_id
AND L.final_approval_status = 'Approved'
AND A.[date] BETWEEN L.start_date AND L.end_date
)
)
-- only delete unattended/absent rows as requested
AND (
(A.status IS NOT NULL AND UPPER(A.status) = 'ABSENT')
OR
(A.check_in_time IS NULL AND A.check_out_time IS NULL)
);
END
END;
GO
GO
------------2.3 k ---------
CREATE OR ALTER PROCEDURE dbo.Replace_employee
@Emp1_ID INT, -- employee being replaced
@Emp2_ID INT, -- replacement employee
@from_date DATE,
@to_date DATE
AS
BEGIN
DECLARE @Emp1Exists BIT = 0;
DECLARE @Emp2Exists BIT = 0;
-- Basic validation
IF @Emp1_ID IS NULL OR @Emp2_ID IS NULL BEGIN PRINT 'Emp1_ID and Emp2_ID must not be NULL.';RETURN;END
IF @from_date IS NULL OR @to_date IS NULL BEGIN PRINT 'from_date and to_date must not be NULL.';RETURN;END
IF @from_date > @to_date BEGIN PRINT 'from_date must be less than or equal to to_date.';RETURN; END
IF @Emp1_ID = @Emp2_ID BEGIN PRINT 'Emp1_ID and Emp2_ID cannot be the same.';RETURN;END
-- Verify employees exist
IF EXISTS (SELECT 1 FROM Employee WHERE employee_id = @Emp1_ID) SET @Emp1Exists = 1;
IF EXISTS (SELECT 1 FROM Employee WHERE employee_id = @Emp2_ID) SET @Emp2Exists = 1;
IF @Emp1Exists = 0 BEGIN PRINT 'Employee with Emp1_ID not found.'; RETURN;END
IF @Emp2Exists = 0 BEGIN PRINT 'Employee with Emp2_ID not found.'; RETURN;END
--overlap check: prevent Emp2 double-assignment
IF EXISTS (
SELECT 1
FROM dbo.Employee_Replace_Employee r
WHERE r.Emp2_ID = @Emp2_ID
AND NOT (r.to_date < @from_date OR r.from_date > @to_date)
)
BEGIN
PRINT 'Replacement employee (Emp2) already has an overlapping replacement period.';
RETURN;
END
-- Perform the insertion
INSERT INTO dbo.Employee_Replace_Employee (Emp1_ID, Emp2_ID, from_date, to_date)
VALUES (@Emp1_ID, @Emp2_ID, @from_date, @to_date);
END;
GO
---------------------------------------------------- 2.4 ----------------------------------------
------------------------------------------- a) ---------------------------------------------------
CREATE FUNCTION HRLoginValidation
(@employee_ID int,
@password varchar(50))
Returns BIT
AS
Begin
Declare @success BIT
IF EXISTS (
SELECT 1 FROM Employee E WHERE employee_id=@employee_ID AND password=@password
and dept_name = 'HR')
SET @success =1
ELSE
SET @success =0;
Return @success
END
go
----------------------------------------------------- b) --------------------------------
CREATE or alter PROC HR_approval_an_acc
@request_ID int,
@HR_ID int
AS
--declare variables
declare @emp_ID int
DECLARE @num_days INT
DECLARE @current_status VARCHAR(50)
DECLARE @annual_balance INT
DECLARE @accidental_balance INT
declare @start_date_acc date
declare @end_date_acc date
declare @date_of_request date
--declare @difference_between_date_start_date int
----------------------- Annual Leave -------------------------------------------
--- check if the balance is greater than 0 and the leave is annual leave -------
if exists(select 1 from Annual_Leave where request_ID = @request_ID)
begin
-- get the employee_ID of the requester
select @emp_ID = emp_ID
from Annual_Leave
where request_ID = @request_ID
-- get total number of days and current status
SELECT @num_days = num_days, @current_status = final_approval_status,@start_date_acc=start_date,@end_date_acc=end_date
FROM Leave
WHERE request_ID = @request_ID;
--get employee annual balance
SELECT @annual_balance = annual_balance
FROM Employee
WHERE employee_id = @emp_ID;
-- check if it is not already pending, print error message
IF @current_status <> 'Pending'
BEGIN
PRINT 'Error: Leave request has already been processed.';
RETURN;
END
-- check balance and Approve/reject
if @annual_balance >= @num_days
begin
-- update the table approve
update Employee_Approve_Leave
set status = 'Approved'
where Emp1_ID = @HR_ID and leave_ID=@request_ID
-- update the final status
update Leave
set final_approval_status = 'Approved'
where request_ID = @request_ID
-- deduct from Annual balance of Employee
UPDATE Employee
SET annual_balance = annual_balance - @num_days
WHERE employee_id = @emp_ID;
-------------------------------------CHANGES-------------------
--if approved we need to update the employee replace employee table
-- needs replacementID , which I can get it from annual Leave
declare @replacement_id int
select @replacement_id=replacement_emp from Annual_Leave where request_ID=@request_ID
-- insert into employee replace employee
insert into Employee_Replace_Employee (Emp1_ID,Emp2_ID,from_date, to_date) values (@emp_ID,@replacement_id,@start_date_acc,@end_date_acc)
-------------------------------------------------------------------
end
else
begin
--reject the leave
UPDATE Employee_Approve_Leave
SET status = 'Rejected'
WHERE leave_ID = @request_ID AND Emp1_ID = @HR_ID;
UPDATE Leave
SET final_approval_status = 'Rejected'
WHERE request_ID = @request_ID;
end
end
---------------------------------- Accidental Leave -------------------------
ELSE IF EXISTS (SELECT 1 FROM Accidental_Leave WHERE request_ID = @request_ID)
BEGIN
--get requester_ID
SELECT @emp_ID = emp_ID
FROM Accidental_Leave
WHERE request_ID = @request_ID;
--get num_days and current_status
SELECT @num_days = num_days, @current_status = final_approval_status
FROM Leave
WHERE request_ID = @request_ID;
-- get accidental balance
SELECT @accidental_balance = accidental_balance
FROM Employee
WHERE employee_id = @emp_ID;
-- Check if leave is already processed, print error message
IF @current_status <> 'Pending'
BEGIN
PRINT 'Error: Leave request has already been processed.';
RETURN;
END
-- check if it is submitted after maximum 2 days -------------------
select @date_of_request = date_of_request, @start_date_acc = start_date
from Leave
where request_ID = @request_ID
-- -- Check balance and num_days is 1 then approve/reject
IF @accidental_balance >= @num_days and @num_days = 1 and DATEDIFF(DAY, @start_date_acc, @date_of_request) <= 2 and @date_of_request > @start_date_acc
BEGIN
-- Update HR approval status
UPDATE Employee_Approve_Leave
SET status = 'Approved'
WHERE leave_ID = @request_ID AND Emp1_ID = @HR_ID;
-- update the final status
UPDATE Leave
SET final_approval_status = 'Approved'
WHERE request_ID = @request_ID;
-- Deduct from employee's accidental balance
UPDATE Employee
SET accidental_balance = accidental_balance - @num_days
WHERE employee_id = @emp_ID;
end
else
begin
-- Update HR approval status
UPDATE Employee_Approve_Leave
SET status = 'Rejected'
WHERE leave_ID = @request_ID AND Emp1_ID = @HR_ID;
-- Reject the leave
UPDATE Leave
SET final_approval_status = 'Rejected'
WHERE request_ID = @request_ID;
end
end
GO
----------------------------------------------- c) --------------------------------------------------
CREATE or alter PROC HR_approval_Unpaid
@request_ID int,
@HR_ID int
as
-- Declare variables
DECLARE @emp_ID INT
DECLARE @num_days INT
DECLARE @current_status VARCHAR(50)
DECLARE @start_date DATE
DECLARE @end_date DATE
DECLARE @approved_unpaid_count_days INT
IF EXISTS (SELECT 1 FROM Unpaid_Leave WHERE request_ID = @request_ID)
BEGIN
-- Get the employee_ID of the requester
SELECT @emp_ID = emp_ID
FROM Unpaid_Leave
WHERE request_ID = @request_ID;
----check that annual balance is zero
declare @annual_balance int
select @annual_balance = annual_balance from Employee
where employee_id = @emp_ID
-- Get number of days, current status, and dates
SELECT @num_days = num_days,
@current_status = final_approval_status,
@start_date = start_date,
@end_date = end_date
FROM Leave
WHERE request_ID = @request_ID;
-- Check if leave is already processed
IF @current_status <> 'Pending'
BEGIN
PRINT 'Error: Leave request has already been processed.';
RETURN;
END
-- Check if any employee in the approval hierarchy rejected the leave
IF EXISTS
(SELECT 1 FROM Employee_Approve_Leave
WHERE leave_ID = @request_ID AND status = 'Rejected')
BEGIN
-- Update HR approval record
UPDATE Employee_Approve_Leave
SET status = 'Rejected'
WHERE Emp1_ID = @HR_ID AND leave_ID = @request_ID;
-- Update the final status in leave table
UPDATE Leave
SET final_approval_status = 'Rejected'
WHERE request_ID = @request_ID;
RETURN;
END
else if (@annual_balance<>0)
begin
UPDATE Employee_Approve_Leave
SET status = 'Rejected'
WHERE Emp1_ID = @HR_ID AND leave_ID = @request_ID;
-- Update the final status in leave table
UPDATE Leave
SET final_approval_status = 'Rejected'
WHERE request_ID = @request_ID;
PRINT 'Error: You have annual balance';
RETURN;
end
-- Check if duration exceeds 30 days
else IF (@num_days > 30)
BEGIN
-- Reject the leave
UPDATE Employee_Approve_Leave
SET status = 'Rejected'
WHERE leave_ID = @request_ID AND Emp1_ID = @HR_ID;
UPDATE Leave
SET final_approval_status = 'Rejected'
WHERE request_ID = @request_ID;
RETURN;
END
--if already has 1 unpaid leave in same year approved reject
else IF(exists(select * FROM Unpaid_Leave un
inner join Leave l
ON l.request_ID = un.request_ID
WHERE un.Emp_ID = @emp_ID and
year(l.start_date) = year(current_timestamp) and final_approval_status = 'approved'
))
BEGIN
UPDATE Employee_Approve_Leave
SET status = 'rejected'
where leave_ID = @request_ID and Emp1_ID = @HR_ID
UPDATE Leave
SET final_approval_status = 'rejected'
WHERE request_ID = @request_ID
END
-- All checks passed - Approve the leave
-- Update HR approval record
else
begin
UPDATE Employee_Approve_Leave
SET status = 'Approved'
WHERE Emp1_ID = @HR_ID AND leave_ID = @request_ID;