-
Notifications
You must be signed in to change notification settings - Fork 693
[Enforced Digital Vouchers] - Attaching E-Documents when using Digital Vouchers #29793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1266f03
efdcbbe
3722878
7a0e6d4
7e36e80
9d7535b
597bf71
d935a24
8b9bd7d
5264655
acc7cb2
c035881
30dc167
42dee03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ using Microsoft.Finance.GeneralLedger.Journal; | |
| using Microsoft.Finance.GeneralLedger.Ledger; | ||
| using Microsoft.Finance.GeneralLedger.Posting; | ||
| using Microsoft.Finance.GeneralLedger.Reports; | ||
| using System.Telemetry; | ||
| using Microsoft.Foundation.AuditCodes; | ||
| using Microsoft.Foundation.Reporting; | ||
| using Microsoft.Purchases.Document; | ||
|
|
@@ -24,6 +25,7 @@ using Microsoft.Service.History; | |
| using Microsoft.Service.Posting; | ||
| using System.Email; | ||
| using System.Environment.Configuration; | ||
| using System.IO; | ||
| using System.Media; | ||
| using System.Reflection; | ||
| using System.Utilities; | ||
|
|
@@ -266,6 +268,143 @@ codeunit 5579 "Digital Voucher Impl." | |
| exit(not IncomingDocumentAttachment.IsEmpty()); | ||
| end; | ||
|
|
||
| local procedure AttachIncomingEDocument(EDocument: Record "E-Document"; SourceDocumentHeader: RecordRef; var TempBlob: Codeunit "Temp Blob") | ||
| var | ||
| DigitalVoucherEntrySetup: Record "Digital Voucher Entry Setup"; | ||
| IncomingDocumentAttachment: Record "Incoming Document Attachment"; | ||
| EDocumentHelper: Codeunit "E-Document Processing"; | ||
| ImportAttachmentIncDoc: Codeunit "Import Attachment - Inc. Doc."; | ||
| FeatureTelemetry: Codeunit "Feature Telemetry"; | ||
| FileNameTok: Label 'E-Document_%1.xml', Locked = true; | ||
| RecordLinkTxt: Text; | ||
| begin | ||
| if not DigitalVoucherFeature.IsFeatureEnabled() then | ||
| exit; | ||
|
|
||
| GetDigitalVoucherEntrySetup(DigitalVoucherEntrySetup, "Digital Voucher Entry Type"::"Sales Document"); | ||
| if DigitalVoucherEntrySetup."Check Type" <> DigitalVoucherEntrySetup."Check Type"::"E-Document" then | ||
| exit; | ||
|
|
||
| if not (EDocument."Document Type" in [ | ||
| EDocument."Document Type"::"Sales Invoice", | ||
| EDocument."Document Type"::"Sales Credit Memo", | ||
| EDocument."Document Type"::"Sales Order", | ||
| EDocument."Document Type"::"Sales Quote", | ||
| EDocument."Document Type"::"Sales Return Order"]) then | ||
| exit; | ||
|
|
||
| RecordLinkTxt := EDocumentHelper.GetRecordLinkText(EDocument); | ||
| IncomingDocumentAttachment.SetRange("Document No.", EDocument."Document No."); | ||
| IncomingDocumentAttachment.SetRange("Posting Date", EDocument."Posting Date"); | ||
| IncomingDocumentAttachment.SetContentFromBlob(TempBlob); | ||
| if not ImportAttachmentIncDoc.ImportAttachment(IncomingDocumentAttachment, StrSubstNo(FileNameTok, RecordLinkTxt), TempBlob) then begin | ||
| FeatureTelemetry.LogError('', 'E-Documents', 'AttachIncomingEDocument', GetLastErrorText(true), GetLastErrorCallStack()); | ||
| exit; | ||
| end; | ||
|
|
||
| IncomingDocumentAttachment."Is E-Document" := true; | ||
| IncomingDocumentAttachment.Modify(false); | ||
| end; | ||
|
|
||
| local procedure AttachOutgoingEDocument(var EDocument: Record "E-Document"; PostedRecord: Variant) | ||
| var | ||
| DigitalVoucherEntrySetup: Record "Digital Voucher Entry Setup"; | ||
| VoucherEDocumentCheck: Codeunit "Voucher E-Document Check"; | ||
| RecRef: RecordRef; | ||
| DocNo: Code[20]; | ||
| PostingDate: Date; | ||
| DocType: Text; | ||
| begin | ||
| if not DigitalVoucherFeature.IsFeatureEnabled() then | ||
| exit; | ||
|
|
||
| GetDigitalVoucherEntrySetup(DigitalVoucherEntrySetup, "Digital Voucher Entry Type"::"Purchase Document"); | ||
| if DigitalVoucherEntrySetup."Check Type" <> DigitalVoucherEntrySetup."Check Type"::"E-Document" then | ||
| exit; | ||
|
|
||
| if not (EDocument."Document Type" in [ | ||
| EDocument."Document Type"::"Purchase Invoice", | ||
| EDocument."Document Type"::"Purchase Credit Memo", | ||
| EDocument."Document Type"::"Purchase Order", | ||
| EDocument."Document Type"::"Purchase Quote", | ||
| EDocument."Document Type"::"Purchase Return Order"]) then | ||
| exit; | ||
|
|
||
| RecRef.GetTable(PostedRecord); | ||
| DigitalVoucherEntry.GetDocNoAndPostingDateFromRecRef(DocType, DocNo, PostingDate, RecRef); | ||
| AttachPurchaseEDocument(EDocument, DocNo, PostingDate); | ||
| end; | ||
|
|
||
| local procedure AttachPurchaseEDocument(EDocument: Record "E-Document"; DocumentNo: Code[20]; PostingDate: Date) | ||
| var | ||
| EDocDataStorage: Record "E-Doc. Data Storage"; | ||
| IncomingDocumentAttachment: Record "Incoming Document Attachment"; | ||
| ImportAttachmentIncDoc: Codeunit "Import Attachment - Inc. Doc."; | ||
| TempBlob: Codeunit "Temp Blob"; | ||
| FeatureTelemetry: Codeunit "Feature Telemetry"; | ||
| EDocumentFileNameLbl: Label 'E-Document_%1.%2', Comment = '%1 = E-Document Entry No., %2 = File Format', Locked = true; | ||
| FileName: Text[250]; | ||
| begin | ||
| if EDocument."Unstructured Data Entry No." = 0 then | ||
| exit; | ||
|
|
||
| if not EDocDataStorage.Get(EDocument."Unstructured Data Entry No.") then | ||
| exit; | ||
|
|
||
| TempBlob := EDocDataStorage.GetTempBlob(); | ||
| if not TempBlob.HasValue() then | ||
| exit; | ||
|
|
||
| if EDocument."File Name" <> '' then | ||
| FileName := CopyStr(EDocument."File Name", 1, MaxStrLen(FileName)) | ||
| else | ||
| FileName := StrSubstNo(EDocumentFileNameLbl, EDocument."Entry No", EDocDataStorage."File Format"); | ||
|
|
||
| IncomingDocumentAttachment.SetRange("Document No.", DocumentNo); | ||
| IncomingDocumentAttachment.SetRange("Posting Date", PostingDate); | ||
| IncomingDocumentAttachment.SetContentFromBlob(TempBlob); | ||
| if not ImportAttachmentIncDoc.ImportAttachment(IncomingDocumentAttachment, FileName, TempBlob) then begin | ||
| FeatureTelemetry.LogError('', 'E-Documents', 'AttachPurchaseEDocument', GetLastErrorText(true), GetLastErrorCallStack()); | ||
| exit; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Telemetry
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| end; | ||
|
|
||
| IncomingDocumentAttachment."Is E-Document" := true; | ||
| IncomingDocumentAttachment.Modify(false); | ||
|
|
||
| if EDocDataStorage."File Format" = EDocDataStorage."File Format"::PDF then begin | ||
| FileName := StrSubstNo(EDocumentFileNameLbl, EDocument."Entry No", EDocDataStorage."File Format"::XML); | ||
| ExtractXMLFromPDF(TempBlob, FileName, IncomingDocumentAttachment); | ||
| end; | ||
| end; | ||
|
|
||
| local procedure ExtractXMLFromPDF(var TempBlob: Codeunit System.Utilities."Temp Blob"; FileName: Text[250]; var IncomingDocumentAttachment: Record "Incoming Document Attachment") | ||
| var | ||
| ImportAttachmentIncDoc: Codeunit "Import Attachment - Inc. Doc."; | ||
| PDFDocument: Codeunit "PDF Document"; | ||
| ExtractedXmlBlob: Codeunit "Temp Blob"; | ||
| FeatureTelemetry: Codeunit "Feature Telemetry"; | ||
| PdfInStream: InStream; | ||
| begin | ||
| TempBlob.CreateInStream(PdfInStream); | ||
| if not PDFDocument.GetDocumentAttachmentStream(PdfInStream, ExtractedXmlBlob) then begin | ||
| FeatureTelemetry.LogError('', 'E-Documents', 'ExtractXMLFromPDF', GetLastErrorText(true), GetLastErrorCallStack()); | ||
| exit; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Telemetry
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| end; | ||
|
|
||
| if not ExtractedXmlBlob.HasValue() then | ||
| exit; | ||
|
|
||
| IncomingDocumentAttachment.Default := false; | ||
| IncomingDocumentAttachment."Main Attachment" := false; | ||
| if not ImportAttachmentIncDoc.ImportAttachment(IncomingDocumentAttachment, FileName, ExtractedXmlBlob) then begin | ||
| FeatureTelemetry.LogError('', 'E-Documents', 'ExtractXMLFromPDF', GetLastErrorText(true), GetLastErrorCallStack()); | ||
| exit; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Telemetry
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| end; | ||
|
|
||
| IncomingDocumentAttachment."Is E-Document" := true; | ||
| IncomingDocumentAttachment.Modify(false); | ||
| end; | ||
|
|
||
| local procedure FilterIncomingDocumentRecordFromRecordRef(var IncomingDocumentAttachment: Record "Incoming Document Attachment"; var IncomingDocument: Record "Incoming Document"; MainRecordRef: RecordRef): Boolean | ||
| begin | ||
| Clear(IncomingDocument); | ||
|
|
@@ -677,6 +816,7 @@ codeunit 5579 "Digital Voucher Impl." | |
| local procedure ExcludeDigitalVouchersOnAttachIncomingDocumentsOnAfterSetFilter(var IncomingDocumentAttachment: Record "Incoming Document Attachment") | ||
| begin | ||
| IncomingDocumentAttachment.SetRange("Is Digital Voucher", false); | ||
| IncomingDocumentAttachment.SetRange("Is E-Document", false); | ||
| end; | ||
|
|
||
| [EventSubscriber(ObjectType::Table, Database::"Digital Voucher Setup", 'OnBeforeDeleteEvent', '', false, false)] | ||
|
|
@@ -699,6 +839,21 @@ codeunit 5579 "Digital Voucher Impl." | |
| CopyDigitalVoucherToCorrectiveDocument("Digital Voucher Entry Type"::"Sales Document", SalesInvoiceHeader, SalesHeader."No.", SalesHeader."Posting Date"); | ||
| end; | ||
|
|
||
| [EventSubscriber(ObjectType::Codeunit, Codeunit::"E-Doc. Export", OnExportEDocumentAfterCreateEDocument, '', false, false)] | ||
| local procedure EDocExportOnExportEDocumentAfterCreateEDocument(EDocument: Record "E-Document"; SourceDocumentHeaderMapped: RecordRef; SourceDocumentLineMapped: RecordRef; var TempBlob: Codeunit "Temp Blob"; Success: Boolean) | ||
| begin | ||
| if not Success then | ||
| exit; | ||
|
|
||
| AttachIncomingEDocument(EDocument, SourceDocumentHeaderMapped, TempBlob); | ||
| end; | ||
|
|
||
| [EventSubscriber(ObjectType::Codeunit, Codeunit::"E-Document Subscribers", OnAfterUpdateToPostedPurchaseEDocument, '', false, false)] | ||
| local procedure EDocumentSubscribers_OnAfterUpdateToPostedPurchaseEDocument(var EDocument: Record "E-Document"; PostedRecord: Variant; DocumentType: Enum "E-Document Type") | ||
| begin | ||
| AttachOutgoingEDocument(EDocument, PostedRecord); | ||
| end; | ||
|
|
||
| [IntegrationEvent(false, false)] | ||
| local procedure OnGenerateDigitalVoucherForDocumentOnCaseElse(RecRef: RecordRef) | ||
| begin | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // ------------------------------------------------------------------------------------------------ | ||
| // 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; | ||
|
|
||
| using System.Utilities; | ||
|
|
||
| codeunit 5588 "Voucher E-Document Check" implements "Digital Voucher Check" | ||
| { | ||
| Access = Internal; | ||
|
|
||
| /// <summary> | ||
| /// Validates that an E-Document is attached to the document before posting. | ||
| /// Only applies to Purchase Documents when the Digital Voucher feature is enabled and Check Type is set to E-Document. | ||
| /// </summary> | ||
| /// <param name="ErrorMessageMgt">Error message management for logging validation errors.</param> | ||
| /// <param name="DigitalVoucherEntryType">The type of digital voucher entry being validated.</param> | ||
| /// <param name="RecRef">Record reference to the document being validated.</param> | ||
| internal procedure CheckVoucherIsAttachedToDocument(var ErrorMessageMgt: Codeunit "Error Message Management"; DigitalVoucherEntryType: Enum "Digital Voucher Entry Type"; RecRef: RecordRef) | ||
| var | ||
| DigitalVoucherEntrySetup: Record "Digital Voucher Entry Setup"; | ||
| EDocument: Record "E-Document"; | ||
| DigitalVoucherFeature: Codeunit "Digital Voucher Feature"; | ||
| DigitalVoucherImpl: Codeunit "Digital Voucher Impl."; | ||
| NotPossibleToPostWithoutEDocumentErr: Label 'Not possible to post without linking an E-Document.'; | ||
| begin | ||
| if not DigitalVoucherFeature.IsFeatureEnabled() then | ||
| exit; | ||
|
|
||
| if DigitalVoucherEntryType <> DigitalVoucherEntryType::"Purchase Document" then | ||
| exit; | ||
|
|
||
| DigitalVoucherImpl.GetDigitalVoucherEntrySetup(DigitalVoucherEntrySetup, DigitalVoucherEntryType); | ||
| if DigitalVoucherEntrySetup."Check Type" <> DigitalVoucherEntrySetup."Check Type"::"E-Document" then | ||
| exit; | ||
|
|
||
| EDocument.SetRange("Document Record ID", RecRef.RecordId()); | ||
| if EDocument.IsEmpty() then begin | ||
| ErrorMessageMgt.LogSimpleErrorMessage(NotPossibleToPostWithoutEDocumentErr); | ||
| exit; | ||
| end; | ||
| end; | ||
|
|
||
| /// <summary> | ||
| /// Generates a digital voucher for a posted document by delegating to the Attachment check type implementation. | ||
| /// This procedure retrieves the digital voucher entry setup and invokes the attachment-based voucher generation. | ||
| /// </summary> | ||
| /// <param name="DigitalVoucherEntryType">The type of digital voucher entry for the posted document.</param> | ||
| /// <param name="RecRef">Record reference to the posted document.</param> | ||
| internal procedure GenerateDigitalVoucherForPostedDocument(DigitalVoucherEntryType: Enum "Digital Voucher Entry Type"; RecRef: RecordRef) | ||
| var | ||
| DigitalVoucherEntrySetup: Record "Digital Voucher Entry Setup"; | ||
| DigitalVoucherImpl: Codeunit "Digital Voucher Impl."; | ||
| DigitalVoucherCheck: Interface "Digital Voucher Check"; | ||
| begin | ||
| DigitalVoucherImpl.GetDigitalVoucherEntrySetup(DigitalVoucherEntrySetup, DigitalVoucherEntryType); | ||
| DigitalVoucherCheck := DigitalVoucherEntrySetup."Check Type"::Attachment; | ||
| DigitalVoucherCheck.GenerateDigitalVoucherForPostedDocument(DigitalVoucherEntrySetup."Entry Type", RecRef); | ||
| end; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "folders": [ | ||
| { | ||
| "path": "app" | ||
| }, | ||
| { | ||
| "path": "test" | ||
| }, | ||
| { | ||
| "path": "test library" | ||
| } | ||
| ], | ||
| "settings": {} | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add telemetry to see when we fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added