Skip to content
Merged
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
Expand Up @@ -31,7 +31,6 @@ codeunit 30174 "Shpfy Create Product"
Getlocations: Boolean;
ProductId: BigInteger;
ItemVariantIsBlockedLbl: Label 'Item variant is blocked or sales blocked.';
TooManyVariantsLbl: Label 'Item has more than 2048 variants. Shopify allows a maximum of 2048 variants per product.';

trigger OnRun()
var
Expand Down Expand Up @@ -79,7 +78,6 @@ codeunit 30174 "Shpfy Create Product"
ItemVariant: Record "Item Variant";
SkippedRecord: Codeunit "Shpfy Skipped Record";
Id: Integer;
ExpectedVariantCount: Integer;
ICreateProductStatus: Interface "Shpfy ICreateProductStatusValue";
begin
Clear(TempShopifyProduct);
Expand All @@ -88,20 +86,9 @@ codeunit 30174 "Shpfy Create Product"
ProductExport.FillInProductFields(Item, TempShopifyProduct);
ICreateProductStatus := Shop."Status for Created Products";
TempShopifyProduct.Status := ICreateProductStatus.GetStatus(Item);
ItemVariant.SetRange("Item No.", Item."No.");
ItemVariant.SetRange(Blocked, false);
ItemVariant.SetRange("Sales Blocked", false);
ExpectedVariantCount := ItemVariant.Count();
if Shop."UoM as Variant" then begin
ItemUnitofMeasure.SetRange("Item No.", Item."No.");
ExpectedVariantCount := ExpectedVariantCount * ItemUnitofMeasure.Count();
end;
if ExpectedVariantCount > GetMaxVariantCount() then begin
SkippedRecord.LogSkippedRecord(Item.RecordId, TooManyVariantsLbl, Shop);
if not ProductExport.CheckItemVariantCount(Item) then
exit;
end;
ItemVariant.SetRange(Blocked);
ItemVariant.SetRange("Sales Blocked");
ItemVariant.SetRange("Item No.", Item."No.");
if ItemVariant.FindSet(false) then
repeat
if ItemVariant.Blocked or ItemVariant."Sales Blocked" then
Expand Down Expand Up @@ -236,11 +223,6 @@ codeunit 30174 "Shpfy Create Product"
end;
end;

local procedure GetMaxVariantCount(): Integer
begin
exit(2048);
end;

/// <summary>
/// Creates a temporary Shopify variant with information from an item.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,61 @@ codeunit 30178 "Shpfy Product Export"
end;
#endregion

#region Shopify Product Options as Item/Variant Attributes
/// <summary>
/// <summary>
/// Checks if the item can be exported to Shopify. Validates that the item is not blocked, has a description, and does not exceed the variant limit.
/// </summary>
/// <param name="Item">The item to check.</param>
/// <returns>True if the item can be exported, false otherwise.</returns>
internal procedure CheckItemCanBeExported(Item: Record Item): Boolean
var
ItemIsBlockedOrSalesBlockedLbl: Label 'Item is blocked or sales blocked.';
ItemDescriptionIsEmptyLbl: Label 'Item description is empty.';
begin
if Item.Blocked or Item."Sales Blocked" then begin
SkippedRecord.LogSkippedRecord(Item.RecordId, ItemIsBlockedOrSalesBlockedLbl, Shop);
exit(false);
end;

if Item.Description = '' then begin
SkippedRecord.LogSkippedRecord(Item.RecordId, ItemDescriptionIsEmptyLbl, Shop);
exit(false);
end;

exit(CheckItemVariantCount(Item));
end;

/// <summary>
/// Checks if the item's expected variant count does not exceed the Shopify maximum of 2048 variants per product.
/// </summary>
/// <param name="Item">The item to check.</param>
/// <returns>True if the variant count is within limits, false otherwise.</returns>
internal procedure CheckItemVariantCount(Item: Record Item): Boolean
var
ItemVariant: Record "Item Variant";
ItemUnitofMeasure: Record "Item Unit of Measure";
ExpectedVariantCount: Integer;
MaxVariantCount: Integer;
TooManyVariantsLbl: Label 'Item has more than %1 variants. Shopify allows a maximum of %1 variants per product.', Comment = '%1 = Maximum number of variants';
begin
MaxVariantCount := 2048;
ItemVariant.SetRange("Item No.", Item."No.");
ItemVariant.SetRange(Blocked, false);
ItemVariant.SetRange("Sales Blocked", false);
ExpectedVariantCount := ItemVariant.Count();
if Shop."UoM as Variant" then begin
ItemUnitofMeasure.SetRange("Item No.", Item."No.");
ExpectedVariantCount := ExpectedVariantCount * ItemUnitofMeasure.Count();
end;
if ExpectedVariantCount > MaxVariantCount then begin
SkippedRecord.LogSkippedRecord(Item.RecordId, StrSubstNo(TooManyVariantsLbl, MaxVariantCount), Shop);
exit(false);
end;

exit(true);
end;

#region Shopify Product Options as Item/Variant Attributes
/// <summary>
/// Checks if item/item variant attributes marked as "As Option" are compatible to be used as product options in Shopify.
/// </summary>
/// <param name="Item">The item to check.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pageextension 30119 "Shpfy Item Card" extends "Item Card"
begin
if SyncProducts.ConfirmAddItemToShopify(Rec, Shop) then begin
ProductExport.SetShop(Shop);
if not ProductExport.CheckItemCanBeExported(Rec) then
exit;
if not ProductExport.CheckItemAttributesCompatibleForProductOptions(Rec) then
exit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ report 30106 "Shpfy Add Item to Shopify"

Clear(ShopifyCreateProduct);
ShopifyCreateProduct.SetShop(ShopCode);
ProductExport.SetShop(ShopifyShop);

if GuiAllowed then begin
CurrItemNo := Item."No.";
Expand All @@ -59,18 +60,9 @@ report 30106 "Shpfy Add Item to Shopify"
end;

trigger OnAfterGetRecord()
var
SkippedRecord: Codeunit "Shpfy Skipped Record";
begin
if Item.Blocked or Item."Sales Blocked" then begin
SkippedRecord.LogSkippedRecord(Item.RecordId, ItemIsBlockedLbl, ShopifyShop);
if not ProductExport.CheckItemCanBeExported(Item) then
exit;
end;

if Item.Description = '' then begin
SkippedRecord.LogSkippedRecord(Item.RecordId, ItemDescriptionIsEmptyLbl, ShopifyShop);
exit;
end;

if GuiAllowed then begin
CurrItemNo := Item."No.";
Expand Down Expand Up @@ -187,6 +179,7 @@ report 30106 "Shpfy Add Item to Shopify"
var
ShopifyShop: Record "Shpfy Shop";
ShopifyCreateProduct: Codeunit "Shpfy Create Product";
ProductExport: Codeunit "Shpfy Product Export";
ShopCode: Code[20];
CurrItemNo: Code[20];
SyncImages: Boolean;
Expand All @@ -200,8 +193,6 @@ report 30106 "Shpfy Add Item to Shopify"
MissingSKUMappingErr: Label 'You selected Business Central Fullment Services as default location. Inventory is stocked at Business Central Fulfilment Services for created products. This setting requires SKU field in the products.';
ChangeDefaultLocationLbl: Label 'Change default location';
ChangeSKUMappingLbl: Label 'Change SKU mapping';
ItemIsBlockedLbl: Label 'Item is blocked or sales blocked.';
ItemDescriptionIsEmptyLbl: Label 'Item description is empty.';

/// <summary>
/// Set Shop.
Expand Down
100 changes: 100 additions & 0 deletions src/Apps/W1/Shopify/Test/Logs/ShpfySkippedRecordLogTest.Codeunit.al
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,109 @@ codeunit 139581 "Shpfy Skipped Record Log Test"
SyncShipmToShopify.OK().Invoke();
end;

[Test]
[HandlerFunctions('AddItemConfirmHandler,SkippedRecordNotificationHandler')]
procedure UnitTestAddBlockedItemToShopifyFromItemCard()
var
Item: Record Item;
SkippedRecord: Record "Shpfy Skipped Record";
ItemCard: TestPage "Item Card";
begin
// [SCENARIO] Skipped record is created when adding a blocked item to Shopify from the Item Card.
Initialize();

// [GIVEN] An item that is blocked.
CreateBlockedItem(Item);
Commit();

// [WHEN] Open Item Card and invoke "Add to Shopify" action.
ItemCard.OpenEdit();
ItemCard.GoToRecord(Item);
ItemCard."Add to Shopify".Invoke();

// [THEN] Related record is created in shopify skipped record table.
SkippedRecord.SetRange("Record ID", Item.RecordId);
LibraryAssert.IsTrue(SkippedRecord.FindFirst(), 'Skipped record is not created');
LibraryAssert.AreEqual('Item is blocked or sales blocked.', SkippedRecord."Skipped Reason", 'Skipped reason is not as expected');
end;

[Test]
[HandlerFunctions('AddItemConfirmHandler,SkippedRecordNotificationHandler')]
procedure UnitTestAddItemWithEmptyDescriptionToShopifyFromItemCard()
var
Item: Record Item;
SkippedRecord: Record "Shpfy Skipped Record";
ItemCard: TestPage "Item Card";
begin
// [SCENARIO] Skipped record is created when adding an item with empty description to Shopify from the Item Card.
Initialize();

// [GIVEN] An item with empty description.
CreateItem(Item);
Commit();

// [WHEN] Open Item Card and invoke "Add to Shopify" action.
ItemCard.OpenEdit();
ItemCard.GoToRecord(Item);
ItemCard."Add to Shopify".Invoke();

// [THEN] Related record is created in shopify skipped record table.
SkippedRecord.SetRange("Record ID", Item.RecordId);
LibraryAssert.IsTrue(SkippedRecord.FindFirst(), 'Skipped record is not created');
LibraryAssert.AreEqual('Item description is empty.', SkippedRecord."Skipped Reason", 'Skipped reason is not as expected');
end;

[Test]
[HandlerFunctions('AddItemConfirmHandler,SkippedRecordNotificationHandler')]
procedure UnitTestAddItemWithTooManyVariantsToShopifyFromItemCard()
var
Item: Record Item;
ItemVariant: Record "Item Variant";
SkippedRecord: Record "Shpfy Skipped Record";
ItemCard: TestPage "Item Card";
i: Integer;
begin
// [SCENARIO] Skipped record is created when adding an item with more than 2048 variants to Shopify from the Item Card.
Initialize();

// [GIVEN] An item with more than 2048 variants.
CreateItem(Item);
Item.Description := 'Test Item';
Item.Modify(false);
for i := 1 to 2049 do begin
ItemVariant.Init();
ItemVariant."Item No." := Item."No.";
ItemVariant.Code := CopyStr(Format(i).PadLeft(10, '0'), 1, MaxStrLen(ItemVariant.Code));
ItemVariant.Insert(false);
end;
Commit();

// [WHEN] Open Item Card and invoke "Add to Shopify" action.
ItemCard.OpenEdit();
ItemCard.GoToRecord(Item);
ItemCard."Add to Shopify".Invoke();

// [THEN] Related record is created in shopify skipped record table.
SkippedRecord.SetRange("Record ID", Item.RecordId);
LibraryAssert.IsTrue(SkippedRecord.FindFirst(), 'Skipped record is not created');
LibraryAssert.AreEqual('Item has more than 2048 variants. Shopify allows a maximum of 2048 variants per product.', SkippedRecord."Skipped Reason", 'Skipped reason is not as expected');
end;

[RequestPageHandler]
procedure AddItemToShopifyHandler(var AddItemToShopify: TestRequestPage "Shpfy Add Item to Shopify")
begin
AddItemToShopify.OK().Invoke();
end;

[ModalPageHandler]
procedure AddItemConfirmHandler(var AddItemConfirm: TestPage "Shpfy Add Item Confirm")
begin
AddItemConfirm.OK().Invoke();
end;

[SendNotificationHandler]
procedure SkippedRecordNotificationHandler(var Notification: Notification): Boolean
begin
exit(true);
end;
}
Loading