From 1e33fb1cbe86dbde93b67dbbc15694f1b9bc512f Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Wed, 12 Aug 2026 16:53:40 +0530 Subject: [PATCH 1/9] Changes --- .../Utilities/DocumentTotals.Codeunit.al | 30 +++++++ .../Utilities/DocumentTotals.Codeunit.al | 30 +++++++ .../Utilities/DocumentTotals.Codeunit.al | 30 +++++++ .../Tests/ERM/DocumentTotalsPages.Codeunit.al | 86 +++++++++++++++++++ 4 files changed, 176 insertions(+) diff --git a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al index 81934a5b4c2..b2d9a0219cb 100644 --- a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -7,6 +7,7 @@ namespace Microsoft.Utilities; using Microsoft.Finance.Currency; using Microsoft.Finance.GeneralLedger.Setup; using Microsoft.Finance.SalesTax; +using Microsoft.Finance.VAT.Calculation; using Microsoft.Purchases.Document; using Microsoft.Purchases.History; using Microsoft.Purchases.Posting; @@ -607,6 +608,8 @@ codeunit 57 "Document Totals" procedure PurchaseDeltaUpdateTotals(var PurchaseLine: Record "Purchase Line"; var xPurchaseLine: Record "Purchase Line"; var TotalPurchaseLine: Record "Purchase Line"; var VATAmount: Decimal; var InvoiceDiscountAmount: Decimal; var InvoiceDiscountPct: Decimal) var + PurchHeader: Record "Purchase Header"; + GroupedVATAmount: Decimal; InvDiscountBaseAmount: Decimal; IsHandled: Boolean; begin @@ -641,6 +644,13 @@ codeunit 57 "Document Totals" InvoiceDiscountPct := Round(100 * InvoiceDiscountAmount / InvDiscountBaseAmount, 0.00001); end; + if PurchHeader.Get(PurchaseLine."Document Type", PurchaseLine."Document No.") then + if TryGetGroupedVATAmount(PurchHeader, GroupedVATAmount) then + if GroupedVATAmount <> VATAmount then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; + end; + OnAfterPurchDeltaUpdateTotals(PurchaseLine, xPurchaseLine, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct); end; @@ -671,6 +681,7 @@ codeunit 57 "Document Totals" TotalPurchaseLine2: Record "Purchase Line"; PurchaseLineWithReverseChargeVAT: Record "Purchase Line"; VATAmountOfLinesWithRevChargeVAT: Decimal; + GroupedVATAmount: Decimal; IsHandled: Boolean; begin IsHandled := false; @@ -749,12 +760,31 @@ codeunit 57 "Document Totals" VATAmount -= PurchaseLine2.GetNonDeductibleVATAmount(); until PurchaseLine2.Next() = 0; + if TryGetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine2."Amount Including VAT" := TotalPurchaseLine2.Amount + VATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine2."Amount Including VAT"; + end; + OnAfterCalculatePurchaseSubPageTotals( TotalPurchaseHeader, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct, TotalPurchaseLine2); TotalPurchaseLine := TotalPurchaseLine2; end; + local procedure TryGetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean + var + PurchLine: Record "Purchase Line"; + TempVATAmountLine: Record "VAT Amount Line" temporary; + begin + if PurchHeader."No." = '' then + exit(false); + + PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); + GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); + exit(true); + end; + procedure CalculatePostedPurchInvoiceTotals(var PurchInvHeader: Record "Purch. Inv. Header"; var VATAmount: Decimal; PurchInvLine: Record "Purch. Inv. Line") var CurrPurchInvLine: Record "Purch. Inv. Line"; diff --git a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al index 9329f220b22..3041228fd78 100644 --- a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -7,6 +7,7 @@ namespace Microsoft.Utilities; using Microsoft.Finance.Currency; using Microsoft.Finance.GeneralLedger.Setup; using Microsoft.Finance.SalesTax; +using Microsoft.Finance.VAT.Calculation; using Microsoft.Purchases.Document; using Microsoft.Purchases.History; using Microsoft.Purchases.Posting; @@ -614,6 +615,8 @@ codeunit 57 "Document Totals" procedure PurchaseDeltaUpdateTotals(var PurchaseLine: Record "Purchase Line"; var xPurchaseLine: Record "Purchase Line"; var TotalPurchaseLine: Record "Purchase Line"; var VATAmount: Decimal; var InvoiceDiscountAmount: Decimal; var InvoiceDiscountPct: Decimal) var + PurchHeader: Record "Purchase Header"; + GroupedVATAmount: Decimal; InvDiscountBaseAmount: Decimal; IsHandled: Boolean; begin @@ -638,6 +641,13 @@ codeunit 57 "Document Totals" InvoiceDiscountPct := Round(100 * InvoiceDiscountAmount / InvDiscountBaseAmount, 0.00001); end; + if PurchHeader.Get(PurchaseLine."Document Type", PurchaseLine."Document No.") then + if TryGetGroupedVATAmount(PurchHeader, GroupedVATAmount) then + if GroupedVATAmount <> VATAmount then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; + end; + OnAfterPurchDeltaUpdateTotals(PurchaseLine, xPurchaseLine, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct); end; @@ -666,6 +676,7 @@ codeunit 57 "Document Totals" PurchaseHeader: Record "Purchase Header"; PurchaseLine2: Record "Purchase Line"; TotalPurchaseLine2: Record "Purchase Line"; + GroupedVATAmount: Decimal; IsHandled: Boolean; begin IsHandled := false; @@ -723,12 +734,31 @@ codeunit 57 "Document Totals" end; end; + if TryGetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine2."Amount Including VAT" := TotalPurchaseLine2.Amount + VATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine2."Amount Including VAT"; + end; + OnAfterCalculatePurchaseSubPageTotals( TotalPurchaseHeader, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct, TotalPurchaseLine2); TotalPurchaseLine := TotalPurchaseLine2; end; + local procedure TryGetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean + var + PurchLine: Record "Purchase Line"; + TempVATAmountLine: Record "VAT Amount Line" temporary; + begin + if PurchHeader."No." = '' then + exit(false); + + PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); + GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); + exit(true); + end; + procedure CalculatePostedPurchInvoiceTotals(var PurchInvHeader: Record "Purch. Inv. Header"; var VATAmount: Decimal; PurchInvLine: Record "Purch. Inv. Line") var IsHandled: Boolean; diff --git a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al index e36b31e1dbc..6899909ef78 100644 --- a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -7,6 +7,7 @@ namespace Microsoft.Utilities; using Microsoft.Finance.Currency; using Microsoft.Finance.GeneralLedger.Setup; using Microsoft.Finance.SalesTax; +using Microsoft.Finance.VAT.Calculation; using Microsoft.Purchases.Document; using Microsoft.Purchases.History; using Microsoft.Purchases.Posting; @@ -599,6 +600,8 @@ codeunit 57 "Document Totals" procedure PurchaseDeltaUpdateTotals(var PurchaseLine: Record "Purchase Line"; var xPurchaseLine: Record "Purchase Line"; var TotalPurchaseLine: Record "Purchase Line"; var VATAmount: Decimal; var InvoiceDiscountAmount: Decimal; var InvoiceDiscountPct: Decimal) var + PurchHeader: Record "Purchase Header"; + GroupedVATAmount: Decimal; InvDiscountBaseAmount: Decimal; IsHandled: Boolean; begin @@ -623,6 +626,13 @@ codeunit 57 "Document Totals" InvoiceDiscountPct := Round(100 * InvoiceDiscountAmount / InvDiscountBaseAmount, 0.00001); end; + if PurchHeader.Get(PurchaseLine."Document Type", PurchaseLine."Document No.") then + if TryGetGroupedVATAmount(PurchHeader, GroupedVATAmount) then + if GroupedVATAmount <> VATAmount then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; + end; + OnAfterPurchDeltaUpdateTotals(PurchaseLine, xPurchaseLine, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct); end; @@ -651,6 +661,7 @@ codeunit 57 "Document Totals" PurchaseHeader: Record "Purchase Header"; PurchaseLine2: Record "Purchase Line"; TotalPurchaseLine2: Record "Purchase Line"; + GroupedVATAmount: Decimal; IsHandled: Boolean; begin IsHandled := false; @@ -708,12 +719,31 @@ codeunit 57 "Document Totals" end; end; + if TryGetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine2."Amount Including VAT" := TotalPurchaseLine2.Amount + VATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine2."Amount Including VAT"; + end; + OnAfterCalculatePurchaseSubPageTotals( TotalPurchaseHeader, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct, TotalPurchaseLine2); TotalPurchaseLine := TotalPurchaseLine2; end; + local procedure TryGetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean + var + PurchLine: Record "Purchase Line"; + TempVATAmountLine: Record "VAT Amount Line" temporary; + begin + if PurchHeader."No." = '' then + exit(false); + + PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); + GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); + exit(true); + end; + procedure CalculatePostedPurchInvoiceTotals(var PurchInvHeader: Record "Purch. Inv. Header"; var VATAmount: Decimal; PurchInvLine: Record "Purch. Inv. Line") var IsHandled: Boolean; diff --git a/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al b/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al index 0bf48e6a64e..3d0fdd0252b 100644 --- a/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al +++ b/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al @@ -2192,6 +2192,82 @@ codeunit 134344 "Document Totals Pages" Assert.AreEqual(NewDescription, SalesOrder.SalesLines.Description.Value(), LineDescriptionRevertedErr); end; + [Test] + [HandlerFunctions('PurchaseInvoiceStatisticsUpdateVATAmountPageHandler')] + procedure PurchInvTotalInclVATMatchesStatisticsAfterVATAdjMixedVATGroupsSameAccount() + var + PurchaseHeader: Record "Purchase Header"; + PurchaseInvoicePage: TestPage "Purchase Invoice"; + PurchaseLine: Record "Purchase Line"; + VATPostingSetup: array[2] of Record "VAT Posting Setup"; + VATProductPostingGroup: Record "VAT Product Posting Group"; + GLAccountNo: Code[20]; + MaxVATDifference: Decimal; + TotalAmountInclVATBefore: Decimal; + TotalVATAmountBefore: Decimal; + VATAdjustment: Decimal; + begin + // [FEATURE] [UI] [VAT] [Purchase] [VAT Difference] + // [SCENARIO 637288] "Total VAT Amount" and "Total Amount Incl. VAT" on Purchase Invoice subform must update + // after VAT adjustment when invoice has mixed VAT groups on the same G/L Account with negative lines. + Initialize(); + + // [GIVEN] "VAT Difference" is allowed with random Max VAT Difference + MaxVATDifference := LibraryRandom.RandDecInRange(1, 5, 2); + LibraryERM.SetMaxVATDifferenceAllowed(MaxVATDifference); + LibraryPurchase.SetAllowVATDifference(true); + + // [GIVEN] Two VAT Posting Setups with random VAT%, same VAT Bus. Posting Group + LibraryERM.CreateVATPostingSetupWithAccounts( + VATPostingSetup[1], VATPostingSetup[1]."VAT Calculation Type"::"Normal VAT", LibraryRandom.RandIntInRange(5, 15)); + LibraryERM.CreateVATProductPostingGroup(VATProductPostingGroup); + LibraryERM.CreateVATPostingSetup(VATPostingSetup[2], VATPostingSetup[1]."VAT Bus. Posting Group", VATProductPostingGroup.Code); + VATPostingSetup[2].Validate("VAT Calculation Type", VATPostingSetup[2]."VAT Calculation Type"::"Normal VAT"); + VATPostingSetup[2].Validate("VAT %", LibraryRandom.RandIntInRange(16, 25)); + VATPostingSetup[2].Validate("VAT Identifier", VATProductPostingGroup.Code); + VATPostingSetup[2]."Purchase VAT Account" := LibraryERM.CreateGLAccountNo(); + VATPostingSetup[2].Modify(true); + + // [GIVEN] Purchase Invoice with mixed VAT lines on the same G/L Account, including negative adjustment lines + GLAccountNo := LibraryERM.CreateGLAccountWithPurchSetup(); + LibraryPurchase.CreatePurchHeader(PurchaseHeader, PurchaseHeader."Document Type"::Invoice, LibraryPurchase.CreateVendorNo()); + CreatePurchLineWithGLAccAndVATSetup(PurchaseLine, PurchaseHeader, VATPostingSetup[1], GLAccountNo, LibraryRandom.RandDecInRange(1000, 5000, 2)); + CreatePurchLineWithGLAccAndVATSetup(PurchaseLine, PurchaseHeader, VATPostingSetup[1], GLAccountNo, -LibraryRandom.RandDecInRange(100, 500, 2)); + CreatePurchLineWithGLAccAndVATSetup(PurchaseLine, PurchaseHeader, VATPostingSetup[2], GLAccountNo, LibraryRandom.RandDecInRange(1000, 5000, 2)); + CreatePurchLineWithGLAccAndVATSetup(PurchaseLine, PurchaseHeader, VATPostingSetup[2], GLAccountNo, -LibraryRandom.RandDecInRange(100, 900, 2)); + + // [GIVEN] Open Purchase Invoice page and capture totals before adjustment + PurchaseInvoicePage.OpenEdit(); + PurchaseInvoicePage.Filter.SetFilter("No.", PurchaseHeader."No."); + PurchaseInvoicePage.PurchLines.Last(); + TotalVATAmountBefore := PurchaseInvoicePage.PurchLines."Total VAT Amount".AsDecimal(); + TotalAmountInclVATBefore := PurchaseInvoicePage.PurchLines."Total Amount Incl. VAT".AsDecimal(); + + // [WHEN] VAT Amount adjusted on Statistics page (within allowed VAT difference) + VATAdjustment := -LibraryRandom.RandDecInRange(0, MaxVATDifference, 2); + LibraryVariableStorage.Enqueue(VATAdjustment); + PurchaseInvoicePage.PurchaseStatistics.Invoke(); + PurchaseInvoicePage.Close(); + + // [THEN] "Total VAT Amount" on the subform reflects the VAT adjustment + PurchaseInvoicePage.OpenEdit(); + PurchaseInvoicePage.Filter.SetFilter("No.", PurchaseHeader."No."); + PurchaseInvoicePage.PurchLines.Last(); + Assert.AreEqual( + TotalVATAmountBefore + VATAdjustment, + PurchaseInvoicePage.PurchLines."Total VAT Amount".AsDecimal(), + StrSubstNo(VATAmountErr, PurchaseInvoicePage.PurchLines."Total VAT Amount".Caption, 'expected Total VAT Amount')); + + // [THEN] "Total Amount Incl. VAT" on the subform reflects the VAT adjustment + Assert.AreEqual( + TotalAmountInclVATBefore + VATAdjustment, + PurchaseInvoicePage.PurchLines."Total Amount Incl. VAT".AsDecimal(), + StrSubstNo(VATAmountErr, PurchaseInvoicePage.PurchLines."Total Amount Incl. VAT".Caption, 'expected Total Amount Incl. VAT')); + + PurchaseInvoicePage.Close(); + LibraryVariableStorage.AssertEmpty(); + end; + local procedure Initialize() begin LibrarySetupStorage.Restore(); @@ -2461,6 +2537,16 @@ codeunit 134344 "Document Totals Pages" SalesLine.Modify(); end; + local procedure CreatePurchLineWithGLAccAndVATSetup(var PurchaseLine: Record "Purchase Line"; PurchaseHeader: Record "Purchase Header"; VATPostingSetup: Record "VAT Posting Setup"; GLAccountNo: Code[20]; DirectUnitCost: Decimal) + begin + LibraryPurchase.CreatePurchaseLine( + PurchaseLine, PurchaseHeader, PurchaseLine.Type::"G/L Account", GLAccountNo, 1); + PurchaseLine."VAT Bus. Posting Group" := VATPostingSetup."VAT Bus. Posting Group"; + PurchaseLine.Validate("VAT Prod. Posting Group", VATPostingSetup."VAT Prod. Posting Group"); + PurchaseLine.Validate("Direct Unit Cost", DirectUnitCost); + PurchaseLine.Modify(true); + end; + [ConfirmHandler] [Scope('OnPrem')] procedure ConfirmHandlerYes(Question: Text[1024]; var Reply: Boolean) From 3463ef5ce4ac9cf92014c9f898d70042a7bb1730 Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Thu, 13 Aug 2026 11:16:59 +0530 Subject: [PATCH 2/9] Changes --- .../Utilities/DocumentTotals.Codeunit.al | 42 ++++++++++++++----- .../Utilities/DocumentTotals.Codeunit.al | 6 +-- .../Utilities/DocumentTotals.Codeunit.al | 6 +-- .../Tests/ERM/DocumentTotalsPages.Codeunit.al | 2 +- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al index b2d9a0219cb..19440a31ebd 100644 --- a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -645,11 +645,12 @@ codeunit 57 "Document Totals" end; if PurchHeader.Get(PurchaseLine."Document Type", PurchaseLine."Document No.") then - if TryGetGroupedVATAmount(PurchHeader, GroupedVATAmount) then - if GroupedVATAmount <> VATAmount then begin - VATAmount := GroupedVATAmount; - TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; - end; + if not PurchaseDocHasRevChargeOrNonDeductibleVAT(PurchHeader) then + if GetGroupedVATAmount(PurchHeader, GroupedVATAmount) then + if GroupedVATAmount <> VATAmount then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; + end; OnAfterPurchDeltaUpdateTotals(PurchaseLine, xPurchaseLine, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct); end; @@ -681,6 +682,7 @@ codeunit 57 "Document Totals" TotalPurchaseLine2: Record "Purchase Line"; PurchaseLineWithReverseChargeVAT: Record "Purchase Line"; VATAmountOfLinesWithRevChargeVAT: Decimal; + NonDeductibleVATAmount: Decimal; GroupedVATAmount: Decimal; IsHandled: Boolean; begin @@ -758,13 +760,16 @@ codeunit 57 "Document Totals" repeat TotalPurchaseLine2.Amount += PurchaseLine2.GetNonDeductibleVATAmount(); VATAmount -= PurchaseLine2.GetNonDeductibleVATAmount(); + NonDeductibleVATAmount += PurchaseLine2.GetNonDeductibleVATAmount(); until PurchaseLine2.Next() = 0; - if TryGetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin - VATAmount := GroupedVATAmount; - TotalPurchaseLine2."Amount Including VAT" := TotalPurchaseLine2.Amount + VATAmount; - TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine2."Amount Including VAT"; - end; + // Reconcile with grouped (statistics) VAT only when no BE-specific reverse charge or non-deductible VAT adjustments apply + if (VATAmountOfLinesWithRevChargeVAT = 0) and (NonDeductibleVATAmount = 0) then + if GetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin + VATAmount := GroupedVATAmount; + TotalPurchaseLine2."Amount Including VAT" := TotalPurchaseLine2.Amount + VATAmount; + TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine2."Amount Including VAT"; + end; OnAfterCalculatePurchaseSubPageTotals( TotalPurchaseHeader, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct, TotalPurchaseLine2); @@ -772,7 +777,7 @@ codeunit 57 "Document Totals" TotalPurchaseLine := TotalPurchaseLine2; end; - local procedure TryGetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean + local procedure GetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean var PurchLine: Record "Purchase Line"; TempVATAmountLine: Record "VAT Amount Line" temporary; @@ -785,6 +790,21 @@ codeunit 57 "Document Totals" exit(true); end; + local procedure PurchaseDocHasRevChargeOrNonDeductibleVAT(PurchHeader: Record "Purchase Header"): Boolean + var + PurchLine: Record "Purchase Line"; + begin + PurchLine.SetRange("Document Type", PurchHeader."Document Type"); + PurchLine.SetRange("Document No.", PurchHeader."No."); + PurchLine.SetRange("VAT Calculation Type", PurchLine."VAT Calculation Type"::"Reverse Charge VAT"); + if not PurchLine.IsEmpty() then + exit(true); + + PurchLine.SetRange("VAT Calculation Type"); + PurchLine.SetFilter("Non Deductible VAT %", '<>%1', 0); + exit(not PurchLine.IsEmpty()); + end; + procedure CalculatePostedPurchInvoiceTotals(var PurchInvHeader: Record "Purch. Inv. Header"; var VATAmount: Decimal; PurchInvLine: Record "Purch. Inv. Line") var CurrPurchInvLine: Record "Purch. Inv. Line"; diff --git a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al index 3041228fd78..8bbe9e259e0 100644 --- a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -642,7 +642,7 @@ codeunit 57 "Document Totals" end; if PurchHeader.Get(PurchaseLine."Document Type", PurchaseLine."Document No.") then - if TryGetGroupedVATAmount(PurchHeader, GroupedVATAmount) then + if GetGroupedVATAmount(PurchHeader, GroupedVATAmount) then if GroupedVATAmount <> VATAmount then begin VATAmount := GroupedVATAmount; TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; @@ -734,7 +734,7 @@ codeunit 57 "Document Totals" end; end; - if TryGetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin + if GetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin VATAmount := GroupedVATAmount; TotalPurchaseLine2."Amount Including VAT" := TotalPurchaseLine2.Amount + VATAmount; TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine2."Amount Including VAT"; @@ -746,7 +746,7 @@ codeunit 57 "Document Totals" TotalPurchaseLine := TotalPurchaseLine2; end; - local procedure TryGetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean + local procedure GetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean var PurchLine: Record "Purchase Line"; TempVATAmountLine: Record "VAT Amount Line" temporary; diff --git a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al index 6899909ef78..789b0c16e70 100644 --- a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -627,7 +627,7 @@ codeunit 57 "Document Totals" end; if PurchHeader.Get(PurchaseLine."Document Type", PurchaseLine."Document No.") then - if TryGetGroupedVATAmount(PurchHeader, GroupedVATAmount) then + if GetGroupedVATAmount(PurchHeader, GroupedVATAmount) then if GroupedVATAmount <> VATAmount then begin VATAmount := GroupedVATAmount; TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; @@ -719,7 +719,7 @@ codeunit 57 "Document Totals" end; end; - if TryGetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin + if GetGroupedVATAmount(TotalPurchaseHeader, GroupedVATAmount) and (GroupedVATAmount <> VATAmount) then begin VATAmount := GroupedVATAmount; TotalPurchaseLine2."Amount Including VAT" := TotalPurchaseLine2.Amount + VATAmount; TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine2."Amount Including VAT"; @@ -731,7 +731,7 @@ codeunit 57 "Document Totals" TotalPurchaseLine := TotalPurchaseLine2; end; - local procedure TryGetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean + local procedure GetGroupedVATAmount(var PurchHeader: Record "Purchase Header"; var GroupedVATAmount: Decimal): Boolean var PurchLine: Record "Purchase Line"; TempVATAmountLine: Record "VAT Amount Line" temporary; diff --git a/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al b/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al index 3d0fdd0252b..1d7ccc488fc 100644 --- a/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al +++ b/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al @@ -2197,10 +2197,10 @@ codeunit 134344 "Document Totals Pages" procedure PurchInvTotalInclVATMatchesStatisticsAfterVATAdjMixedVATGroupsSameAccount() var PurchaseHeader: Record "Purchase Header"; - PurchaseInvoicePage: TestPage "Purchase Invoice"; PurchaseLine: Record "Purchase Line"; VATPostingSetup: array[2] of Record "VAT Posting Setup"; VATProductPostingGroup: Record "VAT Product Posting Group"; + PurchaseInvoicePage: TestPage "Purchase Invoice"; GLAccountNo: Code[20]; MaxVATDifference: Decimal; TotalAmountInclVATBefore: Decimal; From 91dc07eaf1132c2b4ca388fa39d348cc52e481c6 Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Fri, 14 Aug 2026 16:37:36 +0530 Subject: [PATCH 3/9] changes --- src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al index 789b0c16e70..c5e735d38ef 100644 --- a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -739,6 +739,13 @@ codeunit 57 "Document Totals" if PurchHeader."No." = '' then exit(false); + PurchasesPayablesSetup.Get(); + if not PurchasesPayablesSetup."Allow VAT Difference" then + exit(false); + + if PurchHeader."Tax Liable" then + exit(false); + PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); exit(true); From 485915690b8dbf142fbe8a0c602a2a5898fbec73 Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Fri, 14 Aug 2026 16:42:52 +0530 Subject: [PATCH 4/9] InvokeMi --- src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al | 7 +++++++ src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al index 19440a31ebd..806f2488ed1 100644 --- a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -785,6 +785,13 @@ codeunit 57 "Document Totals" if PurchHeader."No." = '' then exit(false); + PurchasesPayablesSetup.Get(); + if not PurchasesPayablesSetup."Allow VAT Difference" then + exit(false); + + if PurchHeader."Tax Liable" then + exit(false); + PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); exit(true); diff --git a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al index 8bbe9e259e0..3edb67d2b21 100644 --- a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -754,6 +754,13 @@ codeunit 57 "Document Totals" if PurchHeader."No." = '' then exit(false); + PurchasesPayablesSetup.Get(); + if not PurchasesPayablesSetup."Allow VAT Difference" then + exit(false); + + if PurchHeader."Tax Liable" then + exit(false); + PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); exit(true); From e7a9301fd79eb21b7301602978f875229715187e Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Sat, 15 Aug 2026 08:16:14 +0530 Subject: [PATCH 5/9] BB Fix --- src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al b/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al index 1d7ccc488fc..3cbd275e8ca 100644 --- a/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al +++ b/src/Layers/W1/Tests/ERM/DocumentTotalsPages.Codeunit.al @@ -2224,7 +2224,7 @@ codeunit 134344 "Document Totals Pages" LibraryERM.CreateVATPostingSetup(VATPostingSetup[2], VATPostingSetup[1]."VAT Bus. Posting Group", VATProductPostingGroup.Code); VATPostingSetup[2].Validate("VAT Calculation Type", VATPostingSetup[2]."VAT Calculation Type"::"Normal VAT"); VATPostingSetup[2].Validate("VAT %", LibraryRandom.RandIntInRange(16, 25)); - VATPostingSetup[2].Validate("VAT Identifier", VATProductPostingGroup.Code); + VATPostingSetup[2].Validate("VAT Identifier", CopyStr(VATProductPostingGroup.Code, 1, MaxStrLen(VATPostingSetup[2]."VAT Identifier"))); VATPostingSetup[2]."Purchase VAT Account" := LibraryERM.CreateGLAccountNo(); VATPostingSetup[2].Modify(true); From 2c82436d802547d10d769283a2382945d08f52bb Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Tue, 18 Aug 2026 10:08:42 +0530 Subject: [PATCH 6/9] BB Fix --- src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al | 5 +++++ src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al | 5 +++++ src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al index 806f2488ed1..e0d69966eaf 100644 --- a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -794,6 +794,11 @@ codeunit 57 "Document Totals" PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); + + TempVATAmountLine.SetFilter("VAT Difference", '<>%1', 0); + if TempVATAmountLine.IsEmpty() then + exit(false); + exit(true); end; diff --git a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al index 3edb67d2b21..805f6bce3cf 100644 --- a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -763,6 +763,11 @@ codeunit 57 "Document Totals" PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); + + TempVATAmountLine.SetFilter("VAT Difference", '<>%1', 0); + if TempVATAmountLine.IsEmpty() then + exit(false); + exit(true); end; diff --git a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al index c5e735d38ef..1a96a9ea91c 100644 --- a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -748,6 +748,11 @@ codeunit 57 "Document Totals" PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); + + TempVATAmountLine.SetFilter("VAT Difference", '<>%1', 0); + if TempVATAmountLine.IsEmpty() then + exit(false); + exit(true); end; From 2c6198ed69c7463daa7fc4bcd0d42860c15abf6f Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Tue, 18 Aug 2026 10:58:47 +0530 Subject: [PATCH 7/9] BB Fix --- src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al | 5 ----- src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al | 5 ----- src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al | 5 ----- 3 files changed, 15 deletions(-) diff --git a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al index e0d69966eaf..806f2488ed1 100644 --- a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -794,11 +794,6 @@ codeunit 57 "Document Totals" PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); - - TempVATAmountLine.SetFilter("VAT Difference", '<>%1', 0); - if TempVATAmountLine.IsEmpty() then - exit(false); - exit(true); end; diff --git a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al index 805f6bce3cf..3edb67d2b21 100644 --- a/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/NA/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -763,11 +763,6 @@ codeunit 57 "Document Totals" PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); - - TempVATAmountLine.SetFilter("VAT Difference", '<>%1', 0); - if TempVATAmountLine.IsEmpty() then - exit(false); - exit(true); end; diff --git a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al index 1a96a9ea91c..c5e735d38ef 100644 --- a/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/W1/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -748,11 +748,6 @@ codeunit 57 "Document Totals" PurchLine.CalcVATAmountLines(0, PurchHeader, PurchLine, TempVATAmountLine); GroupedVATAmount := TempVATAmountLine.GetTotalVATAmount(); - - TempVATAmountLine.SetFilter("VAT Difference", '<>%1', 0); - if TempVATAmountLine.IsEmpty() then - exit(false); - exit(true); end; From 129329598eb2716500b6e0a6bd27ed47c899a175 Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Wed, 19 Aug 2026 12:26:14 +0530 Subject: [PATCH 8/9] BB Fix --- .../BE/BaseApp/Utilities/DocumentTotals.Codeunit.al | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al index 806f2488ed1..ff64108906b 100644 --- a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -608,8 +608,6 @@ codeunit 57 "Document Totals" procedure PurchaseDeltaUpdateTotals(var PurchaseLine: Record "Purchase Line"; var xPurchaseLine: Record "Purchase Line"; var TotalPurchaseLine: Record "Purchase Line"; var VATAmount: Decimal; var InvoiceDiscountAmount: Decimal; var InvoiceDiscountPct: Decimal) var - PurchHeader: Record "Purchase Header"; - GroupedVATAmount: Decimal; InvDiscountBaseAmount: Decimal; IsHandled: Boolean; begin @@ -644,14 +642,6 @@ codeunit 57 "Document Totals" InvoiceDiscountPct := Round(100 * InvoiceDiscountAmount / InvDiscountBaseAmount, 0.00001); end; - if PurchHeader.Get(PurchaseLine."Document Type", PurchaseLine."Document No.") then - if not PurchaseDocHasRevChargeOrNonDeductibleVAT(PurchHeader) then - if GetGroupedVATAmount(PurchHeader, GroupedVATAmount) then - if GroupedVATAmount <> VATAmount then begin - VATAmount := GroupedVATAmount; - TotalPurchaseLine."Amount Including VAT" := TotalPurchaseLine.Amount + VATAmount; - end; - OnAfterPurchDeltaUpdateTotals(PurchaseLine, xPurchaseLine, TotalPurchaseLine, VATAmount, InvoiceDiscountAmount, InvoiceDiscountPct); end; From d426a729df5e61e0e308dba5bf45e816ebe17d31 Mon Sep 17 00:00:00 2001 From: v-shuklad Date: Wed, 19 Aug 2026 13:48:20 +0530 Subject: [PATCH 9/9] Fix --- .../BaseApp/Utilities/DocumentTotals.Codeunit.al | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al index ff64108906b..0684880d4d7 100644 --- a/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al +++ b/src/Layers/BE/BaseApp/Utilities/DocumentTotals.Codeunit.al @@ -787,21 +787,6 @@ codeunit 57 "Document Totals" exit(true); end; - local procedure PurchaseDocHasRevChargeOrNonDeductibleVAT(PurchHeader: Record "Purchase Header"): Boolean - var - PurchLine: Record "Purchase Line"; - begin - PurchLine.SetRange("Document Type", PurchHeader."Document Type"); - PurchLine.SetRange("Document No.", PurchHeader."No."); - PurchLine.SetRange("VAT Calculation Type", PurchLine."VAT Calculation Type"::"Reverse Charge VAT"); - if not PurchLine.IsEmpty() then - exit(true); - - PurchLine.SetRange("VAT Calculation Type"); - PurchLine.SetFilter("Non Deductible VAT %", '<>%1', 0); - exit(not PurchLine.IsEmpty()); - end; - procedure CalculatePostedPurchInvoiceTotals(var PurchInvHeader: Record "Purch. Inv. Header"; var VATAmount: Decimal; PurchInvLine: Record "Purch. Inv. Line") var CurrPurchInvLine: Record "Purch. Inv. Line";