diff --git a/src/Apps/W1/EDocument/App/src/Document/EDocumentSourceType.Enum.al b/src/Apps/W1/EDocument/App/src/Document/EDocumentSourceType.Enum.al index 2e6a4fbd37..7572ab4e06 100644 --- a/src/Apps/W1/EDocument/App/src/Document/EDocumentSourceType.Enum.al +++ b/src/Apps/W1/EDocument/App/src/Document/EDocumentSourceType.Enum.al @@ -12,4 +12,5 @@ enum 6123 "E-Document Source Type" value(0; Customer) { Caption = 'Customer'; } value(1; Vendor) { Caption = 'Vendor'; } value(2; Location) { Caption = 'Location'; } + value(3; Company) { Caption = 'Company'; } } diff --git a/src/Apps/W1/EDocument/App/src/Extensions/EDocCompanyInformation.PageExt.al b/src/Apps/W1/EDocument/App/src/Extensions/EDocCompanyInformation.PageExt.al new file mode 100644 index 0000000000..897c2f6f22 --- /dev/null +++ b/src/Apps/W1/EDocument/App/src/Extensions/EDocCompanyInformation.PageExt.al @@ -0,0 +1,59 @@ +// ------------------------------------------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// ------------------------------------------------------------------------------------------------ + +namespace Microsoft.eServices.EDocument.Extensions; + +using Microsoft.eServices.EDocument; +using Microsoft.eServices.EDocument.Service.Participant; +using Microsoft.Foundation.Company; + +/// +/// A page extension for the Company Information page to show the E-Document service participation. +/// +pageextension 6165 "E-Doc. Company Information" extends "Company Information" +{ + layout + { + addafter(GLN) + { + field("E-Document Service Participation Ids"; ParticipantIdCount) + { + ApplicationArea = All; + Caption = 'E-Document Service Participation'; + DrillDown = true; + Editable = false; + ToolTip = 'Specifies the company participation for the E-Document services.'; + Visible = EDocumentServiceExists; + + trigger OnDrillDown() + begin + ServiceParticipant.RunServiceParticipantPage(Enum::"E-Document Source Type"::Company, ''); + end; + } + } + } + + + var + ServiceParticipant: Codeunit "Service Participant"; + ParticipantIdCount: Integer; + EDocumentServiceExists: Boolean; + + + trigger OnAfterGetCurrRecord() + begin + if TryGetEDocumentServiceParticipation() then; + end; + + [TryFunction] + local procedure TryGetEDocumentServiceParticipation() + var + EDocumentService: Record "E-Document Service"; + begin + EDocumentServiceExists := not EDocumentService.IsEmpty(); + ParticipantIdCount := ServiceParticipant.GetParticipantIdCount(Enum::"E-Document Source Type"::Company, ''); + end; + +} diff --git a/src/Apps/W1/EDocument/App/src/Helpers/EDocumentImportHelper.Codeunit.al b/src/Apps/W1/EDocument/App/src/Helpers/EDocumentImportHelper.Codeunit.al index 02abadb0af..e08ba1d533 100644 --- a/src/Apps/W1/EDocument/App/src/Helpers/EDocumentImportHelper.Codeunit.al +++ b/src/Apps/W1/EDocument/App/src/Helpers/EDocumentImportHelper.Codeunit.al @@ -256,6 +256,21 @@ codeunit 6109 "E-Document Import Helper" LineDiscountAmountFieldRef.Value(LineDiscountAmount); end; + /// + /// Use it to check if receiving company information is in line with Company Information. + /// Also checks if the Receiving Company Id matches a Company Service Participant. + /// + /// The E-Document record. + /// The E-Document Service record to match against. + procedure ValidateReceivingCompanyInfo(EDocument: Record "E-Document"; EDocService: Record "E-Document Service") + begin + // First, check if the Receiving Company Id matches a Company Service Participant + if MatchCompanyByServiceParticipant(EDocument, EDocService) then + exit; + + ValidateReceivingCompanyInfo(EDocument); + end; + /// /// Use it to check if receiving company information is in line with Company Information. /// @@ -281,6 +296,25 @@ codeunit 6109 "E-Document Import Helper" EDocErrorHelper.LogErrorMessage(EDocument, CompanyInformation, CompanyInformation.FieldNo("VAT Registration No."), StrSubstNo(InvalidCompanyInfoVATRegNoErr, EDocument."Receiving Company VAT Reg. No.")); end; + /// + /// Use it to check if receiving company information matches a Company Service Participant for a specific service. + /// + /// The E-Document record. + /// The E-Document Service record to match against. + /// True if a matching Company Service Participant is found. + local procedure MatchCompanyByServiceParticipant(EDocument: Record "E-Document"; EDocService: Record "E-Document Service"): Boolean + var + ServiceParticipant: Record "Service Participant"; + begin + if EDocument."Receiving Company Id" = '' then + exit(false); + + ServiceParticipant.SetRange("Participant Type", ServiceParticipant."Participant Type"::Company); + ServiceParticipant.SetRange("Participant Identifier", EDocument."Receiving Company Id"); + ServiceParticipant.SetRange(Service, EDocService.Code); + exit(not ServiceParticipant.IsEmpty()); + end; + /// /// Use it to check if receiving company name and address is in line with Company Information. /// diff --git a/src/Apps/W1/EDocument/App/src/Processing/EDocImport.Codeunit.al b/src/Apps/W1/EDocument/App/src/Processing/EDocImport.Codeunit.al index 7c9d49b734..e9bfaa57e6 100644 --- a/src/Apps/W1/EDocument/App/src/Processing/EDocImport.Codeunit.al +++ b/src/Apps/W1/EDocument/App/src/Processing/EDocImport.Codeunit.al @@ -278,7 +278,7 @@ codeunit 6140 "E-Doc. Import" EDocErrorHelper.LogWarningMessage(EDocument, EDocument2, EDocument2.FieldNo("Incoming E-Document No."), DocAlreadyExistsMsg); if EDocService."Validate Receiving Company" then - EDocImportHelper.ValidateReceivingCompanyInfo(EDocument); + EDocImportHelper.ValidateReceivingCompanyInfo(EDocument, EDocService); end else EDocErrorHelper.LogSimpleErrorMessage(EDocument, GetLastErrorText()); diff --git a/src/Apps/W1/EDocument/Test/src/Receive/EDocHelperTest.Codeunit.al b/src/Apps/W1/EDocument/Test/src/Receive/EDocHelperTest.Codeunit.al index bf0a7b4f51..4a33a43c12 100644 --- a/src/Apps/W1/EDocument/Test/src/Receive/EDocHelperTest.Codeunit.al +++ b/src/Apps/W1/EDocument/Test/src/Receive/EDocHelperTest.Codeunit.al @@ -5,15 +5,21 @@ namespace Microsoft.eServices.EDocument.Test; using Microsoft.eServices.EDocument; +using Microsoft.eServices.EDocument.Integration; +using Microsoft.eServices.EDocument.Service.Participant; +using Microsoft.Foundation.Company; using Microsoft.Purchases.Document; codeunit 139799 "E-Doc. Helper Test" { Subtype = Test; Access = Internal; + TestPermissions = Disabled; var Assert: Codeunit "Assert"; + LibraryEDoc: Codeunit "Library - E-Document"; + LibraryLowerPermission: Codeunit "Library - Lower Permissions"; trigger OnRun() begin @@ -48,4 +54,82 @@ codeunit 139799 "E-Doc. Helper Test" VendorNo := EDocumentImportHelper.FindVendor('', '', ''); Assert.IsTrue(VendorNo = '', 'Vendor No. should be empty'); end; + + [Test] + procedure ValidateReceivingCompanyInfoWithMatchingServiceParticipant() + var + EDocument: Record "E-Document"; + EDocService: Record "E-Document Service"; + ServiceParticipant: Record "Service Participant"; + EDocumentImportHelper: Codeunit "E-Document Import Helper"; + EDocErrorHelper: Codeunit "E-Document Error Helper"; + TestParticipantId: Text[200]; + begin + LibraryLowerPermission.SetOutsideO365Scope(); + // [SCENARIO] Validation should succeed when a matching Company Service Participant exists + // [GIVEN] An E-Document with a Receiving Company Id + TestParticipantId := '0208:1234567890'; + EDocument.Init(); + EDocument."Entry No" := 0; + EDocument."Receiving Company Id" := TestParticipantId; + EDocument."Receiving Company GLN" := ''; + EDocument."Receiving Company VAT Reg. No." := ''; + EDocument.Insert(true); + + // [GIVEN] An E-Document Service + LibraryEDoc.CreateTestReceiveServiceForEDoc(EDocService, Enum::"Service Integration"::"Mock"); + + // [GIVEN] A matching Company Service Participant + ServiceParticipant.Init(); + ServiceParticipant.Service := EDocService.Code; + ServiceParticipant."Participant Type" := ServiceParticipant."Participant Type"::Company; + ServiceParticipant.Participant := ''; + ServiceParticipant."Participant Identifier" := TestParticipantId; + ServiceParticipant.Insert(true); + + // [WHEN] Validating receiving company info + EDocumentImportHelper.ValidateReceivingCompanyInfo(EDocument, EDocService); + + // [THEN] No errors should be logged (validation should exit early due to Service Participant match) + Assert.IsFalse(EDocErrorHelper.HasErrors(EDocument), 'No errors should be logged when Service Participant matches'); + + // Cleanup + ServiceParticipant.Delete(); + EDocument.Delete(); + end; + + [Test] + procedure ValidateReceivingCompanyInfoFallsBackToVATWhenNoServiceParticipant() + var + EDocument: Record "E-Document"; + EDocService: Record "E-Document Service"; + CompanyInformation: Record "Company Information"; + EDocumentImportHelper: Codeunit "E-Document Import Helper"; + EDocErrorHelper: Codeunit "E-Document Error Helper"; + begin + LibraryLowerPermission.SetOutsideO365Scope(); + // [SCENARIO] Validation should fall back to VAT/GLN matching when no Service Participant exists + // [GIVEN] An E-Document with a Receiving Company Id that doesn't match any Service Participant + // [GIVEN] But has matching VAT Registration No. + CompanyInformation.Get(); + + EDocument.Init(); + EDocument."Entry No" := 0; + EDocument."Receiving Company Id" := '0208:NOMATCH'; + EDocument."Receiving Company VAT Reg. No." := CompanyInformation."VAT Registration No."; + EDocument."Receiving Company GLN" := ''; + EDocument.Insert(true); + + // [GIVEN] An E-Document Service with no matching Company Service Participant + LibraryEDoc.CreateTestReceiveServiceForEDoc(EDocService, Enum::"Service Integration"::"Mock"); + + // [WHEN] Validating receiving company info + EDocumentImportHelper.ValidateReceivingCompanyInfo(EDocument, EDocService); + + // [THEN] No errors should be logged (validation should succeed via VAT matching) + Assert.IsFalse(EDocErrorHelper.HasErrors(EDocument), 'No errors should be logged when VAT Registration No. matches'); + + // Cleanup + EDocument.Delete(); + end; } \ No newline at end of file