Skip to content

Commit

Permalink
Updated with E-document Interfaces v2 microsoft#27058
Browse files Browse the repository at this point in the history
Set Setup tables to Internal
  • Loading branch information
tstefanovicius committed Nov 26, 2024
1 parent 8bfe6c9 commit 42f17bd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Apps/W1/EDocumentConnectors/Logiq/app/src/Auth.Codeunit.al
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Microsoft.EServices.EDocumentConnector.Logiq;
codeunit 6380 Auth
codeunit 6383 Auth
{
internal procedure SetIsolatedStorageValue(var ValueKey: Guid; Value: SecretText; TokenDataScope: DataScope)
begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ table 6380 "Connection Setup"
{
Caption = 'Logiq Connection Setup';
DataClassification = CustomerContent;
Access = Internal;

fields
{
field(1; PK; Code[20])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ table 6381 "Connection User Setup"
{
Caption = 'Logiq Connection User Setup';
DataClassification = CustomerContent;
Access = Internal;

fields
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,35 @@ namespace Microsoft.EServices.EDocumentConnector.Logiq;

using Microsoft.eServices.EDocument;
using System.Utilities;
using Microsoft.eServices.EDocument.Integration.Interfaces;
using Microsoft.eServices.EDocument.Integration.Send;
using Microsoft.eServices.EDocument.Integration.Receive;

codeunit 6381 "E-Document Integration" implements "E-Document Integration"
codeunit 6381 "E-Document Integration" implements IDocumentSender, IDocumentReceiver
{
Access = Internal;

procedure Send(var EDocument: Record "E-Document"; var TempBlob: Codeunit "Temp Blob"; var IsAsync: Boolean; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage)
procedure Send(var EDocument: Record "E-Document"; var EDocumentService: Record "E-Document Service"; SendContext: Codeunit SendContext);
begin
this.LogiqEDocumentManagement.Send(EDocument, TempBlob, IsAsync, HttpRequest, HttpResponse);
this.LogiqEDocumentManagement.Send(EDocument, EDocumentService, SendContext);
end;

#pragma warning disable AA0150
procedure SendBatch(var EDocuments: Record "E-Document"; var TempBlob: Codeunit "Temp Blob"; var IsAsync: Boolean; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage)
procedure ReceiveDocuments(var EDocumentService: Record "E-Document Service"; DocumentsMetadata: Codeunit "Temp Blob List"; ReceiveContext: Codeunit ReceiveContext)
begin
Error('Batch sending is not supported');
end;
#pragma warning restore AA0150
procedure GetResponse(var EDocument: Record "E-Document"; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage): Boolean
begin
Error('Getting response is not supported');
end;

procedure GetApproval(var EDocument: Record "E-Document"; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage): Boolean
begin
Error('Approval is not supported');
this.LogiqEDocumentManagement.ReceiveDocuments(EDocumentService, DocumentsMetadata, ReceiveContext);
end;

procedure Cancel(var EDocument: Record "E-Document"; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage): Boolean
procedure DownloadDocument(var EDocument: Record "E-Document"; var EDocumentService: Record "E-Document Service"; DocumentMetadata: Codeunit "Temp Blob"; ReceiveContext: Codeunit ReceiveContext)
begin
Error('Cancelling sent document is not supported');
this.LogiqEDocumentManagement.DownloadDocument(EDocument, EDocumentService, DocumentMetadata, ReceiveContext);
end;

procedure ReceiveDocument(var TempBlob: Codeunit "Temp Blob"; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage)
begin
this.LogiqEDocumentManagement.DownloadDocuments(TempBlob, HttpRequest, HttpResponse);
end;

procedure GetDocumentCountInBatch(var TempBlob: Codeunit "Temp Blob"): Integer
begin
exit(this.LogiqEDocumentManagement.GetDocumentCountInBatch(TempBlob));
end;

procedure GetIntegrationSetup(var SetupPage: Integer; var SetupTable: Integer)
[EventSubscriber(ObjectType::Page, Page::"E-Document Service", OnBeforeOpenServiceIntegrationSetupPage, '', false, false)]
local procedure OnBeforeOpenServiceIntegrationSetupPage(EDocumentService: Record "E-Document Service"; var SetupPage: Integer)
begin
SetupTable := Database::"Connection Setup";
SetupPage := Page::"Connection Setup";
if EDocumentService."Service Integration V2" = EDocumentService."Service Integration V2"::Logiq then
SetupPage := Page::"Connection Setup";
end;

var
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
namespace Microsoft.EServices.EDocumentConnector.Logiq;

using Microsoft.eServices.EDocument;
using Microsoft.eServices.EDocument.Integration;
using Microsoft.eServices.EDocument.Integration.Interfaces;

enumextension 6381 "E-Document Integration" extends "E-Document Integration"
enumextension 6381 "E-Document Integration" extends "Service Integration"
{
value(6381; "Logiq")
{
Caption = 'Logiq';
Implementation = "E-Document Integration" = "E-Document Integration";
Implementation = IDocumentSender = "E-Document Integration", IDocumentReceiver = "E-Document Integration";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Microsoft.EServices.EDocumentConnector.Logiq;

using Microsoft.eServices.EDocument;
using Microsoft.eServices.EDocument.Integration.Send;
using Microsoft.eServices.EDocument.Integration.Receive;
using System.Utilities;
using System.Xml;

Expand All @@ -10,14 +12,16 @@ codeunit 6382 "E-Document Management"
Permissions = tabledata "E-Document" = m;

#pragma warning disable AA0150
internal procedure Send(var EDocument: Record "E-Document"; var TempBlob: Codeunit "Temp Blob"; var IsAsync: Boolean; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage)
internal procedure Send(var EDocument: Record "E-Document"; var EDocumentService: Record "E-Document Service"; SendContext: Codeunit SendContext)
var
LogiqConnectionUserSetup: Record "Connection User Setup";
LogiqAuth: Codeunit Auth;
Client: HttpClient;
Headers: HttpHeaders;
Content: HttpContent;
ContentHeaders: HttpHeaders;
HttpRequest: HttpRequestMessage;
HttpResponse: HttpResponseMessage;
BodyText: Text;
FileNameText: Text;
Boundary: Text;
Expand All @@ -37,7 +41,7 @@ codeunit 6382 "E-Document Management"
FileNameText := StrSubstNo(this.FileNameTok, EDocument."Document No.");
Boundary := DelChr(Format(CreateGuid()), '<>=', '{}&[]*()!@#$%^+=;:"''<>,.?/|\\~`');

BodyText := this.GetFileContentAsMultipart(TempBlob, FileNameText, Boundary);
BodyText := this.GetFileContentAsMultipart(SendContext.GetTempBlob(), FileNameText, Boundary);
Content.WriteFrom(BodyText);

Content.GetHeaders(ContentHeaders);
Expand Down Expand Up @@ -71,7 +75,7 @@ codeunit 6382 "E-Document Management"
HttpResponse: HttpResponseMessage;
RequestSuccessful: Boolean;
begin
if EDocumentService."Service Integration" <> EDocumentService."Service Integration"::Logiq then
if EDocumentService."Service Integration v2" <> EDocumentService."Service Integration v2"::Logiq then
exit;

RequestSuccessful := this.GetStatus(EDocument, HttpRequest, HttpResponse);
Expand Down Expand Up @@ -114,15 +118,13 @@ codeunit 6382 "E-Document Management"
exit(HttpResponse.IsSuccessStatusCode());
end;

internal procedure DownloadDocuments(var TempBlob: Codeunit "Temp Blob"; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage)
internal procedure DownloadDocuments(var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage)
var
LogiqConnectionUserSetup: Record "Connection User Setup";
LogiqConnectionSetup: Record "Connection Setup";
LogiqAuth: Codeunit Auth;
Client: HttpClient;
Headers: HttpHeaders;
InStr: InStream;
OutStr: OutStream;
begin
HttpRequest.Method('GET');

Expand All @@ -139,25 +141,73 @@ codeunit 6382 "E-Document Management"

Client.Send(HttpRequest, HttpResponse);

if HttpResponse.IsSuccessStatusCode() then begin
HttpResponse.Content.ReadAs(InStr);
TempBlob.CreateOutStream(OutStr, TextEncoding::UTF8);
CopyStream(OutStr, InStr);
end else
if not HttpResponse.IsSuccessStatusCode() then
Error(this.DownloadDocumentsErr, HttpResponse.HttpStatusCode, HttpResponse.ReasonPhrase);
end;

internal procedure GetDocumentCountInBatch(var TempBlob: Codeunit "Temp Blob"): Integer
internal procedure ReceiveDocuments(var EDocumentService: Record "E-Document Service"; Documents: Codeunit "Temp Blob List"; ReceiveContext: Codeunit ReceiveContext)
var
JsonArray: JsonArray;
LogiqConnectionUserSetup: Record "Connection User Setup";
LogiqConnectionSetup: Record "Connection Setup";
LogiqAuth: Codeunit Auth;
TempBlob: Codeunit "Temp Blob";
HttpRequest: HttpRequestMessage;
HttpResponse: HttpResponseMessage;
DocumentsArray: JsonArray;
InStr: InStream;
OutStr: OutStream;
i: Integer;
begin
TempBlob.CreateInStream(InStr, TextEncoding::UTF8);
HttpRequest.Method('GET');

LogiqAuth.CheckSetup(LogiqConnectionSetup);
LogiqAuth.CheckUserSetup(LogiqConnectionUserSetup);
LogiqAuth.CheckUpdateTokens();

if not JsonArray.ReadFrom(InStr) then
exit(0);
this.DownloadDocuments(HttpRequest, HttpResponse);

exit(JsonArray.Count());
HttpResponse.Content.ReadAs(InStr);
DocumentsArray.ReadFrom(InStr);
for i := 0 to DocumentsArray.Count() - 1 do begin
Clear(TempBlob);
TempBlob.CreateOutStream(OutStr, TextEncoding::UTF8);
DocumentsArray.GetObject(i).WriteTo(OutStr);
Documents.Add(TempBlob);
end;

ReceiveContext.Http().SetHttpRequestMessage(HttpRequest);
ReceiveContext.Http().SetHttpResponseMessage(HttpResponse);
end;

internal procedure DownloadDocument(var EDocument: Record "E-Document"; var EDocumentService: Record "E-Document Service"; Document: Codeunit "Temp Blob"; ReceiveContext: Codeunit ReceiveContext)
var
HttpRequest: HttpRequestMessage;
HttpResponse: HttpResponseMessage;
InStr: InStream;
OutStr: OutStream;
DocumentData, FileName : Text;
begin
Document.CreateInStream(InStr, TextEncoding::UTF8);
InStr.ReadText(DocumentData);
if not this.ParseReceivedFileName(DocumentData, FileName) then begin
this.EDocumentErrorHelper.LogSimpleErrorMessage(EDocument, this.FileNameNotFoundErr);
exit;
end;

this.DownloadFile(FileName, HttpRequest, HttpResponse);
this.EDocumentLogHelper.InsertIntegrationLog(EDocument, EDocumentService, HttpRequest, HttpResponse);

HttpResponse.Content.ReadAs(DocumentData);
if DocumentData = '' then
this.EDocumentErrorHelper.LogSimpleErrorMessage(EDocument, StrSubstNo(this.FileNotFoundErr, FileName));

ReceiveContext.GetTempBlob().CreateOutStream(OutStr, TextEncoding::UTF8);
OutStr.WriteText(DocumentData);

this.EDocumentLogHelper.InsertLog(EDocument, EDocumentService, Document, "E-Document Service Status"::Imported);

ReceiveContext.Http().SetHttpRequestMessage(HttpRequest);
ReceiveContext.Http().SetHttpResponseMessage(HttpResponse);
end;

internal procedure DownloadFile(FileName: Text; var HttpRequest: HttpRequestMessage; var HttpResponse: HttpResponseMessage)
Expand Down Expand Up @@ -265,26 +315,14 @@ codeunit 6382 "E-Document Management"
EDocumentsErrorHelper.LogSimpleErrorMessage(EDocument, StrSubstNo(this.SendingFailedErr, ResponseMessage.HttpStatusCode(), ResponseMessage.ReasonPhrase()));
end;

local procedure ParseReceivedFileName(ContentTxt: Text; Index: Integer; var FileName: Text): Boolean
local procedure ParseReceivedFileName(ContentTxt: Text; var FileName: Text): Boolean
var
JsonArray: JsonArray;
JsonObj: JsonObject;
JsonTok: JsonToken;
begin
if not JsonArray.ReadFrom(ContentTxt) then
exit(false);

if Index > JsonArray.Count() then
if not JsonObj.ReadFrom(ContentTxt) then
exit(false);

if Index = 0 then
JsonArray.Get(Index, JsonTok)
else
JsonArray.Get(Index - 1, JsonTok);
if not JsonTok.IsObject() then
exit(false);

JsonObj := JsonTok.AsObject();
if not JsonObj.Get('fileName', JsonTok) then
exit(false);

Expand Down Expand Up @@ -318,52 +356,6 @@ codeunit 6382 "E-Document Management"
end;
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"E-Doc. Import", OnAfterInsertImportedEdocument, '', false, false)]
local procedure OnAfterInsertEdocument(var EDocument: Record "E-Document"; EDocumentService: Record "E-Document Service"; var TempBlob: Codeunit "Temp Blob"; EDocCount: Integer; HttpRequest: HttpRequestMessage; HttpResponse: HttpResponseMessage)
var
LocalHttpRequest: HttpRequestMessage;
LocalHttpResponse: HttpResponseMessage;
DocumentOutStream: OutStream;
ContentData, FileName : Text;
begin
if EDocumentService."Service Integration" <> EDocumentService."Service Integration"::Logiq then
exit;

HttpResponse.Content.ReadAs(ContentData);
if not this.ParseReceivedFileName(ContentData, EDocument."Index In Batch", FileName) then begin
this.EDocumentErrorHelper.LogSimpleErrorMessage(EDocument, this.FileNameNotFoundErr);
exit;
end;

this.DownloadFile(FileName, LocalHttpRequest, LocalHttpResponse);
this.EDocumentLogHelper.InsertIntegrationLog(EDocument, EDocumentService, LocalHttpRequest, LocalHttpResponse);

LocalHttpResponse.Content.ReadAs(ContentData);
if ContentData = '' then
this.EDocumentErrorHelper.LogSimpleErrorMessage(EDocument, StrSubstNo(this.FileNotFoundErr, FileName));

Clear(TempBlob);
TempBlob.CreateOutStream(DocumentOutStream, TextEncoding::UTF8);
DocumentOutStream.WriteText(ContentData);

this.EDocumentLogHelper.InsertLog(EDocument, EDocumentService, TempBlob, "E-Document Service Status"::Imported);
end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::"E-Document Log", OnUpdateEDocumentStatus, '', false, false)]
local procedure OnUpdateEDocumentStatus(var EDocument: Record "E-Document"; var IsHandled: Boolean)
var
EDocumentServiceStatus: Record "E-Document Service Status";
begin
EDocumentServiceStatus.SetRange("E-Document Entry No", EDocument."Entry No");
EDocumentServiceStatus.SetRange(Status, EDocumentServiceStatus.Status::"Failed Logiq");

if not EDocumentServiceStatus.IsEmpty() then begin
EDocument.Validate(Status, EDocument.Status::Error);
EDocument.Modify(false);
IsHandled := true;
end;
end;

// copied from standard codeunit 6108 "E-Document Processing" because it is internal
/// <summary>
/// Updates existing service status record. Throws runtime error if record does not exists.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Microsoft.EServices.EDocumentConnector.Logiq;

using Microsoft.eServices.EDocument;
using Microsoft.eServices.EDocument.Integration;
using Microsoft.Foundation.Company;
using Microsoft.Sales.Customer;
using Microsoft.Purchases.Vendor;
Expand Down Expand Up @@ -367,8 +368,8 @@ codeunit 148201 "Integration Tests"
exit;

this.LibraryEDocument.SetupStandardVAT();
this.LibraryEDocument.SetupStandardSalesScenario(this.Customer, this.EDocumentService, Enum::"E-Document Format"::"PEPPOL BIS 3.0", Enum::"E-Document Integration"::Logiq);
this.LibraryEDocument.SetupStandardPurchaseScenario(this.Vendor, this.EDocumentService, Enum::"E-Document Format"::"PEPPOL BIS 3.0", Enum::"E-Document Integration"::Logiq);
this.LibraryEDocument.SetupStandardSalesScenario(this.Customer, this.EDocumentService, Enum::"E-Document Format"::"PEPPOL BIS 3.0", Enum::"Service Integration"::Logiq);
this.LibraryEDocument.SetupStandardPurchaseScenario(this.Vendor, this.EDocumentService, Enum::"E-Document Format"::"PEPPOL BIS 3.0", Enum::"Service Integration"::Logiq);

this.Vendor."VAT Registration No." := 'NO 777 777 777';
this.Vendor."Receive E-Document To" := Enum::"E-Document Type"::"Purchase Invoice";
Expand Down

0 comments on commit 42f17bd

Please sign in to comment.