{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit CashbookStatementReportFormat; interface uses DatabaseObjects, GeneralUtilities; procedure FormatCashbookStatementReportDetails (CashbookId : int64; UseBeginPeriod : boolean; BeginPeriodDate : TDateTime; UseEndPeriod : boolean; EndPeriodDate : TDateTime; ReportData : TReportData); implementation uses SysUtils, DatabaseManager, Globals, Progress, BusinessObjects, CashbooksCacheUnit; type TLineType = (ltHeading,ltDetail,ltOpeningBalance,ltClosingBalance); procedure FormatCashbookStatementReportDetails (CashbookId : int64; UseBeginPeriod : boolean; BeginPeriodDate : TDateTime; UseEndPeriod : boolean; EndPeriodDate : TDateTime; ReportData : TReportData); var i : integer; Cashbook : TCashbook; CashbookEntry : TCashbookEntry; procedure PrintLine (LineType : TLineType; CashbookEntry : TCashbookEntry; StatementOnly : boolean); var Str : string; Balance : int64; Indicator : string; begin // first determine balance if it is a balance line if LineType = ltOpeningBalance then begin if not UseBeginPeriod then Balance := 0 else Balance := Cashbook.BalanceAsAt(BeginPeriodDate-1,StatementOnly); end else if LineType = ltClosingBalance then begin if not UseEndPeriod then Balance := Cashbook.TotalBalance(StatementOnly) else Balance := Cashbook.BalanceAsAt(EndPeriodDate,StatementOnly); end else Balance := 0; // if it is a detail line create indicator to show unreconciled entries if (CashbookEntry <> nil) and (not CashbookEntry.OnStatement) then Indicator := '*' else Indicator := ''; // now assemble details of line to output Str := ''; // print date if LineType = ltHeading then Str := Str + 'Date ' else if LineType = ltDetail then Str := Str + Format('%-8.8s ',[ShortFormatDate(CashbookEntry.Date)]) else if LineType = ltOpeningBalance then begin if UseBeginPeriod and (not StatementOnly) then Str := Str + Format('%-8.8s ',[ShortFormatDate(BeginPeriodDate)]) else Str := Str + ' '; end else if LineType = ltClosingBalance then begin if UseEndPeriod and (not StatementOnly) then Str := Str + Format('%-8.8s ',[ShortFormatDate(EndPeriodDate)]) else Str := Str + ' '; end; // print description if LineType = ltHeading then Str := Str + 'Description ' else if LineType = ltDetail then begin if CashbookEntry.Description <> '' then Str := Str + Format('%-38.38s ',[CashbookEntry.Description]) // if no description then show other account name else Str := Str + Format('%-38.38s ',[CashbookEntry.OtherAccountName]); end else if LineType = ltOpeningBalance then begin if StatementOnly then Str := Str + Format('%-38.38s ',['Statement Opening Balance']) else Str := Str + Format('%-38.38s ',['Opening Balance']); end else if LineType = ltClosingBalance then begin if StatementOnly then Str := Str + Format('%-38.38s ',['Statement Closing Balance']) else Str := Str + Format('%-38.38s ',['Closing Balance']); end; // print debit if LineType = ltHeading then Str := Str + ' Debit ' else if LineType = ltDetail then begin if CashbookEntry.Debit then Str := Str + Format('%14s ',[Indicator + FormatCurrencyForDisplay(CashbookEntry.AbsoluteAmount)]) else Str := Str + ' '; end else if LineType in [ltOpeningBalance,ltClosingBalance] then begin if Balance > 0 then Str := Str + Format('%14s ',[FormatCurrencyForDisplay(Abs(Balance))]) else if Balance <> 0 then Str := Str + ' ' else Str := Str + ' 0.00 '; end; // print credit if LineType = ltHeading then Str := Str + ' Credit ' else if LineType = ltDetail then begin if CashbookEntry.Credit then Str := Str + Format('%14s ',[Indicator + FormatCurrencyForDisplay(CashbookEntry.AbsoluteAmount)]) else Str := Str + ' '; end else if LineType in [ltOpeningBalance,ltClosingBalance] then begin if Balance > 0 then Str := Str + ' ' else if Balance <> 0 then Str := Str + Format('%14s ',[FormatCurrencyForDisplay(Abs(Balance))]) else Str := Str + ' '; end; // print other account if LineType = ltHeading then Str := Str + ' Other ' else if LineType = ltDetail then Str := Str + Format(' %5.5s ',[CashbookEntry.OtherAccountAbbreviation]) else Str := Str + ' '; // output completed line if LineType = ltHeading then ReportData.WriteBoldLine(Str) else if LineType = ltDetail then ReportData.WriteLine(Str) else if LineType = ltOpeningBalance then ReportData.WriteBoldLine(Str) else if LineType = ltClosingBalance then ReportData.WriteBoldLine(Str); end; begin Cashbook := CashbooksCache.GetCashbook(CashbookId); if Cashbook = nil then begin ReportData.WriteLine('Cashbook not found'); Exit; end; ProgressForm.SetPosition(0); ProgressForm.SetCaption('Generating cashbook statement. Please wait...'); ProgressForm.Show; try // display headings PrintLine(ltHeading,nil,false); // show opening balances PrintLine(ltOpeningBalance,nil,false); PrintLine(ltOpeningBalance,nil,true); // show transactions in period for i := 0 to Cashbook.CashbookEntries.Count - 1 do begin CashbookEntry := TCashbookEntry(Cashbook.CashbookEntries[i]); if UseBeginPeriod and (CashbookEntry.Date < BeginPeriodDate) then Continue; if UseEndPeriod and (CashbookEntry.Date > EndPeriodDate) then Break; PrintLine(ltDetail,CashbookEntry,false); ProgressForm.SetPosition(i * 100 div Cashbook.CashbookEntries.Count); end; // show closing balances PrintLine(ltClosingBalance,nil,false); PrintLine(ltClosingBalance,nil,true); finally ProgressForm.Hide; end; end; end.