From e53154f17d0f0aa3574b5bbe979fe0969a73f045 Mon Sep 17 00:00:00 2001 From: v-shikhverma Date: Tue, 27 Oct 2026 16:52:24 +0530 Subject: [PATCH 1/3] Initial commit --- .../BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al b/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al index cf994cb8810..75086b11cbe 100644 --- a/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al +++ b/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al @@ -508,7 +508,6 @@ codeunit 815 "Sales Post Invoice" implements "Invoice Posting" local procedure PrepareGenJnlLine(var SalesHeader: Record "Sales Header"; var InvoicePostingBuffer: Record "Invoice Posting Buffer"; var GenJnlLine: Record "Gen. Journal Line") var - BillToCust: Record Customer; IsHandled: Boolean; begin InitGenJnlLine(GenJnlLine, SalesHeader, InvoicePostingBuffer); @@ -521,10 +520,8 @@ codeunit 815 "Sales Post Invoice" implements "Invoice Posting" IsHandled := false; SalesPostInvoiceEventsBE.RunOnPrepareGenJnlLineOnBeforeUpdateCountryRegionCode(SalesHeader, GenJnlLine, InvoicePostingBuffer, IsHandled); - if not IsHandled then begin - BillToCust.Get(GenJnlLine."Bill-to/Pay-to No."); - GenJnlLine."Country/Region Code" := BillToCust."Country/Region Code"; - end; + if not IsHandled then + GenJnlLine."Country/Region Code" := SalesHeader."VAT Country/Region Code"; InvoicePostingBuffer.CopyToGenJnlLine(GenJnlLine); if GLSetup."Journal Templ. Name Mandatory" then From 5644f64d61f0d2fde561dc068660a968dc1f0a51 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 Oct 2026 13:34:24 +0000 Subject: [PATCH 2/3] Remove unused Customer using directive Co-authored-by: Shikhverma <241284522+Shikhverma@users.noreply.github.com> --- src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al b/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al index 75086b11cbe..f02acf1d67e 100644 --- a/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al +++ b/src/Layers/BE/BaseApp/Sales/Posting/SalesPostInvoice.Codeunit.al @@ -13,7 +13,6 @@ using Microsoft.Finance.GeneralLedger.Setup; using Microsoft.Finance.ReceivablesPayables; using Microsoft.Finance.VAT.Calculation; using Microsoft.Projects.Project.Posting; -using Microsoft.Sales.Customer; using Microsoft.Sales.Document; using Microsoft.Sales.Receivables; using Microsoft.Sales.Setup; From 8f85260534181ebe358c8ff542bd03294bac71b2 Mon Sep 17 00:00:00 2001 From: v-shikhverma Date: Sat, 31 Oct 2026 14:49:33 +0530 Subject: [PATCH 3/3] Add Test --- .../Local/AltCustVATDocBETests.Codeunit.al | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/src/Layers/BE/Tests/Local/AltCustVATDocBETests.Codeunit.al b/src/Layers/BE/Tests/Local/AltCustVATDocBETests.Codeunit.al index e63afa013aa..15ed41f986e 100644 --- a/src/Layers/BE/Tests/Local/AltCustVATDocBETests.Codeunit.al +++ b/src/Layers/BE/Tests/Local/AltCustVATDocBETests.Codeunit.al @@ -12,6 +12,8 @@ codeunit 144007 "Alt. Cust. VAT Doc. BE Tests" LibrarySales: Codeunit "Library - Sales"; LibraryTestInitialize: Codeunit "Library - Test Initialize"; LibraryERM: Codeunit "Library - ERM"; + LibraryInventory: Codeunit "Library - Inventory"; + LibraryRandom: Codeunit "Library - Random"; LibrarySetupStorage: Codeunit "Library - Setup Storage"; LibraryVariableStorage: Codeunit "Library - Variable Storage"; LibraryLowerPermissions: Codeunit "Library - Lower Permissions"; @@ -641,6 +643,99 @@ codeunit 144007 "Alt. Cust. VAT Doc. BE Tests" LibraryLowerPermissions.SetOutsideO365Scope(); end; + [Test] + [HandlerFunctions('NoNotificationOtherThanShipToAddressSendNotificationHandler')] + procedure PostSalesInvoiceWithAltCustVATRegForDomesticCustomerUsesVATCountryInVATEntry() + var + AltCustVATReg: Record "Alt. Cust. VAT Reg."; + Customer: Record Customer; + SalesHeader: Record "Sales Header"; + PostedDocNo: Code[20]; + begin + // [SCENARIO 654027] VAT Entry gets the VAT country of the sales invoice when an Alternative Customer VAT Registration is applied to a domestic customer + Initialize(); + + // [GIVEN] Belgian customer + LibraryBEHelper.CreateDomesticCustomer(Customer); + // [GIVEN] Alternative Customer VAT Reg. with VAT country "X" and VAT Registration No. "V" + CreateAlternativeCustVATRegWithVATRegNo(AltCustVATReg, Customer."No.", LibraryERM.CreateCountryRegion()); + // [GIVEN] Sales invoice for the customer with VAT country changed to "X" + CreateSalesInvoiceWithItem(SalesHeader, Customer."No."); + SalesHeader.Validate("VAT Country/Region Code", AltCustVATReg."VAT Country/Region Code"); + SalesHeader.Modify(true); + SalesHeader.TestField("VAT Registration No.", AltCustVATReg."VAT Registration No."); + LibraryLowerPermissions.SetO365Setup(); + LibraryLowerPermissions.AddSalesDocsPost(); + + // [WHEN] Post the sales invoice + PostedDocNo := LibrarySales.PostSalesDocument(SalesHeader, true, true); + + // [THEN] VAT Entry has "Country/Region Code" = "X" and "VAT Registration No." = "V" + VerifyVATEntryCountryAndVATRegNo(PostedDocNo, AltCustVATReg."VAT Country/Region Code", AltCustVATReg."VAT Registration No."); + + LibraryLowerPermissions.SetOutsideO365Scope(); + end; + + [Test] + [HandlerFunctions('NoNotificationOtherThanShipToAddressSendNotificationHandler')] + procedure PostSalesInvoiceWithAltCustVATRegForForeignCustomerUsesVATCountryInVATEntry() + var + AltCustVATReg: Record "Alt. Cust. VAT Reg."; + Customer: Record Customer; + SalesHeader: Record "Sales Header"; + PostedDocNo: Code[20]; + begin + // [SCENARIO 654027] VAT Entry gets the VAT country of the sales invoice when an Alternative Customer VAT Registration is applied to a foreign customer + Initialize(); + + // [GIVEN] Customer with foreign country "A" + LibrarySales.CreateCustomerWithCountryCodeAndVATRegNo(Customer); + // [GIVEN] Alternative Customer VAT Reg. with VAT country "X" and VAT Registration No. "V" + CreateAlternativeCustVATRegWithVATRegNo(AltCustVATReg, Customer."No.", LibraryERM.CreateCountryRegion()); + // [GIVEN] Sales invoice for the customer with VAT country changed to "X" + CreateSalesInvoiceWithItem(SalesHeader, Customer."No."); + SalesHeader.Validate("VAT Country/Region Code", AltCustVATReg."VAT Country/Region Code"); + SalesHeader.Modify(true); + SalesHeader.TestField("VAT Registration No.", AltCustVATReg."VAT Registration No."); + LibraryLowerPermissions.SetO365Setup(); + LibraryLowerPermissions.AddSalesDocsPost(); + + // [WHEN] Post the sales invoice + PostedDocNo := LibrarySales.PostSalesDocument(SalesHeader, true, true); + + // [THEN] VAT Entry has "Country/Region Code" = "X", not the customer's country "A" + VerifyVATEntryCountryAndVATRegNo(PostedDocNo, AltCustVATReg."VAT Country/Region Code", AltCustVATReg."VAT Registration No."); + + LibraryLowerPermissions.SetOutsideO365Scope(); + end; + + [Test] + procedure PostSalesInvoiceWithoutAltCustVATRegUsesCustomerCountryInVATEntry() + var + Customer: Record Customer; + SalesHeader: Record "Sales Header"; + PostedDocNo: Code[20]; + begin + // [SCENARIO 654027] VAT Entry keeps the customer's country when no Alternative Customer VAT Registration is applied + Initialize(); + + // [GIVEN] Customer with foreign country "A" and VAT Registration No. "V" + LibrarySales.CreateCustomerWithCountryCodeAndVATRegNo(Customer); + // [GIVEN] Sales invoice for the customer + CreateSalesInvoiceWithItem(SalesHeader, Customer."No."); + SalesHeader.TestField("VAT Country/Region Code", Customer."Country/Region Code"); + LibraryLowerPermissions.SetO365Setup(); + LibraryLowerPermissions.AddSalesDocsPost(); + + // [WHEN] Post the sales invoice + PostedDocNo := LibrarySales.PostSalesDocument(SalesHeader, true, true); + + // [THEN] VAT Entry has "Country/Region Code" = "A" and "VAT Registration No." = "V" + VerifyVATEntryCountryAndVATRegNo(PostedDocNo, Customer."Country/Region Code", Customer."VAT Registration No."); + + LibraryLowerPermissions.SetOutsideO365Scope(); + end; + local procedure Initialize() begin LibrarySetupStorage.Restore(); @@ -710,6 +805,33 @@ codeunit 144007 "Alt. Cust. VAT Doc. BE Tests" Customer.Modify(true); end; + local procedure CreateSalesInvoiceWithItem(var SalesHeader: Record "Sales Header"; CustomerNo: Code[20]) + var + Item: Record Item; + SalesLine: Record "Sales Line"; + begin + LibraryInventory.CreateItem(Item); + LibrarySales.CreateSalesHeader(SalesHeader, SalesHeader."Document Type"::Invoice, CustomerNo); + LibrarySales.CreateSalesLine(SalesLine, SalesHeader, SalesLine.Type::Item, Item."No.", 1); + SalesLine.Validate("Unit Price", LibraryRandom.RandDecInRange(100, 1000, 2)); + SalesLine.Modify(true); + end; + + local procedure VerifyVATEntryCountryAndVATRegNo(DocumentNo: Code[20]; CountryCode: Code[10]; VATRegNo: Text[20]) + var + VATEntry: Record "VAT Entry"; + begin + VATEntry.SetRange(Type, VATEntry.Type::Sale); + VATEntry.SetRange("Document Type", VATEntry."Document Type"::Invoice); + VATEntry.SetRange("Document No.", DocumentNo); + Assert.RecordIsNotEmpty(VATEntry); + VATEntry.FindSet(); + repeat + VATEntry.TestField("Country/Region Code", CountryCode); + VATEntry.TestField("VAT Registration No.", VATRegNo); + until VATEntry.Next() = 0; + end; + local procedure GetDomesticCountryCode(): Code[10] var CompanyInformation: Record "Company Information";