Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#pragma warning disable AA0247
// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace Microsoft.Purchases.Document;

using Microsoft.eServices.EDocument;

tableextension 6169 "E-Doc. Purchase Header" extends "Purchase Header"
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Microsoft.eServices.EDocument;

using Microsoft.eServices.EDocument.Processing.Import;
using Microsoft.eServices.EDocument.Processing.Import.Purchase;
using Microsoft.Finance.GeneralLedger.Journal;
using Microsoft.Finance.GeneralLedger.Ledger;
using Microsoft.Foundation.Reporting;
Expand Down Expand Up @@ -687,6 +688,55 @@ codeunit 6108 "E-Document Processing"
exit(RecCaption);
end;

internal procedure ErrorIfNotAllowedToLinkToExistingDoc(EDocument: Record "E-Document"; EDocumentPurchaseHeader: Record "E-Document Purchase Header")
var
Vendor: Record Vendor;
NoVendorErr: Label 'Cannot link e-document to existing purchase document because vendor number is missing in e-document purchase header.';
begin
if EDocumentPurchaseHeader."[BC] Vendor No." = '' then
Error(NoVendorErr);
if Vendor.Get(EDocumentPurchaseHeader."[BC] Vendor No.") then
Vendor.TestField("IC Partner Code");
end;

procedure OpenPurchaseDocumentList(EDocumentType: Enum "E-Document Type"; var PurchaseHeader: Record "Purchase Header"): Boolean
var
PurchaseInvoices: Page "Purchase Invoices";
PurchaseOrders: Page "Purchase Orders";
PurchaseCreditMemos: Page "Purchase Credit Memos";
begin
case EDocumentType of
EDocumentType::"Purchase Invoice":
begin
PurchaseInvoices.SetTableView(PurchaseHeader);
PurchaseInvoices.LookupMode := true;
if PurchaseInvoices.RunModal() = Action::LookupOK then begin
PurchaseInvoices.GetRecord(PurchaseHeader);
exit(true);
end;
end;
EDocumentType::"Purchase Credit Memo":
begin
PurchaseCreditMemos.SetTableView(PurchaseHeader);
PurchaseCreditMemos.LookupMode := true;
if PurchaseCreditMemos.RunModal() = Action::LookupOK then begin
PurchaseCreditMemos.GetRecord(PurchaseHeader);
exit(true);
end;
end;
EDocumentType::"Purchase Order":
begin
PurchaseOrders.SetTableView(PurchaseHeader);
PurchaseOrders.LookupMode := true;
if PurchaseOrders.RunModal() = Action::LookupOK then begin
PurchaseOrders.GetRecord(PurchaseHeader);
exit(true);
end;
end;
end;
exit(false);
end;

[IntegrationEvent(false, false)]
local procedure OnAfterGetTypeFromSourceDocument(RecordVariant: Variant; var EDocumentType: Enum "E-Document Type")
begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ codeunit 6103 "E-Document Subscribers"
EDocImportParameters."Step to Run / Desired Status" := EDocImportParameters."Step to Run / Desired Status"::"Desired E-Document Status";
EDocImportParameters."Desired E-Document Status" := "Import E-Doc. Proc. Status"::"Draft Ready";
EDocImport.ProcessIncomingEDocument(EDocument, EDocImportParameters);

PurchaseHeader.Get(PurchaseHeader."Document Type", PurchaseHeader."No.");
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Data Classification Eval. Data", 'OnCreateEvaluationDataOnAfterClassifyTablesToNormal', '', false, false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,13 @@ table 6106 "E-Doc. Import Parameters"
{
}
#endregion

/// <summary>
/// Specifies an existing purchase document to link to instead of creating a new one.
/// When set, the ApplyDraftToBC step will link the e-document to this existing document rather than creating a new purchase invoice.
/// </summary>
field(10; "Existing Doc. RecordId"; RecordId)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ codeunit 6117 "E-Doc. Create Purchase Invoice" implements IEDocumentFinishDraft,
TempPOMatchWarnings: Record "E-Doc PO Match Warning" temporary;
EDocPOMatching: Codeunit "E-Doc. PO Matching";
DocumentAttachmentMgt: Codeunit "Document Attachment Mgmt";
EmptyRecordId: RecordId;
IEDocumentFinishPurchaseDraft: Interface IEDocumentCreatePurchaseInvoice;
YourMatchedLinesAreNotValidErr: Label 'The purchase invoice cannot be created because one or more of its matched lines are not valid matches. Review if your configuration allows for receiving at invoice.';
SomeLinesNotYetReceivedErr: Label 'Some of the matched purchase order lines have not yet been received, you need to either receive the lines or remove the matches.';
Expand All @@ -54,14 +55,17 @@ codeunit 6117 "E-Doc. Create Purchase Invoice" implements IEDocumentFinishDraft,
Error(SomeLinesNotYetReceivedErr);

IEDocumentFinishPurchaseDraft := EDocImportParameters."Processing Customizations";
PurchaseHeader := IEDocumentFinishPurchaseDraft.CreatePurchaseInvoice(EDocument);
if EDocImportParameters."Existing Doc. RecordId" <> EmptyRecordId then begin
EDocImpSessionTelemetry.SetBool('LinkedToExisting', true);
PurchaseHeader.Get(EDocImportParameters."Existing Doc. RecordId");
end else
PurchaseHeader := IEDocumentFinishPurchaseDraft.CreatePurchaseInvoice(EDocument);

EDocPOMatching.TransferPOMatchesFromEDocumentToInvoice(EDocument);
PurchaseHeader.SetRecFilter();
PurchaseHeader.FindFirst();
PurchaseHeader."Doc. Amount Incl. VAT" := EDocumentPurchaseHeader.Total;
PurchaseHeader."Doc. Amount VAT" := EDocumentPurchaseHeader."Total VAT";
PurchaseHeader.TestField("Document Type", "Purchase Document Type"::Invoice);
PurchaseHeader.TestField("No.");
PurchaseHeader."E-Document Link" := EDocument.SystemId;
PurchaseHeader.Modify();
Expand All @@ -85,12 +89,14 @@ codeunit 6117 "E-Doc. Create Purchase Invoice" implements IEDocumentFinishDraft,
PurchaseHeader.SetRange("E-Document Link", EDocument.SystemId);
if not PurchaseHeader.FindFirst() then
exit;

EDocPOMatching.TransferPOMatchesFromInvoiceToEDocument(PurchaseHeader);
DocumentAttachmentMgt.CopyAttachments(PurchaseHeader, EDocument);
DocumentAttachmentMgt.DeleteAttachedDocuments(PurchaseHeader);

PurchaseHeader.TestField("Document Type", "Purchase Document Type"::Invoice);
Clear(PurchaseHeader."E-Document Link");
PurchaseHeader.Delete(true);
PurchaseHeader.Modify();
end;

procedure CreatePurchaseInvoice(EDocument: Record "E-Document"): Record "Purchase Header"
Expand Down Expand Up @@ -264,5 +270,4 @@ codeunit 6117 "E-Doc. Create Purchase Invoice" implements IEDocumentFinishDraft,
if PurchaseLine.FindLast() then
exit(PurchaseLine."Line No.");
end;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.eServices.EDocument.Processing.Import.Purchase;
using Microsoft.eServices.EDocument;
using Microsoft.eServices.EDocument.Processing.Import;
using Microsoft.Foundation.Attachment;
using Microsoft.Purchases.Document;
using Microsoft.Purchases.Vendor;
using System.Feedback;
using System.Telemetry;
Expand Down Expand Up @@ -282,8 +283,11 @@ page 6181 "E-Document Purchase Draft"
Visible = ShowFinalizeDraftAction;

trigger OnAction()
var
EDocImportParameters: Record "E-Doc. Import Parameters";
begin
FinalizeEDocument();
Session.LogMessage('0000PCO', FinalizeDraftInvokedTxt, Verbosity::Normal, DataClassification::SystemMetadata, TelemetryScope::All, 'Category', EDocumentPurchaseHeader.FeatureName());
FinalizeEDocument(EDocImportParameters);
end;
}
action(ResetDraftDocument)
Expand Down Expand Up @@ -311,6 +315,19 @@ page 6181 "E-Document Purchase Draft"
AnalyzeEDocument();
end;
}
action(LinkToExistingDocument)
{
ApplicationArea = Basic, Suite;
Caption = 'Link to existing document';
ToolTip = 'Link this electronic document to an existing purchase document in Business Central. Use this when you have already created the document manually and want to attach the e-document to it.';
Image = Links;
Visible = false;

trigger OnAction()
begin
DoLinkToExistingDocument();
end;
}
}
group(ViewDocument)
{
Expand Down Expand Up @@ -570,18 +587,15 @@ page 6181 "E-Document Purchase Draft"
CurrPage.ErrorMessagesFactBox.Page.Update(false);
end;

local procedure FinalizeEDocument()
local procedure FinalizeEDocument(EDocImportParameters: Record "E-Doc. Import Parameters")
var
TempErrorMessage: Record "Error Message" temporary;
ErrorMessage: Record "Error Message";
EDocImportParameters: Record "E-Doc. Import Parameters";
EDocImport: Codeunit "E-Doc. Import";
EDocImpSessionTelemetry: Codeunit "E-Doc. Imp. Session Telemetry";
Telemetry: Codeunit Telemetry;
CustomDimensions: Dictionary of [Text, Text];
begin
Session.LogMessage('0000PCO', FinalizeDraftInvokedTxt, Verbosity::Normal, DataClassification::SystemMetadata, TelemetryScope::All, 'Category', EDocumentPurchaseHeader.FeatureName());

if not GlobalEDocumentHelper.EnsureInboundEDocumentHasService(Rec) then
exit;

Expand Down Expand Up @@ -695,6 +709,29 @@ page 6181 "E-Document Purchase Draft"
end;
end;

local procedure DoLinkToExistingDocument()
var
PurchaseHeader: Record "Purchase Header";
EDocImportParameters: Record "E-Doc. Import Parameters";
ConfirmDialogMgt: Codeunit "Confirm Management";
LinkToExistingDocumentQst: Label 'Do you want to link this e-document to %1 %2?', Comment = '%1 = Document Type, %2 = Document No.';
RelinkToExistingDocumentQst: Label 'This e-document is already linked to a document. Linking to %1 %2 will unlink the currently linked document. You will need to manually clean up that document. Do you want to continue?', Comment = '%1 = Document Type, %2 = Document No.';
ConfirmQst: Text;
begin
EDocumentProcessing.ErrorIfNotAllowedToLinkToExistingDoc(Rec, EDocumentPurchaseHeader);
PurchaseHeader.SetRange("Buy-from Vendor No.", EDocumentPurchaseHeader."[BC] Vendor No.");
PurchaseHeader.SetRange("Doc. Amount Incl. VAT", EDocumentPurchaseHeader.Total);
if not EDocumentProcessing.OpenPurchaseDocumentList(Rec."Document Type", PurchaseHeader) then
exit;

ConfirmQst := StrSubstNo(Rec.Status = Rec.Status::Processed ? RelinkToExistingDocumentQst : LinkToExistingDocumentQst, PurchaseHeader."Document Type", PurchaseHeader."No.");
if not ConfirmDialogMgt.GetResponseOrDefault(ConfirmQst, Rec.Status <> Rec.Status::Processed) then
exit;

EDocImportParameters."Existing Doc. RecordId" := PurchaseHeader.RecordId();
FinalizeEDocument(EDocImportParameters);
end;

var
EDocumentPurchaseHeader: Record "E-Document Purchase Header";
EDocumentServiceStatus: Record "E-Document Service Status";
Expand Down
2 changes: 2 additions & 0 deletions src/Apps/W1/EDocument/Test/src/LibraryEDocument.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ codeunit 139629 "Library - E-Document"
else
EntryNo := EDocument."Entry No";
EDocument."Entry No" := EntryNo;
EDocument.Direction := EDocument.Direction::Incoming;
EDocument.Service := EDocService.Code;
EDocument.Insert();
EDocumentServiceStatus."E-Document Entry No" := EntryNo;
EDocumentServiceStatus."E-Document Service Code" := EDocService.Code;
Expand Down
Loading
Loading