Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 203 additions & 0 deletions src/Layers/BE/Tests/Report/ERMFinancialReports.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ codeunit 134982 "ERM Financial Reports"
PostingGroupDetailsLedEntryErr: Label 'Posting group should be populated from customer ledger entry';
MissingGLEntryErr: Label 'No %1 exists for account %2 and source currency %3.', Comment = '%1 = Table Caption, %2 = G/L Account No., %3 = Source Currency Code';
CloseIncomeAmountMismatchErr: Label 'Close Income Statement %1 mismatch for source currency %2. Expected %3, actual %4.', Comment = '%1 = Field Caption, %2 = Source Currency Code, %3 = Expected Amount, %4 = Actual Amount';
CloseIncomeLineCountErr: Label 'Close Income Statement must create one consolidated line for account %1 when no dimensions are selected. Expected %2, actual %3.', Comment = '%1 = G/L Account No., %2 = Expected line count, %3 = Actual line count';
CloseIncomeLineAmountErr: Label 'The consolidated Close Income Statement line amount must equal the negated sum of the posted G/L entry amounts.';

[Test]
[HandlerFunctions('RHDetailTrialBalance')]
Expand Down Expand Up @@ -1542,6 +1544,168 @@ codeunit 134982 "ERM Financial Reports"
UpdateCurOnGeneralLedgerSetup(AdditionalReportingCurrency);
end;

[Test]
[HandlerFunctions('MessageHandler,ConfirmHandler,CloseIncomeStatementRequestPageHandler')]
procedure CloseIncomeStatementConsolidatesEntriesPerGLAccountWithoutARC()
var
GLAccount: Record "G/L Account";
BalGLAccount: Record "G/L Account";
GenJournalLine: Record "Gen. Journal Line";
GenJournalBatch: Record "Gen. Journal Batch";
GenJournalTemplate: Record "Gen. Journal Template";
ClosingGenJournalLine: Record "Gen. Journal Line";
Date: Record Date;
GeneralLedgerSetup: Record "General Ledger Setup";
DocNo: Code[20];
PostingDate: Date;
Amount1: Decimal;
Amount2: Decimal;
Amount3: Decimal;
OldAdditionalReportingCurrency: Code[10];
begin
// [SCENARIO 646076] Close Income Statement consolidates multiple G/L entries into one journal line per account when no dimensions are selected and ARC is not enabled.
Initialize();

// [GIVEN] No dimensions are selected for the Close Income Statement report
ClearSelectedDimensionsForCloseIncomeStatement();

// [GIVEN] Additional Reporting Currency is blank
GeneralLedgerSetup.Get();
OldAdditionalReportingCurrency := GeneralLedgerSetup."Additional Reporting Currency";
if OldAdditionalReportingCurrency <> '' then
UpdateCurOnGeneralLedgerSetup('');

// [GIVEN] Previous Fiscal Year Closed, New Fiscal Year Created and Closed
LibraryFiscalYear.CloseFiscalYear();
ExecuteUIHandler();
LibraryFiscalYear.CreateFiscalYear();
LibraryFiscalYear.CloseFiscalYear();

// [GIVEN] Income Statement G/L Account "A" and a Balance Sheet G/L Account for balancing
LibraryCostAccounting.CreateIncomeStmtGLAccount(GLAccount);
LibraryERM.CreateGLAccount(BalGLAccount);

// [GIVEN] Three posted G/L entries to account "A" with blank dimensions in the closed fiscal year
PostingDate := LibraryFiscalYear.GetLastPostingDate(true);
Amount1 := LibraryRandom.RandIntInRange(100, 500);
Amount2 := LibraryRandom.RandIntInRange(100, 500);
Amount3 := LibraryRandom.RandIntInRange(100, 500);
CreateAndPostGenJnlLineOnPostingDate(GLAccount."No.", BalGLAccount."No.", PostingDate, Amount1);
CreateAndPostGenJnlLineOnPostingDate(GLAccount."No.", BalGLAccount."No.", PostingDate, Amount2);
CreateAndPostGenJnlLineOnPostingDate(GLAccount."No.", BalGLAccount."No.", PostingDate, Amount3);

// [GIVEN] Gen. Journal Template and Batch for close income statement output
LibraryERM.CreateGenJournalTemplate(GenJournalTemplate);
LibraryERM.CreateGenJournalBatch(GenJournalBatch, GenJournalTemplate.Name);
LibraryERM.ClearGenJournalLines(GenJournalBatch);
GenJournalLine.Init();
GenJournalLine."Journal Template Name" := GenJournalBatch."Journal Template Name";
GenJournalLine."Journal Batch Name" := GenJournalBatch.Name;

// [WHEN] Run Close Income Statement with no dimensions selected
Date.SetRange("Period Type", Date."Period Type"::Month);
Date.SetRange("Period Start", PostingDate);
Date.FindFirst();
DocNo := LibraryUtility.GenerateGUID();
RunCloseIncomeStatement(GenJournalLine, NormalDate(Date."Period End"), DocNo);

// [THEN] Exactly one closing Gen. Journal Line exists for account "A"
ClosingGenJournalLine.SetRange("Journal Template Name", GenJournalBatch."Journal Template Name");
ClosingGenJournalLine.SetRange("Journal Batch Name", GenJournalBatch.Name);
ClosingGenJournalLine.SetRange("Document No.", DocNo);
ClosingGenJournalLine.SetRange("Account No.", GLAccount."No.");
Assert.AreEqual(
1, ClosingGenJournalLine.Count,
StrSubstNo(CloseIncomeLineCountErr, GLAccount."No.", 1, ClosingGenJournalLine.Count));

// [THEN] The closing line amount equals the negated sum of posted amounts
ClosingGenJournalLine.FindFirst();
Assert.AreEqual(
-(Amount1 + Amount2 + Amount3), ClosingGenJournalLine.Amount, CloseIncomeLineAmountErr);

// Cleanup: restore Additional Reporting Currency
if OldAdditionalReportingCurrency <> '' then
UpdateCurOnGeneralLedgerSetup(OldAdditionalReportingCurrency);
end;

[Test]
[HandlerFunctions('MessageHandler,ConfirmHandler,CloseIncomeStatementWithRetainedEarningsRequestPageHandler')]
procedure CloseIncomeStatementConsolidatesEntriesPerGLAccountWithARC()
var
GLAccount: Record "G/L Account";
BalGLAccount: Record "G/L Account";
RetainedEarningsGLAccount: Record "G/L Account";
GenJournalLine: Record "Gen. Journal Line";
GenJournalBatch: Record "Gen. Journal Batch";
ClosingGLEntry: Record "G/L Entry";
Date: Record Date;
DocNo: Code[20];
PostingDate: Date;
Amount1: Decimal;
Amount2: Decimal;
Amount3: Decimal;
OldAdditionalReportingCurrency: Code[10];
begin
// [SCENARIO 646077] Close Income Statement consolidates multiple G/L entries into one journal line per account when no dimensions are selected and ARC is enabled.
Initialize();

// [GIVEN] No dimensions are selected for the Close Income Statement report
ClearSelectedDimensionsForCloseIncomeStatement();

// [GIVEN] Additional Reporting Currency is enabled
OldAdditionalReportingCurrency := UpdateCurOnGeneralLedgerSetup(LibraryERM.CreateCurrencyWithRandomExchRates());

// [GIVEN] Previous Fiscal Year Closed, New Fiscal Year Created and Closed
LibraryFiscalYear.CloseFiscalYear();
ExecuteUIHandler();
LibraryFiscalYear.CreateFiscalYear();
LibraryFiscalYear.CloseFiscalYear();

// [GIVEN] Income Statement G/L Account "A", a balancing account and a Retained Earnings account
LibraryCostAccounting.CreateIncomeStmtGLAccount(GLAccount);
LibraryERM.CreateGLAccount(BalGLAccount);
LibraryERM.CreateGLAccount(RetainedEarningsGLAccount);

// [GIVEN] Three posted G/L entries to account "A" with blank dimensions and blank Business Unit Code
PostingDate := LibraryFiscalYear.GetLastPostingDate(true);
Amount1 := LibraryRandom.RandIntInRange(100, 500);
Amount2 := LibraryRandom.RandIntInRange(100, 500);
Amount3 := LibraryRandom.RandIntInRange(100, 500);
CreateAndPostGenJnlLineOnPostingDate(GLAccount."No.", BalGLAccount."No.", PostingDate, Amount1);
CreateAndPostGenJnlLineOnPostingDate(GLAccount."No.", BalGLAccount."No.", PostingDate, Amount2);
CreateAndPostGenJnlLineOnPostingDate(GLAccount."No.", BalGLAccount."No.", PostingDate, Amount3);

// [GIVEN] Gen. Journal Batch with a No. Series, required because with an Additional Reporting Currency
// report 94 posts the closing entries directly instead of inserting journal lines.
LibraryERM.SelectGenJnlBatch(GenJournalBatch);
LibraryERM.ClearGenJournalLines(GenJournalBatch);
GenJournalLine.Init();
GenJournalLine."Journal Template Name" := GenJournalBatch."Journal Template Name";
GenJournalLine."Journal Batch Name" := GenJournalBatch.Name;

// [WHEN] Run Close Income Statement with no dimensions selected
Date.SetRange("Period Type", Date."Period Type"::Month);
Date.SetRange("Period Start", PostingDate);
Date.FindFirst();
DocNo := LibraryUtility.GenerateGUID();
RunCloseIncomeStatementWithRetainedEarnings(GenJournalLine, NormalDate(Date."Period End"), DocNo, RetainedEarningsGLAccount."No.");

// [THEN] Exactly one closing G/L Entry exists for account "A"
ClosingGLEntry.SetRange("G/L Account No.", GLAccount."No.");
ClosingGLEntry.SetRange("Document No.", DocNo);
Assert.AreEqual(
1, ClosingGLEntry.Count,
StrSubstNo(CloseIncomeLineCountErr, GLAccount."No.", 1, ClosingGLEntry.Count));

// [THEN] The consolidated closing entry equals the negated sum of posted amounts
ClosingGLEntry.CalcSums(Amount);
Assert.AreEqual(
-(Amount1 + Amount2 + Amount3), ClosingGLEntry.Amount, CloseIncomeLineAmountErr);

// Cleanup: restore General Ledger Setup
UpdateCurOnGeneralLedgerSetup(OldAdditionalReportingCurrency);
end;

[Test]
[HandlerFunctions('ConfirmHandler,RHReconcileCustandVendAccs')]
procedure ReconcileCustVendAccounts_AfterExchRateAdjustment()
Expand Down Expand Up @@ -2471,6 +2635,19 @@ codeunit 134982 "ERM Financial Reports"
GenJournalLine.FindSet();
end;

local procedure ClearSelectedDimensionsForCloseIncomeStatement()
var
SelectedDimension: Record "Selected Dimension";
AllObj: Record AllObj;
begin
// Other tests in this codeunit leave Selected Dimension records behind for report 94. They would make
// the report close per dimension, which hides defects that only occur when no dimensions are selected.
SelectedDimension.SetRange("User ID", UserId());
SelectedDimension.SetRange("Object Type", AllObj."Object Type"::Report);
SelectedDimension.SetRange("Object ID", Report::"Close Income Statement");
SelectedDimension.DeleteAll();
end;

local procedure RunCloseIncomeStatement(GenJournalLine: Record "Gen. Journal Line"; PostingDate: Date; DocumentNo: Code[20])
begin
// Enqueue values for CloseIncomeStatementRequestPageHandler.
Expand All @@ -2483,6 +2660,19 @@ codeunit 134982 "ERM Financial Reports"
Report.Run(Report::"Close Income Statement");
end;

local procedure RunCloseIncomeStatementWithRetainedEarnings(GenJournalLine: Record "Gen. Journal Line"; PostingDate: Date; DocumentNo: Code[20]; RetainedEarningsAccNo: Code[20])
begin
// Enqueue values for CloseIncomeStatementWithRetainedEarningsRequestPageHandler.
LibraryVariableStorage.Enqueue(PostingDate);
LibraryVariableStorage.Enqueue(GenJournalLine."Journal Template Name");
LibraryVariableStorage.Enqueue(GenJournalLine."Journal Batch Name");
LibraryVariableStorage.Enqueue(DocumentNo);
LibraryVariableStorage.Enqueue(RetainedEarningsAccNo);

Commit(); // commit requires to run report.
Report.Run(Report::"Close Income Statement");
end;

local procedure ExecuteUIHandler()
begin
// Generate Dummy message. Required for executing the test case successfully in ES.
Expand Down Expand Up @@ -2823,6 +3013,19 @@ codeunit 134982 "ERM Financial Reports"
CloseIncomeStatement.OK().Invoke();
end;

[RequestPageHandler]
procedure CloseIncomeStatementWithRetainedEarningsRequestPageHandler(var CloseIncomeStatement: TestRequestPage "Close Income Statement")
begin
// Same as CloseIncomeStatementRequestPageHandler, but supplies a Retained Earnings Account, which
// report 94 requires when an Additional Reporting Currency is set. Dimensions are left blank.
CloseIncomeStatement.FiscalYearEndingDate.SetValue(LibraryVariableStorage.DequeueDate()); // Fiscal Year Ending Date
CloseIncomeStatement.GenJournalTemplate.SetValue(LibraryVariableStorage.DequeueText()); // Gen. Journal Template
CloseIncomeStatement.GenJournalBatch.SetValue(LibraryVariableStorage.DequeueText()); // Gen. Journal Batch
CloseIncomeStatement.DocumentNo.SetValue(LibraryVariableStorage.DequeueText()); // Document No.
CloseIncomeStatement.RetainedEarningsAcc.SetValue(LibraryVariableStorage.DequeueText()); // Retained Earnings Acc.
CloseIncomeStatement.OK().Invoke();
end;

[RequestPageHandler]
procedure AuditTrailReportRequestPageHandler(var AuditTrail: TestRequestPage "Audit Trail")
begin
Expand Down
Loading
Loading