Project

General

Profile

Feature #2264 » CBIARPTPYPAYSLIPABSENCE.sql

Tri Rizqiaty, 06/20/2023 01:41 PM

 
1
--ALTER PROCEDURE CBIARPTPYPAYSLIPABSENCE
2
--(
3
--    @PayPeriod VARCHAR(6) ,
4
--    @EmpID VARCHAR(20) ,
5
--    @Running VARCHAR(2)
6
--)
7
--AS
8

    
9

    
10
DECLARE @PayPeriod VARCHAR(6) = '202302'
11
DECLARE @EmpID VARCHAR(8) = '11050019'
12
DECLARE @Running VARCHAR(2) = '1'
13

    
14
DECLARE @Begda AS NVARCHAR(8)
15
DECLARE @Endda AS NVARCHAR(8)
16
SET @Begda = @PayPeriod + '01'
17
SET @Endda = CONVERT(NVARCHAR(8), DATEADD(d, -1, DATEADD(m, 1, @Begda)), 112)
18

    
19
DECLARE @TWorkSchedule TABLE
20
(
21
	EmployeeID VARCHAR(20)
22
	,StartDate VARCHAR(20)
23
	,EndDate VARCHAR(20)
24
	,DayType VARCHAR(20)
25
	,Flag VARCHAR(20)
26
)
27
INSERT INTO @TWorkSchedule
28
SELECT DISTINCT pa125.EmployeeID
29
				,pa125.StartDate
30
				,pa125.EndDate
31
				,pa125.DayType
32
				,daytype.Flag
33
FROM PHRPA0125 AS pa125 WITH(NOLOCK)
34
LEFT JOIN PHRTMDAYTYP AS daytype WITH(NOLOCK)
35
	On pa125.DayType = daytype.DayType
36
WHERE (pa125.EmployeeID = @EmpID OR @EmpID = '')
37
	AND pa125.StartDate BETWEEN @Begda AND @Endda
38

    
39
IF( (SELECT COUNT(StartDate) FROM @TWorkSchedule ) <= 0)
40
BEGIN
41
	INSERT INTO @TWorkSchedule
42
	SELECT DISTINCT pa25.EmployeeID
43
					,wscal.DateSpecified
44
					,wscal.DateSpecified
45
					,wscal.DayType
46
					,daytype.Flag
47
	FROM PHRPA0025 AS pa25 WITH(NOLOCK)
48
	LEFT JOIN PHRTMWSCAL AS wscal WITH(NOLOCK)
49
		ON wscal.WorkScheduleType = pa25.WorkScheduleType
50
		AND (wscal.DateSpecified BETWEEN @Begda AND @Endda) 
51
	LEFT JOIN PHRTMDAYTYP AS daytype WITH(NOLOCK)
52
		ON wscal.DayType = daytype.DayType
53
	WHERE (pa25.EmployeeID = @EmpID OR @EmpID = '')
54
		AND pa25.StartDate <= @Endda AND pa25.EndDate >= @Endda
55
END
56

    
57
DECLARE @TPA18 TABLE
58
(
59
	StartDate VARCHAR(20)
60
	,EndDate VARCHAR(20)
61
	,EmployeeID VARCHAR(20)
62
	,AbsenceType VARCHAR(20)
63
	,DateSpecified VARCHAR(20)
64
)
65

    
66
INSERT INTO @TPA18
67
SELECT DISTINCT pa18.StartDate, pa18.EndDate, pa18.EmployeeID, pa18.AbsenceType, wscal.StartDate
68
FROM dbo.PHRPA0018 AS pa18 WITH (NOLOCK)
69
LEFT JOIN @TWorkSchedule AS wscal
70
	ON wscal.StartDate BETWEEN pa18.StartDate AND pa18.EndDate
71
WHERE pa18.EmployeeID = @EmpID
72
	AND (pa18.StartDate BETWEEN @Begda AND @Endda OR pa18.EndDate BETWEEN @Begda AND @Endda)
73
	AND wscal.Flag <> 'FREE'
74
	
75
DECLARE @TPA19 TABLE
76
(
77
	StartDate VARCHAR(20)
78
	,EndDate VARCHAR(20)
79
	,EmployeeID VARCHAR(20)
80
	,AbsenceType VARCHAR(20)
81
	,DateSpecified VARCHAR(20)
82
)
83

    
84
INSERT INTO @TPA19
85
SELECT DISTINCT pa19.StartDate, pa19.EndDate, pa19.EmployeeID, pa19.AttendanceType, wscal.StartDate
86
FROM dbo.PHRPA0019 AS pa19 WITH (NOLOCK)
87
LEFT JOIN @TWorkSchedule AS wscal
88
	ON wscal.StartDate BETWEEN pa19.StartDate AND pa19.EndDate
89
WHERE pa19.EmployeeID = @EmpID
90
	AND (pa19.StartDate BETWEEN @Begda AND @Endda OR pa19.EndDate BETWEEN @Begda AND @Endda)
91
	AND wscal.Flag <> 'FREE'
92

    
93
DECLARE @TResult TABLE
94
(
95
	EmployeeID VARCHAR(20)
96
	,AbsType VARCHAR(50)
97
	,SumAbsence DECIMAL(18,0)
98
)
99

    
100
INSERT INTO @TResult
101
SELECT DISTINCT @EmpID, 'TanpaKeterangan' , 0
102
INSERT INTO @TResult
103
SELECT DISTINCT @EmpID, 'IzinPribadi' , 0
104
INSERT INTO @TResult
105
SELECT DISTINCT @EmpID, 'IzinSakit' , 0
106

    
107

    
108
UPDATE @TResult
109
SET SumAbsence = ISNULL((SELECT DISTINCT COUNT(wscal.StartDate) AS mangkir
110
FROM @TWorkSchedule AS wscal
111
WHERE wscal.StartDate NOT IN ( SELECT DISTINCT DateSpecified FROM @TPA18)
112
	AND wscal.StartDate NOT IN ( SELECT DISTINCT DateSpecified FROM @TPA19)
113
	AND wscal.Flag <> 'FREE'
114
GROUP BY wscal.EmployeeID),0)
115
WHERE AbsType = 'TanpaKeterangan'
116

    
117
UPDATE @TResult
118
SET SumAbsence = ISNULL(( SELECT DISTINCT COUNT([@TPA18].DateSpecified)
119
FROM @TPA18 
120
WHERE AbsenceType = '25'
121
GROUP BY EmployeeID),0)
122
WHERE AbsType = 'IzinPribadi'
123

    
124
UPDATE @TResult
125
SET SumAbsence = ISNULL((SELECT DISTINCT COUNT([@TPA18].DateSpecified)
126
FROM @TPA18 
127
WHERE AbsenceType = '40'
128
GROUP BY EmployeeID),0)
129
WHERE AbsType = 'IzinSakit'
130

    
131
SELECT * FROM 
132
(
133
	SELECT * FROM @TResult
134
)
135
AS SourceTable
136
PIVOT
137
(
138
	SUM(SumAbsence)
139
FOR AbsType IN
140
(	[TanpaKeterangan],
141
	[IzinPribadi],
142
	[IzinSakit] )
143
) AS pvt
144
ORDER BY EmployeeID
(1-1/3)