Skip to content

Commit

Permalink
[releases/24.2] fix bug related to daily no. series lines (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
grobyns authored Jun 5, 2024
1 parent 1f61bf0 commit d578447
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ codeunit 304 "No. Series - Impl."
procedure GetNoSeriesLine(var NoSeriesLine: Record "No. Series Line"; NoSeriesCode: Code[20]; UsageDate: Date; HideErrorsAndWarnings: Boolean): Boolean
var
NoSeriesRec: Record "No. Series";
NoSeriesLine2: Record "No. Series Line";
NoSeries: Codeunit "No. Series";
NoSeriesErrorsImpl: Codeunit "No. Series - Errors Impl.";
#if not CLEAN24
Expand All @@ -163,12 +164,21 @@ codeunit 304 "No. Series - Impl."
UsageDate := WorkDate();

// Find the No. Series Line closest to the usage date
NoSeriesLine.Reset();
NoSeriesLine.SetCurrentKey("Series Code", "Starting Date");
NoSeriesLine.SetRange("Series Code", NoSeriesCode);
NoSeriesLine.SetRange("Starting Date", 0D, UsageDate);
NoSeriesLine.SetRange(Open, true);
if (NoSeriesLine."Line No." <> 0) and (NoSeriesLine."Series Code" = NoSeriesCode) then begin
NoSeriesLine2.Reset();
NoSeriesLine2.SetCurrentKey("Series Code", "Starting Date");
NoSeriesLine2.SetRange("Series Code", NoSeriesCode);
NoSeriesLine2.SetRange("Starting Date", 0D, UsageDate);
NoSeriesLine2.SetRange(Open, true);
#if not CLEAN24
#pragma warning disable AL0432
NoSeriesManagement.RaiseObsoleteOnNoSeriesLineFilterOnBeforeFindLast(NoSeriesLine2);
#pragma warning restore AL0432
#endif
if NoSeriesLine2.FindLast() then;
NoSeriesLine.CopyFilters(NoSeriesLine2);

if (NoSeriesLine."Line No." <> 0) and (NoSeriesLine."Series Code" = NoSeriesCode) and (NoSeriesLine."Starting Date" = NoSeriesLine2."Starting Date") then begin
NoSeriesLine.CopyFilters(NoSeriesLine2);
NoSeriesLine.SetRange("Line No.", NoSeriesLine."Line No.");
#if not CLEAN24
#pragma warning disable AL0432
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ codeunit 134531 "No. Series Batch Tests"
Any: Codeunit Any;
LibraryAssert: Codeunit "Library Assert";
LibraryNoSeries: Codeunit "Library - No. Series";
CannotAssignNewErr: Label 'You cannot assign new numbers from the number series %1.', Comment = '%1=No. Series Code';
CannotAssignNewErr: Label 'You cannot assign new numbers from the number series %1', Comment = '%1=No. Series Code';

#region sequence
[Test]
Expand Down Expand Up @@ -919,6 +919,139 @@ codeunit 134531 "No. Series Batch Tests"
LibraryAssert.AreEqual('2', NoSeriesBatch2.GetNextNo(TempNoSeriesLine, WorkDate()), 'GetNextNo with temporary record returned wrong value');
end;

[Test]
procedure TestDailyNoSeriesBeforeFirstStartDateError()
var
NoSeriesBatch: Codeunit "No. Series - Batch";
NoSeriesCode: Code[20];
begin
// [SCENARIO] When setting up a No. Series Line with a new start date each day, ensure that the correct number is returned for each day
Initialize();

// [GIVEN] A No Series with 3 lines starting on 3 different days
NoSeriesCode := CopyStr(UpperCase(Any.AlphabeticText(MaxStrLen(NoSeriesCode))), 1, MaxStrLen(NoSeriesCode));
LibraryNoSeries.CreateNoSeries(NoSeriesCode);
CreateDailyNoSeriesLines(NoSeriesCode);

// [WHEN] We get the next number for a day before the first starting date
asserterror NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate() - 1);
// [THEN] an error is returned
LibraryAssert.ExpectedError(StrSubstNo(CannotAssignNewErr, NoSeriesCode));
end;

[Test]
procedure TestDailyNoSeries()
var
NoSeriesBatch: Codeunit "No. Series - Batch";
NoSeriesCode: Code[20];
NextNo: Code[20];
begin
// [SCENARIO] When setting up a No. Series Line with a new start date each day, ensure that the correct number is returned for each day
Initialize();

// [GIVEN] A No Series with 3 lines starting on 3 different days
NoSeriesCode := CopyStr(UpperCase(Any.AlphabeticText(MaxStrLen(NoSeriesCode))), 1, MaxStrLen(NoSeriesCode));
LibraryNoSeries.CreateNoSeries(NoSeriesCode);
CreateDailyNoSeriesLines(NoSeriesCode);

// [WHEN] We get the next number for the first starting date
NextNo := NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate());
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-001', WorkDate()), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the second starting date
NextNo := NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate() + 1);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-001', WorkDate() + 1), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the first starting date
NextNo := NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate());
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-002', WorkDate()), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the second starting date
NextNo := NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate() + 1);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-002', WorkDate() + 1), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the third starting date
NextNo := NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate() + 2);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-001', WorkDate() + 2), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the third starting date
NextNo := NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate() + 2);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-002', WorkDate() + 2), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the second starting date
NextNo := NoSeriesBatch.GetNextNo(NoSeriesCode, WorkDate() + 1);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-003', WorkDate() + 1), NextNo, 'Number was not as expected');
end;

#if not CLEAN24
#pragma warning disable AL0432
[Test]
procedure TestLegacyDailyNoSeries()
var
NoSeriesManagement: Codeunit NoSeriesManagement;
NoSeriesCode: Code[20];
NextNo: Code[20];
begin
// [SCENARIO] When setting up a No. Series Line with a new start date each day, ensure that the correct number is returned for each day
Initialize();

// [GIVEN] A No Series with 3 lines starting on 3 different days
NoSeriesCode := CopyStr(UpperCase(Any.AlphabeticText(MaxStrLen(NoSeriesCode))), 1, MaxStrLen(NoSeriesCode));
LibraryNoSeries.CreateNoSeries(NoSeriesCode);
CreateDailyNoSeriesLines(NoSeriesCode);

// [WHEN] We get the next number for the first starting date
NextNo := NoSeriesManagement.GetNextNo(NoSeriesCode, WorkDate(), true);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-001', WorkDate()), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the second starting date
NextNo := NoSeriesManagement.GetNextNo(NoSeriesCode, WorkDate() + 1, true);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-001', WorkDate() + 1), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the first starting date
NextNo := NoSeriesManagement.GetNextNo(NoSeriesCode, WorkDate(), true);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-002', WorkDate()), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the second starting date
NextNo := NoSeriesManagement.GetNextNo(NoSeriesCode, WorkDate() + 1, true);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-002', WorkDate() + 1), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the third starting date
NextNo := NoSeriesManagement.GetNextNo(NoSeriesCode, WorkDate() + 2, true);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-001', WorkDate() + 2), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the third starting date
NextNo := NoSeriesManagement.GetNextNo(NoSeriesCode, WorkDate() + 2, true);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-002', WorkDate() + 2), NextNo, 'Number was not as expected');

// [WHEN] We get the next number for the second starting date
NextNo := NoSeriesManagement.GetNextNo(NoSeriesCode, WorkDate() + 1, true);
// [THEN] the correct number is returned
LibraryAssert.AreEqual(StrSubstNo('%1-003', WorkDate() + 1), NextNo, 'Number was not as expected');
end;
#pragma warning restore AL0432
#endif

local procedure CreateDailyNoSeriesLines(NoSeriesCode: Code[20])
begin
LibraryNoSeries.CreateSequenceNoSeriesLine(NoSeriesCode, 1, StrSubstNo('%1-001', WorkDate()), '', WorkDate());
LibraryNoSeries.CreateSequenceNoSeriesLine(NoSeriesCode, 1, StrSubstNo('%1-001', WorkDate() + 1), '', WorkDate() + 1);
LibraryNoSeries.CreateSequenceNoSeriesLine(NoSeriesCode, 1, StrSubstNo('%1-001', WorkDate() + 2), '', WorkDate() + 2);
end;

local procedure Initialize()
begin
Any.SetDefaultSeed();
Expand Down

0 comments on commit d578447

Please sign in to comment.