Codeunit 6164 E-Doc. Line Matching

E-Document Core, Microsoft.eServices.EDocument.OrderMatch

src/Apps/W1/EDocument/App/src/Processing/OrderMatching/EDocLineMatching.Codeunit.al605 lines, Copyright (c) Microsoft Corporation. MIT

namespace Microsoft.eServices.EDocument.OrderMatch;using Microsoft.Bank.Reconciliation;using Microsoft.eServices.EDocument;using Microsoft.Finance.GeneralLedger.Account;using Microsoft.Inventory.Item;using Microsoft.Inventory.Item.Catalog;using Microsoft.Purchases.Document;using Microsoft.Purchases.Vendor;using System.Utilities;codeunit 6164 "E-Doc. Line Matching"{    Access = Internal;    Permissions =        tabledata "E-Doc. Imported Line" = im,        tabledata "E-Doc. Order Match" = idm;    var        EmptyRecordErr: Label 'Empty selection cannot be matched.';        DiscountErr: Label 'Varied Discount found among selected %1 entries. Please review and deselect entries with different Discount in order to match selection', Comment = '%1 - Table name for selected entries';        UnitCostErr: Label 'Varied Unit Costs found among selected %1 entries. Please review and deselect entries with different Unit Costs in order to match selection', Comment = '%1 - Table name for selected entries';        AmountDiffErr: Label 'Varied amounts found in matching for Import line %1. Please review and undo previous matching in order to match selection', Comment = '%1 - Import line number';        MatchErr: Label 'Discrepancy detected in line amount for 1 or more matches for Purchase Line %1.', Comment = '%1 - Purchase Line No';        UOMErr: Label 'Varied Unit Of Measures found among selected %1 entries. Please review and deselect entries with different Unit Of Measures in order to match selection', Comment = '%1 - Table name for selected entries';        UnmatchedImportLineErr: Label 'Matching of Imported Line %1 is incomplete. It is not fully matched to purchase order lines.', Comment = '%1 - Imported Line No.';        OverwriteExistingMatchesTxt: Label 'There are lines for this e-document that are already matched with this purchase order.\\ Do you want to overwrite the existing matches?';        PurchaseHeaderAlreadyLinkedErr: Label 'Cannot apply to purchase order as it is already linked to E-Document';        AutoGeneratedEDocumentTxt: Label 'Auto-generated reference from E-Document %1', Comment = '%1 = the Document No. of an E-Document';        CannotCreateMatchingRuleErr: Label 'Cannot create matching rule for vendor %1', Comment = '%1 - Vendor No.';        PurchaseLineCreatedMsg: Label 'The purchase line has been created.';        PurchaseLineCreatedReceiveItemsMsg: Label 'The purchase line has been created. Please review the purchase order and receive items.';    procedure RunMatching(var EDocument: Record "E-Document")    var        EDocService: Record "E-Document Service";        EDocServiceStatus: Record "E-Document Service Status";        EDocLog: Codeunit "E-Document Log";        EDocOrderLineMatching: Page "E-Doc. Order Line Matching";    begin        EDocument.TestField("Document Type", Enum::"E-Document Type"::"Purchase Order");        EDocument.TestField(Status, Enum::"E-Document Status"::"In Progress");        EDocument.TestField(Direction, Enum::"E-Document Direction"::Incoming);        EDocService := EDocLog.GetLastServiceFromLog(EDocument);        EDocServiceStatus.Get(EDocument."Entry No", EDocService.Code);        EDocServiceStatus.TestField(Status, Enum::"E-Document Service Status"::"Order Linked");        EDocOrderLineMatching.SetTempRecord(EDocument);        EDocOrderLineMatching.Run();    end;#if not CLEAN29    [Obsolete('Deprecated functionality', '29.0')]    procedure RunMatching(var EDocument: Record "E-Document"; WithCopilot: Boolean)    begin        RunMatching(EDocument);    end;#endif    procedure ApplyToPurchaseOrder(var EDocument: Record "E-Document"; var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary)    var        PurchaseLine: Record "Purchase Line";        PurchaseHeader: Record "Purchase Header";        EDocOrderMatch: Record "E-Doc. Order Match";        EDocService: Record "E-Document Service";        EDocumentLog: Codeunit "E-Document Log";        EDocLogHelper: Codeunit "E-Document Log Helper";        RecordRef: RecordRef;    begin        // Check that all imorted lines have been matched        if TempEDocumentImportedLine.FindSet() then            repeat                if TempEDocumentImportedLine."Matched Quantity" <> TempEDocumentImportedLine.Quantity then                    Error(UnmatchedImportLineErr, TempEDocumentImportedLine."Line No.");            until TempEDocumentImportedLine.Next() = 0;        RecordRef.Get(EDocument."Document Record ID");        RecordRef.SetTable(PurchaseHeader);        PurchaseHeader.Validate("Document Date", EDocument."Document Date");        PurchaseHeader.Validate("Vendor Invoice No.", CopyStr(EDocument."Incoming E-Document No.", 1, MaxStrLen(PurchaseHeader."Vendor Invoice No.")));        if PurchaseHeader.IsLinkedToEDoc(EDocument) then            Error(PurchaseHeaderAlreadyLinkedErr);        PurchaseHeader.Validate("E-Document Link", EDocument.SystemId);        PurchaseHeader.Modify();        EDocOrderMatch.SetRange("E-Document Entry No.", EDocument."Entry No");        EDocOrderMatch.SetRange("Document Order No.", EDocument."Order No.");        PurchaseLine.SetRange("Document Type", Enum::"Purchase Document Type"::Order);        PurchaseLine.SetRange("Document No.", EDocument."Order No.");        if PurchaseLine.FindSet() then            repeat                EDocOrderMatch.SetRange("Document Line No.", PurchaseLine."Line No.");                // We check that if there is a set, then Direct Cost, UOM and Discount % is all the same, otherwise we cant                TestLineTotalAreTheSameInSet(EDocOrderMatch);                if EDocOrderMatch.FindFirst() then begin                    if PurchaseLine."Direct Unit Cost" <> EDocOrderMatch."E-Document Direct Unit Cost" then                        PurchaseLine.Validate("Direct Unit Cost", EDocOrderMatch."E-Document Direct Unit Cost");                    if PurchaseLine."Line Discount %" <> EDocOrderMatch."Line Discount %" then                        PurchaseLine.Validate("Line Discount %", EDocOrderMatch."Line Discount %");                    PurchaseLine.Validate("Amount Including VAT");                    PurchaseLine.Modify();                end            until PurchaseLine.Next() = 0;        EDocService := EDocumentLog.GetLastServiceFromLog(EDocument);        EDocLogHelper.InsertLog(EDocument, EDocService, Enum::"E-Document Service Status"::"Order Updated");    end;    local procedure TestLineTotalAreTheSameInSet(var EDocOrderMatch: Record "E-Doc. Order Match")    var        EDocOrderMatch2: Record "E-Doc. Order Match";        Total, Total2 : Decimal;    begin        if not EDocOrderMatch.FindFirst() then            exit;        Total := EDocOrderMatch."E-Document Direct Unit Cost" - (EDocOrderMatch."E-Document Direct Unit Cost" * EDocOrderMatch."Line Discount %" / 100);        EDocOrderMatch2.Copy(EDocOrderMatch);        if EDocOrderMatch2.FindSet() then            repeat                Total2 := EDocOrderMatch2."E-Document Direct Unit Cost" - (EDocOrderMatch2."E-Document Direct Unit Cost" * EDocOrderMatch2."Line Discount %" / 100);                if Total <> Total2 then                    Error(MatchErr, EDocOrderMatch."Document Line No.");            until EDocOrderMatch2.Next() = 0;    end;    procedure ResetMatchedQty(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary)    begin        UpdateMatchedQty(TempEDocumentImportedLine, -TempEDocumentImportedLine."Matched Quantity");    end;    procedure UpdateMatchedQty(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; Quantity: Decimal)    begin        TempEDocumentImportedLine.Validate("Matched Quantity", TempEDocumentImportedLine."Matched Quantity" + Quantity);        TempEDocumentImportedLine.Modify(true);    end;    procedure ResetQtyToInvoice(var TempPurchaseLine: Record "Purchase Line" temporary)    begin        UpdateQtyToInvoice(TempPurchaseLine, -TempPurchaseLine."Qty. to Invoice");    end;    procedure UpdateQtyToInvoice(var TempPurchaseLine: Record "Purchase Line" temporary; Quantity: Decimal)    begin        TempPurchaseLine.Validate("Qty. to Invoice", TempPurchaseLine."Qty. to Invoice" + Quantity);        TempPurchaseLine.Modify(true);    end;    procedure PersistsUpdates(var TempEDocMatchesThatWasMatched: Record "E-Doc. Order Match" temporary; RemoveMatch: Boolean)    var        EDocImportedLine: Record "E-Doc. Imported Line";        EDocOrderMatch: Record "E-Doc. Order Match";        EDocument: Record "E-Document";        PurchaseLine: Record "Purchase Line";    begin        TempEDocMatchesThatWasMatched.Reset();        if TempEDocMatchesThatWasMatched.FindSet() then            repeat                EDocOrderMatch.Copy(TempEDocMatchesThatWasMatched);                PurchaseLine := EDocOrderMatch.GetPurchaseLine();                PurchaseLine.Validate("Qty. to Invoice", PurchaseLine."Qty. to Invoice" + EDocOrderMatch."Precise Quantity");                PurchaseLine.Modify(true);                EDocImportedLine := EDocOrderMatch.GetImportedLine();                EDocImportedLine.Validate("Matched Quantity", EDocImportedLine."Matched Quantity" + EDocOrderMatch."Precise Quantity");                EDocImportedLine.Modify(true);                if TempEDocMatchesThatWasMatched."Learn Matching Rule" and EDocument.Get(EDocOrderMatch."E-Document Entry No.") then                    CreateMatchingRule(PurchaseLine, EDocument, EDocImportedLine);                if RemoveMatch then                    EDocOrderMatch.Delete()                else                    EDocOrderMatch.Insert();            until TempEDocMatchesThatWasMatched.Next() = 0;    end;    procedure ResetQtyToInvoiceBasedOnMatches(var EDocument: Record "E-Document"; var TempPurchaseLine: Record "Purchase Line" temporary)    var        EDocOrderMatch: Record "E-Doc. Order Match";    begin        EDocOrderMatch.SetRange("E-Document Entry No.", EDocument."Entry No");        TempPurchaseLine.Reset();        if TempPurchaseLine.FindSet() then            repeat                EDocOrderMatch.SetRange("Document Order No.", TempPurchaseLine."Document No.");                EDocOrderMatch.SetRange("Document Line No.", TempPurchaseLine."Line No.");                EDocOrderMatch.CalcSums("Precise Quantity");                TempPurchaseLine.Validate("Qty. to Invoice", EDocOrderMatch."Precise Quantity");                TempPurchaseLine.Modify();            until TempPurchaseLine.Next() = 0;    end;    procedure ResetQtyToInvoice(var EDocument: Record "E-Document")    var        PurchaseLine: Record "Purchase Line";    begin        PurchaseLine.SetRange("Document Type", Enum::"Purchase Document Type"::Order);        PurchaseLine.SetRange("Document No.", EDocument."Order No.");        PurchaseLine.ModifyAll("Qty. to Invoice", 0, true);    end;    procedure RemoveAllMatches(var EDocument: Record "E-Document")    var        EDocOrderMatch: Record "E-Doc. Order Match";        EDocImportedLine: Record "E-Doc. Imported Line";        PurchaseLine: Record "Purchase Line";    begin        EDocOrderMatch.SetRange("E-Document Entry No.", EDocument."Entry No");        EDocOrderMatch.DeleteAll();        EDocImportedLine.SetRange("E-Document Entry No.", EDocument."Entry No");        EDocImportedLine.ModifyAll("Matched Quantity", 0);        EDocImportedLine.ModifyAll("Fully Matched", false);        PurchaseLine.SetRange("Document Type", Enum::"Purchase Document Type"::Order);        PurchaseLine.SetRange("Document No.", EDocument."Order No.");        PurchaseLine.ModifyAll("Qty. to Invoice", 0);    end;    procedure FindMatchesToRemove(var EDocument: Record "E-Document"; var TempPurchaseLine: Record "Purchase Line" temporary; var TempEDocMatchesThatWasRemoved: Record "E-Doc. Order Match" temporary)    var        EDocOrderMatch: Record "E-Doc. Order Match";    begin        if TempPurchaseLine.FindSet() then begin            EDocOrderMatch.SetRange("E-Document Entry No.", EDocument."Entry No");            EDocOrderMatch.SetRange("Document Order No.", TempPurchaseLine."Document No.");            repeat                EDocOrderMatch.SetRange("Document Line No.", TempPurchaseLine."Line No.");                if EDocOrderMatch.FindSet() then                    repeat                        TempEDocMatchesThatWasRemoved := EDocOrderMatch;                        TempEDocMatchesThatWasRemoved."Precise Quantity" *= -1;                        TempEDocMatchesThatWasRemoved.Insert();                    until EDocOrderMatch.Next() = 0;            until TempPurchaseLine.Next() = 0;        end    end;    procedure MatchManually(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary; var TempEDocMatchesThatWasMatched: Record "E-Doc. Order Match" temporary)    begin        if TempEDocumentImportedLine.IsEmpty() or TempPurchaseLine.IsEmpty() then            Error(EmptyRecordErr);        VerifyPurchaseOrderLinesHaveSameFieldValuesOrError(TempPurchaseLine);        VerifyImportedLinesHaveSameFieldValuesOrError(TempEDocumentImportedLine);        VerifyExistingMatchesHasSameFieldValue(TempEDocumentImportedLine, TempPurchaseLine);        if TempPurchaseLine.FindSet() then            repeat                MatchManyToOne(TempEDocumentImportedLine, TempPurchaseLine, TempEDocMatchesThatWasMatched);            until TempPurchaseLine.Next() = 0;    end;    local procedure VerifyImportedLinesHaveSameFieldValuesOrError(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary)    var        UnitCost: Decimal;        Discount: Decimal;        UOM: Code[20];    begin        TempEDocumentImportedLine.FindFirst();        UnitCost := TempEDocumentImportedLine."Direct Unit Cost";        Discount := TempEDocumentImportedLine."Line Discount %";        UOM := TempEDocumentImportedLine."Unit of Measure Code";        TempEDocumentImportedLine.FindSet();        repeat            if UnitCost <> TempEDocumentImportedLine."Direct Unit Cost" then                Error(UnitCostErr, TempEDocumentImportedLine.TableCaption());            if Discount <> TempEDocumentImportedLine."Line Discount %" then                Error(DiscountErr, TempEDocumentImportedLine.TableCaption());            if UOM <> TempEDocumentImportedLine."Unit of Measure Code" then                Error(UOMErr, TempEDocumentImportedLine.TableCaption());        until TempEDocumentImportedLine.Next() = 0;    end;    local procedure VerifyPurchaseOrderLinesHaveSameFieldValuesOrError(var TempPurchaseLine: Record "Purchase Line" temporary)    var        UnitCost: Decimal;        Discount: Decimal;        UOM: Code[20];    begin        TempPurchaseLine.FindFirst();        UnitCost := TempPurchaseLine."Direct Unit Cost";        Discount := TempPurchaseLine."Line Discount %";        UOM := TempPurchaseLine."Unit of Measure Code";        TempPurchaseLine.FindSet();        repeat            if UnitCost <> TempPurchaseLine."Direct Unit Cost" then                Error(UnitCostErr, TempPurchaseLine.TableCaption());            if Discount <> TempPurchaseLine."Line Discount %" then                Error(DiscountErr, TempPurchaseLine.TableCaption());            if UOM <> TempPurchaseLine."Unit of Measure Code" then                Error(UOMErr, TempPurchaseLine.TableCaption());        until TempPurchaseLine.Next() = 0;    end;    local procedure VerifyExistingMatchesHasSameFieldValue(var TempEDocImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary)    var        EDocOrderMatch: Record "E-Doc. Order Match";        TempTotal, Total : Decimal;    begin        TempPurchaseLine.FindFirst();        TempEDocImportedLine.FindFirst();        EDocOrderMatch.SetRange("Document Order No.", TempPurchaseLine."Document No.");        EDocOrderMatch.SetRange("E-Document Entry No.", TempEDocImportedLine."E-Document Entry No.");        EDocOrderMatch.SetRange("Document Line No.", TempPurchaseLine."Line No.");        // We only have to check first match as other matches would also have been checked.        if EDocOrderMatch.FindFirst() then begin            TempTotal := TempEDocImportedLine."Direct Unit Cost" - (TempEDocImportedLine."Direct Unit Cost" * TempEDocImportedLine."Line Discount %" / 100);            Total := EDocOrderMatch."E-Document Direct Unit Cost" - (EDocOrderMatch."E-Document Direct Unit Cost" * EDocOrderMatch."Line Discount %" / 100);            if TempTotal <> Total then                Error(AmountDiffErr, EDocOrderMatch."E-Document Line No.");        end;    end;    procedure AskToOverwrite(EDocument: Record "E-Document"; var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary)    var        EDocOrderMatch: Record "E-Doc. Order Match";        PurchaseLine: Record "Purchase Line";        EDocImportedLine: Record "E-Doc. Imported Line";        ConfirmManagement: Codeunit "Confirm Management";        Overwrite: Boolean;    begin        EDocOrderMatch.SetRange("E-Document Entry No.", EDocument."Entry No");        if not EDocOrderMatch.IsEmpty() then begin            Overwrite := ConfirmManagement.GetResponseOrDefault(OverwriteExistingMatchesTxt, false);            if Overwrite then begin                // Reset filters and qty                TempEDocumentImportedLine.Reset();                TempPurchaseLine.Reset();                if TempPurchaseLine.FindSet() then                    repeat                        ResetQtyToInvoice(TempPurchaseLine);                        PurchaseLine := TempPurchaseLine;#pragma warning disable AA0214                        PurchaseLine.Modify(true);#pragma warning restore AA0214                    until TempPurchaseLine.Next() = 0;                if TempEDocumentImportedLine.FindSet() then                    repeat                        ResetMatchedQty(TempEDocumentImportedLine);                        EDocImportedLine := TempEDocumentImportedLine;#pragma warning disable AA0214                        EDocImportedLine.Modify(true);#pragma warning restore AA0214                    until TempEDocumentImportedLine.Next() = 0;                EDocOrderMatch.DeleteAll();            end        end;    end;    procedure MatchAutomatically(EDocument: Record "E-Document"; var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary; var TempEDocMatchesThatWasMatched: Record "E-Doc. Order Match" temporary)    begin        if TempEDocumentImportedLine.IsEmpty() or TempPurchaseLine.IsEmpty() then            Error(EmptyRecordErr);        // Automatic matching will do the following:        // - Filter on items with the same unit of measure, direct unit cost and line discount %        // - If string nearness is above 80% then match automatically         if TempEDocumentImportedLine.FindSet() then            repeat                TempPurchaseLine.SetRange("Unit of Measure Code", TempEDocumentImportedLine."Unit Of Measure Code");                TempPurchaseLine.SetRange("Direct Unit Cost", TempEDocumentImportedLine."Direct Unit Cost");                TempPurchaseLine.SetRange("Line Discount %", TempEDocumentImportedLine."Line Discount %");                if TempPurchaseLine.FindSet() then                    repeat                        if LinesMatch(EDocument, TempEDocumentImportedLine, TempPurchaseLine) then                            MatchOneToOne(TempEDocumentImportedLine, TempPurchaseLine, TempEDocMatchesThatWasMatched);                    until TempPurchaseLine.Next() = 0;            until TempEDocumentImportedLine.Next() = 0;        TempPurchaseLine.Reset();        TempEDocumentImportedLine.Reset();    end;    local procedure LinesMatch(EDocument: Record "E-Document"; TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; TempPurchaseLine: Record "Purchase Line" temporary): Boolean    var        ItemReference: Record "Item Reference";        TextToAccountMapping: Record "Text-to-Account Mapping";        RecordMatchMgt: Codeunit "Record Match Mgt.";    begin        // Match G/L Account lines that have remaining quantity        if (TempPurchaseLine.Type <> Enum::"Purchase Line Type"::Item) and (TempPurchaseLine.Quantity - TempPurchaseLine."Quantity Invoiced" <= 0) then            exit(false);        if (TempPurchaseLine.Type = Enum::"Purchase Line Type"::Item) and (TempPurchaseLine.MaxQtyToInvoice() <= TempPurchaseLine."Qty. to Invoice") then            exit(false);        if not TempEDocumentImportedLine."Converted to Internal Notation" then begin            // Item Reference and Text to Account Mapping should only be used if we haven't already converted the line to internal notation            if TempPurchaseLine.Type = Enum::"Purchase Line Type"::Item then begin                ItemReference.SetRange("Item No.", TempPurchaseLine."No.");                ItemReference.SetRange("Unit of Measure", TempPurchaseLine."Unit of Measure Code");                ItemReference.SetRange("Reference Type", Enum::"Item Reference Type"::Vendor);                ItemReference.SetRange("Reference Type No.", EDocument."Bill-to/Pay-to No.");                ItemReference.SetRange("Reference No.", TempEDocumentImportedLine."No.");                if not ItemReference.IsEmpty() then                    exit(true); // An item reference matches the two lines (Item matches)            end;            if TempPurchaseLine.Type = Enum::"Purchase Line Type"::"G/L Account" then begin                TextToAccountMapping.SetRange("Vendor No.", EDocument."Bill-to/Pay-to No.");                TextToAccountMapping.SetRange("Debit Acc. No.", TempPurchaseLine."No.");                TextToAccountMapping.SetRange("Mapping Text", TempEDocumentImportedLine."No.");                if not TextToAccountMapping.IsEmpty() then                    exit(true); // A Text to account mapping matches the two lines (G/L Account matches)            end;        end;        if TempEDocumentImportedLine."Converted to Internal Notation" and (TempEDocumentImportedLine."No." = TempPurchaseLine."No.") then            exit(true);        if RecordMatchMgt.CalculateStringNearness(TempPurchaseLine.Description, TempEDocumentImportedLine.Description, 4, 100) > 80 then            exit(true);        exit(false);    end;    procedure InsertOrderMatch(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary; var TempEDocMatchesThatWasMatched: Record "E-Doc. Order Match" temporary; Quantity: Decimal; FullMatch: Boolean)    begin        TempEDocMatchesThatWasMatched.SetRange("Document Order No.", TempPurchaseLine."Document No.");        TempEDocMatchesThatWasMatched.SetRange("Document Line No.", TempPurchaseLine."Line No.");        TempEDocMatchesThatWasMatched.SetRange("E-Document Entry No.", TempEDocumentImportedLine."E-Document Entry No.");        TempEDocMatchesThatWasMatched.SetRange("E-Document Line No.", TempEDocumentImportedLine."Line No.");        TempEDocMatchesThatWasMatched.DeleteAll();        TempEDocMatchesThatWasMatched.Reset();        TempEDocMatchesThatWasMatched.Init();        TempEDocMatchesThatWasMatched.Validate("Document Order No.", TempPurchaseLine."Document No.");        TempEDocMatchesThatWasMatched.Validate("Document Line No.", TempPurchaseLine."Line No.");        TempEDocMatchesThatWasMatched.Validate("E-Document Entry No.", TempEDocumentImportedLine."E-Document Entry No.");        TempEDocMatchesThatWasMatched.Validate("E-Document Line No.", TempEDocumentImportedLine."Line No.");        TempEDocMatchesThatWasMatched.Validate("Precise Quantity", Quantity);        TempEDocMatchesThatWasMatched.Validate("E-Document Direct Unit Cost", TempEDocumentImportedLine."Direct Unit Cost");        TempEDocMatchesThatWasMatched.Validate("PO Direct Unit Cost", TempPurchaseLine."Direct Unit Cost");        TempEDocMatchesThatWasMatched.Validate("Line Discount %", TempEDocumentImportedLine."Line Discount %");        TempEDocMatchesThatWasMatched.Validate("Unit of Measure Code", TempEDocumentImportedLine."Unit of Measure Code");        TempEDocMatchesThatWasMatched.Validate("Fully Matched", FullMatch);        TempEDocMatchesThatWasMatched."PO Description" := TempPurchaseLine.Description;        TempEDocMatchesThatWasMatched."E-Document Description" := TempEDocumentImportedLine.Description;        TempEDocMatchesThatWasMatched.Insert();    end;    procedure MatchOneToOne(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary; var TempEDocMatchesThatWasMatched: Record "E-Doc. Order Match" temporary)    var        RemainingQuantityToMatch, TotalThatCanBeInvoiced : Decimal;        FullMatch: Boolean;    begin        // Calculate the quantity that is available to match for purchase order line        TotalThatCanBeInvoiced := CalculateAmountThatCanBeInvoiced(TempPurchaseLine);        if TotalThatCanBeInvoiced <= 0 then            exit;        // Calculate the quantity available to match for this imported line.         RemainingQuantityToMatch := TempEDocumentImportedLine.Quantity - TempEDocumentImportedLine."Matched Quantity";        if RemainingQuantityToMatch < TotalThatCanBeInvoiced then begin            TotalThatCanBeInvoiced := RemainingQuantityToMatch;            FullMatch := true;        end;        if TotalThatCanBeInvoiced < 1 then            exit;        TempPurchaseLine.Validate("Qty. to Invoice", TempPurchaseLine."Qty. to Invoice" + TotalThatCanBeInvoiced);        TempPurchaseLine.Modify(true);        TempEDocumentImportedLine.Validate("Matched Quantity", TempEDocumentImportedLine."Matched Quantity" + TotalThatCanBeInvoiced);        TempEDocumentImportedLine.Modify(true);        InsertOrderMatch(TempEDocumentImportedLine, TempPurchaseLine, TempEDocMatchesThatWasMatched, TotalThatCanBeInvoiced, FullMatch);    end;    procedure MatchManyToOne(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary; var TempEDocMatchesThatWasMatched: Record "E-Doc. Order Match" temporary)    begin        if TempEDocumentImportedLine.FindSet() then            repeat                MatchOneToOne(TempEDocumentImportedLine, TempPurchaseLine, TempEDocMatchesThatWasMatched)            until TempEDocumentImportedLine.Next() = 0;    end;    procedure MatchOneToMany(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary; var TempEDocMatchesThatWasMatched: Record "E-Doc. Order Match" temporary)    begin        if TempPurchaseLine.FindSet() then            repeat                MatchOneToOne(TempEDocumentImportedLine, TempPurchaseLine, TempEDocMatchesThatWasMatched)            until TempPurchaseLine.Next() = 0;    end;    procedure FilterOutFullyMatchedLines(var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary; var TempPurchaseLine: Record "Purchase Line" temporary)    var        TotalThatCanBeInvoiced: Decimal;    begin        TempEDocumentImportedLine.SetRange("Fully Matched", false);        if TempPurchaseLine.FindSet() then            repeat                TotalThatCanBeInvoiced := CalculateAmountThatCanBeInvoiced(TempPurchaseLine);                if TotalThatCanBeInvoiced > 0 then                    TempPurchaseLine.Mark(true);            until TempPurchaseLine.Next() = 0;        TempPurchaseLine.MarkedOnly(true);    end;    procedure CreateMatchingRule(PurchaseLine: Record "Purchase Line"; EDocument: Record "E-Document"; EDocImportedLine: Record "E-Doc. Imported Line")    var        Item: Record Item;        ItemReference: Record "Item Reference";        GLAccount: Record "G/L Account";        Vendor: Record Vendor;        TextToAccountMapping: Record "Text-to-Account Mapping";        TextToAccountMappingLookup: Record "Text-to-Account Mapping";    begin        OnBeforeCreateMatchingRule(PurchaseLine, EDocument);        if not Vendor.Get(EDocument."Bill-to/Pay-to No.") then            Error(CannotCreateMatchingRuleErr, EDocument."Bill-to/Pay-to No.");        if PurchaseLine.Type = Enum::"Purchase Line Type"::Item then            if Item.Get(PurchaseLine."No.") then begin                ItemReference."Item No." := Item."No.";                ItemReference."Unit of Measure" := PurchaseLine."Unit of Measure Code";                ItemReference."Reference Type" := Enum::"Item Reference Type"::Vendor;                ItemReference."Reference Type No." := Vendor."No.";                ItemReference."Reference No." := EDocImportedLine."No.";                ItemReference.Description := StrSubstNo(AutoGeneratedEDocumentTxt, EDocument."Entry No");                ItemReference.Insert();            end;        if PurchaseLine.Type = Enum::"Purchase Line Type"::"G/L Account" then            if GLAccount.Get(PurchaseLine."No.") then begin                if TextToAccountMappingLookup.FindLast() then;                TextToAccountMapping."Line No." := TextToAccountMappingLookup."Line No." + 1000;                TextToAccountMapping."Vendor No." := Vendor."No.";                TextToAccountMapping."Debit Acc. No." := GLAccount."No.";                TextToAccountMapping."Mapping Text" := EDocImportedLine."No.";                TextToAccountMapping.Insert();            end;    end;    internal procedure CreatePurchaseOrderLine(EDocument: Record "E-Document"; var TempEDocumentImportedLine: Record "E-Doc. Imported Line" temporary)    var        TempPurchaseLine: Record "Purchase Line" temporary;        PurchaseLine: Record "Purchase Line";        Item: Record Item;        GLAccount: Record "G/L Account";        PurchaseHeader: Record "Purchase Header";        EDocCreatePurchOrderLine: Page "E-Doc. Create Purch Order Line";    begin        // Create purchase line        TempPurchaseLine."Document Type" := TempPurchaseLine."Document Type"::Order;        TempPurchaseLine."Document No." := EDocument."Order No.";        PurchaseLine.SetRange("Document Type", TempPurchaseLine."Document Type");        PurchaseLine.SetRange("Document No.", TempPurchaseLine."Document No.");        if PurchaseLine.FindLast() then;        TempPurchaseLine."Line No." := PurchaseLine."Line No." + 10000;        TempPurchaseLine.Validate(Type, TempPurchaseLine.Type::"G/L Account"); // Default to G/L Account        TempPurchaseLine.Validate("Currency Code", EDocument."Currency Code");        if GLAccount.Get(TempEDocumentImportedLine."No.") then begin            TempPurchaseLine.Validate(Type, Enum::"Purchase Line Type"::"G/L Account");            TempPurchaseLine.Validate("No.", GLAccount."No.");        end else            if Item.Get(TempEDocumentImportedLine."No.") then begin                TempPurchaseLine.Validate(Type, Enum::"Purchase Line Type"::Item);                TempPurchaseLine.Validate("No.", Item."No.");            end;        TempPurchaseLine.Insert();        // Open purchase order line creation page        EDocCreatePurchOrderLine.SetSharedTable(TempPurchaseLine);        EDocCreatePurchOrderLine.SetEDocImportedLine(TempEDocumentImportedLine);        EDocCreatePurchOrderLine.LookupMode := true;        if EDocCreatePurchOrderLine.RunModal() = Action::LookupOK then begin            TempPurchaseLine.FindFirst();            PurchaseLine := TempPurchaseLine;            PurchaseLine.Insert(true);            if PurchaseLine.Type in [Enum::"Purchase Line Type"::Item, Enum::"Purchase Line Type"::"Fixed Asset"] then begin                Message(PurchaseLineCreatedReceiveItemsMsg);                PurchaseHeader.Get(Enum::"Purchase Document Type"::Order, TempPurchaseLine."Document No.");                Page.Run(Page::"Purchase Order", PurchaseHeader);            end else                Message(PurchaseLineCreatedMsg);        end;    end;    local procedure CalculateAmountThatCanBeInvoiced(var TempPurchaseLine: Record "Purchase Line" temporary): Decimal    begin        if TempPurchaseLine.Type <> Enum::"Purchase Line Type"::Item then            exit((TempPurchaseLine.Quantity - TempPurchaseLine."Quantity Invoiced") - TempPurchaseLine."Qty. to Invoice");        exit((TempPurchaseLine."Quantity Received" - TempPurchaseLine."Quantity Invoiced") - TempPurchaseLine."Qty. to Invoice");    end;    [IntegrationEvent(true, false)]    local procedure OnBeforeCreateMatchingRule(var PurchaseLine: Record "Purchase Line"; EDocument: Record "E-Document")    begin    end;}