From c5fc2a8fb7dfd074266794558caab1cc79165453 Mon Sep 17 00:00:00 2001 From: Tine Staric Date: Fri, 15 Mar 2024 15:13:12 +0200 Subject: [PATCH 1/2] Add Location Loop to Sync Inventory --- .../ShpfyBackgroundSyncs.Codeunit.al | 58 ++++++++++++++++--- .../app/src/Base/Pages/ShpfyShopCard.Page.al | 2 +- .../Codeunits/ShpfyInventoryAPI.Codeunit.al | 8 ++- .../Codeunits/ShpfySyncInventory.Codeunit.al | 29 +++++++--- .../Reports/ShpfySyncStockToShopify.Report.al | 23 +++++--- .../Tables/ShpfyShopLocation.Table.al | 2 + .../src/Products/Pages/ShpfyProducts.Page.al | 2 +- 7 files changed, 98 insertions(+), 26 deletions(-) diff --git a/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al b/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al index de7e4eebdc..0b9020c209 100644 --- a/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al +++ b/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al @@ -199,8 +199,38 @@ codeunit 30101 "Shpfy Background Syncs" exit(JobQueueEntry.ID); end; + /// + /// Opens a request page where the user can select the shop and location for which to sync inventory and then starts the inventory sync. + /// + /// Code of the shop for which to sync inventory. This parameter is used to preselect the shop on the request page. + internal procedure InventorySyncWithReqPage(ShopCode: Code[20]) + var + Shop: Record "Shpfy Shop"; + ShopLocation: Record "Shpfy Shop Location"; + BackgroundSyncs: Codeunit "Shpfy Background Syncs"; + FilterBuilder: FilterPageBuilder; + InventorySyncLbl: Label 'Inventory Sync'; + begin + Shop.Get(ShopCode); + Shop.SetRecFilter(); + ShopLocation.SetRange("Shop Code", Shop.Code); + + FilterBuilder.PageCaption(InventorySyncLbl); + + FilterBuilder.AddRecord(ShopLocation.TableCaption, ShopLocation); + FilterBuilder.AddField(ShopLocation.TableCaption, ShopLocation.Id); + FilterBuilder.SetView(ShopLocation.TableCaption, ShopLocation.GetView()); + + if FilterBuilder.RunModal() then begin + ShopLocation.SetView(FilterBuilder.GetView(ShopLocation.TableCaption)); + Shop.SetFilter(Code, ShopLocation.GetFilter("Shop Code")); + + BackgroundSyncs.InventorySync(Shop, ShopLocation); + end; + end; + /// - /// Inventory Sync. + /// Synchronizes inventory for a shop. /// /// Parameter of type Code[20]. internal procedure InventorySync(ShopCode: Code[20]) @@ -214,25 +244,37 @@ codeunit 30101 "Shpfy Background Syncs" end; /// - /// Inventory Sync. + /// Synchronizes inventory for a shop. /// /// Parameter of type Record "Shopify Shop". internal procedure InventorySync(var Shop: Record "Shpfy Shop") var - ShopifyShopInventory: Record "Shpfy Shop Inventory"; + ShopLocation: Record "Shpfy Shop Location"; + begin + ShopLocation.SetRange("Shop Code", Shop.Code); + InventorySync(Shop, ShopLocation); + end; + + /// + /// Synchronizes inventory for a shop. + /// + /// Shopify shop for which to sync inventory. + /// Shopify shop location for which to sync inventory. + internal procedure InventorySync(var Shop: Record "Shpfy Shop"; var ShopLocation: Record "Shpfy Shop Location") + var Parameters: Text; - InventoryParametersTxt: Label '%1', Comment = '%1 = Shop Record View', Locked = true; + InventoryParametersTxt: Label '%1%2', Comment = '%1 = Shop Record View, %2 = Shop Location Record View', Locked = true; begin Shop.SetRange("Allow Background Syncs", true); if not Shop.IsEmpty then begin - Parameters := StrSubstNo(InventoryParametersTxt, Shop.GetView()); + Parameters := StrSubstNo(InventoryParametersTxt, Shop.GetView(), ShopLocation.GetView()); EnqueueJobEntry(Report::"Shpfy Sync Stock to Shopify", Parameters, StrSubstNo(SyncDescriptionTxt, InventorySyncTypeTxt, Shop.GetFilter(Code)), true, true); end; + Shop.SetRange("Allow Background Syncs", false); if not Shop.IsEmpty then begin - ShopifyShopInventory.Reset(); - ShopifyShopInventory.SetRange("Shop Code", Shop.Code); - Codeunit.Run(Codeunit::"Shpfy Sync Inventory", ShopifyShopInventory); + Parameters := StrSubstNo(InventoryParametersTxt, Shop.GetView(), ShopLocation.GetView()); + EnqueueJobEntry(Report::"Shpfy Sync Stock to Shopify", Parameters, StrSubstNo(SyncDescriptionTxt, InventorySyncTypeTxt, Shop.GetFilter(Code)), false, true); end; end; diff --git a/Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al b/Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al index 01b3d303ab..266a0b0a5b 100644 --- a/Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al +++ b/Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al @@ -906,7 +906,7 @@ page 30101 "Shpfy Shop Card" var BackgroundSyncs: Codeunit "Shpfy Background Syncs"; begin - BackgroundSyncs.InventorySync(Rec.Code); + BackgroundSyncs.InventorySyncWithReqPage(Rec.Code); end; } action(SyncCustomers) diff --git a/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfyInventoryAPI.Codeunit.al b/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfyInventoryAPI.Codeunit.al index 75b30ad6c4..49f5ba6083 100644 --- a/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfyInventoryAPI.Codeunit.al +++ b/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfyInventoryAPI.Codeunit.al @@ -102,12 +102,18 @@ codeunit 30195 "Shpfy Inventory API" exit(JsonHelper.GetJsonObject(JLocation, JResult, 'inventoryLevels')); end; - internal procedure SetInventoryIds() + /// + /// Collects all the inventory ids for a specific location. + /// It is used to remove the inventory ids that are not in the Shopify location after the inventory levels are imported. + /// + /// + internal procedure SetInventoryIds(LocationId: BigInteger) var ShopInventory: Record "Shpfy Shop Inventory"; begin Clear(InventoryIds); ShopInventory.SetRange("Shop Code", ShopifyShop.Code); + ShopInventory.SetRange("Location Id", LocationId); ShopInventory.LoadFields(SystemId); if ShopInventory.FindSet() then repeat diff --git a/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfySyncInventory.Codeunit.al b/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfySyncInventory.Codeunit.al index 7fe5866b85..0d0915ca81 100644 --- a/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfySyncInventory.Codeunit.al +++ b/Apps/W1/Shopify/app/src/Inventory/Codeunits/ShpfySyncInventory.Codeunit.al @@ -16,16 +16,12 @@ codeunit 30197 "Shpfy Sync Inventory" var ShopInventory: Record "Shpfy Shop Inventory"; ShopLocation: Record "Shpfy Shop Location"; - ShopFilter: Text; begin - ShopFilter := Rec.GetFilter("Shop Code"); - if ShopFilter <> '' then begin - ShopLocation.SetRange("Shop Code", ShopFilter); - ShopInventory.SetRange("Shop Code", ShopFilter); - end; + SetShopAndLocationFilters(Rec, ShopInventory, ShopLocation); + if ShopLocation.FindSet(false) then begin InventoryApi.SetShop(ShopLocation."Shop Code"); - InventoryApi.SetInventoryIds(); + InventoryApi.SetInventoryIds(ShopLocation.Id); repeat InventoryApi.ImportStock(ShopLocation); until ShopLocation.Next() = 0; @@ -34,4 +30,23 @@ codeunit 30197 "Shpfy Sync Inventory" InventoryApi.ExportStock(ShopInventory); end; + + local procedure SetShopAndLocationFilters(var FilteredInventory: Record "Shpfy Shop Inventory"; var ShopInventory: Record "Shpfy Shop Inventory"; var ShopLocation: Record "Shpfy Shop Location") + var + ShopFilter: Text; + LocationFilter: BigInteger; + begin + ShopFilter := FilteredInventory.GetFilter("Shop Code"); + Evaluate(LocationFilter, FilteredInventory.GetFilter("Location Id")); + + if ShopFilter <> '' then begin + ShopLocation.SetRange("Shop Code", ShopFilter); + ShopInventory.SetRange("Shop Code", ShopFilter); + end; + + if LocationFilter <> 0 then begin + ShopLocation.SetRange(Id, LocationFilter); + ShopInventory.SetRange("Location Id", LocationFilter); + end; + end; } \ No newline at end of file diff --git a/Apps/W1/Shopify/app/src/Inventory/Reports/ShpfySyncStockToShopify.Report.al b/Apps/W1/Shopify/app/src/Inventory/Reports/ShpfySyncStockToShopify.Report.al index 50a48cd947..0f3997906f 100644 --- a/Apps/W1/Shopify/app/src/Inventory/Reports/ShpfySyncStockToShopify.Report.al +++ b/Apps/W1/Shopify/app/src/Inventory/Reports/ShpfySyncStockToShopify.Report.al @@ -16,14 +16,21 @@ report 30102 "Shpfy Sync Stock to Shopify" { RequestFilterFields = Code; - trigger OnAfterGetRecord() - var - ShopifyShopInventory: Record "Shpfy Shop Inventory"; - begin - ShopifyShopInventory.Reset(); - ShopifyShopInventory.SetRange("Shop Code", Shop.Code); - CodeUnit.Run(Codeunit::"Shpfy Sync Inventory", ShopifyShopInventory); - end; + dataitem(ShopLocation; "Shpfy Shop Location") + { + RequestFilterFields = Id; + DataItemLink = "Shop Code" = field(Code); + + trigger OnAfterGetRecord() + var + ShopifyShopInventory: Record "Shpfy Shop Inventory"; + begin + ShopifyShopInventory.Reset(); + ShopifyShopInventory.SetRange("Shop Code", Shop.Code); + ShopifyShopInventory.SetRange("Location Id", ShopLocation.Id); + CodeUnit.Run(Codeunit::"Shpfy Sync Inventory", ShopifyShopInventory); + end; + } } } diff --git a/Apps/W1/Shopify/app/src/Inventory/Tables/ShpfyShopLocation.Table.al b/Apps/W1/Shopify/app/src/Inventory/Tables/ShpfyShopLocation.Table.al index a65031fa5c..09e19856f7 100644 --- a/Apps/W1/Shopify/app/src/Inventory/Tables/ShpfyShopLocation.Table.al +++ b/Apps/W1/Shopify/app/src/Inventory/Tables/ShpfyShopLocation.Table.al @@ -27,6 +27,8 @@ table 30113 "Shpfy Shop Location" { Caption = 'Id'; DataClassification = CustomerContent; + TableRelation = "Shpfy Shop Location".Id where("Shop Code" = field("Shop Code")); + ValidateTableRelation = false; Editable = false; } diff --git a/Apps/W1/Shopify/app/src/Products/Pages/ShpfyProducts.Page.al b/Apps/W1/Shopify/app/src/Products/Pages/ShpfyProducts.Page.al index 29ce2f2628..e2ab272401 100644 --- a/Apps/W1/Shopify/app/src/Products/Pages/ShpfyProducts.Page.al +++ b/Apps/W1/Shopify/app/src/Products/Pages/ShpfyProducts.Page.al @@ -384,7 +384,7 @@ page 30126 "Shpfy Products" var BackgroundSyncs: Codeunit "Shpfy Background Syncs"; begin - BackgroundSyncs.InventorySync(Rec."Shop Code"); + BackgroundSyncs.InventorySyncWithReqPage(Rec."Shop Code"); end; } } From 5bc36ca25ca7f9cef4ab0ac95b2e60f6b62df976 Mon Sep 17 00:00:00 2001 From: Tine Staric Date: Fri, 15 Mar 2024 15:58:37 +0200 Subject: [PATCH 2/2] Adjust documentation --- .../src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al b/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al index 0b9020c209..5e453b5016 100644 --- a/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al +++ b/Apps/W1/Shopify/app/src/Base/Codeunits/ShpfyBackgroundSyncs.Codeunit.al @@ -232,7 +232,7 @@ codeunit 30101 "Shpfy Background Syncs" /// /// Synchronizes inventory for a shop. /// - /// Parameter of type Code[20]. + /// Code of the shop for which to sync inventory. internal procedure InventorySync(ShopCode: Code[20]) var Shop: Record "Shpfy Shop"; @@ -246,7 +246,7 @@ codeunit 30101 "Shpfy Background Syncs" /// /// Synchronizes inventory for a shop. /// - /// Parameter of type Record "Shopify Shop". + /// Filtered shopify shop record set for which to sync inventory. internal procedure InventorySync(var Shop: Record "Shpfy Shop") var ShopLocation: Record "Shpfy Shop Location"; @@ -258,8 +258,8 @@ codeunit 30101 "Shpfy Background Syncs" /// /// Synchronizes inventory for a shop. /// - /// Shopify shop for which to sync inventory. - /// Shopify shop location for which to sync inventory. + /// Filtered shopify shop record set for which to sync inventory. + /// Filtered shopify shop location record set for which to sync inventory. internal procedure InventorySync(var Shop: Record "Shpfy Shop"; var ShopLocation: Record "Shpfy Shop Location") var Parameters: Text;