Project

General

Profile

Bug #2894 » PRPTFILOTRIALBALANCE_202407251437.sql

Tri Rizqiaty, 07/25/2024 02:38 PM

 
1
??ALTER PROCEDURE [dbo].[PRPTFILOTRIALBALANCE]
2

3
(
4

5
	@CompanyID VARCHAR(20)
6

7
	,@FiscalYear VARCHAR(20)
8

9
	,@FiscalPeriod VARCHAR(20)
10

11
	,@BusinessUnit VARCHAR(20)
12

13
	,@CostCenter VARCHAR(20)
14

15
	,@UserID VARCHAR(20)
16

17
)
18

19
AS
20

21

22

23
--DECLARE @CompanyID VARCHAR(20) = '1000'
24

25
--DECLARE @FiscalYear VARCHAR(20) = '2024'
26

27
--DECLARE @FiscalPeriod VARCHAR(20) = '01'
28

29

30

31
DECLARE @FiscalYearLast VARCHAR(20) = dbo.fn_formatdatetime((DATEADD(dd, -1, CONVERT(DATETIME,(@FiscalYear+ @FiscalPeriod + '01')))),'yyyy')
32

33
DECLARE @FiscalPeriodLast VARCHAR(20) = dbo.fn_formatdatetime((DATEADD(dd, -1, CONVERT(DATETIME,(@FiscalYear+ @FiscalPeriod + '01')))),'mm')
34

35
DECLARE @PeriodDesc VARCHAR(200) = dbo.fn_formatdatetime((DATEADD(dd, -1, DATEADD(m, 1,(CONVERT(DATETIME,(@FiscalYear+ @FiscalPeriod + '01')))))),'dd mmmm yyyy')
36

37

38

39
DECLARE @TBalanceAmount TABLE
40

41
(
42

43
	AccountSubTyp VARCHAR(20)
44

45
	,AccountClass VARCHAR(20)
46

47
	,GlAccoundDesc VARCHAR(250)
48

49
	,GlAccountID VARCHAR(20)
50

51
	,AmountDebet DECIMAL(30,0)
52

53
	,AmountCredit DECIMAL(30,0)
54

55
	,AmountDebetCompCurr DECIMAL(30,0)
56

57
	,AmountCreditCompCurr DECIMAL(30,0)
58

59
	,AmountBalanceCompCurr DECIMAL(30,0)
60

61
	,AmountBalanceCompCurrReport DECIMAL(30,0)
62

63
)
64

65

66

67
DECLARE @TBalance TABLE
68

69
(
70

71
	AccountSubTyp VARCHAR(20)
72

73
	,AccountClass VARCHAR(20)
74

75
	,GlAccoundDesc VARCHAR(250)
76

77
	,GlAccountID VARCHAR(20)
78

79
	,AmountDebet DECIMAL(30,0)
80

81
	,AmountCredit DECIMAL(30,0)
82

83
	,AmountDebetCompCurr DECIMAL(30,0)
84

85
	,AmountCreditCompCurr DECIMAL(30,0)
86

87
	,AmountBalanceCompCurr DECIMAL(30,0)
88

89
	,AmountBalanceCompCurrReport DECIMAL(30,0)
90

91
)
92

93

94

95
INSERT INTO @TBalanceAmount
96

97
SELECT coagl.AccountSubTyp
98

99
		,coagl.AccountClass
100

101
		,coagl.Description AS GlAccoundDesc
102

103
		,accbal.GlAccount
104

105
		,ISNULL(accbal.AmountDebet, 0) AS AmountDebet
106

107
		,ISNULL(accbal.AmountCredit, 0) AS AmountCredit
108

109
		,ISNULL(accbal.AmountDebetCompCurr, 0) AS AmountDebetCompCurr
110

111
		,ISNULL(accbal.AmountCreditCompCurr, 0) AS AmountCreditCompCurr
112

113
		,ISNULL(accbal.AmountBalanceCompCurr,0) AS AmountBalanceCompCurr
114

115
		,CASE 
116

117
			WHEN coagl.AccountSubTyp = 'AS' THEN ISNULL(accbal.AmountBalanceCompCurr,0)
118

119
			WHEN coagl.AccountSubTyp = 'LI' THEN CASE WHEN coagl.AccountClass = 'EQX' THEN (ISNULL(accbal.AmountBalanceCompCurr,0)) ELSE (ISNULL(accbal.AmountBalanceCompCurr,0)) * -1 END
120

121
			WHEN coagl.AccountSubTyp = 'IN' THEN (ISNULL(accbal.AmountBalanceCompCurr,0)) * -1
122

123
			WHEN coagl.AccountSubTyp = 'EX' THEN ISNULL(accbal.AmountBalanceCompCurr,0)
124

125
		 END AS AmountBalanceCompCurrReport
126

127
FROM dbo.PTRFICOMPACCBALANCE AS accbal
128

129
LEFT JOIN dbo.PCMFILOCOAGL AS coagl
130

131
	ON coagl.GlAccount = accbal.GlAccount
132

133
WHERE (accbal.CompanyID = @CompanyID OR @CompanyID = '')
134

135
	AND accbal.FiscalYear = @FiscalYearLast
136

137
	AND accbal.FiscalPeriod = @FiscalPeriodLast
138

139

140

141
INSERT INTO @TBalance
142

143
SELECT coagl.AccountSubTyp
144

145
		,coagl.AccountClass		
146

147
		,coagl.Description AS GlAccoundDesc
148

149
		,coagl.GlAccount
150

151
		,ISNULL(SUM(accbal.AmountDebet), 0) AS AmountDebet
152

153
		,ISNULL(SUM(accbal.AmountCredit), 0) AS AmountCredit
154

155
		,ISNULL(SUM(accbal.AmountDebetCompCurr), 0) AS AmountDebetCompCurr
156

157
		,ISNULL(SUM(accbal.AmountCreditCompCurr), 0) AS AmountCreditCompCurr
158

159
		,ISNULL(SUM(accbal.AmountBalanceCompCurr),0) AS AmountBalanceCompCurr
160

161
		,ISNULL(SUM(accbal.AmountBalanceCompCurrReport), 0) AS AmountBalanceCompCurrReport
162

163
FROM dbo.PCMFILOCOAGL AS coagl
164

165
LEFT JOIN @TBalanceAmount AS accbal
166

167
	ON coagl.GlAccount = accbal.GlAccountID
168

169
GROUP BY coagl.AccountSubTyp
170

171
		,coagl.AccountClass		
172

173
		,coagl.Description
174

175
		,coagl.GlAccount
176

177

178

179

180

181
DECLARE @TPosting TABLE
182

183
(
184

185
	AccountSubTyp VARCHAR(20)
186

187
	,GlAccoundDesc VARCHAR(250)
188

189
	,GlAccountID VARCHAR(20)
190

191
	,AmountDebet DECIMAL(30,0)
192

193
	,AmountCredit DECIMAL(30,0)
194

195
	,AmountDebetCompCurr DECIMAL(30,0)
196

197
	,AmountCreditCompCurr DECIMAL(30,0)
198

199
)
200

201

202

203
INSERT INTO @TPosting	
204

205
SELECT coagl.AccountSubTyp
206

207
		,coagl.Description
208

209
		,posting.GlAccountID
210

211
		,SUM(posting.AmountDebet)
212

213
		,SUM(posting.AmountCredit)
214

215
		,SUM(posting.AmountDebetCompCurr)
216

217
		,SUM(posting.AmountCreditCompCurr)
218

219
		----,posting.AmountDebet,posting.AmountCredit,posting.AmountDebetCompCurr,posting.AmountCreditCompCurr
220

221
FROM dbo.PTRFILOPOSTING AS posting
222

223
LEFT JOIN dbo.PCMFILOCOAGL AS coagl
224

225
	ON posting.GlAccountID = coagl.GlAccount
226

227
WHERE posting.FiscalYear = @FiscalYear
228

229
	AND posting.FiscalPeriod = @FiscalPeriod
230

231
GROUP BY coagl.AccountSubTyp
232

233
		,coagl.Description
234

235
		,posting.GlAccountID
236

237

238

239
SELECT tb.*
240

241
		,CASE 
242

243
			WHEN tb.AccountSubTyp = 'AS' THEN ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountDebetCompCurr,0) - ISNULL(tp.AmountCreditCompCurr,0)
244

245
			WHEN tb.AccountSubTyp = 'LI' THEN CASE WHEN tb.AccountClass = 'EQX' THEN (ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountCreditCompCurr,0) + ISNULL(tp.AmountDebetCompCurr,0)) ELSE (ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountCreditCompCurr,0) - ISNULL(tp.AmountDebetCompCurr,0)) * -1 END
246

247
			WHEN tb.AccountSubTyp = 'IN' THEN (ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountCreditCompCurr,0) - ISNULL(tp.AmountDebetCompCurr,0)) * -1
248

249
			WHEN tb.AccountSubTyp = 'EX' THEN ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountDebetCompCurr,0) - ISNULL(tp.AmountCreditCompCurr,0)
250

251
		 END AmountBalanceNew
252

253

254

255
		 ,ISNULL(tp.AmountDebetCompCurr,0) AS AmountDebetNew
256

257
		 ,ISNULL(tp.AmountCreditCompCurr,0) AS AmountCreditNew
258

259
		--,CASE 
260

261
		--	WHEN tb.AccountSubTyp = 'AS' THEN ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountDebetCompCurr,0) - ISNULL(tp.AmountCreditCompCurr,0)
262

263
		--	WHEN tb.AccountSubTyp = 'EX' THEN ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountDebetCompCurr,0) - ISNULL(tp.AmountCreditCompCurr,0)
264

265
		--	ELSE 0
266

267
		-- END AmountDebetNew
268

269
		--,CASE 
270

271
		--	WHEN tb.AccountSubTyp = 'LI' THEN ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountCreditCompCurr,0) - ISNULL(tp.AmountDebetCompCurr,0) 
272

273
		--	WHEN tb.AccountSubTyp = 'IN' THEN ISNULL(tb.AmountBalanceCompCurr,0) + ISNULL(tp.AmountCreditCompCurr,0) - ISNULL(tp.AmountDebetCompCurr,0)
274

275
		--	ELSE 0
276

277
		-- END AmountCreditNew
278

279
		,@PeriodDesc AS PeriodDesc
280

281
		----,tp.AmountDebet,tp.AmountCredit,tp.AmountDebetCompCurr,tp.AmountCreditCompCurr
282

283
FROM @TBalance AS tb
284

285
LEFT JOIN @TPosting AS tp
286

287
	ON tp.AccountSubTyp = tb.AccountSubTyp
288

289
	AND tp.GlAccountID = tb.GlAccountID
290

291

292

293

(2-2/2)