Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[releases/24.2] fix bug related to daily no. series lines #1245

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
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
Loading