This content is archived. See latest version here

Last updated: Mar 31 2014

Introduction

This document provides some examples of how to use the ECF API to work with warehouses and inventory features. Examples covered here are for instance how to get, save and delete warehouses and inventories.

Working with warehouses

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 warehouse inventories

Listing warehouse inventories

You can use IWarehouseInventoryService.List() to get list of warehouse inventory. There are overloads of List() to list warehouse inventory for a specifiec catalog entry, a specific warehouse, for a list of entries, list of warehouses or a combination of entries and warehouses. The examples below illustrates how it works.

Using IWarehouseInventoryService.List(IWarehouse) or IWarehouseInventoryService.List(IEnumerable<IWarehouse>) to get a list inventory for one or several warehouses.

C#
// List all inventories for a specific warehouse, separated by catalog entry.
public IEnumerable<IWarehouseInventory> ListInventoriesByWarehouse(string warehouseCode)
{
    IWarehouseInventoryService warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();

    var warehouse = ServiceLocator.Current.GetInstance<IWarehouseRepository>().Get(warehouseCode);

    return warehouseInventoryService.List(warehouse);
}
// List all inventories for one or more warehouses, separated by catalog entry.
public IEnumerable<IWarehouseInventory> ListInventoriesByWarehouses(List<string> warehouseCodes)
{
    IWarehouseInventoryService warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    var warehouses = warehouseCodes.Select(w => warehouseRepository.Get(w)).ToList();

    return warehouseInventoryService.List(warehouses);
}

Using IWarehouseInventoryService.List(CatalogKey) or IWarehouseInventoryService.List(IEnumerable<CatalogKey>) to get a list inventory for one or several catalog entries.

C#
// List all inventories for a specific catalog entry, separated by warehouse.
public IEnumerable<IWarehouseInventory> ListWarehouseInventoryByEntry(Entry catalogEntry)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();

    var catalogKey = new CatalogKey(catalogEntry);

    return warehouseInventoryService.List(catalogKey);
}
// List inventory records for one or more catalog entries at a specific warehouse.
public IEnumerable<IWarehouseInventory> ListWarehouseInventoryByEntries(List<Entry> catalogEntries)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();

    var catalogKeys = catalogEntries.Select(e => new CatalogKey(e)).ToList();

    return warehouseInventoryService.List(catalogKeys);
}

Using IWarehouseInventoryService.List(IEnumerable<CatalogKey>, IEnumerable<IWarehouse>) to get a list inventory for one or more catalog entries in one or more warehouses.

C#
// List inventory records for one or more catalog entries at one or more warehouses.
public IEnumerable<IWarehouseInventory> ListWarehouseInventoryByEntriesWarehouses(List<Entry> catalogEntries, List<string> warehouseCodes)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    var catalogKeys = catalogEntries.Select(e => new CatalogKey(e)).ToList();
    var warehouses = warehouseCodes.Select(w => warehouseRepository.Get(w)).ToList();

    return warehouseInventoryService.List(catalogKeys, warehouses);
}

Getting warehouse inventory totals

Using IWarehouseInventoryService.GetTotal(), IWarehouseInventoryService.ListTotals() to get a single inventory record list of warehouse inventories.

Scenario: there are 5 pieces of product A in warehouse No.1, and 3 pieces product A in warehouse No.2. GetTotal will return one row with an inventory record being 8 pieces for the product A and ListTotals will return 2 rows for each warehouse.

There are overloads of GetTotal, ListTotals to list warehouse inventories for a specifiec catalog entry, a specific warehouse, for a list of entries, a list of warehouses or a combination of entries and warehouses. The examples below illustrates how it works.

Using IWarehouseInventoryService.GetTotal(CatalogKey) or IWarehouseInventoryService.GetTotal(CatalogKey, IEnumerable<IWarehouse>) to get the inventory total for a catalog entry.

C#
// Gets a single inventory record for a specific catalog entry that rolls up all relevant warehouse records.
public IWarehouseInventory GetTotalInventoryByEntry(Entry catalogEntry)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var catalogKey = new CatalogKey(catalogEntry);

    return warehouseInventoryService.GetTotal(catalogKey);
}

// Gets a single inventory record for a catalog entry that rolls up its inventories across all of the specified warehouse records.
public IWarehouseInventory GetTotalInventoryByEntry(Entry catalogEntry, List<string> warehouseCodes)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    var catalogKey = new CatalogKey(catalogEntry);
    var warehouses = warehouseCodes.Select(w => warehouseRepository.Get(w)).ToList();

    return warehouseInventoryService.GetTotal(catalogKey, warehouses);
}

Using IWarehouseInventoryService.ListTotals(IEnumerable<CatalogKey>) or IWarehouseInventoryService.ListTotals(IEnumerable<CatalogKey>, IEnumerable<IWarehouse>) to get list inventory totals for a list of catalog entries.

C#
// Gets a single inventory record for each provided catalog entry that rolls up all relevant warehouse records for that catalog entry.
public IEnumerable<IWarehouseInventory> ListTotalInventoryByEntry(List<Entry> catalogEntries)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var catalogKeys = catalogEntries.Select(e => new CatalogKey(e)).ToList();

    return warehouseInventoryService.ListTotals(catalogKeys);
}

// Gets a single inventory record for each provided catalog entry that rolls up its inventories across all of the specified warehouse records.
public IEnumerable<IWarehouseInventory> ListTotalInventoryByEntry(List<Entry> catalogEntries, List<string> warehouseCodes)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    var catalogKeys = catalogEntries.Select(e => new CatalogKey(e)).ToList();
    var warehouses = warehouseCodes.Select(w => warehouseRepository.Get(w)).ToList();

    return warehouseInventoryService.ListTotals(catalogKeys, warehouses);
}

Saving warehouse inventories

Using IWarehouseInventoryService.Save() to add/edit and save warehouse inventories. The example below illustrates adding an inventory for a catalog entry to a warehouse.

C#
public void SaveWarehouseInventory(Entry catalogEntry)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();

    // Save the inventory for a single CatalogEntry at a specific warehouse.
    var inventory = new WarehouseInventory()
                    {
                        CatalogKey = new CatalogKey(catalogEntry), 
                        WarehouseCode = "SampleWarehouseCode",
                        InStockQuantity = 10 
                    };

    warehouseInventoryService.Save(inventory);
}

Deleting warehouse inventories

Using IWarehouseInventoryService.Delete() to delete warehouse inventories. The overloads of Delete() can be used to delete inventory information for an entry, inventory information for a warehouse or inventory information for a combination of entri(es) and warehouse(s). The examples below illustrates how it works.

Using IWarehouseInventoryService.Delete(CatalogKey, Warehouse) to delete a specific inventory for a catalog entry in a warehouse.

C#
// Deletes the single inventory record for a specific catalog entry and warehouse combination.
public void DeleteSingleInventory(Entry catalogEntry, string warehouseCode)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var warehouse = ServiceLocator.Current.GetInstance<IWarehouseRepository>().Get(warehouseCode);

    warehouseInventoryService.Delete(new CatalogKey(catalogEntry), warehouse);
}

Using IWarehouseInventoryService.Delete(IWarehouse) or IWarehouseInventoryService.Delete(IEnumerable<IWarehouse>) to delete all inventories in one or several warehouses.

C#
// Deletes all inventory records for a specific catalog warehouse.
public void DeleteWarehouseInventoryByWarehouse(string warehouseCode)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    var warehouse = warehouseRepository.Get(warehouseCode);
    warehouseInventoryService.Delete(warehouse);
}
// Deletes all inventory records for one or more catalog entries.
public void DeleteWarehouseInventoryByWarehouses(List<string> warehouseCodes)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();

    var warehouses = warehouseCodes.Select(w => warehouseRepository.Get(w)).ToList();
    warehouseInventoryService.Delete(warehouses);
}

Using IWarehouseInventoryService.Delete(CatalogKey) or IWarehouseInventoryService.Delete(IEnumerable<CatalogKey>) to delete all inventories for one or more catalog entries.

C#
// Deletes all inventory records for a specific catalog entry.
public void DeleteWarehouseInventoryByEntry(Entry catalogEntry)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();
    warehouseInventoryService.Delete(new CatalogKey(catalogEntry));
}
// Deletes all inventory records for a specific catalog entry.
public void DeleteWarehouseInventoryByEntries(List<Entry> catalogEntries)
{
    var warehouseInventoryService = ServiceLocator.Current.GetInstance<IWarehouseInventoryService>();

    var catalogKeys = catalogEntries.Select(e => new CatalogKey(e)).ToList();
    warehouseInventoryService.Delete(catalogKeys);
}

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/