SELECT
DocNum,
STUFF((SELECT ', ' + CAST(DocEntry AS VARCHAR(MAX)) AS [text()]
FROM RCT2 b
WHERE a.DocNum = b.DocNum
FOR XML PATH('')), 1, 2, '') AS DocEntry
INTO #Invoices
FROM RCT2 a
GROUP BY DocNum
ORDER BY DocNum
SELECT T0.TransId, T0.Account, T0.Debit As Credit, T0.Credit As Debit, T0.ShortName, T0.ContraAct, T5.CardCode As Code, T5.CardName As Name, T0.TaxDate, T0.DueDate, 'Ch.No. ' + Cast(T4.CheckNum As VARchar) + ' Rec. No. ' + Cast(T4.RcptNum AS VARCHAR) + ' ' + IsNull('Invoice No. ' + T3.DocEntry, '') + ' ' + IsNull('Clear Date ' + Convert(VARCHAR, T2.MthDate, 103), '') + IsNull(T0.U_Narration, '') AS U_Narration,
T0.TransType, T0.BaseRef
INTO #Ledger1
FROM JDT1 T0
INNER JOIN OACT T1 ON T0.Account = T1.AcctCode
INNER JOIN JDT1 T2 ON T0.TransId = T2.TransId AND T2.Account = @CashAccount
LEFT JOIN OCHH T4 ON T0.TransId = T4.TransNum AND T0.Ref3Line = T4.CheckNum AND T0.Credit = T4.CheckSum
LEFT JOIN OCRD T5 ON T4.CardCode = T5.CardCode
LEFT JOIN #Invoices T3 ON T4.RcptNum = T3.DocNum
WHERE T0.ContraAct = @CashAccount
AND (T0.ShortName NOT LIKE 'S%' AND T0.ShortName NOT LIKE 'C%')
AND T0.TaxDate BETWEEN @Start AND @Finish
AND T0.TransType = 25
UNION ALL
SELECT T0.TransId, T0.Account, T0.Debit As Credit, T0.Credit As Debit, T0.ShortName, T0.ContraAct, T1.AcctCode As Code, T1.AcctName As Name, T0.TaxDate, T0.DueDate, T0.U_Narration, T0.TransType, T0.BaseRef
FROM JDT1 T0
INNER JOIN OACT T1 ON T0.Account = T1.AcctCode
WHERE T0.ContraAct = @CashAccount
AND (T0.ShortName NOT LIKE 'S%' AND T0.ShortName NOT LIKE 'C%')
AND T0.TaxDate BETWEEN @Start AND @Finish
AND T0.TransType <> 25
UNION
ALL
SELECT T0.TransId, T0.Account, T0.Debit As Credit, T0.Credit As Debit, T0.ShortName, T0.ContraAct, T1.CardCode As Code, T1.CardName As Name, T0.TaxDate, T0.DueDate, T0.U_Narration, T0.TransType, T0.BaseRef
FROM JDT1 T0
INNER JOIN OCRD T1 ON T0.ShortName = T1.CardCode
WHERE T0.ContraAct = @CashAccount
AND (T0.ShortName LIKE 'S%' OR T0.ShortName LIKE 'C%')
AND T0.TaxDate BETWEEN @Start AND @Finish
SELECT T0.Account, Sum(T0.Debit) As SumDebit, Sum(T0.Credit) As SumCredit
INTO #OpeningBalances
FROM JDT1 T0
WHERE T0.TaxDate BETWEEN @YearStart AND DateAdd(dd, -1, @Start)
AND T0.Account = @CashAccount
GROUP BY T0.Account
SELECT 0 As TCode, 0 As TransId, T0.Account, T1.AcctName, T1.GroupMask, T0.SumDebit - T0.SumCredit As Debit, 0 As Credit, 'Dr.' As BalType, '' As Code, '' As Name, '' As TaxDate, '' As DueDate, '' As U_Narration, T1.AcctCode As Code1, T1.AcctName As Name1, 0 As TransType, '' As BaseRef
FROM #OpeningBalances T0
INNER JOIN OACT T1 ON T0.Account = T1.AcctCode
WHERE T1.GroupMask = 1
Or T1.GroupMask = 5
UNION
ALL
SELECT 0 As TCode, 0 As TransId, T0.Account, T1.AcctName, T1.GroupMask, 0 As Debit, T0.SumCredit - T0.SumDebit As Credit, 'Cr.' As BalType, '' As Code, '' As Name, '' As TaxDate, '' As DueDate, '' As U_Narration, T1.AcctCode As Code1, T1.AcctName As Name1, 0 As TransType, '' As BaseRef
FROM #OpeningBalances T0
INNER JOIN OACT T1 ON T0.Account = T1.AcctCode
WHERE T1.GroupMask = 2
Or T1.GroupMask = 4
UNION
ALL
SELECT 1 As TCode, T0.TransId, T0.Account, T1.AcctName, T1.GroupMask, T0.Debit, T0.Credit, '' As BalType, T0.Code, T0.Name, T0.TaxDate, T0.DueDate, T0.U_Narration, T1.AcctCode As Code1, T1.AcctName As Name1, T0.TransType, T0.BaseRef
FROM #Ledger1 T0
INNER JOIN OACT T1 ON T0.Account = T1.AcctCode
Regards
Kennedy