diff --git a/src/Apps/W1/Quality Management/Test Library/src/QltyInspectionUtility.Codeunit.al b/src/Apps/W1/Quality Management/Test Library/src/QltyInspectionUtility.Codeunit.al index 14c1d2a042..4fc0d0cb84 100644 --- a/src/Apps/W1/Quality Management/Test Library/src/QltyInspectionUtility.Codeunit.al +++ b/src/Apps/W1/Quality Management/Test Library/src/QltyInspectionUtility.Codeunit.al @@ -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"; diff --git a/src/Apps/W1/Quality Management/app/src/Document/QltyInspectionHeader.Table.al b/src/Apps/W1/Quality Management/app/src/Document/QltyInspectionHeader.Table.al index 154bddd992..52d0ca5327 100644 --- a/src/Apps/W1/Quality Management/app/src/Document/QltyInspectionHeader.Table.al +++ b/src/Apps/W1/Quality Management/app/src/Document/QltyInspectionHeader.Table.al @@ -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; diff --git a/src/Apps/W1/Quality Management/test/src/QltyTestsMisc.Codeunit.al b/src/Apps/W1/Quality Management/test/src/QltyTestsMisc.Codeunit.al index 781d16aa7f..2c234ffb49 100644 --- a/src/Apps/W1/Quality Management/test/src/QltyTestsMisc.Codeunit.al +++ b/src/Apps/W1/Quality Management/test/src/QltyTestsMisc.Codeunit.al @@ -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