Codeunit 6166 EDoc Import PEPPOL BIS 3.0

E-Document Core, Microsoft.eServices.EDocument.IO.Peppol

src/Apps/W1/EDocument/App/src/Format/EDocImportPEPPOLBIS30.Codeunit.al641 lines, Copyright (c) Microsoft Corporation. MIT

namespace Microsoft.eServices.EDocument.IO.Peppol;using Microsoft.eServices.EDocument;using Microsoft.Finance.GeneralLedger.Setup;using Microsoft.Foundation.Attachment;using Microsoft.Purchases.Document;using Microsoft.Purchases.Vendor;using System.IO;using System.Text;using System.Utilities;codeunit 6166 "EDoc Import PEPPOL BIS 3.0"{    procedure ParseBasicInfo(var EDocument: Record "E-Document"; var TempBlob: Codeunit "Temp Blob")    var        GLSetup: Record "General Ledger Setup";        TempXMLBuffer: Record "XML Buffer" temporary;        DocStream: InStream;        RootPath: Text;    begin        TempXMLBuffer.DeleteAll();        TempBlob.CreateInStream(DocStream, TextEncoding::UTF8);        TempXMLBuffer.LoadFromStream(DocStream);        GLSetup.Get();        LCYCode := GLSetup."LCY Code";        EDocument.Direction := EDocument.Direction::Incoming;        case UpperCase(GetDocumentType(TempXMLBuffer, RootPath)) of            InvoiceTok:                ParseInvoiceBasicInfo(EDocument, TempXMLBuffer, RootPath);            CreditNoteTok:                ParseCreditMemoBasicInfo(EDocument, TempXMLBuffer, RootPath);        end;    end;    procedure ParseCompleteInfo(var EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: record "Purchase Line" temporary; var TempBlob: Codeunit "Temp Blob")    var        TempXMLBuffer: Record "XML Buffer" temporary;        DocStream: InStream;        RootPath: Text;    begin        TempXMLBuffer.DeleteAll();        TempBlob.CreateInStream(DocStream, TextEncoding::UTF8);        TempXMLBuffer.LoadFromStream(DocStream);        PurchaseHeader."Buy-from Vendor No." := EDocument."Bill-to/Pay-to No.";        PurchaseHeader."Currency Code" := EDocument."Currency Code";        case UpperCase(GetDocumentType(TempXMLBuffer, RootPath)) of            InvoiceTok:                CreateInvoice(EDocument, PurchaseHeader, PurchaseLine, TempXMLBuffer, RootPath);            CreditNoteTok:                CreateCreditMemo(EDocument, PurchaseHeader, PurchaseLine, TempXMLBuffer, RootPath);        end;    end;    local procedure ParseInvoiceBasicInfo(var EDocument: Record "E-Document"; var TempXMLBuffer: Record "XML Buffer" temporary; RootPath: Text)    var        DueDate, IssueDate : Text;        Currency: Text[10];    begin        EDocument."Document Type" := EDocument."Document Type"::"Purchase Invoice";        EDocument."Incoming E-Document No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:ID'), 1, MaxStrLen(EDocument."Document No."));        ParseAccountingSupplierParty(EDocument, TempXMLBuffer, RootPath);        ParseAccountingCustomerParty(EDocument, TempXMLBuffer, RootPath);        DueDate := GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:DueDate');        if DueDate <> '' then            Evaluate(EDocument."Due Date", DueDate, 9);        IssueDate := GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:IssueDate');        if IssueDate <> '' then            Evaluate(EDocument."Document Date", IssueDate, 9);        Evaluate(EDocument."Amount Excl. VAT", GetNodeByPath(TempXMLBuffer, RootPath + '/cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount'), 9);        Evaluate(EDocument."Amount Incl. VAT", GetNodeByPath(TempXMLBuffer, RootPath + '/cac:LegalMonetaryTotal/cbc:PayableAmount'), 9);        EDocument."Order No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:OrderReference/cbc:ID'), 1, MaxStrLen(EDocument."Order No."));        Currency := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:DocumentCurrencyCode'), 1, MaxStrLen(EDocument."Currency Code"));        if LCYCode <> Currency then            EDocument."Currency Code" := Currency;    end;    local procedure ParseCreditMemoBasicInfo(var EDocument: Record "E-Document"; var TempXMLBuffer: Record "XML Buffer" temporary; RootPath: Text)    var        DueDate, IssueDate : Text;        Currency: Text[10];    begin        EDocument."Document Type" := EDocument."Document Type"::"Purchase Credit Memo";        EDocument."Incoming E-Document No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:ID'), 1, MaxStrLen(EDocument."Document No."));        ParseAccountingSupplierParty(EDocument, TempXMLBuffer, RootPath);        ParseAccountingCustomerParty(EDocument, TempXMLBuffer, RootPath);        DueDate := GetNodeByPath(TempXMLBuffer, RootPath + '/cac:PaymentMeans/cbc:PaymentDueDate');        if DueDate <> '' then            Evaluate(EDocument."Due Date", DueDate, 9);        IssueDate := GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:IssueDate');        if IssueDate <> '' then            Evaluate(EDocument."Document Date", IssueDate, 9);        Evaluate(EDocument."Amount Excl. VAT", GetNodeByPath(TempXMLBuffer, RootPath + '/cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount'), 9);        Evaluate(EDocument."Amount Incl. VAT", GetNodeByPath(TempXMLBuffer, RootPath + '/cac:LegalMonetaryTotal/cbc:PayableAmount'), 9);        EDocument."Order No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:OrderReference/cbc:ID'), 1, MaxStrLen(EDocument."Order No."));        Currency := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:DocumentCurrencyCode'), 1, MaxStrLen(EDocument."Currency Code"));        if LCYCode <> Currency then            EDocument."Currency Code" := Currency;    end;    local procedure ParseAccountingSupplierParty(var EDocument: Record "E-Document"; var TempXMLBuffer: Record "XML Buffer" temporary; RootPath: Text)    var        EDocumentService: Record "E-Document Service";        Vendor: Record Vendor;        EDocumentHelper: Codeunit "E-Document Helper";        GLN: Code[13];        VendorNo: Code[20];        VendorAddress, VendorName : Text;        VendorID: Text;        VATRegistrationNo: Text[20];    begin        // Read GLN or VAT Registration No based on the scheme ID.        if GetNodeAttributeByPath(TempXMLBuffer, RootPath + '/cac:AccountingSupplierParty/cac:Party/cbc:EndpointID/@schemeID') = GLNSchemeId() then            GLN := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingSupplierParty/cac:Party/cbc:EndpointID'), 1, MaxStrLen(GLN));        VATRegistrationNo := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingSupplierParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID'), 1, MaxStrLen(VATRegistrationNo));        VendorNo := EDocumentImportHelper.FindVendor('', GLN, VATRegistrationNo);        // If vendor not found, try to find by Service Participant.        if VendorNo = '' then begin            VendorID := GetNodeAttributeByPath(TempXMLBuffer, RootPath + '/cac:AccountingSupplierParty/cac:Party/cbc:EndpointID/@schemeID') + ':';            VendorID += this.GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingSupplierParty/cac:Party/cbc:EndpointID');            EDocumentHelper.GetEdocumentService(EDocument, EDocumentService);            VendorNo := EDocumentImportHelper.FindVendorByServiceParticipant(CopyStr(VendorID, 1, 200), EDocumentService.Code);        end;        // If vendor not found, try to find by name and address.        if VendorNo = '' then begin            VendorName := GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name');            VendorAddress := GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName');            VendorNo := EDocumentImportHelper.FindVendorByNameAndAddress(VendorName, VendorAddress);            EDocument."Bill-to/Pay-to Name" := CopyStr(VendorName, 1, MaxStrLen(EDocument."Bill-to/Pay-to Name"));        end;        Vendor := EDocumentImportHelper.GetVendor(EDocument, VendorNo);        if Vendor."No." <> '' then begin            EDocument."Bill-to/Pay-to No." := Vendor."No.";            EDocument."Bill-to/Pay-to Name" := Vendor.Name;        end;    end;    local procedure ParseAccountingCustomerParty(var EDocument: Record "E-Document"; var TempXMLBuffer: Record "XML Buffer" temporary; RootPath: Text)    var        CompanyIdentifierValue, SchemaId : Text;        ReceivingId: Text[250];    begin        Clear(EDocument."Receiving Company Name");        Clear(EDocument."Receiving Company Address");        Clear(EDocument."Receiving Company VAT Reg. No.");        Clear(EDocument."Receiving Company GLN");        Clear(EDocument."Receiving Company Id");        EDocument."Receiving Company Name" := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cac:PartyName/cbc:Name'), 1, MaxStrLen(EDocument."Receiving Company Name"));        EDocument."Receiving Company Address" := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName'), 1, MaxStrLen(EDocument."Receiving Company Address"));        SchemaId := GetNodeAttributeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID');        CompanyIdentifierValue := GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cbc:EndpointID');        EDocument."Receiving Company VAT Reg. No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cac:PartyTaxScheme/cbc:CompanyID'), 1, MaxStrLen(EDocument."Receiving Company VAT Reg. No."));        if SchemaId = '0088' then            EDocument."Receiving Company GLN" := CopyStr(CompanyIdentifierValue, 1, MaxStrLen(EDocument."Receiving Company GLN"));        if (EDocument."Receiving Company VAT Reg. No." = '') and (SchemaId <> '0088') then            EDocument."Receiving Company VAT Reg. No." := CopyStr(CompanyIdentifierValue, 1, MaxStrLen(EDocument."Receiving Company VAT Reg. No."));        SchemaId := GetNodeAttributeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID/@schemeID');        CompanyIdentifierValue := GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID');        if (EDocument."Receiving Company GLN" = '') and (SchemaId = '0088') then            EDocument."Receiving Company GLN" := CopyStr(CompanyIdentifierValue, 1, MaxStrLen(EDocument."Receiving Company GLN"));        ReceivingId := CopyStr(this.GetNodeAttributeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cbc:EndpointID/@schemeID'), 1, (MaxStrLen(EDocument."Receiving Company Id") - 1)) + ':';        ReceivingId += CopyStr(this.GetNodeByPath(TempXMLBuffer, RootPath + '/cac:AccountingCustomerParty/cac:Party/cbc:EndpointID'), 1, MaxStrLen(EDocument."Receiving Company Id") - StrLen(ReceivingId));        EDocument."Receiving Company Id" := ReceivingId;    end;    local procedure CreateInvoice(var EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: record "Purchase Line" temporary; var TempXMLBuffer: Record "XML Buffer" temporary; RootPath: Text)    var        DocumentAttachment: Record "Document Attachment";        DocumentAttachmentData: Codeunit "Temp Blob";        InStream: InStream;        LineNo: Integer;    begin        PurchaseHeader."Document Type" := PurchaseHeader."Document Type"::Invoice;        PurchaseHeader."No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:ID'), 1, MaxStrLen(PurchaseHeader."No."));        PurchaseHeader."Vendor Order No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cac:OrderReference/cbc:ID'), 1, MaxStrLen(PurchaseHeader."Vendor Order No."));        // Currency        PurchaseHeader.Insert();        TempXMLBuffer.Reset();        if TempXMLBuffer.FindSet() then            repeat                ParseInvoice(EDocument, PurchaseHeader, PurchaseLine, DocumentAttachment, DocumentAttachmentData, TempXMLBuffer, LineNo, RootPath);            until TempXMLBuffer.Next() = 0;        // Insert last document attachment        if (DocumentAttachment."No." <> '') and (DocumentAttachment."File Name" <> '') then begin            DocumentAttachmentData.CreateInStream(InStream, TextEncoding::UTF8);            EDocumentAttachmentGen.Insert(EDocument, InStream, DocumentAttachment.FindUniqueFileName(DocumentAttachment."File Name", DocumentAttachment."File Extension"));            Clear(DocumentAttachment);        end;        // Insert last line        PurchaseLine.Insert();        PurchaseHeader."Prices Including VAT" := false;        PurchaseHeader.Modify();        // Allowance charge        CreateInvoiceAllowanceChargeLines(EDocument, PurchaseHeader, PurchaseLine, TempXMLBuffer, RootPath);    end;    local procedure CreateCreditMemo(var EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: record "Purchase Line" temporary; var TempXMLBuffer: Record "XML Buffer" temporary; RootPath: Text)    var        DocumentAttachment: Record "Document Attachment";        DocumentAttachmentData: Codeunit "Temp Blob";        InStream: InStream;        LineNo: Integer;    begin        PurchaseHeader."Document Type" := PurchaseHeader."Document Type"::"Credit Memo";        PurchaseHeader."No." := CopyStr(GetNodeByPath(TempXMLBuffer, RootPath + '/cbc:ID'), 1, MaxStrLen(PurchaseHeader."No."));        PurchaseHeader.Insert();        TempXMLBuffer.Reset();        if TempXMLBuffer.FindSet() then            repeat                ParseCreditMemo(EDocument, PurchaseHeader, PurchaseLine, DocumentAttachment, DocumentAttachmentData, TempXMLBuffer, LineNo, RootPath);            until TempXMLBuffer.Next() = 0;        // Insert last document attachment        if (DocumentAttachment."No." <> '') and (DocumentAttachment."File Name" <> '') then begin            DocumentAttachmentData.CreateInStream(InStream, TextEncoding::UTF8);            EDocumentAttachmentGen.Insert(EDocument, InStream, DocumentAttachment.FindUniqueFileName(DocumentAttachment."File Name", DocumentAttachment."File Extension"));            Clear(DocumentAttachment);        end;        // Insert last line        PurchaseLine.Insert();        PurchaseHeader.Modify();        // Allowance charge        CreateInvoiceAllowanceChargeLines(EDocument, PurchaseHeader, PurchaseLine, TempXMLBuffer, RootPath);    end;    local procedure CreateInvoiceAllowanceChargeLines(var EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: record "Purchase Line" temporary; var TempXMLBuffer: Record "XML Buffer" temporary; RootPath: Text)    var        LineNo: Integer;    begin        TempXMLBuffer.Reset();        TempXMLBuffer.SetFilter(Path, RootPath + '/cac:AllowanceCharge*');        PurchaseLine.FindLast();        LineNo := PurchaseLine."Line No." + 10000;        if TempXMLBuffer.FindSet() then            repeat                case TempXMLBuffer.Path of                    RootPath + '/cac:AllowanceCharge/cbc:ChargeIndicator':                        if TempXMLBuffer.Value = 'true' then begin                            SetGLAccountAndInsertLine(EDocument, PurchaseLine, LineNo);                            PurchaseLine.Init();                            PurchaseLine."Document Type" := PurchaseHeader."Document Type";                            PurchaseLine."Document No." := PurchaseHeader."No.";                            PurchaseLine."Line No." := LineNo;                            PurchaseLine.Quantity := 1;                            PurchaseLine.Type := PurchaseLine.Type::"G/L Account";                        end;                    RootPath + '/cac:AllowanceCharge/cbc:Amount':                        if TempXMLBuffer.Value <> '' then begin                            Evaluate(PurchaseLine."Direct Unit Cost", TempXMLBuffer.Value, 9);                            Evaluate(PurchaseLine.Amount, TempXMLBuffer.Value, 9);                        end;                    RootPath + '/cac:AllowanceCharge/cbc:AllowanceChargeReason':                        PurchaseLine.Description := CopyStr(TempXMLBuffer.Value, 1, MaxStrLen(PurchaseLine.Description));                end;            until TempXMLBuffer.Next() = 0;        SetGLAccountAndInsertLine(EDocument, PurchaseLine, LineNo);    end;    local procedure SetGLAccountAndInsertLine(var EDocument: Record "E-Document"; var PurchaseLine: record "Purchase Line" temporary; var LineNo: Integer)    var        RecRef: RecordRef;    begin        if PurchaseLine."Line No." = LineNo then begin            RecRef.GetTable(PurchaseLine);            EDocumentImportHelper.FindGLAccountForLine(EDocument, RecRef);            PurchaseLine."No." := RecRef.Field(PurchaseLine.FieldNo("No.")).Value;            PurchaseLine.Insert();            LineNo += 10000;        end;    end;    /// <summary>    /// Parses credit memo information line by line from TempXMLBuffer.    /// We handle the insert of Purchase Order Line and Document Attachment after the call to this function.    /// </summary>    local procedure ParseCreditMemo(EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: record "Purchase Line" temporary; var DocumentAttachment: Record "Document Attachment"; DocumentAttachmentData: Codeunit "Temp Blob"; var TempXMLBuffer: Record "XML Buffer" temporary; var LineNo: Integer; RootPath: Text)    var        Base64Convert: Codeunit "Base64 Convert";        InStream: InStream;        OutStream: OutStream;        Path, Value : Text;    begin        Path := TempXMLBuffer.Path;        Value := TempXMLBuffer.Value;        case Path of            RootPath + '/cbc:ID':                PurchaseHeader."Vendor Invoice No." := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Vendor Invoice No."));            RootPath + '/cbc:IssueDate':                if Value <> '' then                    Evaluate(PurchaseHeader."Document Date", Value, 9);            RootPath + '/cac:OrderReference/cbc:ID':                PurchaseHeader."Vendor Order No." := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Vendor Order No."));            RootPath + '/cac:BillingReference/cac:InvoiceDocumentReference/cbc:ID':                PurchaseHeader."Applies-to Doc. No." := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Applies-to Doc. No."));            RootPath + '/cac:PayeeParty/cac:PartyName/cbc:Name':                PurchaseHeader."Pay-to Name" := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Pay-to Name"));            RootPath + '/cac:LegalMonetaryTotal/cbc:AllowanceTotalAmount':                if Value <> '' then                    Evaluate(PurchaseHeader."Invoice Discount Value", Value, 9);            RootPath + '/cac:LegalMonetaryTotal/cbc:PayableAmount':                if Value <> '' then                    Evaluate(PurchaseHeader."Amount Including VAT", Value, 9);            RootPath + '/cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ID':                PurchaseHeader."Your Reference" := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Your Reference"));            RootPath + '/cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName':                PurchaseHeader."Buy-from Address" := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Buy-from Address"));            RootPath + '/cac:PayeeParty/cac:PartyLegalEntity/cbc:CompanyID', RootPath + '/cac:PayeeParty/cac:PartyIdentification/cbc:ID':                PurchaseHeader."VAT Registration No." := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."VAT Registration No."));            RootPath + '/cac:PaymentMeans/cbc:PaymentDueDate':                if Value <> '' then                    Evaluate(PurchaseHeader."Due Date", Value, 9);            RootPath + '/cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount':                if Value <> '' then                    Evaluate(PurchaseHeader.Amount, Value, 9);            RootPath + '/cac:AdditionalDocumentReference/cbc:ID':                begin                    if (DocumentAttachment."No." <> '') and (DocumentAttachment."File Name" <> '') then begin                        DocumentAttachmentData.CreateInStream(InStream, TextEncoding::UTF8);                        EDocumentAttachmentGen.Insert(EDocument, InStream, DocumentAttachment.FindUniqueFileName(DocumentAttachment."File Name", DocumentAttachment."File Extension"));                        Clear(DocumentAttachment);                    end;                    DocumentAttachment.Init();                    DocumentAttachment."No." := CopyStr(PurchaseHeader."Vendor Invoice No.", 1, MaxStrLen(DocumentAttachment."No."));                end;            RootPath + '/cac:AdditionalDocumentReference/cac:Attachment/cbc:EmbeddedDocumentBinaryObject':                begin                    DocumentAttachmentData.CreateOutStream(OutStream, TextEncoding::UTF8);                    TempXMLBuffer.CalcFields("Value BLOB");                    TempXMLBuffer."Value BLOB".CreateInStream(InStream);                    InStream.Read(Value, InStream.Length);                    Base64Convert.FromBase64(Value, OutStream);                end;            RootPath + '/cac:AdditionalDocumentReference/cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@mimeCode':                DocumentAttachment.Validate("File Extension", DetermineFileType(Value));            RootPath + '/cac:AdditionalDocumentReference/cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@filename':                DocumentAttachment."File Name" := CopyStr(Value.Split('.').Get(1), 1, MaxStrLen(DocumentAttachment."File Name"));            RootPath + '/cac:CreditNoteLine':                begin                    if PurchaseLine."Document No." <> '' then                        PurchaseLine.Insert();                    PurchaseLine.Init();                    PurchaseLine."Document Type" := PurchaseHeader."Document Type";                    PurchaseLine."Document No." := PurchaseHeader."No.";                    LineNo += 10000;                    PurchaseLine."Line No." := LineNo;                end;            RootPath + '/cac:CreditNoteLine/cbc:CreditedQuantity':                if Value <> '' then                    Evaluate(PurchaseLine.Quantity, Value, 9);            RootPath + '/cac:CreditNoteLine/cbc:CreditedQuantity/@unitCode':                PurchaseLine."Unit of Measure Code" := CopyStr(Value, 1, MaxStrLen(PurchaseLine."Unit of Measure Code"));            RootPath + '/cac:CreditNoteLine/cbc:LineExtensionAmount':                if Value <> '' then                    Evaluate(PurchaseLine.Amount, Value, 9);            RootPath + '/cac:CreditNoteLine/cac:AllowanceCharge/cbc:Amount':                if Value <> '' then                    Evaluate(PurchaseLine."Line Discount Amount", Value, 9);            RootPath + '/cac:CreditNoteLine/cac:TaxTotal/cbc:TaxAmount':                if Value <> '' then                    Evaluate(PurchaseLine."Amount Including VAT", Value, 9);            RootPath + '/cac:CreditNoteLine/cac:Item/cbc:Description':                PurchaseLine."Description 2" := CopyStr(Value, 1, MaxStrLen(PurchaseLine."Description 2"));            RootPath + '/cac:CreditNoteLine/cac:Item/cbc:Name':                PurchaseLine.Description := CopyStr(Value, 1, MaxStrLen(PurchaseLine.Description));            RootPath + '/cac:CreditNoteLine/cac:Item/cac:SellersItemIdentification/cbc:ID':                PurchaseLine."Item Reference No." := CopyStr(Value, 1, MaxStrLen(PurchaseLine."Item Reference No."));            RootPath + '/cac:CreditNoteLine/cac:Item/cac:StandardItemIdentification/cbc:ID':                PurchaseLine."No." := CopyStr(Value, 1, MaxStrLen(PurchaseLine."No."));            RootPath + '/cac:CreditNoteLine/cac:Item/cac:ClassifiedTaxCategory/cbc:Percent':                if Value <> '' then                    Evaluate(PurchaseLine."VAT %", Value, 9);            RootPath + '/cac:CreditNoteLine/cac:Price/cbc:PriceAmount':                if Value <> '' then                    Evaluate(PurchaseLine."Direct Unit Cost", Value, 9);            RootPath + '/cac:CreditNoteLine/cac:Price/cbc:BaseQuantity':                if Value <> '' then                    Evaluate(PurchaseLine."Quantity (Base)", Value, 9);            RootPath + '/cac:CreditNoteLine/cbc:Note':                setlineType(PurchaseLine, Value);        end;        OnAfterParseCreditMemo(EDocument, PurchaseHeader, PurchaseLine, DocumentAttachment, DocumentAttachmentData, TempXMLBuffer, Path, Value);    end;    /// <summary>    /// Parses invoice information line by line from TempXMLBuffer.    /// We handle the insert of Purchase Order Line and Document Attachment after the call to this function.    /// </summary>    local procedure ParseInvoice(EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: Record "Purchase Line" temporary; var DocumentAttachment: Record "Document Attachment"; DocumentAttachmentData: Codeunit "Temp Blob"; var TempXMLBuffer: Record "XML Buffer" temporary; var LineNo: Integer; RootPath: Text)    var        Base64Convert: Codeunit "Base64 Convert";        InStream: InStream;        OutStream: OutStream;        Path, Value : Text;    begin        Path := TempXMLBuffer.Path;        Value := TempXMLBuffer.Value;        case Path of            RootPath + '/cbc:ID':                PurchaseHeader."Vendor Invoice No." := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Vendor Invoice No."));            RootPath + '/cac:OrderReference/cbc:ID':                PurchaseHeader."Vendor Order No." := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Vendor Order No."));            RootPath + '/cac:PayeeParty/cac:PartyName/cbc:Name':                PurchaseHeader."Pay-to Name" := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Pay-to Name"));            RootPath + '/cac:LegalMonetaryTotal/cbc:AllowanceTotalAmount':                if Value <> '' then                    Evaluate(PurchaseHeader."Invoice Discount Value", Value, 9);            RootPath + '/cac:LegalMonetaryTotal/cbc:PayableAmount':                if Value <> '' then                    Evaluate(PurchaseHeader."Amount Including VAT", Value, 9);            RootPath + '/cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ID':                PurchaseHeader."Your Reference" := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Your Reference"));            RootPath + '/cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName':                PurchaseHeader."Buy-from Address" := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."Buy-from Address"));            RootPath + '/cac:PayeeParty/cac:PartyLegalEntity/cbc:CompanyID':                PurchaseHeader."VAT Registration No." := CopyStr(Value, 1, MaxStrLen(PurchaseHeader."VAT Registration No."));            RootPath + '/cac:LegalMonetaryTotal/cbc:TaxExclusiveAmount':                if Value <> '' then                    Evaluate(PurchaseHeader.Amount, Value, 9);            RootPath + '/cbc:DueDate':                if Value <> '' then                    Evaluate(PurchaseHeader."Due Date", Value, 9);            RootPath + '/cbc:IssueDate':                if Value <> '' then                    Evaluate(PurchaseHeader."Document Date", Value, 9);            RootPath + '/cac:AdditionalDocumentReference/cbc:ID':                begin                    if (DocumentAttachment."No." <> '') and (DocumentAttachment."File Name" <> '') then begin                        DocumentAttachmentData.CreateInStream(InStream, TextEncoding::UTF8);                        EDocumentAttachmentGen.Insert(EDocument, InStream, DocumentAttachment.FindUniqueFileName(DocumentAttachment."File Name", DocumentAttachment."File Extension"));                        Clear(DocumentAttachment);                    end;                    DocumentAttachment.Init();                    DocumentAttachment."No." := CopyStr(Value, 1, MaxStrLen(DocumentAttachment."No."));                end;            RootPath + '/cac:AdditionalDocumentReference/cac:Attachment/cbc:EmbeddedDocumentBinaryObject':                begin                    DocumentAttachmentData.CreateOutStream(OutStream, TextEncoding::UTF8);                    TempXMLBuffer.CalcFields("Value BLOB");                    TempXMLBuffer."Value BLOB".CreateInStream(InStream);                    InStream.Read(Value, InStream.Length);                    Base64Convert.FromBase64(Value, OutStream);                end;            RootPath + '/cac:AdditionalDocumentReference/cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@mimeCode':                DocumentAttachment.Validate("File Extension", DetermineFileType(Value));            RootPath + '/cac:AdditionalDocumentReference/cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@filename':                DocumentAttachment."File Name" := CopyStr(Value.Split('.').Get(1), 1, MaxStrLen(DocumentAttachment."File Name"));            RootPath + '/cac:InvoiceLine':                begin                    if PurchaseLine."Document No." <> '' then                        PurchaseLine.Insert();                    PurchaseLine.Init();                    PurchaseLine."Document Type" := PurchaseHeader."Document Type";                    PurchaseLine."Document No." := PurchaseHeader."No.";                    LineNo += 10000;                    PurchaseLine."Line No." := LineNo;                end;            RootPath + '/cac:InvoiceLine/cbc:InvoicedQuantity':                if Value <> '' then                    Evaluate(PurchaseLine.Quantity, Value, 9);            RootPath + '/cac:InvoiceLine/cbc:InvoicedQuantity/@unitCode':                PurchaseLine."Unit of Measure Code" := CopyStr(Value, 1, MaxStrLen(PurchaseLine."Unit of Measure Code"));            RootPath + '/cac:InvoiceLine/cbc:LineExtensionAmount':                if Value <> '' then                    Evaluate(PurchaseLine.Amount, Value, 9);            RootPath + '/cac:InvoiceLine/cac:AllowanceCharge/cbc:Amount':                if Value <> '' then                    Evaluate(PurchaseLine."Line Discount Amount", Value, 9);            RootPath + '/cac:InvoiceLine/cac:TaxTotal/cbc:TaxAmount':                if Value <> '' then                    Evaluate(PurchaseLine."Amount Including VAT", Value, 9);            RootPath + '/cac:InvoiceLine/cac:Item/cbc:Description':                PurchaseLine."Description 2" := CopyStr(Value, 1, MaxStrLen(PurchaseLine."Description 2"));            RootPath + '/cac:InvoiceLine/cac:Item/cbc:Name':                PurchaseLine.Description := CopyStr(Value, 1, MaxStrLen(PurchaseLine.Description));            RootPath + '/cac:InvoiceLine/cac:Item/cac:SellersItemIdentification/cbc:ID':                PurchaseLine."Item Reference No." := CopyStr(Value, 1, MaxStrLen(PurchaseLine."Item Reference No."));            RootPath + '/cac:InvoiceLine/cac:Item/cac:StandardItemIdentification/cbc:ID':                PurchaseLine."No." := CopyStr(Value, 1, MaxStrLen(PurchaseLine."No."));            RootPath + '/cac:InvoiceLine/cac:Item/cac:ClassifiedTaxCategory/cbc:Percent':                if Value <> '' then                    Evaluate(PurchaseLine."VAT %", Value, 9);            RootPath + '/cac:InvoiceLine/cac:Price/cbc:PriceAmount':                if Value <> '' then                    Evaluate(PurchaseLine."Direct Unit Cost", Value, 9);            RootPath + '/cac:InvoiceLine/cac:Price/cbc:BaseQuantity':                if Value <> '' then                    Evaluate(PurchaseLine."Quantity (Base)", Value, 9);            RootPath + '/cac:InvoiceLine/cbc:Note':                setlineType(PurchaseLine, Value);        end;        OnAfterParseInvoice(EDocument, PurchaseHeader, PurchaseLine, DocumentAttachment, DocumentAttachmentData, TempXMLBuffer, Path, Value);    end;    procedure DetermineFileType(MimeType: Text) FileExension: Text    begin        case MimeType of            'image/jpeg':                exit('jpeg');            'image/png':                exit('png');            'application/pdf':                exit('pdf');            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',            'application/vnd.oasis.opendocument.spreadsheet':                exit('xlsx');            else                exit('');        end;    end;    local procedure GetNodeByPath(var TempXMLBuffer: Record "XML Buffer" temporary; XPath: Text): Text    begin        TempXMLBuffer.Reset();        TempXMLBuffer.SetRange(Type, TempXMLBuffer.Type::Element);        TempXMLBuffer.SetRange(Path, XPath);        if TempXMLBuffer.FindFirst() then            exit(TempXMLBuffer.Value);    end;    local procedure GetNodeAttributeByPath(var TempXMLBuffer: Record "XML Buffer" temporary; XPath: Text): Text    begin        TempXMLBuffer.Reset();        TempXMLBuffer.SetRange(Type, TempXMLBuffer.Type::Attribute);        TempXMLBuffer.SetRange(Path, XPath);        if TempXMLBuffer.FindFirst() then            exit(TempXMLBuffer.Value);    end;    local procedure GLNSchemeId(): Text    begin        exit('0088');    end;    local procedure GetDocumentType(var TempXMLBuffer: Record "XML Buffer" temporary; var RootPath: Text): Text    var        InvalidXMLFileErr: Label 'Invalid XML file.';    begin        TempXMLBuffer.Reset();        TempXMLBuffer.SetRange(Type, TempXMLBuffer.Type::Element);        TempXMLBuffer.SetRange("Parent Entry No.", 0);        if not TempXMLBuffer.FindFirst() then            Error(InvalidXMLFileErr);        RootPath := TempXMLBuffer.Path;        TempXMLBuffer.Reset();        exit(TempXMLBuffer.Name);    end;    local procedure SetLineType(var PurchaseLine: record "Purchase Line" temporary; Value: Text): Text    var        ItemTok: Label 'ITEM', Locked = true;        ChargeItemTok: Label 'CHARGE (ITEM)', Locked = true;        ResourceTok: Label 'RESOURCE', Locked = true;        GLAccountTok: Label 'G/L ACCOUNT', Locked = true;    begin        case UpperCase(Value) of            ItemTok:                PurchaseLine.Type := PurchaseLine.Type::Item;            ChargeItemTok:                PurchaseLine.Type := PurchaseLine.Type::"Charge (Item)";            ResourceTok:                PurchaseLine.Type := PurchaseLine.Type::Resource;            GLAccountTok:                PurchaseLine.Type := PurchaseLine.Type::"G/L Account";        end;    end;    [EventSubscriber(ObjectType::Table, Database::"E-Document Log", 'OnBeforeExportDataStorage', '', false, false)]    local procedure SetFileExt(EDocumentLog: Record "E-Document Log"; var FileName: Text)    var        FileManagement: Codeunit "File Management";    begin        if EDocumentLog."Document Format" <> Enum::"E-Document Format"::"PEPPOL BIS 3.0" then            exit;        if FileManagement.GetExtension(FileName) <> '' then            exit;        FileName += '.xml';    end;    var        EDocumentAttachmentGen: Codeunit "E-Doc. Attachment Processor";        EDocumentImportHelper: Codeunit "E-Document Import Helper";        InvoiceTok: Label 'INVOICE', Locked = true;        CreditNoteTok: Label 'CREDITNOTE', Locked = true;        LCYCode: Code[10];    [IntegrationEvent(false, false)]    internal procedure OnAfterParseInvoice(EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: Record "Purchase Line" temporary; DocumentAttachment: Record "Document Attachment"; DocumentAttachmentData: Codeunit "Temp Blob"; TempXMLBuffer: Record "XML Buffer" temporary; Path: Text; Value: Text)    begin    end;    [IntegrationEvent(false, false)]    internal procedure OnAfterParseCreditMemo(EDocument: Record "E-Document"; var PurchaseHeader: Record "Purchase Header" temporary; var PurchaseLine: Record "Purchase Line" temporary; DocumentAttachment: Record "Document Attachment"; DocumentAttachmentData: Codeunit "Temp Blob"; TempXMLBuffer: Record "XML Buffer" temporary; Path: Text; Value: Text)    begin    end;}