Skip to content
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

[E-Documents Core] - Enable deleting incorrect E-Documents #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions Apps/W1/EDocument/app/src/Document/EDocument.Table.al
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ table 6121 "E-Document"
}
}

trigger OnDelete()
begin
if (Rec.Status = Rec.Status::Processed) then
Error(this.DeleteProcessedNotAllowedErr);

if (Rec."Document No." <> '') then
Error(this.DeleteLinkedNotAllowedErr);

if (not this.IsDuplicate()) then
Error(this.DeleteUniqueNotAllowedErr);
end;

local procedure IsDuplicate(): Boolean
var
EDocument: Record "E-Document";
begin
EDocument.SetRange("Incoming E-Document No.", Rec."Incoming E-Document No.");
EDocument.SetRange("Bill-to/Pay-to No.", Rec."Bill-to/Pay-to No.");
EDocument.SetRange("Document Date", Rec."Document Date");
EDocument.SetFilter("Entry No", '<>%1', Rec."Entry No");
exit(not EDocument.IsEmpty());
end;

internal procedure OpenEDocument(EDocumentRecordId: RecordId)
var
EDocument: Record "E-Document";
Expand Down Expand Up @@ -227,4 +250,7 @@ table 6121 "E-Document"

var
ToStringLbl: Label '%1,%2,%3,%4', Locked = true;
DeleteLinkedNotAllowedErr: Label 'The E-Document is linked to sales or purchase document and cannot be deleted.';
DeleteProcessedNotAllowedErr: Label 'The E-Document has already been processed and cannot be deleted.';
DeleteUniqueNotAllowedErr: Label 'Only duplicate E-Documents can be deleted.';
}
84 changes: 84 additions & 0 deletions Apps/W1/EDocument/test/src/Processing/EDocE2ETest.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ codeunit 139624 "E-Doc E2E Test"
FailedToGetBlobErr: Label 'Failed to get exported blob from EDocument %1', Comment = '%1 - E-Document No.';
SendingErrStateErr: Label 'E-document is Pending response and can not be sent in this state.';
DeleteNotAllowedErr: Label 'Deletion of Purchase Header linked to E-Document is not allowed.';
DeleteProcessedNotAllowedErr: Label 'The E-Document has already been processed and cannot be deleted.';
DeleteUniqueNotAllowedErr: Label 'Only duplicate E-Documents can be deleted.';

[Test]
procedure CreateEDocumentBeforeAfterEventsSuccessful()
Expand Down Expand Up @@ -1448,6 +1450,87 @@ codeunit 139624 "E-Doc E2E Test"
PurchaseHeader.Delete();
end;

[Test]
internal procedure DeleteDuplicateEDocumentSuccess()
var
EDocument: Record "E-Document";
VendorNo: Code[20];
begin
// [FEATURE] [E-Document] [Deleting]
// [SCENARIO]
Initialize(Enum::"E-Document Integration"::"Mock");

// [GIVEN] Create duplicate e-document
VendorNo := this.LibraryPurchase.CreateVendorNo();
CreateIncomingEDocument(VendorNo, Enum::"E-Document Status"::"In Progress");
CreateIncomingEDocument(VendorNo, Enum::"E-Document Status"::"In Progress");

// [THEN] Get last E-Document
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a GIVEN no?

EDocument.FindLast();

// [THEN] Delete ok
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a WHEN

And a THEN would be an assert that EDocument no longer exists, or can also just be a comment that no error occured.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the GIVEN WHEN THEN on other tests as well

EDocument.Delete(true);
end;

[Test]
internal procedure DeleteNonDuplicateEDocumentNotAllowed()
var
EDocument: Record "E-Document";
VendorNo: Code[20];
begin
// [FEATURE] [E-Document] [Deleting]
// [SCENARIO]
Initialize(Enum::"E-Document Integration"::"Mock");

// [GIVEN] Create single e-document
VendorNo := this.LibraryPurchase.CreateVendorNo();
CreateIncomingEDocument(VendorNo, Enum::"E-Document Status"::"In Progress");

// [THEN] Get last E-Document
EDocument.FindLast();

// [THEN] Delete not allowed
asserterror EDocument.Delete(true);
Assert.ExpectedError(this.DeleteUniqueNotAllowedErr);
end;

[Test]
internal procedure DeleteProcessedEDocumentNotAllowed()
var
EDocument: Record "E-Document";
VendorNo: Code[20];
begin
// [FEATURE] [E-Document] [Deleting]
// [SCENARIO]
Initialize(Enum::"E-Document Integration"::"Mock");

// [GIVEN] Create duplicate e-document and set to processed
VendorNo := this.LibraryPurchase.CreateVendorNo();
CreateIncomingEDocument(VendorNo, Enum::"E-Document Status"::"In Progress");
CreateIncomingEDocument(VendorNo, Enum::"E-Document Status"::Processed);

// [THEN] Get last E-Document
EDocument.FindLast();

// [THEN] Delete not allowed
asserterror EDocument.Delete(true);
Assert.ExpectedError(this.DeleteProcessedNotAllowedErr);
end;

local procedure CreateIncomingEDocument(VendorNo: Code[20]; Status: Enum "E-Document Status")
var
EDocument: Record "E-Document";
begin
EDocument.Init();
EDocument."Document Type" := "E-Document Type"::"Purchase Invoice";
EDocument.Direction := Enum::"E-Document Direction"::Incoming;
EDocument."Document Date" := WorkDate();
EDocument."Incoming E-Document No." := 'TEST';
EDocument."Bill-to/Pay-to No." := VendorNo;
EDocument.Status := Status;
EDocument.Insert(false);
end;

[ModalPageHandler]
internal procedure EDocServicesPageHandler(var EDocServicesPage: TestPage "E-Document Services")
var
Expand Down Expand Up @@ -2253,6 +2336,7 @@ codeunit 139624 "E-Doc E2E Test"
PurchaseHeader.Delete();
end;


#endif
#pragma warning restore AS0018

Expand Down