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

[Shopify] Shopify Return Location on Refunds tests #27578

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
Expand Up @@ -101,7 +101,7 @@ codeunit 30228 "Shpfy Refunds API"
exit(ReturnsAPI.GetReturnLocations(ReturnId));
end;

local procedure FillInRefundLine(RefundId: BigInteger; JLine: JsonObject; NonZeroOrReturnRefund: Boolean; ReturnLocations: Dictionary of [BigInteger, BigInteger])
internal procedure FillInRefundLine(RefundId: BigInteger; JLine: JsonObject; NonZeroOrReturnRefund: Boolean; ReturnLocations: Dictionary of [BigInteger, BigInteger])
var
DataCapture: Record "Shpfy Data Capture";
RefundLine: Record "Shpfy Refund Line";
Expand Down
251 changes: 223 additions & 28 deletions Apps/W1/Shopify/test/Order Refunds/ShpfyOrderRefundTest.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,18 @@ codeunit 139611 "Shpfy Order Refund Test"
TestPermissions = Disabled;

var
ShpfyInitializeTest: Codeunit "Shpfy Initialize Test";
LibraryAssert: Codeunit "Library Assert";
Any: Codeunit Any;
ShopifyIds: Dictionary of [Text, List of [BigInteger]];
IsInitialized: Boolean;

trigger OnRun()
begin
// [FEATURE] [Account Schedule] [Chart]
IsInitialized := false;
end;

local procedure Initialize()
var
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
begin
if IsInitialized then
exit;

ShopifyIds := OrderRefundsHelper.CreateShopifyDocuments();

IsInitialized := true;
Commit();
end;

local procedure ResetProccesOnRefund(ReFundId: Integer)
var
ShpfyDocLinkToDoc: Record "Shpfy Doc. Link To Doc.";
begin
ShpfyDocLinkToDoc.SetRange("Shopify Document Type", ShpfyDocLinkToDoc."Shopify Document Type"::"Shopify Shop Refund");
ShpfyDocLinkToDoc.SetRange("Shopify Document Id", ReFundId);
ShpfyDocLinkToDoc.DeleteAll();
end;

[Test]
procedure UnitTestCreateCrMemoFromRefundWithFullyRefundedItem()
var
Expand All @@ -45,7 +27,6 @@ codeunit 139611 "Shpfy Order Refund Test"
ErrorInfo: ErrorInfo;
begin
// [SCENARIO] Create a Credit Memo from a Shopify Refund where the item is totally refunded.
Codeunit.Run(Codeunit::"Shpfy Initialize Test");
Initialize();

// [GIVEN] Set the process of the document: "Auto Create Credit Memo";
Expand Down Expand Up @@ -82,7 +63,6 @@ codeunit 139611 "Shpfy Order Refund Test"
ErrorInfo: ErrorInfo;
begin
// [SCENARIO] Create a Credit Memo from a Shopify Refund where only the shipment is refunded.
Codeunit.Run(Codeunit::"Shpfy Initialize Test");
Initialize();

// [GIVEN] Set the process of the document: "Auto Create Credit Memo";
Expand Down Expand Up @@ -119,7 +99,6 @@ codeunit 139611 "Shpfy Order Refund Test"
ErrorInfo: ErrorInfo;
begin
// [SCENARIO] Create a Credit Memo from a Shopify Refund where the item is not refunded.
Codeunit.Run(Codeunit::"Shpfy Initialize Test");
Initialize();

// [GIVEN] Set the process of the document: "Auto Create Credit Memo";
Expand Down Expand Up @@ -158,7 +137,6 @@ codeunit 139611 "Shpfy Order Refund Test"
// Non-zero refund = true
// Linked return refund = true
// Zero and not linked refund = false
Codeunit.Run(Codeunit::"Shpfy Initialize Test");
Initialize();

// [GIVEN] Non-zero refund
Expand All @@ -177,7 +155,224 @@ codeunit 139611 "Shpfy Order Refund Test"
LibraryAssert.ExpectedError('The refund imported from Shopify can''t be used to create a credit memo. Only refunds for paid items can be used to create credit memos.');
end;

[Test]
procedure UnitTestFillInRefundLineWithLocation()
var
ShopifyIds: Dictionary of [Text, List Of [BigInteger]];
IsInitialized: Boolean;
RefundLine: Record "Shpfy Refund Line";
RefundsAPI: Codeunit "Shpfy Refunds API";
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
RefundId: BigInteger;
JRefundLine: JsonObject;
ReturnLocations: Dictionary of [BigInteger, BigInteger];
RefundLocationId: BigInteger;
RefundLineId: BigInteger;
begin
// [SCENARIO] Import refund lines with location
Initialize();

// [GIVEN] Refund Header
RefundId := OrderRefundsHelper.CreateRefundHeader();
// [GIVEN] Refund Line response
RefundLocationId := Any.IntegerInRange(100000, 999999);
RefundLineId := Any.IntegerInRange(100000, 999999);
CreateRefundLineResponse(JRefundLine, RefundLineId, RefundLocationId);

// [WHEN] Execute RefundsAPI.FillInRefundLine
RefundsAPI.FillInRefundLine(RefundId, JRefundLine, false, ReturnLocations);

// [THEN] Refund Line with location is created
LibraryAssert.IsTrue(RefundLine.Get(RefundId, RefundLineId), 'Refund line not creatred');
LibraryAssert.AreEqual(RefundLocationId, RefundLine."Location Id", 'Refund line location not set');
end;

[Test]
procedure UnitTestFillInRefundLineWithReturnLocations()
var
RefundLine: Record "Shpfy Refund Line";
RefundsAPI: Codeunit "Shpfy Refunds API";
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
RefundId: BigInteger;
JRefundLine: JsonObject;
ReturnLocations: Dictionary of [BigInteger, BigInteger];
RefundLineId: BigInteger;
ReturnLocationId: BigInteger;
begin
// [SCENARIO] Import refund lines with locations
Initialize();

// [GIVEN] Refund Header
RefundId := OrderRefundsHelper.CreateRefundHeader();
// [GIVEN] Refund Line response
RefundLineId := Any.IntegerInRange(100000, 999999);
CreateRefundLineResponse(JRefundLine, RefundLineId, 0);
//[GIVEN] Return Locations
ReturnLocationId := Any.IntegerInRange(100000, 999999);
ReturnLocations.Add(RefundLineId, ReturnLocationId);

// [WHEN] Execute RefundsAPI.FillInRefundLine
RefundsAPI.FillInRefundLine(RefundId, JRefundLine, false, ReturnLocations);

// [THEN] Refund Line with location is created
LibraryAssert.IsTrue(RefundLine.Get(RefundId, RefundLineId), 'Refund line not creatred');
LibraryAssert.AreEqual(ReturnLocationId, RefundLine."Location Id", 'Refund line location not set');
end;

[Test]
procedure UnitTestCreateSalesOrderLineFromRefundWithDefaultLocation()
var
SalesHeader: Record "Sales Header";
SalesLine: Record "Sales Line";
Shop: Record "Shpfy Shop";
Location: Record Location;
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
IReturnRefundProcess: Interface "Shpfy IReturnRefund Process";
RefundId: BigInteger;
OrderId, OrderLineId : BigInteger;
ReturnId: BigInteger;
begin
// [SCENARIO] Create sales credit memo line from refund with default location
Initialize();

// [GIVEN] Location
CreateLocation(Location);

// [GIVEN] Shop with setup to use default return location
Shop := ShpfyInitializeTest.CreateShop();
Shop."Return Location Priority" := Enum::"Shpfy Return Location Priority"::"Default Return Location";
Shop."Return Location" := Location.Code;
Shop.Modify(false);

//[GIVEN] Processed Shopify Order
CerateProcessedShopifyOrder(OrderId, OrderLineId);
// [GIVEN] Shopify Return
CreateShopifyReturn(ReturnId, OrderId, Shop.Code);
// [GIVEN] Refund Header
RefundId := OrderRefundsHelper.CreateRefundHeader(OrderId, ReturnId, 156.38, Shop.Code);
// [GIVEN] Refund line without location
OrderRefundsHelper.CreateRefundLine(RefundId, OrderLineId, 0);

// [WHEN] Execute create credit memo
IReturnRefundProcess := Enum::"Shpfy ReturnRefund ProcessType"::"Auto Create Credit Memo";
SalesHeader := IReturnRefundProcess.CreateSalesDocument(Enum::"Shpfy Source Document Type"::Refund, RefundId);

// [THEN] Credit Memo Line with default location is created
SalesLine.SetRange("Document Type", SalesHeader."Document Type");
SalesLine.SetRange("Document No.", SalesHeader."No.");
SalesLine.FindFirst();
LibraryAssert.AreEqual(Location.Code, SalesLine."Location Code", 'Sales line location not set');
end;

[Test]
procedure UnitTestCreateSalesCrMemoLineFromRefundWithReturnLocation()
var
SalesHeader: Record "Sales Header";
SalesLine: Record "Sales Line";
Shop: Record "Shpfy Shop";
Location: Record Location;
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
IReturnRefundProcess: Interface "Shpfy IReturnRefund Process";
RefundId: BigInteger;
OrderId, OrderLineId : BigInteger;
ReturnId: BigInteger;
LocationId: BigInteger;
begin
// [SCENARIO] Create sales credit memo line from refund with return location
Initialize();

// [GIVEN] Shop with setup to use original return location
Shop := ShpfyInitializeTest.CreateShop();
Shop."Return Location Priority" := Enum::"Shpfy Return Location Priority"::"Original -> Default Location";
Shop."Return Location" := '';
Shop.Modify(false);
// [GIVEN] Location
CreateLocation(Location);
// [GIVEN] Shop Location
LocationId := CreateShopLocation(Shop.Code, Location.Code);
//[GIVEN] Processed Shopify Order
CerateProcessedShopifyOrder(OrderId, OrderLineId);
// [GIVEN] Shopify Return
CreateShopifyReturn(ReturnId, OrderId, Shop.Code);
// [GIVEN] Refund Header
RefundId := OrderRefundsHelper.CreateRefundHeader(OrderId, ReturnId, 156.38, Shop.Code);
// [GIVEN] Refund line without location
OrderRefundsHelper.CreateRefundLine(RefundId, OrderLineId, LocationId);

// [WHEN] Execute create credit memo
IReturnRefundProcess := Enum::"Shpfy ReturnRefund ProcessType"::"Auto Create Credit Memo";
SalesHeader := IReturnRefundProcess.CreateSalesDocument(Enum::"Shpfy Source Document Type"::Refund, RefundId);

// [THEN] Credit Memo Line with return location is created
SalesLine.SetRange("Document Type", SalesHeader."Document Type");
SalesLine.SetRange("Document No.", SalesHeader."No.");
SalesLine.FindFirst();
LibraryAssert.AreEqual(Location.Code, SalesLine."Location Code", 'Sales line location not set');
end;

local procedure Initialize()
var
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
begin
Any.SetDefaultSeed();

if IsInitialized then
exit;

ShpfyInitializeTest.Run();
ShopifyIds := OrderRefundsHelper.CreateShopifyDocuments();

IsInitialized := true;
Commit();
end;

local procedure ResetProccesOnRefund(ReFundId: Integer)
var
ShpfyDocLinkToDoc: Record "Shpfy Doc. Link To Doc.";
begin
ShpfyDocLinkToDoc.SetRange("Shopify Document Type", ShpfyDocLinkToDoc."Shopify Document Type"::"Shopify Shop Refund");
ShpfyDocLinkToDoc.SetRange("Shopify Document Id", ReFundId);
ShpfyDocLinkToDoc.DeleteAll();
end;

local procedure CreateRefundLineResponse(var JRefundLine: JsonObject; RefundLineId: BigInteger; RefundLocationId: BigInteger)
begin
JRefundLine.ReadFrom(StrSubstNo('{"lineItem": {"id": "gid://shopify/LineItem/%1"}, "quantity": 1, "restockType": "no_restock", "location": {"legacyResourceId": %2}}', RefundLineId, RefundLocationId));
end;

local procedure CerateProcessedShopifyOrder(var OrderId: BigInteger; var OrderLineId: BigInteger)
var
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
begin
OrderRefundsHelper.SetDefaultSeed();
OrderId := OrderRefundsHelper.CreateShopifyOrder();
OrderLineId := OrderRefundsHelper.CreateOrderLine(OrderId, 10000, Any.IntegerInRange(100000, 999999), Any.IntegerInRange(100000, 999999));
OrderRefundsHelper.ProcessShopifyOrder(OrderId);
end;

local procedure CreateShopifyReturn(var ReturnId: BigInteger; OrderId: BigInteger; ShopCode: Code[20])
JesperSchulz marked this conversation as resolved.
Show resolved Hide resolved
var
OrderRefundsHelper: Codeunit "Shpfy Order Refunds Helper";
begin
OrderRefundsHelper.SetDefaultSeed();
ReturnId := OrderRefundsHelper.CreateReturn(OrderId);
OrderRefundsHelper.CreateReturnLine(ReturnId, OrderId, '');
end;

local procedure CreateLocation(var Location: Record Location)
begin
Location.Init();
Location.Code := Any.AlphanumericText(10);
Location.Insert();
end;

local procedure CreateShopLocation(ShopCode: Code[20]; LocationCode: Code[10]): BigInteger
var
ShopLocation: Record "Shpfy Shop Location";
begin
ShopLocation.Init();
ShopLocation."Shop Code" := ShopCode;
ShopLocation.Id := Any.IntegerInRange(100000, 999999);
ShopLocation."Default Location Code" := LocationCode;
ShopLocation.Insert(false);
exit(ShopLocation.Id);
end;
}
Loading
Loading