Developer forum

Forum » Integration » Live integration of stock from several locations

Live integration of stock from several locations

Tomas Gomez
Reply

Hi,

We need to implement a live integration of the stock. The stock is stored at several locations. The live integration is from an OnPremise Bussines Central server to a DW 9.124.5 with Swift 1.16.

Some months ago the live stock was not available on a  former DW version. Is it available now? Is it available to live-integrate it with several locations?

If not, what is the best approach? A scheduled task?

Is there any other consideration we should take into account?

Regards,
Tomas


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi Tomas,

 

Unfortunately this is still not available. There is a workaround though.

 

Considerations

  • The ViewModels (hence the webapi as well) don't get the Live Prices updated automatically
  • This also means that in Swift, the lazy loading of data will be useless - the workaround will make the request to the erp on page request
    This also means if the ERP is slow to respond, the webpage will take a bit longer (not feel zippy since it's held hostage by the ERP request/response)
    This will be noticeable if you apply the workaround to templates used in a product list

 

C# method

I'd recommend you put this into a dll. This method updates the ViewModel Prices collection with the Live Integraton response values.

        public static void UpdateProductViewModelPrices(ProductViewModel product)
        {
            if (!Dynamicweb.Ecommerce.DynamicwebLiveIntegration.IsWebServiceConnectionAvailable())
            {
                return;
            }
            var productIdentifierKey = Dynamicweb.Ecommerce.LiveIntegration.Examples.CustomProductProvider.GetProductIdentifier(product.Id, product.VariantId, product.LanguageId, product.Number, product.DefaultUnitId);
            var productInfo = Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products.ProductManager.GetProductInfo(productIdentifierKey);
 
            if (productInfo == null)
            {
                Dynamicweb.Ecommerce.LiveIntegration.TemplatesHelper.UpdateProduct(product.Id, product.VariantId, 0, product.DefaultUnitId, true);
                productInfo = Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Products.ProductManager.GetProductInfo(productIdentifierKey);
 
                if (productInfo == null)
                {
                    return;
                }
            }
            
            var totalPrice = Dynamicweb.Core.Converter.ToDouble(productInfo["TotalPrice"]);
            product.Price.Price = totalPrice;
            product.Price.PriceFormatted = Dynamicweb.Ecommerce.Services.Currencies.Format(Dynamicweb.Ecommerce.Common.Context.Currency, totalPrice);
 
            var productInfoPrices = ((IList<ProductPrice>)productInfo["Prices"] ?? Enumerable.Empty<ProductPrice>()).Where(p =>
                (p.ValidFrom == null || p.ValidFrom.Value <= DateTime.Now.Date) &&
                (p.ValidTo == null || p.ValidTo.Value >= DateTime.Now.Date) &&
                ((p.UnitId ?? "") == "" || (p.UnitId ?? "") == (product.DefaultUnitId ?? ""))).ToList();
 
            if (!productInfoPrices.Any()) return;
            
            product.Prices.Clear();
                
            foreach (var productInfoPrice in productInfoPrices.Where(productInfoPrice => productInfoPrice.Amount != null && productInfoPrice.Quantity != null))
            {
                var amount = productInfoPrice.Amount ?? 0;
                product.Prices.Add(new PriceListViewModel {
                    Price = new PriceViewModel { 
                        Price = amount, 
                        PriceFormatted = Dynamicweb.Ecommerce.Services.Currencies.Format(Dynamicweb.Ecommerce.Common.Context.Currency, amount) 
                    },
                    Quantity = productInfoPrice.Quantity ?? 0
                });
            }
        }

 

 

Templates

In the templates all you need to do it

        Your.Custom.Assembly.UpdateProductViewModelPrices(product);
        isLazyLoadingForProductInfoEnabled = false;

The first line updates the ViewModel; The second line should update all of the other template logic to work accordingly (because this does not work with lazy loading of live integration data).

These lines need to be applied after both the "product" variable and the "isLazyLoadingForProductInfoEnabled" variable are defined. Ideally if this is fixed in the near future, you simply need to delete these 2 lines and you are good to go.

 

Final notes

The C# methods I got you were slightly altered from what we use, because we created overloads under the same class (in our project) so it was easier to use and understand. Nonetheless, I looked up in DW's source code and I believe I got the right namespaces and classes for all the methods. The method names are accurate though.

 

Hope this helps.

 

Best Regards,

Nuno Aguiar

Votes for this answer: 1
 
Tomas Gomez
Reply

Thanks Nuno for the explanations, and specially for the code :-)

Regards,
Tomas

 
Tomas Gomez
Reply

Hi Nuno,

I am just going to implement your code :-)

The point is that the ERP (Bussines Central) store the products in several locations, so I need to modidy the code to fetch stock intead of prices and to regard the several locations.

There is a method GetUnitStock(StockLocation, UnitId) but there is not a "SetUnitStock(StockLocation, UnitId)". How should I store the stock of each location?  By chance, do you have a similar code for the stock? 

Regards,
Tomas

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Tomas,

 

My response was only for prices. I haven't come across your challenge (yet). I'd sure be interested to know what you find though.

 

Best Regards,

Nuno Aguiar

 
Tomas Gomez
Reply

Thanks anyway; I'll keep you informed :-)

BR Tomas

 

You must be logged in to post in the forum