[Bug]: Amount calculation error when posting a sales order.
Describe the issue
When posting a sales order, if currency conversion is required and the last Sales Line is a comment line, a calculation error occurs.
if not IsHandled then
if SalesHeader."Currency Code" <> '' then begin
NoVAT := SalesLine.Amount = SalesLine."Amount Including VAT";
if SalesLine."VAT Calculation Type" = SalesLine."VAT Calculation Type"::"Sales Tax" then
SalesLine."Amount Including VAT" :=
Round(
CurrExchRate.ExchangeAmtFCYToLCY(
SalesHeader.GetUseDate(), SalesHeader."Currency Code",
TotalSalesLine."Amount Including VAT", SalesHeader."Currency Factor") -
CurrExchRate.ExchangeAmtFCYToLCY(
SalesHeader.GetUseDate(), SalesHeader."Currency Code",
BefIncrTotalSalesLineAmtInclVAT, SalesHeader."Currency Factor"))
else
SalesLine."Amount Including VAT" :=
Round(
CurrExchRate.ExchangeAmtFCYToLCY(
SalesHeader.GetUseDate(), SalesHeader."Currency Code",
TotalSalesLine."Amount Including VAT", SalesHeader."Currency Factor")) -
TotalSalesLineLCY."Amount Including VAT";
if NoVAT then
SalesLine.Amount := SalesLine."Amount Including VAT"
else
SalesLine.Amount :=
Round(
CurrExchRate.ExchangeAmtFCYToLCY(
SalesHeader.GetUseDate(), SalesHeader."Currency Code",
TotalSalesLine.Amount, SalesHeader."Currency Factor")) -
TotalSalesLineLCY.Amount;
In our case : When posting a Sales Order in CAD that requires currency conversion to LCY, if the last Sales Line is a comment line, the system produces incorrect LCY amounts for some lines due to rounding and allocation sequence.
Scenario:
Currency: CAD
Currency Factor: 1.36986301369863014
Total Amount Including Tax: 5,525.70 CAD
Line 10000 (SalesTax): 5,017.20 CAD (Amount Incl. Tax)
Line 20000 (SalesTax): 508.50 CAD (Amount Incl. Tax)
Line 30000: Comment line (no VAT)
Observed Calculation:
For Line 10000, LCY Amount Incl. Tax is calculated as: Round(5017.2 / Factor) = 3,662.56
For Line 20000, LCY Amount Incl. Tax is calculated as: Round(5525.7 / Factor - 5017.2 / Factor) = Round(4033.761 - 3662.556) = 371.21
For Line 30000 (comment line), LCY Amount Incl. Tax should be 0, but due to the calculation order, the TotalSalesLineLCY."Amount Including VAT" becomes 4033.77 instead of 4033.76. This number is the result of rounding up caused by summing twice. 4033.76 - 4033.77 = -0.01
Similar differences occur for Amount (excl. tax) fields:
Line 10000: 3,241.20 LCY
Line 20000: 328.50 LCY
Line 30000: Comment line (no VAT) -0.01 LCY (unexpected)
==>cause NoVAT := SalesLine.Amount = SalesLine."Amount Including VAT";
so Line 30000 is setup to NoVAT = True
And the Amount in Line 30000 is set to -0.01 cause
if NoVAT then
SalesLine.Amount := SalesLine."Amount Including VAT"
Impact: This results in an incorrect small negative amount (-0.01 LCY) for the last non-tax line when it’s a comment line, which causes posting validation errors.
Expected behavior
The "amount" and "Amount Including VAT" should be 0 when the sales line type is "comment".
Steps to reproduce
When posting a sales order, if currency conversion is required and the last Sales Line is a comment line, sometimes a calculation error occurs. Cause rounding issue is not happen everytimes.
Additional context
No response
I will provide a fix for a bug
- [x] I will provide a fix for a bug