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
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ codeunit 139940 "Qlty. Inspection Utility"
QltyInspectionCreate.GetCreatedInspection(OutCreatedQltyInspectionHeader);
end;

internal procedure CalculateSampleSizeUsingPercentSource(SamplePercentage: Decimal; SourceQuantityBase: Decimal) SampleSize: Integer
var
QltyInspectionHeader: Record "Qlty. Inspection Header";
QltyInspectionTemplateHdr: Record "Qlty. Inspection Template Hdr.";
begin
CreateABasicTemplateAndInstanceOfAInspection(QltyInspectionHeader, QltyInspectionTemplateHdr);

QltyInspectionTemplateHdr.Validate("Sample Source", QltyInspectionTemplateHdr."Sample Source"::"Percent of Quantity");
QltyInspectionTemplateHdr.Validate("Sample Percentage", SamplePercentage);
QltyInspectionTemplateHdr.Modify(true);

QltyInspectionHeader.Validate("Source Quantity (Base)", SourceQuantityBase);
QltyInspectionHeader.Modify(true);

exit(QltyInspectionHeader."Sample Size");
end;

internal procedure CreateTemplate(var OutQltyInspectionTemplateHdr: Record "Qlty. Inspection Template Hdr."; HowManyFields: Integer)
var
IgnoredQltyTest: Record "Qlty. Test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1492,15 +1492,24 @@ table 20405 "Qlty. Inspection Header"
local procedure UpdateSampleSize()
var
QltyInspectionTemplateHdr: Record "Qlty. Inspection Template Hdr.";
CalculatedSampleSize: Decimal;
MaxSampleSize: Integer;
begin
if not QltyInspectionTemplateHdr.Get(Rec."Template Code") then
exit;

MaxSampleSize := 2147483647; // Maximum value of an Integer field; protects the "Sample Size" field against overflow.

case QltyInspectionTemplateHdr."Sample Source" of
QltyInspectionTemplateHdr."Sample Source"::"Fixed Quantity":
Rec.Validate("Sample Size", QltyInspectionTemplateHdr."Sample Fixed Amount");
QltyInspectionTemplateHdr."Sample Source"::"Percent of Quantity":
Rec.Validate("Sample Size", Round(Rec."Source Quantity (Base)" * QltyInspectionTemplateHdr."Sample Percentage" / 100.0, 1, '>'));
begin
CalculatedSampleSize := Round(Rec."Source Quantity (Base)" * QltyInspectionTemplateHdr."Sample Percentage" / 100.0, 1, '>');
if CalculatedSampleSize > MaxSampleSize then
CalculatedSampleSize := MaxSampleSize;
Rec.Validate("Sample Size", CalculatedSampleSize);
end;
end;
end;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ codeunit 139964 "Qlty. Tests - Misc."
LibraryAssert.AreEqual(1234567890.99, Max, 'thousands separator decimal max');
end;

[Test]
procedure SampleSizeCalculationDoesNotOverflowForLargeSourceQuantity()
var
SampleSize: Integer;
begin
// [SCENARIO] Calculating the sample size from a percentage of a very large source quantity does not overflow the Integer "Sample Size" field.

Initialize();

// [GIVEN] A template using "Percent of Quantity" at 100% and an inspection whose source quantity exceeds the maximum Integer value
// [WHEN] The sample size is recalculated from the source quantity
SampleSize := QltyInspectionUtility.CalculateSampleSizeUsingPercentSource(100, 3000000000.0);

// [THEN] The sample size is clamped to the maximum Integer value instead of raising an overflow error
LibraryAssert.AreEqual(2147483647, SampleSize, 'Sample size should be clamped to the maximum integer value to avoid overflow.');
end;

[Test]
procedure GetArbitraryMaximumRecursion()
begin
Expand Down