For the first example, if a customer orders five of item ‘sku-0’ then the inventory system can be used to allocate, or “purchase” that item:

String itemId = "sku-0";
long quantity = 5;
// Assume inventory manager is defined as a property in this class.
InventoryManager inventory = getInventoryManager();
int status = inventory.purchase(itemId, quantity);

The purchase call status at this time is either INVENTORY_STATUS_SUCCEED, or INVENTORY_STATUS_FAIL. If it is INVENTORY_STATUS_SUCCEED, then everything is ready and processing can continue. If the status is INVENTORY_STATUS_FAIL, then check the availability status of the item to determine why it failed:

int availability = inventory.queryAvailabilityStatus(itemId);

The next step depends on the availability status. The following sample sets the newStatus value based on the retrieved availability status.

 int newStatus;

 if(availability == inventory.AVAILABILITY_STATUS_BACKORDERABLE)
newStatus = inventory.backorder(itemId, quantity);

 else if(availability == inventory.AVAILABILITY_STATUS_IN_STOCK)
newStatus = inventory.backorder(itemId, quantity);

 else if(availability == inventory.AVAILABILITY_STATUS_PREORDERABLE_
newStatus = inventory.preorder(itemId, quantity);

 else  // the only other option is AVAILABILITY_STATUS_OUT_OF_STOCK
newStatus = inventory.INVENTORY_STATUS_FAIL;

If the availability status is IN_STOCK, then backorder is called. Backorder is called because the only way that the purchase call could fail if the item is in stock is if the requested quantity is higher than the current stocklevel. If the item is backordered, it can be reallocated later, after the stock level increases.

The newStatus value INVENTORY_STATUS_FAIL indicates one of the following situations:

If the newStatus value is INVENTORY_STATUS_FAIL during fulfillment, then the system sets the state of the item to FAILED.

The above example does not include tracking features. Tracking is an important part of the inventory system. For example, you can track backordering or preordering the item and then change that item’s state. If an item is backordered and then the stockLevel is later increased, simply calling purchase is insufficient to remove the item from backorder.

The following changes must be made to the first code sample. This ensures that the backorderLevel and preorderLevel for the given items are current. For more information on states, see ATG Commerce States.

String itemId = "sku-0";
long quantity = 5;
// assume we have some way of getting the state of the given item
// in DCS during fulfillment this is done using
// ShippingGroupCommerceItemRelationship.getState();
int itemState = getItemState();
InventoryManager inventory = getInventoryManager();
// now, use the appropriate method, depending on the state
int status;
if(itemState == INITIAL) // normal case
status = inventory.purchase(itemId, quantity);
else if(itemState == BACK_ORDERED)
status = inventory.purchaseOffBackorder(itemId, quantity);
else if(itemState == PRE_ORDERED)
status = inventory.purchaseOffPreorder(itemId, quantity)
loading table of contents...