Codeunit 6124 E-Doc. Providers
src/Apps/W1/EDocument/App/src/Processing/Import/PrepareDraft/EDocProviders.Codeunit.al232 lines, Copyright (c) Microsoft Corporation. MIT
// ------------------------------------------------------------------------------------------------// 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.Processing.Import;using Microsoft.Bank.Reconciliation;using Microsoft.eServices.EDocument;using Microsoft.eServices.EDocument.Processing.Import.Purchase;using Microsoft.eServices.EDocument.Processing.Interfaces;using Microsoft.eServices.EDocument.Service.Participant;using Microsoft.Foundation.UOM;using Microsoft.Inventory.Item;using Microsoft.Inventory.Item.Catalog;using Microsoft.Purchases.Document;using Microsoft.Purchases.Vendor;using System.Log;codeunit 6124 "E-Doc. Providers" implements IPurchaseLineProvider, IUnitOfMeasureProvider, IVendorProvider, IPurchaseOrderProvider{ Access = Internal; var NoVendorInformationErr: Label 'There is no vendor information in the source document. Verify that the source document is an invoice, and if it''s not, consider deleting this E-Document.'; PurchaseOrderNoTruncatedMsg: Label 'Purchase Order No. was truncated because it exceeded the maximum length of 20 characters.', Locked = true; ItemDescriptionReasonMsg: Label 'Item was found by an exact description match.'; ItemDescriptionSourceMsg: Label 'Item %1', Comment = '%1 - Item No.'; procedure GetVendor(EDocument: Record "E-Document") Vendor: Record Vendor var EDocumentPurchaseHeader: Record "E-Document Purchase Header"; ServiceParticipant: Record "Service Participant"; EDocErrorHelper: Codeunit "E-Document Error Helper"; EDocumentImportHelper: Codeunit "E-Document Import Helper"; EDocImpSessionTelemetry: Codeunit "E-Doc. Imp. Session Telemetry"; EDocumentHasNoVendorInformation: Boolean; begin EDocumentPurchaseHeader.GetFromEDocument(EDocument); EDocumentHasNoVendorInformation := (EDocumentPurchaseHeader."Vendor GLN" = '') and (EDocumentPurchaseHeader."Vendor VAT Id" = '') and (EDocumentPurchaseHeader."Vendor External Id" = '') and (EDocumentPurchaseHeader."Vendor Company Name" = '') and (EDocumentPurchaseHeader."Vendor Address" = ''); EDocImpSessionTelemetry.SetBool('Vendor Info Present', not EDocumentHasNoVendorInformation); if EDocumentHasNoVendorInformation then begin // We warn if there's no vendor information extracted from the E-Document, unless we are aware that it is a blank draft if EDocument."Read into Draft Impl." <> "E-Doc. Read into Draft"::"Blank Draft" then EDocErrorHelper.LogWarningMessage(EDocument, EDocumentPurchaseHeader, EDocumentPurchaseHeader.FieldNo("[BC] Vendor No."), NoVendorInformationErr); // If the E-Document has no vendor information, we can't find the vendor, so we exit early EDocImpSessionTelemetry.SetText('Vendor Match Method', 'None - No Vendor Info'); exit; end; if Vendor.Get(EDocumentImportHelper.FindVendor('', EDocumentPurchaseHeader."Vendor GLN", CopyStr(EDocumentPurchaseHeader."Vendor VAT Id", 1, 20))) then exit; ServiceParticipant.SetRange("Participant Type", ServiceParticipant."Participant Type"::Vendor); ServiceParticipant.SetRange("Participant Identifier", EDocumentPurchaseHeader."Vendor External Id"); ServiceParticipant.SetRange(Service, EDocument.GetEDocumentService().Code); if not ServiceParticipant.FindFirst() then begin ServiceParticipant.SetRange(Service); if ServiceParticipant.FindFirst() then; end; if Vendor.Get(ServiceParticipant.Participant) then begin EDocImpSessionTelemetry.SetText('Vendor Match Method', 'Service Participant'); exit; end; if Vendor.Get(EDocumentImportHelper.FindVendorByNameAndAddress(EDocumentPurchaseHeader."Vendor Company Name", EDocumentPurchaseHeader."Vendor Address")) then begin EDocImpSessionTelemetry.SetText('Vendor Match Method', 'Name and Address'); exit; end; EDocImpSessionTelemetry.SetText('Vendor Match Method', 'None - No Match'); end; procedure GetUnitOfMeasure(EDocumentHeader: Record "E-Document"; EDocumentLineId: Integer; ExternalUnitOfMeasure: Text) UnitOfMeasure: Record "Unit of Measure" begin if ExternalUnitOfMeasure = '' then exit; UnitOfMeasure.SetRange(Code, ExternalUnitOfMeasure); if UnitOfMeasure.FindFirst() then exit; Clear(UnitOfMeasure); UnitOfMeasure.SetRange("International Standard Code", ExternalUnitOfMeasure); if UnitOfMeasure.FindFirst() then exit; Clear(UnitOfMeasure); UnitOfMeasure.SetRange(Description, ExternalUnitOfMeasure); if UnitOfMeasure.FindFirst() then; end; local procedure SetActivityLog(SystemId: Guid; FieldNo: Integer; Reasoning: Text[250]; RecordRef: RecordRef; PageId: Integer; RefTitle: Text[250]; ActivityLog: Codeunit "Activity Log Builder"): Boolean begin ActivityLog .Init(Database::"E-Document Purchase Line", FieldNo, SystemId) .SetExplanation(Reasoning) .SetReferenceSource(PageId, RecordRef) .SetReferenceTitle(RefTitle); end; procedure GetPurchaseLine(var EDocumentPurchaseLine: Record "E-Document Purchase Line") var EDocumentPurchaseHeader: Record "E-Document Purchase Header"; Item: Record Item; ItemReference: Record "Item Reference"; EDocument: Record "E-Document"; TextToAccountMapping: Record "Text-to-Account Mapping"; EDocImpSessionTelemetry: Codeunit "E-Doc. Imp. Session Telemetry"; EDocActivityLogSession: Codeunit "E-Doc. Activity Log Session"; ActivityLog: Codeunit "Activity Log Builder"; VendorNo: Code[20]; FilterInvalidCharTxt: Label '(&)', Locked = true; ItemReferenceReasonMsg: Label 'Item reference was found for vendor %1.', Comment = '%1 - Vendor No.'; ItemReferenceSourceMsg: Label 'Item Reference %1', Comment = '%1 - Item Reference No.'; AccountNumberReasonMsg: Label 'Account was matched with Text to Account Mapping setup for vendor %1.', Comment = '%1 - Vendor No.'; AccountNumberSourceMsg: Label 'Text to Account Mapping %1', Comment = '%1 - Text to Account Mapping Line No.'; begin EDocument.Get(EDocumentPurchaseLine."E-Document Entry No."); EDocumentPurchaseHeader.GetFromEDocument(EDocument); VendorNo := EDocumentPurchaseHeader."[BC] Vendor No."; if GetPurchaseLineItemRef(EDocumentPurchaseLine, ItemReference) then begin EDocumentPurchaseLine."[BC] Purchase Line Type" := "Purchase Line Type"::Item; EDocumentPurchaseLine.Validate("[BC] Purchase Type No.", ItemReference."Item No."); EDocumentPurchaseLine.Validate("[BC] Unit of Measure", ItemReference."Unit of Measure"); EDocumentPurchaseLine.Validate("[BC] Variant Code", ItemReference."Variant Code"); EDocumentPurchaseLine.Validate("[BC] Item Reference No.", ItemReference."Reference No."); EDocImpSessionTelemetry.SetLineBool(EDocumentPurchaseLine.SystemId, 'Item Reference ', true); SetActivityLog(EDocumentPurchaseLine.SystemId, EDocumentPurchaseLine.FieldNo("[BC] Purchase Type No."), StrSubstNo(ItemReferenceReasonMsg, VendorNo), ItemReference, Page::"Item References", StrSubstNo(ItemReferenceSourceMsg, ItemReference."Reference No."), ActivityLog); EDocActivityLogSession.Set(EDocActivityLogSession.ItemRefTok(), ActivityLog); exit; end; TextToAccountMapping.SetRange("Vendor No.", VendorNo); TextToAccountMapping.SetFilter("Mapping Text", '%1', '@' + DelChr(EDocumentPurchaseLine.Description, '=', FilterInvalidCharTxt)); if TextToAccountMapping.FindFirst() then begin EDocumentPurchaseLine."[BC] Purchase Line Type" := "Purchase Line Type"::"G/L Account"; EDocumentPurchaseLine.Validate("[BC] Purchase Type No.", TextToAccountMapping."Debit Acc. No."); EDocImpSessionTelemetry.SetLineBool(EDocumentPurchaseLine.SystemId, 'Text To Account Mapping', true); SetActivityLog(EDocumentPurchaseLine.SystemId, EDocumentPurchaseLine.FieldNo("[BC] Purchase Type No."), StrSubstNo(AccountNumberReasonMsg, VendorNo), TextToAccountMapping, Page::"Text-to-Account Mapping", StrSubstNo(AccountNumberSourceMsg, TextToAccountMapping."Line No."), ActivityLog); EDocActivityLogSession.Set(EDocActivityLogSession.TextToAccountMappingTok(), ActivityLog); exit; end; if GetPurchaseLineItemByDescription(EDocumentPurchaseLine, Item) then begin EDocumentPurchaseLine."[BC] Purchase Line Type" := "Purchase Line Type"::Item; EDocumentPurchaseLine.Validate("[BC] Purchase Type No.", Item."No."); EDocImpSessionTelemetry.SetLineBool(EDocumentPurchaseLine.SystemId, 'ItemDescriptionMatch', true); SetActivityLog(EDocumentPurchaseLine.SystemId, EDocumentPurchaseLine.FieldNo("[BC] Purchase Type No."), ItemDescriptionReasonMsg, Item, Page::"Item Card", StrSubstNo(ItemDescriptionSourceMsg, Item."No."), ActivityLog); EDocActivityLogSession.Set(EDocActivityLogSession.ItemDescriptionTok(), ActivityLog); exit; end; end; procedure GetPurchaseOrder(EDocumentPurchaseHeader: Record "E-Document Purchase Header") PurchaseHeader: Record "Purchase Header" var PurchaseOrderNo: Code[20]; begin PurchaseOrderNo := CopyStr(EDocumentPurchaseHeader."Purchase Order No.", 1, MaxStrLen(PurchaseOrderNo)); if StrLen(EDocumentPurchaseHeader."Purchase Order No.") > MaxStrLen(PurchaseOrderNo) then Session.LogMessage('0000RJ0', PurchaseOrderNoTruncatedMsg, Verbosity::Warning, DataClassification::SystemMetadata, TelemetryScope::All, 'Category', 'E-Document'); if PurchaseHeader.Get("Purchase Document Type"::Order, PurchaseOrderNo) then; end; local procedure GetPurchaseLineItemRef(EDocumentPurchaseLine: Record "E-Document Purchase Line"; var ItemReference: Record "Item Reference"): Boolean var EDocument: Record "E-Document"; EDocumentPurchaseHeader: Record "E-Document Purchase Header"; Item: Record Item; VendorNo: Code[20]; begin EDocument.Get(EDocumentPurchaseLine."E-Document Entry No."); EDocumentPurchaseHeader.GetFromEDocument(EDocument); VendorNo := EDocumentPurchaseHeader."[BC] Vendor No."; ItemReference.SetRange("Reference Type", Enum::"Item Reference Type"::Vendor); ItemReference.SetRange("Reference Type No.", VendorNo); ItemReference.SetRange("Reference No.", EDocumentPurchaseLine."Product Code"); ItemReference.SetRange("Unit of Measure", EDocumentPurchaseLine."[BC] Unit of Measure"); ItemReference.SetFilter("Starting Date", '<= %1', WorkDate()); ItemReference.SetFilter("Ending Date", '>= %1 | %2', WorkDate(), 0D); if ItemReference.FindSet() then repeat if ItemReference.HasValidUnitOfMeasure() then exit(true); until ItemReference.Next() = 0; ItemReference.SetRange("Unit of Measure", ''); if ItemReference.FindSet() then repeat if ItemReference.HasValidUnitOfMeasure() then exit(true); until ItemReference.Next() = 0; ItemReference.SetRange("Unit of Measure"); if ItemReference.FindFirst() then if Item.Get(ItemReference."Item No.") then exit(true); end; local procedure GetPurchaseLineItemByDescription(EDocumentPurchaseLine: Record "E-Document Purchase Line"; var Item: Record Item): Boolean var EDocImpSessionTelemetry: Codeunit "E-Doc. Imp. Session Telemetry"; begin if EDocumentPurchaseLine.Description = '' then exit(false); Item.SetLoadFields("No."); Item.SetRange(Description, EDocumentPurchaseLine.Description); Item.SetRange(Blocked, false); Item.SetRange("Purchasing Blocked", false); if not Item.FindFirst() then begin EDocImpSessionTelemetry.SetLineText(EDocumentPurchaseLine.SystemId, 'ItemDescriptionMatchResult', 'NoMatch'); exit(false); end; if Item.Count() = 1 then begin EDocImpSessionTelemetry.SetLineText(EDocumentPurchaseLine.SystemId, 'ItemDescriptionMatchResult', 'Single'); exit(true); end; EDocImpSessionTelemetry.SetLineText(EDocumentPurchaseLine.SystemId, 'ItemDescriptionMatchResult', 'Multiple'); exit(false); end;}