Hide menu Last updated: Oct 12 2015

This topic provides some examples of how to use the ECF API to work with warehouses and inventory features. The following examples show how to get, save, and delete warehouses and inventories.

Listing warehouses

Using IWarehouseRepository.List() to list information for all warehouses.

C#
// Get list Warehouse
public IEnumerable<IWarehouse> ListAllWarehouses()
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    return warehouseRepository.List();
}

Getting a warehouse

Using IWarehouseRepository.Get() to get a single warehouse. You can get a warehouse by Id or by Code.

C#
// Get list Warehouse by ID
public IWarehouse GetWarehouse(int warehouseId)
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    return warehouseRepository.Get(warehouseId);
}
C#
// Get list Warehouse by Code
public IWarehouse GetWarehouse(string warehouseCode)
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    return warehouseRepository.Get(warehouseCode);
}

Saving a warehouse

Using IWarehouseRepository.Save(IWarehouse) to add/edit and save a warehouse.

C#
public void SaveWarehouse()
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    var warehouse = new Warehouse() 
        {
           ApplicationId = AppContext.Current.ApplicationId,
           Code = "NY",
           Name = "New York store",
           IsActive = true,
           IsPrimary = false,
           IsFulfillmentCenter = false,
           IsPickupLocation = true,
           IsDeliveryLocation = true
        };

    warehouseRepository.Save(warehouse);
}

Deleting a warehouse

Using IWarehouseRepository.Delete(int) to delete a warehouse.

C#
public void DeleteWarehouse(int warehouseId)
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    warehouseRepository.Delete(warehouseId);
}

Working with inventory records

public IEnumerable<InventoryRecord> ListAllInventories()
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.List();
}
public IEnumerable<InventoryRecord> QueryInventoriesByWarehouse(string warehouseCode)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByWarehouse(new[] { warehouseCode });
}
public IEnumerable<InventoryRecord> QueryInventoriesByEntryCode(string code)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByEntry(new [] { code });
}
public InventoryRecord GetInventoryByEntryCodeAndWarehouseCode(string code, string warehouseCode)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Get(code, warehouseCode);
}
public decimal GetTotalInventoryByEntry(string code)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByEntry(new[] { code }).Sum(x => x.PurchaseAvailableQuantity);
}
public void AddInventoryRecord(string code, string warehouseCode, decimal quantity)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    inventoryService.Insert(new [] 
    {
        new InventoryRecord
        {
            AdditionalQuantity = 0,
            BackorderAvailableQuantity = 0,
            BackorderAvailableUtc = DateTime.UtcNow,
            CatalogEntryCode = code,
            IsTracked = true,
            PreorderAvailableQuantity = 0,
            PreorderAvailableUtc = DateTime.UtcNow,
            PurchaseAvailableQuantity = quantity,
            PurchaseAvailableUtc = DateTime.UtcNow,
            WarehouseCode = warehouseCode
        } 
    });
}
public void UpdateInventoryRecord(InventoryRecord record)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    inventoryService.Update(new [] { record });
}
public void DeleteInventoryRecord(string code, string warehouseCode)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    inventoryService.Delete(new[] { new InventoryKey(code, warehouseCode) });
}
public InventoryResponse RequestInventory(string code, string warehouseCode, decimal quantity, int lineItemIndex)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    var response = inventoryService.Request(new InventoryRequest()
    {
        RequestDateUtc = DateTime.UtcNow,
        Items = new []
        {
            new InventoryRequestItem 
            {
                RequestType = InventoryRequestType.Purchase, 
                CatalogEntryCode = code, 
                Quantity = quantity,
                WarehouseCode = warehouseCode,
                ItemIndex = 0
            }
        }
    });
    return response;
}
public InventoryResponse CompleteInventoryRequest(Shipment shipment, string code)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    var lineItemIndex = shipment.Parent.LineItems.IndexOf(shipment.Parent.LineItems.FindItemByCatalogEntryId(code));
    var keys = shipment.GetInventoryOperationKey(lineItemIndex);
    var itemIndexStart = 0;
    var response = inventoryService.Request(new InventoryRequest()
    {
        RequestDateUtc = DateTime.UtcNow,
        Items = keys.Select(x =>
            new InventoryRequestItem 
            {
                RequestType = InventoryRequestType.Complete, 
                ItemIndex = itemIndexStart++,
                OperationKey = x
            }
        ).ToList()
    });

    if (!response.IsSuccess)
    {
        return response;
    }

    keys.ForEach(x => shipment.RemoveOperationKey(lineItemIndex, x));
    shipment.AcceptChanges();
    return response;
}
public InventoryResponse CancelInventoryRequest(Shipment shipment, string code)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    var lineItemIndex = shipment.Parent.LineItems.IndexOf(shipment.Parent.LineItems.FindItemByCatalogEntryId(code));
    var keys = shipment.GetInventoryOperationKey(lineItemIndex);
    var itemIndexStart = 0;
    var response = inventoryService.Request(new InventoryRequest()
    {
        RequestDateUtc = DateTime.UtcNow,
        Items = keys.Select(x =>
            new InventoryRequestItem
            {
                RequestType = InventoryRequestType.Cancel,
                ItemIndex = itemIndexStart++,
                OperationKey = x
            }
        ).ToList()
    });

    if (!response.IsSuccess)
    {
        return response;
    }

    keys.ForEach(x => shipment.RemoveOperationKey(lineItemIndex, x));
    shipment.AcceptChanges();
    return response;
}

Comments