Skip to content

Commit

Permalink
Add Location Loop to Sync Inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
Tine Staric committed Mar 15, 2024
1 parent 8d8b3ba commit c5fc2a8
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,38 @@ codeunit 30101 "Shpfy Background Syncs"
exit(JobQueueEntry.ID);
end;

/// <summary>
/// Opens a request page where the user can select the shop and location for which to sync inventory and then starts the inventory sync.
/// </summary>
/// <param name="ShopCode">Code of the shop for which to sync inventory. This parameter is used to preselect the shop on the request page.</param>
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;

/// <summary>
/// Inventory Sync.
/// Synchronizes inventory for a shop.
/// </summary>
/// <param name="ShopCode">Parameter of type Code[20].</param>
internal procedure InventorySync(ShopCode: Code[20])
Expand All @@ -214,25 +244,37 @@ codeunit 30101 "Shpfy Background Syncs"
end;

/// <summary>
/// Inventory Sync.
/// Synchronizes inventory for a shop.
/// </summary>
/// <param name="Shop">Parameter of type Record "Shopify Shop".</param>
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;

/// <summary>
/// Synchronizes inventory for a shop.
/// </summary>
/// <param name="Shop">Shopify shop for which to sync inventory.</param>
/// <param name="ShopLocation">Shopify shop location for which to sync inventory.</param>
internal procedure InventorySync(var Shop: Record "Shpfy Shop"; var ShopLocation: Record "Shpfy Shop Location")
var
Parameters: Text;
InventoryParametersTxt: Label '<?xml version="1.0" standalone="yes"?><ReportParameters name="Shpfy Sync Stock To Shopify" id="30102"><DataItems><DataItem name="Shop">%1</DataItem></DataItems></ReportParameters>', Comment = '%1 = Shop Record View', Locked = true;
InventoryParametersTxt: Label '<?xml version="1.0" standalone="yes"?><ReportParameters name="Shpfy Sync Stock To Shopify" id="30102"><DataItems><DataItem name="Shop">%1</DataItem><DataItem name="ShopLocation">%2</DataItem></DataItems></ReportParameters>', 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;

Expand Down
2 changes: 1 addition & 1 deletion Apps/W1/Shopify/app/src/Base/Pages/ShpfyShopCard.Page.al
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,18 @@ codeunit 30195 "Shpfy Inventory API"
exit(JsonHelper.GetJsonObject(JLocation, JResult, 'inventoryLevels'));
end;

internal procedure SetInventoryIds()
/// <summary>
/// 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.
/// </summary>
/// <param name="LocationId"></param>
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit c5fc2a8

Please sign in to comment.