Project

General

Profile

Bug #2265 » CBIATMTOPYCPOTARIF_20230622.sql

Tri Rizqiaty, 06/22/2023 03:51 PM

 
1
??USE [MinovaES_BIA_Dev]
2

3
GO
4

5
/****** Object:  StoredProcedure [dbo].[CBIATMTOPYCPOTARIF]    Script Date: 22/06/2023 15.51.00 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER PROCEDURE [dbo].[CBIATMTOPYCPOTARIF]
16

17
(
18

19
	@EmployeeID VARCHAR(20)
20

21
	,@StartDate VARCHAR(20)
22

23
	,@EndDate VARCHAR(20)
24

25
)
26

27
AS
28

29

30

31
--DECLARE @EmployeeID VARCHAR(20) = '22053147'
32

33
--DECLARE @StartDate VARCHAR(20) = '20230501'
34

35
--DECLARE @EndDate VARCHAR(20) = '20230531'
36

37

38

39
DECLARE @CTARIF TABLE
40

41
(
42

43
	EmployeeID VARCHAR(20)
44

45
	,DayFlag VARCHAR(20)
46

47
	,TipeCPO VARCHAR(20)
48

49
	,PKS01 DECIMAL(22,2)
50

51
	,PKS02 DECIMAL(22,2)
52

53
	,PKS03 DECIMAL(22,2)
54

55
)
56

57

58

59
INSERT INTO @CTARIF
60

61
SELECT @EmployeeID, 'WORK', * FROM (SELECT TipeCPO, FieldName, TarifHariKerja FROM CTARIFCPO ) AS SourceTable
62

63
PIVOT (MAX(TarifHariKerja)
64

65
FOR FieldName IN
66

67
(	[PKS01],
68

69
	[PKS02],
70

71
	[PKS03])
72

73
) AS pvt
74

75

76

77
INSERT INTO @CTARIF
78

79
SELECT @EmployeeID, 'FREE', * FROM (SELECT TipeCPO, FieldName, TarifHariLibur FROM CTARIFCPO ) AS SourceTable
80

81
PIVOT (MAX(TarifHariLibur)
82

83
FOR FieldName IN
84

85
(	[PKS01],
86

87
	[PKS02],
88

89
	[PKS03])
90

91
) AS pvt
92

93

94

95
DECLARE @TWSCal TABLE
96

97
(
98

99
	DateSpecified VARCHAR(20)
100

101
	,DayFlag VARCHAR(50)
102

103
)
104

105

106

107
INSERT INTO @TWSCal
108

109
SELECT DISTINCT pa125.StartDate, dt.Flag
110

111
FROM dbo.PHRPA0125 AS pa125 WITH (NOLOCK)
112

113
LEFT JOIN dbo.PHRTMDAYTYP AS dt
114

115
	ON dt.DayType = pa125.DayType
116

117
WHERE pa125.EmployeeID = @EmployeeID
118

119
AND (pa125.StartDate BETWEEN @StartDate AND @EndDate)
120

121
AND dt.Flag = 'WORK'
122

123

124

125

126

127
IF( (SELECT COUNT(DateSpecified) FROM @TWSCal) = 0 ) 
128

129
BEGIN
130

131
	INSERT INTO @TWSCal
132

133
	SELECT DISTINCT wscal.DateSpecified, dt.Flag
134

135
	FROM dbo.PHRPA0025 AS pa25 WITH (NOLOCK)
136

137
	LEFT JOIN dbo.PHRTMWSCAL AS wscal WITH (NOLOCK)
138

139
		ON wscal.WorkScheduleType = pa25.WorkScheduleType
140

141
	LEFT JOIN dbo.PHRTMDAYTYP AS dt
142

143
		ON wscal.DayType = dt.DayType
144

145
	WHERE pa25.EmployeeID = @EmployeeID
146

147
		AND (pa25.StartDate <= @EndDate AND pa25.EndDate >= @EndDate)
148

149
		AND (wscal.DateSpecified BETWEEN @StartDate AND @EndDate)
150

151
END
152

153

154

155
DECLARE @TblResult TABLE
156

157
(
158

159
	EmployeeID VARCHAR(20)
160

161
	,CPORett VARCHAR(20)
162

163
	,CPO2Rett VARCHAR(20)
164

165
	,CPO3Rett VARCHAR(20)
166

167
	,StartDate VARCHAR(20)
168

169
	,TarifCPORett DECIMAL(22,0)
170

171
	,TarifCPO2Rett DECIMAL(22,0)
172

173
	,TarifCPO3Rett DECIMAL(22,0)
174

175
)
176

177

178

179
INSERT INTO @TblResult
180

181
SELECT DISTINCT md.EmployeeID, md.CPORett,  md.CPO2Rett,  md.CPO3Rett, md.StartDate
182

183
				,CONVERT(DECIMAL(22,2),CONVERT(DECIMAL(18,2),CASE WHEN md.PKS1Tersus <> '' THEN md.PKS1Tersus ELSE 0 END) * tarif.PKS01) AS TarifCPORett
184

185
				,CONVERT(DECIMAL(22,2),CONVERT(DECIMAL(18,2),CASE WHEN md.PKS2Tersus <> '' THEN md.PKS2Tersus ELSE 0 END) * tarif.PKS02) AS TarifCPO2Rett
186

187
				,CONVERT(DECIMAL(22,2),CONVERT(DECIMAL(18,2),CASE WHEN md.PKS3Tersus <> '' THEN md.PKS3Tersus ELSE 0 END) * tarif.PKS03) AS TarifCPO3Rett
188

189
FROM dbo.CUSPYDATACPO  AS md 
190

191
LEFT JOIN @TWSCal AS twscal
192

193
	ON md.StartDate = twscal.DateSpecified
194

195
LEFT JOIN @CTARIF AS tarif
196

197
	ON md.EmployeeID = tarif.EmployeeID
198

199
	AND twscal.DayFlag = tarif.DayFlag
200

201
WHERE  md.EmployeeID = @EmployeeID
202

203
	AND (md.StartDate BETWEEN @StartDate AND @EndDate)
204

205
	AND (md.NoUnit = tarif.TipeCPO)
206

207

208

209
SELECT DISTINCT EmployeeID
210

211
				,SUM(TarifCPORett) AS TarifCPORett
212

213
				,SUM(TarifCPO2Rett) AS TarifCPO2Rett
214

215
				,SUM(TarifCPO3Rett) AS TarifCPO3Rett
216

217
FROM @TblResult
218

219
GROUP BY EmployeeID
(2-2/2)