Project

General

Profile

Feature #1374 » Product_GetBudgetActualMonthly_20220621.sql

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

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

3
(	
4

5
	-- Add the parameters for the function here
6

7
	@year VARCHAR(4),
8

9
	@month_awal VARCHAR(2),
10

11
	@month_akhir VARCHAR(2),
12

13
	@CostCenter VARCHAR(20),
14

15
	@EmployeeID VARCHAR(20)
16

17
)
18

19
RETURNS TABLE
20

21
AS
22

23
RETURN 
24

25
(
26

27
	
28

29
	--DECLARE @year VARCHAR(4) = '2022'
30

31
	--DECLARE @month_awal VARCHAR(2) = '01'
32

33
	--DECLARE @month_akhir VARCHAR(2) = '12'
34

35
	--DECLARE @CostCenter VARCHAR(20) = ''
36

37
	--DECLARE @EmployeeID VARCHAR(20) = ''
38

39

40

41
	
42

43
	-- Add the SELECT statement with parameter references here
44

45
	SELECT header.CostCenter
46

47
			, wt_def.BudgetAcc 
48

49
			,cg.RelationshipObject AS cc_group	
50

51
			,eg.RelationshipObject AS ce_group
52

53
			,CASE
54

55
			WHEN wt_def.Regular <> 'Y'
56

57
				THEN (SUM( CAST(dbo.GetPEN(pay_month.Amount) AS DECIMAL) + 
58

59
					  CAST(ISNULL(dbo.GetPEN(pay_month_retro.Amount), '0') AS DECIMAL) )) 
60

61
			ELSE (SUM(CAST(dbo.GetPEN(pay_month.Amount) AS DECIMAL) + 
62

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

65
			END
66

67
			AS 'amount'
68

69
	FROM dbo.PHRPYTR0300 header
70

71
	INNER JOIN dbo.PHRPYTR0301 pay_month 
72

73
		ON header.EmployeeID = pay_month.EmployeeID
74

75
		   AND header.PayPeriodMonth = pay_month.PayPeriodMonth
76

77
		   AND header.PayPeriodYear = pay_month.PayPeriodYear
78

79
		   AND header.RunPeriodMonth = pay_month.RunPeriodMonth
80

81
		   AND header.RunPeriodYear = pay_month.RunPeriodYear
82

83
		   AND header.SplitIndicator = pay_month.SplitIndicator
84

85
	LEFT OUTER JOIN dbo.PHRPYTR0301RET pay_month_retro
86

87
		ON pay_month_retro.EmployeeID = pay_month.EmployeeID
88

89
		   AND pay_month_retro.PayPeriodMonth = pay_month.PayPeriodMonth
90

91
		   AND pay_month_retro.PayPeriodYear = pay_month.PayPeriodYear
92

93
		   AND pay_month_retro.RunPeriodMonth = pay_month.RunPeriodMonth
94

95
		   AND pay_month_retro.RunPeriodYear = pay_month.RunPeriodYear
96

97
		   AND pay_month_retro.SplitIndicator = pay_month.SplitIndicator
98

99
		   AND pay_month_retro.WageType = pay_month.WageType
100

101
	INNER JOIN dbo.PHRPYCU0300 wt_def
102

103
		ON pay_month.WageType = wt_def.WageTypeDefinition
104

105
		LEFT OUTER JOIN
106

107
		(SELECT assignment.EmployeeID, om_p.CostElement
108

109
		FROM dbo.PHRPA0002 assignment
110

111
		INNER JOIN dbo.PHROM0006 om_p 
112

113
			ON om_p.ObjectID = assignment.Position
114

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

117
			  (assignment.StartDate <= (@year + '1231') AND assignment.EndDate >= (@year + '1231'))
118

119
		)ce_on_emp
120

121
		ON header.EmployeeID = ce_on_emp.EmployeeID
122

123
	LEFT JOIN dbo.PHROM0002 AS cg
124

125
		ON cg.ObjectClass = 'CC' AND cg.RelationshipClass = 'CG'
126

127
		AND cg.ObjectID = header.CostCenter
128

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

131
	LEFT JOIN dbo.PHROM0002 AS eg
132

133
		ON eg.ObjectClass = 'CE' AND eg.RelationshipClass = 'EG'
134

135
		AND eg.ObjectID = wt_def.BudgetAcc
136

137
		AND eg.StartDate <= (@year + '1231') AND eg.EndDate >= (@year + '1231') 
138

139
	WHERE header.PayPeriodYear = @year
140

141
		AND header.RunPeriodYear = @year
142

143
		AND wt_def.StartDate <= (@year + '1231')
144

145
		AND wt_def.EndDate >= (@year + '1231')
146

147
		--AND wt_def.IncludeToBudget = '1'
148

149
		AND wt_def.BudgetAcc <> ''
150

151
		AND header.PayPeriodMonth = header.RunPeriodMonth
152

153
		AND header.RunPeriodMonth >= @month_awal
154

155
		AND header.RunPeriodMonth <= @month_akhir
156

157
		AND (header.CostCenter = @CostCenter OR @CostCenter = '')
158

159
		AND (header.EmployeeID = @EmployeeID OR @EmployeeID = '')
160

161
	GROUP BY header.CostCenter, wt_def.BudgetAcc, wt_def.Regular ,cg.RelationshipObject ,eg.RelationshipObject 
162

163
)
164

165

(4-4/10)