Project

General

Profile

Feature #1371 » PHRBUDGETIRREGULARPAYROLL_20220707.sql

Tri Rizqiaty, 07/21/2022 02:51 PM

 
1
??CREATE PROCEDURE [dbo].[PHRBUDGETIRREGULARPAYROLL]
2

3
(
4

5
	@EmployeeID VARCHAR(20)
6

7
	,@StartDate VARCHAR(20)
8

9
	,@EndDate VARCHAR(20)
10

11
	,@now VARCHAR(20)
12

13
)
14

15
AS
16

17

18

19
--DECLARE @EmployeeID VARCHAR(20) = '10000192' ---- I1, I2 BBGProd 1438 - 00000276 - 2020 ;  I3 TPIDev 1442 - 00000474 - 2021; BKTDev 103 - 10000164 - 2021; BKTDev 103 - 10000192 - 2023
20

21
--DECLARE @StartDate VARCHAR(20) = '20230101' 
22

23
--DECLARE @EndDate VARCHAR(20) = '20231231'
24

25
--DECLARE @now  VARCHAR(20) = '20220616'
26

27

28

29
DECLARE @TDataEmp TABLE
30

31
(
32

33
	StartDate VARCHAR(10)
34

35
	,EndDate VARCHAR(10)
36

37
	,EmployeeID VARCHAR(20)
38

39
	,WageType VARCHAR(20)
40

41
	,Valuation VARCHAR(10)
42

43
	,Number DECIMAL(18,2)
44

45
	,Amount DECIMAL(22,2)
46

47
	,Base VARCHAR(20)
48

49
)
50

51
DECLARE @TWTPayroll TABLE
52

53
(
54

55
	StartDate VARCHAR(10)
56

57
	,EndDate VARCHAR(10)
58

59
	,EmployeeID VARCHAR(20)
60

61
	,WageType VARCHAR(20)
62

63
	,Valuation VARCHAR(10)
64

65
	,Number DECIMAL(18,2)
66

67
	,Amount DECIMAL(22,2)
68

69
)
70

71
DECLARE @TWTBase TABLE
72

73
(
74

75
	EmployeeID VARCHAR(20)
76

77
	,WageTypeMD VARCHAR(20)
78

79
	,Base VARCHAR(20)
80

81
	,WageType VARCHAR(20)
82

83
	,Persen DECIMAL(18,10)
84

85
)
86

87
DECLARE @StartDateLast VARCHAR(10) = CONVERT(NVARCHAR(8), DATEADD(YEAR, -1, @StartDate), 112)
88

89
DECLARE @EndDateLast VARCHAR(10) = CONVERT(NVARCHAR(8), DATEADD(YEAR, -1, @EndDate), 112)
90

91

92

93
INSERT INTO @TDataEmp
94

95
SELECT DISTINCT PA5.StartDate ,PA5.EndDate ,PA5.EmployeeID ,PA5.WageType ,cu300.Valuation ,PA5.Number
96

97
				,CAST(REPLACE(ISNULL(dbo.GetPEN(PA5.Amount), '0'), ',', '.') AS DECIMAL(22,0)) AS Amount
98

99
				,cu300.Base
100

101
FROM PHRPA0005 AS PA5
102

103
LEFT JOIN dbo.PHRPYCU0300 AS cu300
104

105
	ON PA5.WageType = cu300.WageTypeDefinition
106

107
WHERE (PA5.StartDate BETWEEN @StartDateLast AND @EndDateLast) 
108

109
	AND (PA5.EmployeeID = @EmployeeID OR @EmployeeID = '') 
110

111
ORDER BY PA5.EmployeeID
112

113

114

115
INSERT INTO @TWTPayroll
116

117
SELECT DISTINCT TDataEmp.StartDate ,TDataEmp.EndDate ,TDataEmp.EmployeeID ,TDataEmp.WageType ,TDataEmp.Valuation ,TDataEmp.Number ,TDataEmp.Amount
118

119
FROM @TDataEmp AS TDataEmp
120

121
WHERE TDataEmp.Valuation = 'D'
122

123

124

125
INSERT INTO @TWTPayroll
126

127
SELECT DISTINCT TDataEmp.StartDate ,TDataEmp.EndDate ,TDataEmp.EmployeeID ,TDataEmp.WageType ,TDataEmp.Valuation ,TDataEmp.Number
128

129
				,TDataEmp.Number * cu303.Amount AS Amount
130

131
FROM @TDataEmp AS TDataEmp
132

133
LEFT JOIN PHRPYCU0303 AS cu303
134

135
	ON TDataEmp.WageType = cu303.WageType
136

137
	AND cu303.StartDate <= TDataEmp.EndDate AND cu303.EndDate >= TDataEmp.EndDate
138

139
WHERE TDataEmp.Valuation = 'I1'
140

141

142

143
INSERT INTO @TWTPayroll
144

145
SELECT DISTINCT TDataEmp.StartDate ,TDataEmp.EndDate ,TDataEmp.EmployeeID ,TDataEmp.WageType ,TDataEmp.Valuation ,TDataEmp.Number
146

147
				,TDataEmp.Number * cu304.Amount AS Amount
148

149
FROM @TDataEmp AS TDataEmp
150

151
LEFT JOIN dbo.PHRPA0003 AS PA3
152

153
	ON PA3.EmployeeID = TDataEmp.EmployeeID
154

155
	AND PA3.StartDate <= '20210101' AND PA3.EndDate >= '20210101'
156

157
LEFT JOIN dbo.PHRPYCU0304 AS cu304
158

159
	ON cu304.WageType = TDataEmp.WageType
160

161
	AND (cu304.PayrollType = '*' OR PA3.PayrollType = cu304.PayrollType)
162

163
	AND (cu304.PayrollArea = '*' OR PA3.PayrollArea = cu304.PayrollArea)
164

165
	AND (cu304.PayrollClass = '*' OR cu304.PayrollClass = PA3.PayrollClass)
166

167
	AND (cu304.PayrollGrade = '*' OR cu304.PayrollGrade = PA3.PayrollGrade)
168

169
	AND cu304.StartDate <= TDataEmp.EndDate AND cu304.EndDate >= TDataEmp.EndDate
170

171
WHERE TDataEmp.Valuation = 'I2'
172

173

174

175
INSERT INTO @TWTBase
176

177
SELECT DISTINCT TDataEmp.EmployeeID ,TDataEmp.WageType ,TDataEmp.Base, cu305.WageType 
178

179
			,CASE WHEN cu300.Percentage >= 100 THEN (cu300.Percentage / 100)  ELSE cu300.Percentage END Percentage
180

181
FROM @TDataEmp AS TDataEmp
182

183
LEFT JOIN PHRPYCU0305 AS cu305
184

185
	ON cu305.WageTypeCum = TDataEmp.Base
186

187
	AND cu305.StartDate <= TDataEmp.EndDate AND cu305.EndDate >= TDataEmp.EndDate
188

189
LEFT JOIN dbo.PHRPYCU0300 AS cu300
190

191
	ON cu300.WageTypeDefinition = TDataEmp.Base	
192

193
	AND cu300.StartDate <= TDataEmp.EndDate AND cu300.EndDate >= TDataEmp.EndDate
194

195
WHERE TDataEmp.Valuation = 'I3' AND TDataEmp.Base <> ''
196

197

198

199
IF((SELECT COUNT(Base) FROM @TWTBase) > 0)
200

201
BEGIN
202

203
	INSERT @TWTPayroll
204

205
	SELECT DISTINCT PA4.StartDate ,PA4.EndDate ,PA4.EmployeeID ,TWTBase.WageTypeMD ,'I3' AS Valuation ,1 AS Number
206

207
					,SUM(CAST(REPLACE(ISNULL(dbo.GetPEN(PA4.Amount), '0'), ',', '.') AS DECIMAL(22,0)) * TWTBase.Persen) AS Amount 
208

209
	FROM dbo.PHRPA0004 AS PA4
210

211
	INNER JOIN @TWTBase AS TWTBase
212

213
		ON TWTBase.WageType = PA4.WageType
214

215
	WHERE PA4.StartDate <= @EndDateLast AND PA4.EndDate >= @EndDateLast
216

217
		AND PA4.EmployeeID = TWTBase.EmployeeID
218

219
	GROUP BY  PA4.StartDate ,PA4.EndDate ,PA4.EmployeeID ,TWTBase.WageTypeMD
220

221
END
222

223

224

225
SELECT DISTINCT EmployeeID ,WageType ,ISNULL(CONVERT(DECIMAL(22,2),SUM(Amount)/12),0) AS Amount
226

227
FROM @TWTPayroll
228

229
GROUP BY EmployeeID ,WageType
230

231

(8-8/9)