Project

General

Profile

Feature #2131 » CBIATMTOPYPOTGAJI.sql

Tri Rizqiaty, 05/03/2023 11:35 AM

 
1
ALTER PROCEDURE CBIATMTOPYPOTGAJI
2
(
3
	@EmployeeID VARCHAR(20)
4
	,@StartDate VARCHAR(20)
5
	,@EndDate VARCHAR(20)
6
)
7
AS
8

    
9
--DECLARE @EmployeeID VARCHAR(20) = '17010491'
10
--DECLARE @StartDate VARCHAR(20) = '20221201'
11
--DECLARE @EndDate VARCHAR(20) = '20221231'
12

    
13
DECLARE @WTBase VARCHAR(250)
14
SELECT @WTBase = Value1
15
FROM dbo.PCMEPGENPARAM WITH (NOLOCK)
16
WHERE Parameter = 'CBIATMTOPYPOTGAJIBASE'
17

    
18
DECLARE @AbsType VARCHAR(250)
19
SELECT @AbsType = Value1
20
FROM dbo.PCMEPGENPARAM WITH (NOLOCK)
21
WHERE Parameter = 'CBIATMTOPYPOTGAJIABSENCETYPE'
22

    
23
DECLARE @PayClass VARCHAR(250)
24
SELECT @PayClass = Value1
25
FROM dbo.PCMEPGENPARAM WITH (NOLOCK)
26
WHERE Parameter = 'CBIATMTOPYPOTGAJIPAYCLASS'
27

    
28
DECLARE @BaseAmount DECIMAL(22,0) = 0
29
SELECT DISTINCT @BaseAmount = cu304.Amount
30
FROM dbo.PHRPA0003 AS pa3 WITH (NOLOCK)
31
LEFT JOIN dbo.PHRPA0004 AS pa4 WITH (NOLOCK)
32
	ON pa3.EmployeeID = pa4.EmployeeID
33
	AND pa4.StartDate <= @EndDate AND pa4.EndDate >= @EndDate
34
LEFT JOIN dbo.PHRPYCU0304 AS cu304 WITH (NOLOCK)
35
	ON (CASE WHEN cu304.PayrollType = '*' THEN pa3.PayrollType ELSE cu304.PayrollType END) = pa3.PayrollType
36
	AND (CASE WHEN cu304.PayrollArea = '*' THEN pa3.PayrollArea ELSE cu304.PayrollArea END) = pa3.PayrollArea
37
	AND (CASE WHEN cu304.PayrollClass = '*' THEN pa3.PayrollClass ELSE cu304.PayrollClass END) = pa3.PayrollClass
38
	AND (CASE WHEN cu304.PayrollGrade = '*' THEN pa3.PayrollGrade ELSE cu304.PayrollGrade END) = pa3.PayrollGrade
39
	AND pa4.WageType = cu304.WageType
40
WHERE pa3.EmployeeID = @EmployeeID
41
	AND pa3.StartDate <= @EndDate AND pa3.EndDate >= @EndDate
42
	AND pa4.WageType IN (SELECT DISTINCT * FROM dbo.Split(',', @WTBase))
43
	AND pa3.PayrollClass NOT IN (SELECT DISTINCT * FROM dbo.Split(',', @PayClass))
44

    
45
SELECT DISTINCT @BaseAmount = @BaseAmount + cu304.Amount
46
FROM dbo.PHRPA0003 AS pa3 WITH (NOLOCK)
47
LEFT JOIN dbo.PHRPA0005 AS pa5 WITH (NOLOCK)
48
	ON pa3.EmployeeID = pa5.EmployeeID
49
	AND pa5.StartDate <= @EndDate AND pa5.EndDate >= @EndDate
50
LEFT JOIN dbo.PHRPYCU0304 AS cu304 WITH (NOLOCK)
51
	ON (CASE WHEN cu304.PayrollType = '*' THEN pa3.PayrollType ELSE cu304.PayrollType END) = pa3.PayrollType
52
	AND (CASE WHEN cu304.PayrollArea = '*' THEN pa3.PayrollArea ELSE cu304.PayrollArea END) = pa3.PayrollArea
53
	AND (CASE WHEN cu304.PayrollClass = '*' THEN pa3.PayrollClass ELSE cu304.PayrollClass END) = pa3.PayrollClass
54
	AND (CASE WHEN cu304.PayrollGrade = '*' THEN pa3.PayrollGrade ELSE cu304.PayrollGrade END) = pa3.PayrollGrade
55
	AND pa5.WageType = cu304.WageType
56
WHERE pa3.EmployeeID = @EmployeeID
57
	AND pa3.StartDate <= @EndDate AND pa3.EndDate >= @EndDate
58
	AND pa5.WageType IN (SELECT DISTINCT * FROM dbo.Split(',', @WTBase))
59
	AND pa3.PayrollClass IN (SELECT DISTINCT * FROM dbo.Split(',', @PayClass))
60

    
61
DECLARE @TWSCALEMP TABLE
62
(
63
	EmployeeID VARCHAR(20)
64
	,WSType VARCHAR(20)
65
	,DateSpecified VARCHAR(20)
66
	,DayType VARCHAR(20)
67
	,Flag VARCHAR(20)
68
)
69

    
70
INSERT INTO @TWSCALEMP
71
SELECT DISTINCT pa25.EmployeeID, pa25.WorkScheduleType, wscal.DateSpecified, wscal.DayType, daytype.Flag
72
FROM dbo.PHRPA0025 AS pa25 WITH (NOLOCK)
73
LEFT JOIN dbo.PHRTMWSCAL AS wscal WITH (NOLOCK)
74
	ON wscal.WorkScheduleType = pa25.WorkScheduleType
75
LEFT JOIN dbo.PHRTMDAYTYP AS daytype
76
	ON wscal.DayType = daytype.DayType
77
WHERE EmployeeID = @EmployeeID
78
	AND pa25.StartDate <= @EndDate AND pa25.EndDate = @EndDate
79
	AND (wscal.DateSpecified BETWEEN @StartDate AND @EndDate)
80

    
81
DECLARE @TPA18 TABLE
82
(
83
	StartDate VARCHAR(20)
84
	,EndDate VARCHAR(20)
85
	,EmployeeID VARCHAR(20)
86
	,AbsenceType VARCHAR(20)
87
	,DateSpecified VARCHAR(20)
88
)
89

    
90
INSERT INTO @TPA18
91
SELECT DISTINCT pa18.StartDate, pa18.EndDate, pa18.EmployeeID, pa18.AbsenceType, wscal.DateSpecified
92
FROM dbo.PHRPA0018 AS pa18 WITH (NOLOCK)
93
LEFT JOIN @TWSCALEMP AS wscal
94
	ON wscal.DateSpecified BETWEEN pa18.StartDate AND pa18.EndDate
95
WHERE pa18.EmployeeID = @EmployeeID
96
	AND (pa18.StartDate BETWEEN @StartDate AND @EndDate OR pa18.EndDate BETWEEN @StartDate AND @EndDate)
97
	AND wscal.Flag <> 'FREE'
98
	
99
DECLARE @TPA19 TABLE
100
(
101
	StartDate VARCHAR(20)
102
	,EndDate VARCHAR(20)
103
	,EmployeeID VARCHAR(20)
104
	,AbsenceType VARCHAR(20)
105
	,DateSpecified VARCHAR(20)
106
)
107

    
108
INSERT INTO @TPA19
109
SELECT DISTINCT pa19.StartDate, pa19.EndDate, pa19.EmployeeID, pa19.AttendanceType, wscal.DateSpecified
110
FROM dbo.PHRPA0019 AS pa19 WITH (NOLOCK)
111
LEFT JOIN @TWSCALEMP AS wscal
112
	ON wscal.DateSpecified BETWEEN pa19.StartDate AND pa19.EndDate
113
WHERE pa19.EmployeeID = @EmployeeID
114
	AND (pa19.StartDate BETWEEN @StartDate AND @EndDate OR pa19.EndDate BETWEEN @StartDate AND @EndDate)
115
	AND wscal.Flag <> 'FREE'
116

    
117
DECLARE @TResult TABLE
118
(
119
	EmployeeID VARCHAR(20)
120
	,AbsType VARCHAR(50)
121
	,SumAbsence DECIMAL(18,0)
122
)
123

    
124
INSERT INTO @TResult
125
SELECT DISTINCT wscal.EmployeeID, 'mangkir' AS AbsType, COUNT(wscal.DateSpecified) AS mangkir
126
FROM @TWSCALEMP AS wscal
127
WHERE wscal.DateSpecified NOT IN ( SELECT DISTINCT DateSpecified FROM @TPA18)
128
	AND wscal.DateSpecified NOT IN ( SELECT DISTINCT DateSpecified FROM @TPA19)
129
	AND wscal.Flag <> 'FREE'
130
GROUP BY wscal.EmployeeID
131

    
132
INSERT INTO @TResult
133
SELECT DISTINCT EmployeeID, 'izin sakit' AS AbsType, COUNT([@TPA18].DateSpecified)
134
FROM @TPA18 
135
WHERE AbsenceType IN (SELECT DISTINCT * FROM dbo.Split(',', @AbsType))
136
GROUP BY EmployeeID
137

    
138
SELECT SUM(SumAbsence) AS SumAbsence , @BaseAmount AS AmountBase, SUM(SumAbsence) * (@BaseAmount/25) AS PotonganGaji
139
FROM @TResult
(5-5/6)