0% found this document useful (0 votes)
12 views4 pages

Cash Journal

Casg

Uploaded by

Amarnarh A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Cash Journal

Casg

Uploaded by

Amarnarh A
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE OR ALTER PROCEDURE [Report].

[CashJournal]
(
@EntityID INT
, @StartDate DATETIME2
, @EndDate DATETIME2
, @StatementFormatName VARCHAR(100)
, @IncludeFund BIT = 0
)
AS
BEGIN

DECLARE
@superEntityID INT
,@statementID INT
,@initDate DATETIME2(0)
,@IsClosedMonth BIT

SET @superEntityID = (SELECT super_entity_id FROM gl_entities WHERE


GL_Entity_ID = @EntityID)
SET @statementID = (SELECT TOP 1 Statement_Format FROM statement_formats
WHERE Statement_Name = @StatementFormatName)
SET @initDate = '1900-01-01 00:00:00'
SET @IsClosedMonth =
CASE
WHEN (SELECT MONTH(Fiscal_Year_End) FROM gl_entities WHERE
GL_Entity_ID = @EntityID) = MONTH(@StartDate)
THEN 1
ELSE 0
END

DECLARE @tmpAccts TABLE


(
GroupNumber INT
,LedgerGroup INT
,LineNumber INT
,LineType VARCHAR(20)
,LowAccount VARCHAR(20)
,HighAccount VARCHAR(20)
,Header VARCHAR(255)
,GLAccountID INT
,AccountNumber VARCHAR(20)
,AccountDescription VARCHAR(255)
,ChartOfAccountsID INT
,CoADescription VARCHAR(255)
,SubAccountID INT
,SubAccount VARCHAR(50)
,SubDescription VARCHAR(255)
,IsReversed BIT
)

INSERT INTO @tmpAccts


(
GroupNumber
,LedgerGroup
,LineNumber
,LineType
,LowAccount
,HighAccount
,Header
,GLAccountID
,AccountNumber
,AccountDescription
,ChartOfAccountsID
,CoADescription
,SubAccountID
,SubAccount
,SubDescription
,IsReversed
)
EXEC usp_rpt_GetFinStmtAccounts @statementID, @superEntityID;

;WITH CTE_StatementAccounts AS
(
SELECT DISTINCT
SuperEntityID = @superEntityID
,GLEntityID = @EntityID
,EntityNumber = (SELECT EntityNumber FROM Entity.EntityInfo WHERE
GlEntityID = @EntityID)
,EntityName = (SELECT EntityName FROM Entity.EntityInfo WHERE
GlEntityID = @EntityID)
,ChartOfAccountID = ChartOfAccountsID
,ChartDescription = CoADescription
,GLAccountID
,GLAccountNumber = AccountNumber
,GLAccountDescription = AccountDescription
,SubAccountID
,SubAccountNumber = SubAccount
,SubAccountDescription = SubDescription
FROM @tmpAccts tmp
WHERE
ChartOfAccountsID <> 0
)

/*
RETRIEVE THE BEGINNING BALANCE FOR EACH CHART LISTED ABOVE
*/
,CTE_OpenBalance AS
(
SELECT
acbs.SuperEntityID
,acbs.GLEntityID
,acbs.ChartOfAccountID
,acbs.GLAccountID
,acbs.SubAccountID
,Fund = ISNULL(ff.[Name],'')
,Balance = ISNULL(SUM(jeli.Debit) - SUM(jeli.Credit),0)
FROM CTE_StatementAccounts acbs
LEFT JOIN journal_entry_line_items jeli on acbs.ChartOfAccountID
= jeli.Chart_of_Accounts_ID and acbs.GLEntityID = jeli.GL_Entity_ID and
acbs.SubAccountID = CASE WHEN acbs.SubAccountID > 0 THEN jeli.GL_Sub_Account_ID
ELSE 0 END
LEFT JOIN journal_entries je on je.Journal_Entry_ID =
jeli.Journal_Entry_ID and acbs.SuperEntityID = je.Super_Entity_ID and
je.Journal_Entry_Status_ID = 2
LEFT JOIN journal_entry_types jet on je.Journal_Entry_Type_ID =
jet.Journal_Entry_Type_ID
LEFT JOIN Fund.Fund ff on jeli.FundID = ff.FundID and
@IncludeFund = 1
WHERE
jeli.Active = 1
and je.GL_Date < @StartDate
GROUP BY
acbs.SuperEntityID
,acbs.GLEntityID
,acbs.ChartOfAccountID
,acbs.GLAccountID
,acbs.SubAccountID
,ISNULL(ff.[Name],'')
)

/*
FIRST DATA SET - OPENING BALANCES FOR ALL CHARTS
*/
SELECT DISTINCT
acbs.SuperEntityID
,acbs.GLEntityID
,acbs.EntityNumber
,acbs.EntityName
,acbs.ChartOfAccountID
,acbs.ChartDescription
,acbs.GLAccountID
,acbs.GLAccountNumber
,acbs.SubAccountID
,acbs.SubAccountNumber
,acbs.SubAccountDescription
,Fund = isnull(ob.Fund,'')
,Balance = ISNULL(SUM(ob.Balance),0.0)
INTO #CashJournalOpenBalances
FROM CTE_StatementAccounts acbs
LEFT JOIN CTE_OpenBalance ob on acbs.ChartOfAccountID =
ob.ChartOfAccountID and acbs.SubAccountID = ob.SubAccountID
GROUP BY
acbs.SuperEntityID
,acbs.GLEntityID
,acbs.EntityNumber
,acbs.EntityName
,acbs.ChartOfAccountID
,acbs.ChartDescription
,acbs.GLAccountID
,acbs.GLAccountNumber
,acbs.SubAccountID
,acbs.SubAccountNumber
,acbs.SubAccountDescription
,ob.Fund

;WITH CTE_EndingBalance AS
(
SELECT
ChartOfAccountsID = jeli.Chart_of_Accounts_ID
,SubAccountID = jeli.GL_Sub_Account_ID
,Fund = ISNULL(ff.[Name],'')
,Increases = SUM(jeli.Debit)
,Decreases = SUM(jeli.Credit) * -1
FROM #CashJournalOpenBalances cjob
LEFT JOIN journal_entry_line_items jeli on cjob.ChartOfAccountID
= jeli.Chart_of_Accounts_ID and cjob.SubAccountID = CASE WHEN cjob.SubAccountID > 0
THEN jeli.GL_Sub_Account_ID ELSE 0 END
LEFT JOIN journal_entries je on jeli.Journal_Entry_ID =
je.Journal_Entry_ID and je.Super_Entity_ID = @superEntityID
LEFT JOIN Fund.Fund ff on jeli.FundID = ff.FundID
WHERE
jeli.Active = 1
and je.Journal_Entry_Status_ID = 2
and jeli.GL_Entity_ID = @EntityID
and je.GL_Date BETWEEN @StartDate and @EndDate
GROUP BY
jeli.Chart_of_Accounts_ID
,jeli.GL_Sub_Account_ID
,ISNULL(ff.[Name],'')
,cjob.ChartDescription
,je.Journal_Entry_Status_ID
)

/*
GET FINAL RESULT SET
*/
SELECT
AccountDescription = RTRIM(LTRIM(cjob.ChartDescription)) +
CASE
WHEN LTRIM(RTRIM(cjob.SubAccountDescription)) = ''
THEN ''
ELSE ' - ' + RTRIM(LTRIM(cjob.SubAccountDescription))
END
,AccountNumber = RTRIM(LTRIM(cjob.GLAccountNumber)) +
CASE
WHEN cjob.SubAccountID = 0
THEN ''
ELSE ' ' + RTRIM(LTRIM(cjob.SubAccountNumber))
END
,ChartOfAccountsID = cjob.ChartOfAccountID
,SubAccountID = cjob.SubAccountID
,BeginningBalance = cjob.Balance
,Increases = ISNULL(cteeb.Increases,0.0)
,Decreases = ISNULL(cteeb.Decreases,0.0)
,Fund = ISNULL(cteeb.Fund,'')
,EndingBalance = (cjob.Balance + ISNULL(cteeb.Increases,0.0) +
ISNULL(cteeb.Decreases,0.0))
FROM #CashJournalOpenBalances cjob
LEFT JOIN CTE_EndingBalance cteeb on cjob.ChartOfAccountID =
cteeb.ChartOfAccountsID and cjob.SubAccountID = CASE WHEN cjob.SubAccountID > 0
THEN cteeb.SubAccountID ELSE 0 END and cjob.Fund = cteeb.Fund
WHERE
NOT
(
cjob.Balance = 0.0
and ISNULL(cteeb.Increases,0.0) = 0.0
and ISNULL(cteeb.Decreases,0.0) = 0.0
and (cjob.Balance + ISNULL(cteeb.Increases,0.0) +
ISNULL(cteeb.Decreases,0.0)) = 0.0
)

OPTION (RECOMPILE)
END
GO

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy