1
|
ALTER PROCEDURE [dbo].[CBIATMTOPYOVERTIME]
|
2
|
(
|
3
|
@EmployeeID VARCHAR(20)
|
4
|
,@StartDate VARCHAR(20)
|
5
|
,@EndDate VARCHAR(20)
|
6
|
,@WSType VARCHAR(20)
|
7
|
)
|
8
|
AS
|
9
|
|
10
|
--DECLARE @EmployeeID VARCHAR(20) = '11100027'
|
11
|
--DECLARE @StartDate VARCHAR(20) = '20230101'
|
12
|
--DECLARE @EndDate VARCHAR(20) = '20230131'
|
13
|
--DECLARE @WSType VARCHAR(20) = '6D7H'
|
14
|
|
15
|
IF(@WSType = '6D7H')
|
16
|
BEGIN
|
17
|
DECLARE @wscal TABLE
|
18
|
(
|
19
|
EmployeeID VARCHAR(20)
|
20
|
,DateSpecified VARCHAR(20)
|
21
|
,DayType VARCHAR(20)
|
22
|
,FlagDay VARCHAR(20)
|
23
|
,StartWorkTime VARCHAR(20)
|
24
|
,EndWorkTime VARCHAR(20)
|
25
|
)
|
26
|
INSERT INTO @wscal
|
27
|
SELECT DISTINCT @EmployeeID, cal.DateSpecified, cal.DayType, dt.Flag, dt.StartTime, dt.EndTime
|
28
|
FROM dbo.PHRTMWSCAL AS cal
|
29
|
LEFT JOIN dbo.PHRTMDAYTYP AS dt
|
30
|
ON dt.DayType = cal.DayType
|
31
|
WHERE cal.WorkScheduleType = @WSType
|
32
|
AND (cal.DateSpecified BETWEEN @StartDate AND @EndDate)
|
33
|
|
34
|
DECLARE @TblMDOvt TABLE
|
35
|
(
|
36
|
EmployeeID VARCHAR(20)
|
37
|
,StartDate VARCHAR(20)
|
38
|
,StartTime VARCHAR(20)
|
39
|
,EndTime VARCHAR(20)
|
40
|
,Duration DECIMAL(18,2)
|
41
|
,DayType VARCHAR(20)
|
42
|
,FlagDay VARCHAR(20)
|
43
|
,StartWorkTime VARCHAR(20)
|
44
|
,EndWorkTime VARCHAR(20)
|
45
|
,OvtDuration16 DECIMAL(18,2)
|
46
|
)
|
47
|
|
48
|
INSERT INTO @TblMDOvt
|
49
|
SELECT DISTINCT pa16.EmployeeID, pa16.StartDate, pa16.StartTime, pa16.EndTime, pa16.Duration
|
50
|
,wscal.DayType, wscal.FlagDay, wscal.StartWorkTime, wscal.EndWorkTime
|
51
|
,dbo.fn_diffTime(pa16.StartDate, pa16.StartDate, pa16.StartTime, pa16.EndTime) AS OvtDuration16
|
52
|
FROM PHRPA0016 AS pa16
|
53
|
LEFT JOIN @wscal AS wscal
|
54
|
ON pa16.StartDate = wscal.DateSpecified
|
55
|
WHERE pa16.EmployeeID = @EmployeeID
|
56
|
AND (pa16.StartDate BETWEEN @StartDate AND @EndDate)
|
57
|
|
58
|
DECLARE @TblMDAtt TABLE
|
59
|
(
|
60
|
EmployeeID VARCHAR(20)
|
61
|
,StartDate VARCHAR(20)
|
62
|
,StartTime VARCHAR(20)
|
63
|
,EndTime VARCHAR(20)
|
64
|
,DayType VARCHAR(20)
|
65
|
,FlagDay VARCHAR(20)
|
66
|
,StartWorkTime VARCHAR(20)
|
67
|
,EndWorkTime VARCHAR(20)
|
68
|
,OvtDuration19 DECIMAL(18,2)
|
69
|
)
|
70
|
INSERT INTO @TblMDAtt
|
71
|
SELECT DISTINCT pa19.EmployeeID, pa19.StartDate, pa19.StartTime, pa19.EndTime
|
72
|
,wscal.DayType, wscal.FlagDay, wscal.StartWorkTime, wscal.EndWorkTime
|
73
|
,CASE WHEN dbo.fn_diffTime(pa19.StartDate, pa19.StartDate, wscal.EndWorkTime, pa19.EndTime) >= 1 THEN 1 ELSE 0 END AS OvtDuration19
|
74
|
FROM PHRPA0019 AS pa19
|
75
|
LEFT JOIN @wscal AS wscal
|
76
|
ON pa19.StartDate = wscal.DateSpecified
|
77
|
WHERE pa19.EmployeeID = @EmployeeID
|
78
|
AND (pa19.StartDate BETWEEN @StartDate AND @EndDate)
|
79
|
|
80
|
DECLARE @TblOvtDur TABLE
|
81
|
(
|
82
|
EmployeeID VARCHAR(20)
|
83
|
,StartDate VARCHAR(20)
|
84
|
,FlagDay VARCHAR(20)
|
85
|
,SumOvtDuration DECIMAL(18,2)
|
86
|
)
|
87
|
|
88
|
INSERT INTO @TblOvtDur
|
89
|
SELECT DISTINCT ovt.EmployeeID, ovt.StartDate, ovt.FlagDay, --ovt.StartTime, ovt.EndTime, ovt.OvtDuration16, att.StartTime, att.EndTime, att.OvtDuration19,
|
90
|
CASE WHEN ovt.StartTime <= '1700' THEN ovt.OvtDuration16 + att.OvtDuration19 - (dbo.fn_diffTime(ovt.StartDate, ovt.StartDate, ovt.StartTime, '1700')) ELSE ovt.OvtDuration16 + att.OvtDuration19 END AS OvtDuration
|
91
|
FROM @TblMDOvt AS ovt
|
92
|
LEFT JOIN @TblMDAtt AS att
|
93
|
ON att.EmployeeID = ovt.EmployeeID
|
94
|
WHERE ovt.StartDate = att.StartDate
|
95
|
|
96
|
INSERT INTO @TblOvtDur
|
97
|
SELECT DISTINCT mdovt.EmployeeID, mdovt.StartDate, mdovt.FlagDay, mdovt.OvtDuration16
|
98
|
FROM @TblMDOvt AS mdovt
|
99
|
WHERE mdovt.StartDate NOT IN (SELECT DISTINCT StartDate FROM @TblOvtDur)
|
100
|
|
101
|
DECLARE @TblSumOvtDur TABLE
|
102
|
(
|
103
|
EmployeeID VARCHAR(20)
|
104
|
,StartDate VARCHAR(20)
|
105
|
,FlagDay VARCHAR(20)
|
106
|
,SumOvtDuration DECIMAL(18,2)
|
107
|
,OVT150 DECIMAL(18,2)
|
108
|
,OVT200 DECIMAL(18,2)
|
109
|
,OVT300 DECIMAL(18,2)
|
110
|
,OVT400 DECIMAL(18,2)
|
111
|
)
|
112
|
|
113
|
INSERT INTO @TblSumOvtDur
|
114
|
SELECT DISTINCT *
|
115
|
,CASE
|
116
|
WHEN FlagDay = 'WORK' THEN CASE WHEN SumOvtDuration < 1 THEN SumOvtDuration ELSE 1 END
|
117
|
ELSE 0
|
118
|
END AS OVT150
|
119
|
,CASE
|
120
|
WHEN FlagDay = 'WORK' THEN CASE WHEN SumOvtDuration >= 1 THEN (SumOvtDuration - 1) END
|
121
|
WHEN FlagDay = 'FREE' THEN CASE WHEN SumOvtDuration < 7 THEN SumOvtDuration ELSE 7 END
|
122
|
ELSE 0
|
123
|
END AS OVT200
|
124
|
,CASE
|
125
|
WHEN FlagDay = 'FREE' THEN CASE WHEN SumOvtDuration >= 8 THEN 1 WHEN SumOvtDuration BETWEEN 7 AND 8 THEN SumOvtDuration - 7 ELSE 0 END
|
126
|
ELSE 0
|
127
|
END AS OVT300
|
128
|
,CASE
|
129
|
WHEN FlagDay = 'FREE' THEN CASE WHEN SumOvtDuration >= 9 THEN (SumOvtDuration - 9) ELSE 0 END
|
130
|
ELSE 0
|
131
|
END AS OVT400
|
132
|
FROM @TblOvtDur
|
133
|
|
134
|
SELECT DISTINCT SumDur.EmployeeID
|
135
|
,SUM(SumDur.OVT150) AS OVT150
|
136
|
,SUM(SumDur.OVT200) AS OVT200
|
137
|
,SUM(SumDur.OVT300) AS OVT300
|
138
|
,SUM(SumDur.OVT400) AS OVT400
|
139
|
FROM @TblSumOvtDur AS SumDur
|
140
|
GROUP BY SumDur.EmployeeID
|
141
|
END
|
142
|
ELSE
|
143
|
BEGIN
|
144
|
SELECT DISTINCT @EmployeeID
|
145
|
,0 AS OVT150
|
146
|
,0 AS OVT200
|
147
|
,0 AS OVT300
|
148
|
,0 AS OVT400
|
149
|
END
|