Project

General

Profile

Bug #1203 » Product_GetBudgetActualMonthly_20220621.sql

Tri Rizqiaty, 06/29/2022 10:00 AM

 
1
??USE [MinovaES_Bank_Kalteng_Dev]
2

3
GO
4

5
/****** Object:  UserDefinedFunction [dbo].[Product_GetBudgetActualMonthly]    Script Date: 21/06/2022 10.10.05 ******/
6

7
SET ANSI_NULLS ON
8

9
GO
10

11
SET QUOTED_IDENTIFIER ON
12

13
GO
14

15
ALTER FUNCTION [dbo].[Product_GetBudgetActualMonthly] -- SELECT * FROM dbo.[GetBudgetActualMonthly]('100', '2010', '01', '12', 'scriberion', 'triadblank')
16

17
(	
18

19
	-- Add the parameters for the function here
20

21
	@year VARCHAR(4),
22

23
	@month_awal VARCHAR(2),
24

25
	@month_akhir VARCHAR(2),
26

27
	@CostCenter VARCHAR(20),
28

29
	@EmployeeID VARCHAR(20)
30

31
)
32

33
RETURNS TABLE
34

35
AS
36

37
RETURN 
38

39
(
40

41
	
42

43
	--DECLARE @year VARCHAR(4) = '2022'
44

45
	--DECLARE @month_awal VARCHAR(2) = '01'
46

47
	--DECLARE @month_akhir VARCHAR(2) = '12'
48

49
	--DECLARE @CostCenter VARCHAR(20) = ''
50

51
	--DECLARE @EmployeeID VARCHAR(20) = ''
52

53

54

55
	
56

57
	-- Add the SELECT statement with parameter references here
58

59
	SELECT header.CostCenter
60

61
			, wt_def.BudgetAcc 
62

63
			,cg.RelationshipObject AS cc_group	
64

65
			,eg.RelationshipObject AS ce_group
66

67
			,CASE
68

69
			WHEN wt_def.Regular <> 'Y'
70

71
				THEN (SUM( CAST(dbo.GetPEN(pay_month.Amount) AS DECIMAL) + 
72

73
					  CAST(ISNULL(dbo.GetPEN(pay_month_retro.Amount), '0') AS DECIMAL) )) 
74

75
			ELSE (SUM(CAST(dbo.GetPEN(pay_month.Amount) AS DECIMAL) + 
76

77
					  CAST(ISNULL(dbo.GetPEN(pay_month_retro.Amount), '0') AS DECIMAL) ) * (12/MAX(header.RunPeriodMonth)))
78

79
			END
80

81
			AS 'amount'
82

83
	FROM dbo.PHRPYTR0300 header
84

85
	INNER JOIN dbo.PHRPYTR0301 pay_month 
86

87
		ON header.EmployeeID = pay_month.EmployeeID
88

89
		   AND header.PayPeriodMonth = pay_month.PayPeriodMonth
90

91
		   AND header.PayPeriodYear = pay_month.PayPeriodYear
92

93
		   AND header.RunPeriodMonth = pay_month.RunPeriodMonth
94

95
		   AND header.RunPeriodYear = pay_month.RunPeriodYear
96

97
		   AND header.SplitIndicator = pay_month.SplitIndicator
98

99
	LEFT OUTER JOIN dbo.PHRPYTR0301RET pay_month_retro
100

101
		ON pay_month_retro.EmployeeID = pay_month.EmployeeID
102

103
		   AND pay_month_retro.PayPeriodMonth = pay_month.PayPeriodMonth
104

105
		   AND pay_month_retro.PayPeriodYear = pay_month.PayPeriodYear
106

107
		   AND pay_month_retro.RunPeriodMonth = pay_month.RunPeriodMonth
108

109
		   AND pay_month_retro.RunPeriodYear = pay_month.RunPeriodYear
110

111
		   AND pay_month_retro.SplitIndicator = pay_month.SplitIndicator
112

113
		   AND pay_month_retro.WageType = pay_month.WageType
114

115
	INNER JOIN dbo.PHRPYCU0300 wt_def
116

117
		ON pay_month.WageType = wt_def.WageTypeDefinition
118

119
		LEFT OUTER JOIN
120

121
		(SELECT assignment.EmployeeID, om_p.CostElement
122

123
		FROM dbo.PHRPA0002 assignment
124

125
		INNER JOIN dbo.PHROM0006 om_p 
126

127
			ON om_p.ObjectID = assignment.Position
128

129
		WHERE (om_p.StartDate <= (@year + '1231') AND om_p.EndDate >= (@year + '1231')) AND
130

131
			  (assignment.StartDate <= (@year + '1231') AND assignment.EndDate >= (@year + '1231'))
132

133
		)ce_on_emp
134

135
		ON header.EmployeeID = ce_on_emp.EmployeeID
136

137
	LEFT JOIN dbo.PHROM0002 AS cg
138

139
		ON cg.ObjectClass = 'CC' AND cg.RelationshipClass = 'CG'
140

141
		AND cg.ObjectID = header.CostCenter
142

143
		AND cg.StartDate <= (@year + '1231') AND cg.EndDate >= (@year + '1231')
144

145
	LEFT JOIN dbo.PHROM0002 AS eg
146

147
		ON eg.ObjectClass = 'CE' AND eg.RelationshipClass = 'EG'
148

149
		AND eg.ObjectID = wt_def.BudgetAcc
150

151
		AND eg.StartDate <= (@year + '1231') AND eg.EndDate >= (@year + '1231') 
152

153
	WHERE header.PayPeriodYear = @year
154

155
		AND header.RunPeriodYear = @year
156

157
		AND wt_def.StartDate <= (@year + '1231')
158

159
		AND wt_def.EndDate >= (@year + '1231')
160

161
		--AND wt_def.IncludeToBudget = '1'
162

163
		AND wt_def.BudgetAcc <> ''
164

165
		AND header.PayPeriodMonth = header.RunPeriodMonth
166

167
		AND header.RunPeriodMonth >= @month_awal
168

169
		AND header.RunPeriodMonth <= @month_akhir
170

171
		AND (header.CostCenter = @CostCenter OR @CostCenter = '')
172

173
		AND (header.EmployeeID = @EmployeeID OR @EmployeeID = '')
174

175
	GROUP BY header.CostCenter, wt_def.BudgetAcc, wt_def.Regular ,cg.RelationshipObject ,eg.RelationshipObject 
176

177
)
178

179

(8-8/21)