Skip to content

Commit

Permalink
pr fixes, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
petemchlk committed Jan 24, 2025
1 parent fe085f2 commit 1dcb965
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
15 changes: 5 additions & 10 deletions Apps/W1/EDocument/app/src/Processing/EDocImport.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,8 @@ codeunit 6140 "E-Doc. Import"
local procedure ImportEDocumentFromStream(var EDocument: Record "E-Document"; EDocumentService: Record "E-Document Service"; var InStr: InStream)
var
TempBlob: Codeunit "Temp Blob";
OutStr: OutStream;
begin
TempBlob.CreateOutStream(OutStr);
CopyStream(OutStr, InStr);
CopyStream(TempBlob.CreateOutStream(), InStr);

EDocument.Direction := EDocument.Direction::Incoming;
EDocument."Document Type" := Enum::"E-Document Type"::None;
Expand Down Expand Up @@ -676,10 +674,8 @@ codeunit 6140 "E-Doc. Import"
CopyStream(TempBlob.CreateOutStream(), DocumentInstream);
if HasDuplciate(EDocument, TempBlob, EDocumentService."Document Format") then
NotProcessedDocuments += 1
else begin
DocumentInstream.ResetPosition();
else
CreateEDocumentFromStream(EDocument, EDocumentService, DocumentInstream);
end;
end;

if NotProcessedDocuments > 0 then
Expand All @@ -688,7 +684,7 @@ codeunit 6140 "E-Doc. Import"
Message(DocsImportedMsg);
end;

internal procedure HandleSingleDocumentUpload(DocumentInstream: InStream; EDocument: Record "E-Document"; EDocumentService: Record "E-Document Service")
internal procedure HandleSingleDocumentUpload(DocumentInstream: InStream; var EDocument: Record "E-Document"; EDocumentService: Record "E-Document Service")
var
TempBlob: Codeunit "Temp Blob";
begin
Expand All @@ -702,10 +698,8 @@ codeunit 6140 "E-Doc. Import"
EDocument."Bill-to/Pay-to No.",
EDocument.FieldCaption("Document Date"),
EDocument."Document Date")
else begin
DocumentInstream.ResetPosition();
else
CreateEDocumentFromStream(EDocument, EDocumentService, DocumentInstream);
end;

if not this.HideDialogs and EDocErrorHelper.HasErrors(EDocument) then
if Confirm(DocNotCreatedQst, true, EDocument."Document Type") then
Expand All @@ -724,6 +718,7 @@ codeunit 6140 "E-Doc. Import"
EDocumentService: Record "E-Document Service";
var DocumentInstream: InStream)
begin
DocumentInstream.ResetPosition();
this.ImportEDocumentFromStream(EDocument, EDocumentService, DocumentInstream);
this.ProcessDocument(EDocument, false);
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,49 @@ codeunit 139501 "E-Doc. Manual Import Test"
VerifyDocumentCreated(EDocument);
end;

[Test]
procedure ManuallyCreateEDocumentFromStreamTwiceShouldThrowError()
var
EDocService: Record "E-Document Service";
EDocument: Record "E-Document";
Item: Record Item;
VATPostingSetup: Record "VAT Posting Setup";
DocumentVendor: Record Vendor;
TempBlob: Codeunit "Temp Blob";
DocumentInStream: InStream;
DocumentInStream2: InStream;
ErrorMessage: Text;
begin
// [FEATURE] [E-Document] [Import] [Manual] [Duplicate]
// [SCENARIO] Manually create e-document from stream twice should throw error for duplicate document
Initialize();

// [GIVEN] e-Document service to receive one single purchase invoice
CreateEDocServiceToReceivePurchaseInvoice(EDocService);
// [GIVEN] Vendor with VAT Posting Setup
CreateVendorWithVatPostingSetup(DocumentVendor, VATPostingSetup);
// [GIVEN] Item with item reference
CreateItemWithReference(Item, VATPostingSetup);
// [GIVEN] Incoming PEPPOL document stream (creating two streams from same blob for two imports)
CreateIncomingPEPPOLBlob(DocumentVendor, TempBlob);
TempBlob.CreateInStream(DocumentInStream, TextEncoding::UTF8);
TempBlob.CreateInStream(DocumentInStream2, TextEncoding::UTF8);

// [WHEN] Creating first e-document from stream
Clear(EDocument);
CreateEDocFromStream(EDocument, EDocService, DocumentInStream);

// [THEN] First document should be created successfully
VerifyDocumentCreated(EDocument);

// [WHEN] Trying to create second e-document from same stream
Clear(EDocument);
asserterror CreateEDocFromStream(EDocument, EDocService, DocumentInStream2);

// [THEN] Error should be thrown about duplicate document
Assert.ExpectedError(GetDuplciateErrorText(EDocument));
end;

local procedure CreateEDocServiceToReceivePurchaseInvoice(var EDocService: Record "E-Document Service")
begin
LibraryEDoc.CreateTestReceiveServiceForEDoc(EDocService, Enum::"Service Integration"::"Mock");
Expand Down Expand Up @@ -195,4 +238,11 @@ codeunit 139501 "E-Doc. Manual Import Test"
EDocService."Validate Receiving Company" := false;
EDocService.Modify(false);
end;

local procedure GetDuplciateErrorText(EDocument: Record "E-Document"): Text
var
EDocumentAlreadyExistErr: Label 'E-Document with %1 %2, %3 %4 and %5 %6 already exists.', Comment = '%1 - Incoming E-Document No. field caption, %2 - Incoming E-Document No. value, %3 - Bill-to/Pay-to No. field caption, %4 - Bill-to/Pay-to No. value, %5 - Document Date field caption, %6 - Document Date value.';
begin
exit(StrSubstNo(EDocumentAlreadyExistErr, EDocument.FieldCaption("Incoming E-Document No."), EDocument."Incoming E-Document No.", EDocument.FieldCaption("Bill-to/Pay-to No."), EDocument."Bill-to/Pay-to No.", EDocument.FieldCaption("Document Date"), EDocument."Document Date"));
end;
}

0 comments on commit 1dcb965

Please sign in to comment.