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 @@ -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;
Expand Down Expand Up @@ -508,7 +507,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);
Expand All @@ -521,10 +519,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
Expand Down
122 changes: 122 additions & 0 deletions src/Layers/BE/Tests/Local/AltCustVATDocBETests.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -641,6 +643,99 @@ codeunit 144007 "Alt. Cust. VAT Doc. BE Tests"
LibraryLowerPermissions.SetOutsideO365Scope();
end;

[Test]
[HandlerFunctions('NoNotificationOtherThanShipToAddressSendNotificationHandler')]
procedure PostSalesInvoiceWithAltCustVATRegForDomesticCustomerUsesVATCountryInVATEntry()
Comment thread
Shikhverma marked this conversation as resolved.
Resolved
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();
Expand Down Expand Up @@ -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);
Comment thread
Shikhverma marked this conversation as resolved.
Resolved
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";
Expand Down
Loading