diff --git a/Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLGetProductOptions.Codeunit.al b/Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLGetProductOptions.Codeunit.al new file mode 100644 index 0000000000..46a753e828 --- /dev/null +++ b/Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLGetProductOptions.Codeunit.al @@ -0,0 +1,27 @@ +namespace Microsoft.Integration.Shopify; + +/// +/// Codeunit Shpfy GQL GetProductOptions (ID 30345) implements Interface Shpfy IGraphQL. +/// +codeunit 30345 "Shpfy GQL GetProductOptions" implements "Shpfy IGraphQL" +{ + Access = Internal; + + /// + /// GetGraphQL. + /// + /// Return value of type Text. + internal procedure GetGraphQL(): Text + begin + exit('{"query":"{product(id: \"gid://shopify/Product/{{ProductId}}\") {id title options {id name}}}"}'); + end; + + /// + /// GetExpectedCost. + /// + /// Return value of type Integer. + internal procedure GetExpectedCost(): Integer + begin + exit(2); + end; +} diff --git a/Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLProductVariantDelete.Codeunit.al b/Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLProductVariantDelete.Codeunit.al new file mode 100644 index 0000000000..e362b36a96 --- /dev/null +++ b/Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLProductVariantDelete.Codeunit.al @@ -0,0 +1,27 @@ +namespace Microsoft.Integration.Shopify; + +/// +/// Codeunit Shpfy GQL ProductVariantDelete (ID 30344) implements Interface Shpfy IGraphQL. +/// +codeunit 30344 "Shpfy GQL ProductVariantDelete" implements "Shpfy IGraphQL" +{ + Access = Internal; + + /// + /// GetGraphQL. + /// + /// Return value of type Text. + internal procedure GetGraphQL(): Text + begin + exit('{"query":"mutation {productVariantDelete(id: \"gid://shopify/ProductVariant/{{VariantId}}\") {deletedProductVariantId userErrors{field message}}}"}'); + end; + + /// + /// GetExpectedCost. + /// + /// Return value of type Integer. + internal procedure GetExpectedCost(): Integer + begin + exit(10); + end; +} diff --git a/Apps/W1/Shopify/app/src/GraphQL/Enums/ShpfyGraphQLType.Enum.al b/Apps/W1/Shopify/app/src/GraphQL/Enums/ShpfyGraphQLType.Enum.al index 031d9283ae..c3ee92e22b 100644 --- a/Apps/W1/Shopify/app/src/GraphQL/Enums/ShpfyGraphQLType.Enum.al +++ b/Apps/W1/Shopify/app/src/GraphQL/Enums/ShpfyGraphQLType.Enum.al @@ -405,6 +405,16 @@ enum 30111 "Shpfy GraphQL Type" implements "Shpfy IGraphQL" Caption = 'Get Order Transactions'; Implementation = "Shpfy IGraphQL" = "Shpfy GQL OrderTransactions"; } + value(85; ProductVariantDelete) + { + Caption = 'Product Variant Delete'; + Implementation = "Shpfy IGraphQL" = "Shpfy GQL ProductVariantDelete"; + } + value(86; GetProductOptions) + { + Caption = 'Get Product Options'; + Implementation = "Shpfy IGraphQL" = "Shpfy GQL GetProductOptions"; + } value(87; GetReverseFulfillmentOrders) { Caption = 'Get Reverse Fulfillment Orders'; diff --git a/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateItemAsVariant.Codeunit.al b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateItemAsVariant.Codeunit.al new file mode 100644 index 0000000000..d323d2a4b5 --- /dev/null +++ b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateItemAsVariant.Codeunit.al @@ -0,0 +1,110 @@ +namespace Microsoft.Integration.Shopify; + +using Microsoft.Inventory.Item; + +codeunit 30343 "Shpfy Create Item As Variant" +{ + TableNo = Item; + Access = Internal; + + var + Shop: Record "Shpfy Shop"; + ShopifyProduct: Record "Shpfy Product"; + CreateProduct: Codeunit "Shpfy Create Product"; + VariantApi: Codeunit "Shpfy Variant API"; + ProductApi: Codeunit "Shpfy Product API"; + DefaultVariantId: BigInteger; + + trigger OnRun() + begin + CreateVariantFromItem(Rec); + end; + + /// + /// Creates a variant from a given item and adds it to the parent product. + /// + /// The item to be added as a variant. + internal procedure CreateVariantFromItem(var Item: Record "Item") + var + TempShopifyVariant: Record "Shpfy Variant" temporary; + begin + CreateProduct.CreateTempShopifyVariantFromItem(Item, TempShopifyVariant); + TempShopifyVariant."Product Id" := ShopifyProduct."Id"; + TempShopifyVariant.Title := Item."No."; + TempShopifyVariant."Option 1 Name" := 'Variant'; + TempShopifyVariant."Option 1 Value" := Item."No."; + + if VariantApi.AddProductVariant(TempShopifyVariant) then begin + ShopifyProduct."Has Variants" := true; + ShopifyProduct.Modify(true); + end; + end; + + + /// + /// Checks if items can be added as variants to the product. The items cannot be added as variants if: + /// - The product has more than one option. + /// - The UoM as Variant setting is enabled. + /// + internal procedure CheckProductAndShopSettings() + var + MultipleOptionsErr: Label 'The product has more than one option. Items cannot be added as variants to a product with multiple options.'; + UOMAsVariantEnabledErr: Label 'Items cannot be added as variants to a product with the "%1" setting enabled for this store.', Comment = '%1 - UoM as Variant field caption'; + Options: Dictionary of [Text, Text]; + begin + if Shop."UoM as Variant" then + Error(UOMAsVariantEnabledErr, Shop.FieldCaption("UoM as Variant")); + + Options := ProductApi.GetProductOptions(ShopifyProduct.Id); + + if Options.Count > 1 then + Error(MultipleOptionsErr); + end; + + /// + /// Finds the default variant ID for the product if the product has no variants. + /// If new variants will be added, the default variant will be removed. + /// + internal procedure FindDefaultVariantId() + var + ProductVariantIds: Dictionary of [BigInteger, DateTime]; + begin + if not ShopifyProduct."Has Variants" then begin + VariantApi.RetrieveShopifyProductVariantIds(ShopifyProduct, ProductVariantIds); + DefaultVariantId := ProductVariantIds.Keys.Get(1); + end; + end; + + /// + /// Removes the default variant if new variants were added to the product. + /// + internal procedure RemoveDefaultVariant() + var + ShopifyVariant: Record "Shpfy Variant"; + begin + if (DefaultVariantId <> 0) and ShopifyProduct."Has Variants" then begin + VariantApi.DeleteProductVariant(DefaultVariantId); + if ShopifyVariant.Get(DefaultVariantId) then + ShopifyVariant.Delete(true); + end; + end; + + /// + /// Sets the parent product to which the variant will be added. + /// + /// The parent Shopify product ID. + internal procedure SetParentProduct(ShopifyProductId: BigInteger) + begin + ShopifyProduct.Get(ShopifyProductId); + SetShop(ShopifyProduct."Shop Code"); + end; + + local procedure SetShop(ShopCode: Code[20]) + begin + Shop.Get(ShopCode); + VariantApi.SetShop(Shop); + ProductApi.SetShop(Shop); + CreateProduct.SetShop(Shop); + end; + +} \ No newline at end of file diff --git a/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateProduct.Codeunit.al b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateProduct.Codeunit.al index 307e45a4ea..e81da72f87 100644 --- a/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateProduct.Codeunit.al +++ b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateProduct.Codeunit.al @@ -87,22 +87,7 @@ codeunit 30174 "Shpfy Create Product" ProductPriceCalc.CalcPrice(Item, ItemVariant.Code, ItemUnitofMeasure.Code, TempShopifyVariant."Unit Cost", TempShopifyVariant.Price, TempShopifyVariant."Compare at Price"); TempShopifyVariant.Title := ItemVariant.Description; TempShopifyVariant."Inventory Policy" := Shop."Default Inventory Policy"; - case Shop."SKU Mapping" of - Shop."SKU Mapping"::"Bar Code": - TempShopifyVariant.SKU := TempShopifyVariant.Barcode; - Shop."SKU Mapping"::"Item No.": - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := ItemVariant.Code; - Shop."SKU Mapping"::"Item No. + Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := Item."No." + Shop."SKU Field Separator" + ItemVariant.Code - else - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Vendor Item No.": - TempShopifyVariant.SKU := Item."Vendor Item No."; - end; + TempShopifyVariant.SKU := GetVariantSKU(TempShopifyVariant.Barcode, Item."No.", ItemVariant.Code, Item."Vendor Item No."); TempShopifyVariant."Tax Code" := Item."Tax Group Code"; TempShopifyVariant.Taxable := true; TempShopifyVariant.Weight := Item."Gross Weight"; @@ -125,22 +110,7 @@ codeunit 30174 "Shpfy Create Product" ProductPriceCalc.CalcPrice(Item, ItemVariant.Code, Item."Sales Unit of Measure", TempShopifyVariant."Unit Cost", TempShopifyVariant.Price, TempShopifyVariant."Compare at Price"); TempShopifyVariant.Title := ItemVariant.Description; TempShopifyVariant."Inventory Policy" := Shop."Default Inventory Policy"; - case Shop."SKU Mapping" of - Shop."SKU Mapping"::"Bar Code": - TempShopifyVariant.SKU := TempShopifyVariant.Barcode; - Shop."SKU Mapping"::"Item No.": - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := ItemVariant.Code; - Shop."SKU Mapping"::"Item No. + Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := Item."No." + Shop."SKU Field Separator" + ItemVariant.Code - else - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Vendor Item No.": - TempShopifyVariant.SKU := CopyStr(GetVendorItemNo(Item."No.", ItemVariant.Code, Item."Sales Unit of Measure"), 1, MaxStrLen(TempShopifyVariant.SKU)); - end; + TempShopifyVariant.SKU := GetVariantSKU(TempShopifyVariant.Barcode, Item."No.", ItemVariant.Code, GetVendorItemNo(Item."No.", ItemVariant.Code, Item."Sales Unit of Measure")); TempShopifyVariant."Tax Code" := Item."Tax Group Code"; TempShopifyVariant.Taxable := true; TempShopifyVariant.Weight := Item."Gross Weight"; @@ -157,6 +127,7 @@ codeunit 30174 "Shpfy Create Product" ItemUnitofMeasure.SetRange("Item No.", Item."No."); if ItemUnitofMeasure.FindSet(false) then repeat + TempShopifyProduct."Has Variants" := true; Id += 1; Clear(TempShopifyVariant); TempShopifyVariant.Id := Id; @@ -165,22 +136,7 @@ codeunit 30174 "Shpfy Create Product" ProductPriceCalc.CalcPrice(Item, '', ItemUnitofMeasure.Code, TempShopifyVariant."Unit Cost", TempShopifyVariant.Price, TempShopifyVariant."Compare at Price"); TempShopifyVariant.Title := Item.Description; TempShopifyVariant."Inventory Policy" := Shop."Default Inventory Policy"; - case Shop."SKU Mapping" of - Shop."SKU Mapping"::"Bar Code": - TempShopifyVariant.SKU := TempShopifyVariant.Barcode; - Shop."SKU Mapping"::"Item No.": - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := ItemVariant.Code; - SHop."SKU Mapping"::"Item No. + Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := Item."No." + Shop."SKU Field Separator" + ItemVariant.Code - else - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Vendor Item No.": - TempShopifyVariant.SKU := Item."Vendor Item No."; - end; + TempShopifyVariant.SKU := GetVariantSKU(TempShopifyVariant.Barcode, Item."No.", '', Item."Vendor Item No."); TempShopifyVariant."Tax Code" := Item."Tax Group Code"; TempShopifyVariant.Taxable := true; TempShopifyVariant.Weight := Item."Gross Weight"; @@ -191,39 +147,11 @@ codeunit 30174 "Shpfy Create Product" TempShopifyVariant."UoM Option Id" := 1; TempShopifyVariant.Insert(false); until ItemUnitofMeasure.Next() = 0; - end else begin - Clear(TempShopifyVariant); - TempShopifyVariant."Available For Sales" := true; - TempShopifyVariant.Barcode := CopyStr(GetBarcode(Item."No.", '', Item."Sales Unit of Measure"), 1, MaxStrLen(TempShopifyVariant.Barcode)); - ProductPriceCalc.CalcPrice(Item, '', Item."Sales Unit of Measure", TempShopifyVariant."Unit Cost", TempShopifyVariant.Price, TempShopifyVariant."Compare at Price"); - TempShopifyVariant.Title := ItemVariant.Description; - TempShopifyVariant."Inventory Policy" := Shop."Default Inventory Policy"; - case Shop."SKU Mapping" of - Shop."SKU Mapping"::"Bar Code": - TempShopifyVariant.SKU := TempShopifyVariant.Barcode; - Shop."SKU Mapping"::"Item No.": - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := ItemVariant.Code; - SHop."SKU Mapping"::"Item No. + Variant Code": - if ItemVariant.Code <> '' then - TempShopifyVariant.SKU := Item."No." + Shop."SKU Field Separator" + ItemVariant.Code - else - TempShopifyVariant.SKU := Item."No."; - Shop."SKU Mapping"::"Vendor Item No.": - TempShopifyVariant.SKU := Item."Vendor Item No."; - end; - TempShopifyVariant."Tax Code" := Item."Tax Group Code"; - TempShopifyVariant.Taxable := true; - TempShopifyVariant.Weight := Item."Gross Weight"; - TempShopifyVariant."Shop Code" := Shop.Code; - TempShopifyVariant."Item SystemId" := Item.SystemId; - TempShopifyVariant.Insert(false); - end; + end else + CreateTempShopifyVariantFromItem(Item, TempShopifyVariant); + TempShopifyProduct.Insert(false); Events.OnAfterCreateTempShopifyProduct(Item, TempShopifyProduct, TempShopifyVariant, TempShopifyTag); - end; /// @@ -246,8 +174,8 @@ codeunit 30174 "Shpfy Create Product" /// Parameter of type Code[20]. /// Parameter of type Code[10]. /// Parameter of type Code[10]. - /// Return value of type Text. - local procedure GetVendorItemNo(ItemNo: Code[20]; VariantCode: Code[10]; UoM: Code[10]): Text; + /// Return value of type Code[50]. + local procedure GetVendorItemNo(ItemNo: Code[20]; VariantCode: Code[10]; UoM: Code[10]): Code[50]; var Item: Record Item; ItemReferenceMgt: Codeunit "Shpfy Item Reference Mgt."; @@ -256,6 +184,48 @@ codeunit 30174 "Shpfy Create Product" exit(ItemReferenceMgt.GetItemReference(ItemNo, VariantCode, UoM, "Item Reference Type"::Vendor, Item."Vendor No.")); end; + local procedure GetVariantSKU(BarCode: Text[50]; ItemNo: Text[20]; VariantCode: Text[10]; VendorItemNo: Text[50]): Text[50] + begin + case Shop."SKU Mapping" of + Shop."SKU Mapping"::"Bar Code": + exit(BarCode); + Shop."SKU Mapping"::"Item No.": + exit(ItemNo); + Shop."SKU Mapping"::"Variant Code": + if VariantCode <> '' then + exit(VariantCode); + Shop."SKU Mapping"::"Item No. + Variant Code": + if VariantCode <> '' then + exit(ItemNo + Shop."SKU Field Separator" + VariantCode) + else + exit(ItemNo); + Shop."SKU Mapping"::"Vendor Item No.": + exit(VendorItemNo); + end; + end; + + /// + /// Creates a temporary Shopify variant with information from an item. + /// + /// The item to create the variant from. + /// The temporary Shopify variant record set where the variant will be inserted. + internal procedure CreateTempShopifyVariantFromItem(Item: Record Item; var TempShopifyVariant: Record "Shpfy Variant" temporary) + begin + Clear(TempShopifyVariant); + TempShopifyVariant."Available For Sales" := true; + TempShopifyVariant.Barcode := CopyStr(GetBarcode(Item."No.", '', Item."Sales Unit of Measure"), 1, MaxStrLen(TempShopifyVariant.Barcode)); + ProductPriceCalc.CalcPrice(Item, '', Item."Sales Unit of Measure", TempShopifyVariant."Unit Cost", TempShopifyVariant.Price, TempShopifyVariant."Compare at Price"); + TempShopifyVariant.Title := ''; // Title will be assigned to "Default Title" in Shopify as no Options are set. + TempShopifyVariant."Inventory Policy" := Shop."Default Inventory Policy"; + TempShopifyVariant.SKU := GetVariantSKU(TempShopifyVariant.Barcode, Item."No.", '', Item."Vendor Item No."); + TempShopifyVariant."Tax Code" := Item."Tax Group Code"; + TempShopifyVariant.Taxable := true; + TempShopifyVariant.Weight := Item."Gross Weight"; + TempShopifyVariant."Shop Code" := Shop.Code; + TempShopifyVariant."Item SystemId" := Item.SystemId; + TempShopifyVariant.Insert(false); + end; + /// /// Set Shop. /// diff --git a/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyProductAPI.Codeunit.al b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyProductAPI.Codeunit.al index 8e7042e9a4..3878464a4b 100644 --- a/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyProductAPI.Codeunit.al +++ b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyProductAPI.Codeunit.al @@ -608,4 +608,24 @@ codeunit 30176 "Shpfy Product API" begin exit(Value.Names.Get(Value.Ordinals.IndexOf(Value.AsInteger())).ToUpper()); end; + + /// + /// Gets all the Product Options for a product. + /// + /// Shopify product ID for which the options are to be retrieved. + /// Dictionary of option IDs and option names. + internal procedure GetProductOptions(ShopifyProductId: BigInteger) Options: Dictionary of [text, Text] + var + Parameters: Dictionary of [Text, Text]; + JResponse: JsonToken; + JOptions: JsonArray; + JOption: JsonToken; + begin + Parameters.Add('ProductId', Format(ShopifyProductId)); + JResponse := CommunicationMgt.ExecuteGraphQL(Enum::"Shpfy GraphQL Type"::GetProductOptions, Parameters); + + JsonHelper.GetJsonArray(JResponse, JOptions, 'data.product.options'); + foreach JOption in JOptions do + Options.Add(JsonHelper.GetValueAsText(JOption, 'id'), JsonHelper.GetValueAsText(JOption, 'name')); + end; } \ No newline at end of file diff --git a/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyVariantAPI.Codeunit.al b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyVariantAPI.Codeunit.al index f8829bff70..6f69b2787d 100644 --- a/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyVariantAPI.Codeunit.al +++ b/Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyVariantAPI.Codeunit.al @@ -55,11 +55,12 @@ codeunit 30189 "Shpfy Variant API" end; end; - /// - /// Add Product Variant. + /// + /// Adds a product variant to Shopify. /// - /// Parameter of type Record "Shopify Variant". - internal procedure AddProductVariant(var ShopifyVariant: Record "Shpfy Variant") + /// Shopify Variant record to add. + /// True if the variant was added successfully; otherwise, false. + internal procedure AddProductVariant(var ShopifyVariant: Record "Shpfy Variant"): Boolean var ShopLocation: Record "Shpfy Shop Location"; NewShopifyVariant: Record "Shpfy Variant"; @@ -140,8 +141,12 @@ codeunit 30189 "Shpfy Variant API" JResponse := CommunicationMgt.ExecuteGraphQL(GraphQuery.ToText()); NewShopifyVariant := ShopifyVariant; NewShopifyVariant.Id := JsonHelper.GetValueAsBigInteger(JResponse, 'data.productVariantCreate.productVariant.legacyResourceId'); - if NewShopifyVariant.Id > 0 then + if NewShopifyVariant.Id > 0 then begin NewShopifyVariant.Insert(); + exit(true); + end; + + exit(false); end; /// @@ -593,4 +598,16 @@ codeunit 30189 "Shpfy Variant API" if JsonHelper.GetJsonArray(JNode, JMetafields, 'edges') then foreach JItem in JMetafields do; end; + + /// + /// Deletes a product variant from Shopify. + /// + /// Id of the Shopify variant to delete. + internal procedure DeleteProductVariant(ShopifyVariantId: BigInteger) + var + Parameters: Dictionary of [Text, Text]; + begin + Parameters.Add('VariantId', Format(ShopifyVariantId)); + CommunicationMgt.ExecuteGraphQL(Enum::"Shpfy GraphQL Type"::ProductVariantDelete, Parameters); + end; } \ No newline at end of file diff --git a/Apps/W1/Shopify/app/src/Products/Pages/ShpfyVariants.Page.al b/Apps/W1/Shopify/app/src/Products/Pages/ShpfyVariants.Page.al index 73fa6d2127..3291b4e345 100644 --- a/Apps/W1/Shopify/app/src/Products/Pages/ShpfyVariants.Page.al +++ b/Apps/W1/Shopify/app/src/Products/Pages/ShpfyVariants.Page.al @@ -213,6 +213,26 @@ page 30127 "Shpfy Variants" end; end; } + action(AddItemsAsVariants) + { + ApplicationArea = All; + Caption = 'Add Items as Shopify Variants'; + Image = AddAction; + ToolTip = 'Add existing items as new Shopify variants for the selected parent product.'; + + trigger OnAction() + var + AddItemAsVariant: Report "Shpfy Add Item As Variant"; + ParentProductId: BigInteger; + begin + Rec.FilterGroup(4); + Evaluate(ParentProductId, Rec.GetFilter("Product Id")); + Rec.FilterGroup(0); + + AddItemAsVariant.SetParentProduct(ParentProductId); + AddItemAsVariant.Run(); + end; + } } } } diff --git a/Apps/W1/Shopify/app/src/Products/Reports/ShpfyAddItemAsVariant.Report.al b/Apps/W1/Shopify/app/src/Products/Reports/ShpfyAddItemAsVariant.Report.al new file mode 100644 index 0000000000..b7774c6e54 --- /dev/null +++ b/Apps/W1/Shopify/app/src/Products/Reports/ShpfyAddItemAsVariant.Report.al @@ -0,0 +1,116 @@ +namespace Microsoft.Integration.Shopify; + +using Microsoft.Inventory.Item; + +/// +/// A report to add items as variants to a parent product. +/// +report 30117 "Shpfy Add Item As Variant" +{ + ApplicationArea = All; + Caption = 'Add Item as Shopify Variant'; + ProcessingOnly = true; + UsageCategory = Administration; + + dataset + { + dataitem(Item; Item) + { + RequestFilterFields = "No."; + trigger OnPreDataItem() + begin + if ParentProductNo = 0 then + Error(MissingProductErr); + + Clear(CreateItemAsVariant); + CreateItemAsVariant.SetParentProduct(ParentProductNo); + CreateItemAsVariant.CheckProductAndShopSettings(); + CreateItemAsVariant.FindDefaultVariantId(); + + if GuiAllowed then begin + CurrItemNo := Item."No."; + ProcessDialog.Open(ProcessMsg, CurrItemNo); + ProcessDialog.Update(); + end; + end; + + trigger OnAfterGetRecord() + begin + if GuiAllowed then begin + CurrItemNo := Item."No."; + ProcessDialog.Update(); + end; + + CreateItemAsVariant.Run(Item); + end; + + trigger OnPostDataItem() + begin + CreateItemAsVariant.RemoveDefaultVariant(); + + if GuiAllowed then + ProcessDialog.Close(); + end; + } + } + + requestpage + { + SaveValues = true; + layout + { + area(Content) + { + group(ShopFilter) + { + Caption = 'Options'; + field(Shop; ShopCode) + { + ApplicationArea = All; + Caption = 'Shop Code'; + Editable = false; + ToolTip = 'Specifies the Shopify Shop.'; + ShowMandatory = true; + } + field(ParentItemNo; ParentProductNo) + { + ApplicationArea = All; + TableRelation = "Shpfy Product"; + LookupPageId = "Shpfy Products"; + Lookup = true; + Caption = 'Parent Item No.'; + ToolTip = 'Specifies the parent item number to which the variants will be added.'; + ShowMandatory = true; + + trigger OnValidate() + begin + SetParentProduct(ParentProductNo); + end; + } + } + } + } + } + + var + CreateItemAsVariant: Codeunit "Shpfy Create Item As Variant"; + ShopCode: Code[20]; + ParentProductNo: BigInteger; + CurrItemNo: Code[20]; + ProcessDialog: Dialog; + ProcessMsg: Label 'Adding item #1####################', Comment = '#1 = Item no.'; + MissingProductErr: Label 'You must select a parent product to add the items as variants to.'; + + /// + /// Sets the parent product to which items will be added as variants. + /// + /// The parent product number. + internal procedure SetParentProduct(NewParentProductNo: BigInteger) + var + ShopifyProduct: Record "Shpfy Product"; + begin + ParentProductNo := NewParentProductNo; + ShopifyProduct.Get(ParentProductNo); + ShopCode := ShopifyProduct."Shop Code"; + end; +} \ No newline at end of file