{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit BalanceSheetReportFormat; interface uses DatabaseObjects, GeneralUtilities; procedure FormatBalanceSheetReportDetails (CompanyId : int64; AsAtDate : TDateTime; ReportData : TReportData); implementation uses Classes, SysUtils, DatabaseManager, Globals, Progress, BusinessObjects, AccountsCacheUnit, Utilities; type TLineType = (ltHeading,ltDetail,ltTotal,ltSubtotal); procedure FormatBalanceSheetReportDetails (CompanyId : int64; AsAtDate : TDateTime; ReportData : TReportData); var i : integer; Str : string; Account : TAccount; ReportLayout : TReportLayout; Element : TBusinessObject; AccountBalance : int64; ProcessingGroup : boolean; GroupElement : TReportGroupElement; GroupBalance : int64; SubtotalBalance : int64; TotalBalance : int64; SignAdjustmentFactor : integer; const Margin = ' '; procedure CheckGroup; begin // check to see if there is a group to display if ProcessingGroup then begin Str := Margin; Str := Str + Format(' %-38.38s ',[GroupElement.Text]); Str := Str + Format('%25.25s ',[FormatCurrencyForDisplay(GroupBalance*SignAdjustmentFactor)]); ReportData.WriteLine(Str); SubtotalBalance := SubtotalBalance + GroupBalance; TotalBalance := TotalBalance + GroupBalance; ProcessingGroup := false; end; end; begin ReportLayout := Utilities.ReportLayout(CompanyId,rtBalanceSheet); if ReportLayout = nil then begin ReportData.WriteLine('Missing report layout for this company.'); Exit; end; ProgressForm.SetPosition(0); ProgressForm.SetCaption('Generating balance sheet. Please wait...'); ProgressForm.Show; if ClientCommunicator <> nil then ClientCommunicator.SetServerThreadPriority(tpLowest); try // initialise the flag used to indicate that we are processing a group // of accounts and other variables ProcessingGroup := false; GroupElement := nil; GroupBalance := 0; SubtotalBalance := 0; TotalBalance := 0; SignAdjustmentFactor := -1; ReportData.WriteLine(''); for i := 0 to ReportLayout.Elements.Count - 1 do begin Element := ReportLayout.Elements[i]; if Element is TReportHeadingElement then begin CheckGroup; Str := Margin; Str := Str + TReportHeadingElement(Element).Text; ReportData.WriteLine(''); ReportData.WriteBoldLine(Str); end else if Element is TReportGroupElement then begin CheckGroup; ProcessingGroup := true; GroupElement := TReportGroupElement(Element); GroupBalance := 0; end else if Element is TReportAccountElement then begin // get the account balance for the given date Account := AccountsCache.GetAccount( TReportAccountElement(Element).AccountId); if Account <> nil then begin AccountBalance := Account.BalanceAsAt(AsAtDate); // reverse the sign on debit accounts if Account.Debit then AccountBalance := -AccountBalance; end else AccountBalance := 0; // if we are processing a group then add to group balance if ProcessingGroup and TReportAccountElement(Element).InGroup then GroupBalance := GroupBalance + AccountBalance // otherwise display account balance else begin CheckGroup; Str := Margin; Str := Str + Format(' %-38.38s ',[TReportAccountElement(Element).AccountName]); Str := Str + Format('%25.25s ',[FormatCurrencyForDisplay(AccountBalance*SignAdjustmentFactor)]); ReportData.WriteLine(Str); SubtotalBalance := SubtotalBalance + AccountBalance; TotalBalance := TotalBalance + AccountBalance; end; end else if Element is TReportSubtotalElement then begin CheckGroup; Str := Margin; Str := Str + RepeatChar(' ',50); Str := Str + RepeatChar('-',16); ReportData.WriteLine(Str); Str := Margin; Str := Str + Format(' %-38.38s ',[TReportSubtotalElement(Element).Text]); Str := Str + RepeatChar(' ',17); Str := Str + Format('%25.25s ',[FormatCurrencyForDisplay(SubtotalBalance*SignAdjustmentFactor)]); ReportData.WriteLine(Str); SubtotalBalance := 0; end else if Element is TReportTotalElement then begin CheckGroup; Str := Margin; Str := Str + RepeatChar(' ',67); Str := Str + RepeatChar('-',16); ReportData.WriteLine(Str); Str := Margin; Str := Str + Format('%-40.40s ',[TReportTotalElement(Element).Text]); Str := Str + RepeatChar(' ',17); Str := Str + Format('%25.25s ',[FormatCurrencyForDisplay(TotalBalance*SignAdjustmentFactor)]); ReportData.WriteBoldLine(Str); TotalBalance := 0; SignAdjustmentFactor := 1; Str := Margin; Str := Str + RepeatChar(' ',67); Str := Str + RepeatChar('=',16); ReportData.WriteLine(Str); end else if Element is TReportRetainedEarningsElement then begin // get the retained earnings for the given date AccountBalance := RetainedEarnings(CompanyId,AsAtDate); // if we are processing a group then add to group balance if ProcessingGroup and TReportRetainedEarningsElement(Element).InGroup then GroupBalance := GroupBalance + AccountBalance // otherwise display account balance else begin CheckGroup; Str := Margin; Str := Str + Format(' %-38.38s ',[TReportRetainedEarningsElement(Element).Text]); Str := Str + Format('%25.25s ',[FormatCurrencyForDisplay(AccountBalance*SignAdjustmentFactor)]); ReportData.WriteLine(Str); SubtotalBalance := SubtotalBalance + AccountBalance; TotalBalance := TotalBalance + AccountBalance; end; end; ProgressForm.SetPosition(i * 100 div ReportLayout.Elements.Count); end; finally ProgressForm.Hide; if ClientCommunicator <> nil then ClientCommunicator.SetServerThreadPriority(tpNormal); end; end; end.