internal async Task FulfillOrderAsync()
{
await this.SetOrderStatusAsync(CustomerOrderStatus.InProcess);
IList<CustomerOrderItem> orderedItems = await this.StateManager.GetStateAsync<IList<CustomerOrderItem>>(OrderItemListPropertyName);
ActorEventSource.Current.ActorMessage(this, "Fullfilling customer order. ID: {0}. Items: {1}", this.Id.GetGuidId(), orderedItems.Count);
foreach (CustomerOrderItem tempitem in orderedItems)
{
ActorEventSource.Current.Message("OrderContains:{0}", tempitem);
}
//We loop through the customer order list.
//For every item that cannot be fulfilled, we add to backordered.
foreach (CustomerOrderItem item in orderedItems.Where(x => x.FulfillmentRemaining > 0))
{
IInventoryService inventoryService = this.ServiceProxyFactory.CreateServiceProxy<IInventoryService>(this.builder.ToUri(), item.ItemId.GetPartitionKey());
//First, check the item is listed in inventory.
//This will avoid infinite backorder status.
if ((await inventoryService.IsItemInInventoryAsync(item.ItemId, this.tokenSource.Token)) == false)
{
await this.SetOrderStatusAsync(CustomerOrderStatus.Canceled);
return;
}
int numberItemsRemoved =
await
inventoryService.RemoveStockAsync(
item.ItemId,
item.Quantity,
new CustomerOrderActorMessageId(
new ActorId(this.Id.GetGuidId()),
await this.StateManager.GetStateAsync<long>(RequestIdPropertyName)));
item.FulfillmentRemaining -= numberItemsRemoved;
}
IList<CustomerOrderItem> items = await this.StateManager.GetStateAsync<IList<CustomerOrderItem>>(OrderItemListPropertyName);
bool backordered = false;
// Set the status appropriately
foreach (CustomerOrderItem item in items)
{
if (item.FulfillmentRemaining > 0)
{
backordered = true;
break;
}
}
if (backordered)
{
await this.SetOrderStatusAsync(CustomerOrderStatus.Backordered);
}
else
{
await this.SetOrderStatusAsync(CustomerOrderStatus.Shipped);
}
ActorEventSource.Current.ActorMessage(
this,
"{0}; Fulfilled: {1}. Backordered: {2}",
await this.GetOrderStatusAsStringAsync(),
items.Count(x => x.FulfillmentRemaining == 0),
items.Count(x => x.FulfillmentRemaining > 0));
long messageRequestId = await this.StateManager.GetStateAsync<long>(RequestIdPropertyName);
await this.StateManager.SetStateAsync<long>(RequestIdPropertyName, ++messageRequestId);
}