-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAccountSuccinctly_CreateProcs.sql
145 lines (132 loc) · 3.71 KB
/
AccountSuccinctly_CreateProcs.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Code: Accounting Succinctly from SyncFusion
Purpose: Journal processing stored procedures
*/
IF Object_id('AddTransaction') is not null
DROP PROCEDURE [dbo].AddTransaction
GO
CREATE PROCEDURE [dbo].AddTransaction
(
@AcctList VARCHAR(1000), -- Comma Separated: Format is AcctNum|D or C|Amount,
@JrnlType CHAR(2) ='GJ'
)
AS
BEGIN
SET NOCOUNT ON
-- Split the parameter into a table
DECLARE @TransTable TABLE (AccoutNum VARCHAR(12),ID INT,DC CHAR(1),amt MONEY)
INSERT INTO @TransTable
SELECT * FROM [dbo].TransToTable(@AcctList)
-- Validate all accounts, return -1 if any invalid accounts
DECLARE @nCtr INT
SELECT @nCtr = COUNT(*) FROM @TransTable WHERE ID <0
IF (@nCtr >0 )
BEGIN
-- Optionally, could raise an error
PRINT 'Missing account numbers'
RETURN -1
END
-- Validate Debits = Credits, return -2 if not
DECLARE @DebitTot MONEY
DECLARE @CreditTot MONEY
SELECT @DebitTot = SUM(amt) FROM @TransTable WHERE DC='D'
SELECT @CreditTot = SUM(amt) FROM @TransTable WHERE DC='C'
IF (@DebitTot <> @CreditTot )
BEGIN
-- Optionally, could raise an error
PRINT 'Debits <> Credits'
RETURN -2
END
-- Post the transaction into journals
BEGIN TRANSACTION
DECLARE @nNext INT
SELECT @nNext = IsNull(max(transNum)+1,1) FROM [dbo].Journals WHERE jrnlType=@JrnlType
INSERT INTO [dbo].Journals (AccountID,JrnlType,TransNum,DC,Amount)
SELECT ID,@JrnlType,@nNext,DC,amt
FROM @TransTable
COMMIT
RETURN 0
END
GO
IF Object_id('TransToTable') is not null
DROP FUNCTION [dbo].TransToTable
GO
CREATE Function [dbo].TransToTable
(@AcctList VARCHAR(1000) )
RETURNS
@RowTable TABLE
( AcctNumber VARCHAR(12),
Jrnl_Account_ID INT,
DebitCredit CHAR(1),
Amt MONEY
)
AS
BEGIN
DECLARE @X INT
DECLARE @Y INT
DECLARE @OneLine VARCHAR(30)
DECLARE @acctNUM VARCHAR(12)
DECLARE @DebCred CHAR(1)
DECLARE @TransAmt MONEY
SET @AcctList=@AcctList+','
SET @x = CHARINDEX(',',@AcctList)
WHILE @x >0
BEGIN
SET @OneLine = LEFT(@AcctList,@x-1)
SET @AcctList = RTRIM(SUBSTRING(@AcctList,@x+1,9999))
if LEN(@OneLine) > 0
begin
SET @Y = CHARINDEX('|',@OneLine)
SET @AcctNum = LEFT(@OneLine,@y-1)
SET @DebCred = SUBSTRING(@OneLine,@y+1,1)
SET @OneLine = RTRIM(SUBSTRING(@OneLine,@y+3,9999))
SET @TransAmt = CAST(@OneLine AS MONEY)
INSERT INTO @RowTable VALUES (@AcctNum,-1,@DebCred,@TransAmt)
end
UPDATE @rowTable SET Jrnl_Account_ID = xx.id
FROM (select id,accountNum FROM [dbo].chart_of_accounts) xx
WHERE xx.accountNum=AcctNumber
SET @x = CHARINDEX(',',@AcctList)
END
RETURN
END
GO
IF Object_id('PostTransaction') is not null
DROP PROCEDURE [dbo].PostTransaction
GO
CREATE PROCEDURE [dbo].PostTransaction( @TransNumb INT = 0 )
AS
BEGIN
SET NOCOUNT ON
UPDATE [dbo].Chart_of_Accounts SET Balance = Balance +xx.PostAmt
FROM
(
SELECT AccountID,
Sum(
CASE WHEN jl.dc='D' THEN amount ELSE -1*amount END
) as PostAmt
FROM [dbo].Journals jl
JOIN [dbo].Chart_of_Accounts ca on jl.AccountID=ca.id
WHERE jl.posted='N' AND (Transnum = @TransNumb or @TransNumb=0) AND ca.AcctType in ('A','E')
GROUP BY AccountID
) xx
WHERE xx.accountID=ID
UPDATE [dbo].Chart_of_Accounts SET Balance = Balance +xx.PostAmt
FROM
(
SELECT AccountID,
Sum(
CASE WHEN jl.dc='C' THEN amount ELSE -1*amount END
) as PostAmt
FROM [dbo].Journals jl
JOIN [dbo].Chart_of_Accounts ca on jl.AccountID=ca.id
WHERE jl.posted='N' AND (Transnum = @TransNumb or @TransNumb=0) AND ca.AcctType in ('L','O','R')
GROUP BY AccountID
) xx
WHERE xx.accountID=ID
UPDATE [dbo].Journals SET posted='Y',PostDate=getDate() WHERE posted='N' AND (Transnum = @TransNumb or @TransNumb=0)
END
GO
IF Object_id('ClosingEntry') is not null
DROP PROCEDURE [dbo].ClosingEntry
GO