/// <summary> /// Will create ItemReservationDisplay /// for a given ItemReservation object /// </summary> /// <param name="item"></param> /// <param name="i_d"></param> /// <param name="dbContext"></param> /// <returns></returns> public static ItemReservationDisplay CreateNewItemReservationDisplay(ItemReservation i_r, InventoryContext dbContext) { DateTime expectedPickUpDate = dbContext.Reservations.OfType <CurrentReservation>() .Where(r => r.ReservationID == i_r.ReservationID) .Select(d => d.ExpectedPickUpDate) .First(); return(new ItemReservationDisplay(i_r.ReservationID, expectedPickUpDate, i_r.Quantity)); }
public override int GetHashCode() { int hash = 1; if (itemReservation_ != null) { hash ^= ItemReservation.GetHashCode(); } if (lodgingReservation_ != null) { hash ^= LodgingReservation.GetHashCode(); } return(hash); }
public async Task <int> AddReservation(int quantity) { var SKU = this.Id.GetId(); var state = await this.StateManager.TryGetStateAsync <ItemState>(StateName); ItemState value = state.HasValue ? state.Value : new ItemState() { SKU = SKU, Changes = new List <ItemReservation>() }; // update balance var initialBalanceQuantity = value.BalanceQuantity; value.BalanceQuantity -= quantity; // record change ItemReservation change = new ItemReservation() { SKU = SKU, Quantity = quantity, ReservedOn = DateTime.UtcNow }; value.Changes.Add(change); if (value.Changes.Count > 10) { value.Changes.RemoveAt(0); } // Data is saved to configured state store implicitly after each method execution by Actor's runtime. // Data can also be saved explicitly by calling this.StateManager.SaveStateAsync(); // State to be saved must be DataContract serializable. await this.StateManager.SetStateAsync <ItemState>( StateName, // state name value); // data saved for the named state "my_data" Console.WriteLine($"Balance of {SKU} was {initialBalanceQuantity}, now {value.BalanceQuantity}"); return(value.BalanceQuantity); }
public async Task <ActionResult <Item> > Reserve(Item reservation, [FromServices] DaprClient daprClient) { Console.WriteLine("Enter item reservation"); var state = await daprClient.GetStateEntryAsync <ItemState>(StoreName, reservation.SKU); state.Value ??= new ItemState() { SKU = reservation.SKU, Changes = new List <ItemReservation>() }; // update balance state.Value.BalanceQuantity -= reservation.Quantity; // record change ItemReservation change = new ItemReservation() { SKU = reservation.SKU, Quantity = reservation.Quantity, ReservedOn = DateTime.UtcNow }; state.Value.Changes.Add(change); if (state.Value.Changes.Count > 10) { state.Value.Changes.RemoveAt(0); } await state.SaveAsync(); // return current balance var result = new Item() { SKU = state.Value.SKU, Quantity = state.Value.BalanceQuantity }; Console.WriteLine($"Reservation of {result.SKU} is now {result.Quantity}"); return(result); }
public void MergeFrom(IncidentalItemReservationRemoveItemRequest other) { if (other == null) { return; } if (other.itemReservation_ != null) { if (itemReservation_ == null) { itemReservation_ = new global::HOLMS.Types.Booking.Indicators.IncidentalItemReservationIndicator(); } ItemReservation.MergeFrom(other.ItemReservation); } if (other.lodgingReservation_ != null) { if (lodgingReservation_ == null) { lodgingReservation_ = new global::HOLMS.Types.Booking.Indicators.ReservationIndicator(); } LodgingReservation.MergeFrom(other.LodgingReservation); } }
/// <summary> /// Convert unit of measure for item reservations. /// </summary> /// <param name="context">The request context.</param> /// <param name="itemReservations">The item reservations.</param> /// <param name="itemUnitConversions">The item unit conversions.</param> /// <exception cref="DataValidationException">Thrown if there is no conversion rules defined for any of item reservations.</exception> internal static void ConvertUnitOfMeasure(RequestContext context, IEnumerable <ItemReservation> itemReservations, IEnumerable <ItemUnitConversion> itemUnitConversions) { Collection <DataValidationFailure> validationFailures = new Collection <DataValidationFailure>(); Dictionary <ItemUnitConversion, UnitOfMeasureConversion> conversions = GetUnitOfMeasureConversions(context, itemUnitConversions); IEnumerator <ItemReservation> itemReservationEnumerator = itemReservations.GetEnumerator(); IEnumerator <ItemUnitConversion> itemUnitConversionEnumerator = itemUnitConversions.GetEnumerator(); while (itemReservationEnumerator.MoveNext() && itemUnitConversionEnumerator.MoveNext()) { ItemUnitConversion itemUnitConversion = itemUnitConversionEnumerator.Current; if (!itemUnitConversion.IsNop) { UnitOfMeasureConversion unitOfMeasureConversion; if (conversions.TryGetValue(itemUnitConversion, out unitOfMeasureConversion)) { ItemReservation itemReservation = itemReservationEnumerator.Current; itemReservation.Quantity = unitOfMeasureConversion.Convert(itemReservation.Quantity); } else { DataValidationFailure validationFailure = new DataValidationFailure(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_UnitOfMeasureConversionNotFound); validationFailure.ErrorContext = string.Format(CultureInfo.InvariantCulture, "No conversion rules defined for ItemUnitConversion:{0}", itemUnitConversion); validationFailures.Add(validationFailure); } } } if (validationFailures.Count > 0) { throw new DataValidationException( DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_AggregateValidationError, validationFailures, string.Format("There are {0} item reservations whose unit of measures cannot be converted.", validationFailures.Count)); } }
public async Task <ActionResult <OrderReservation> > ReserveOrder(Order order, [FromServices] DaprClient daprClient) { // retrieve state of whole reservation request var stateReservation = await daprClient.GetStateEntryAsync <ReservationState>(StoreName_reservation, order.Id.ToString()); stateReservation.Value ??= new ReservationState() { OrderId = order.Id, ReservedItems = new List <ItemReservation>() }; var result = new OrderReservation() { OrderId = order.Id, ReservedItems = new List <Item>() }; // iterate over state of each reservation item foreach (var item in order.Items) { var SKU = item.ProductCode; var quantity = item.Quantity; var stateItem = await daprClient.GetStateEntryAsync <ItemState>(StoreName_item, SKU); stateItem.Value ??= new ItemState() { SKU = SKU, Changes = new List <ItemReservation>() }; // update balance stateItem.Value.BalanceQuantity -= quantity; // record change ItemReservation change = new ItemReservation() { SKU = SKU, Quantity = quantity, ReservedOn = DateTime.UtcNow }; stateItem.Value.Changes.Add(change); // keep only the 10 latest changes if (stateItem.Value.Changes.Count > 10) { stateItem.Value.Changes.RemoveAt(0); } // save item state await stateItem.SaveAsync(); // compose result result.ReservedItems.Add(new Item { SKU = SKU, BalanceQuantity = stateItem.Value.BalanceQuantity }); // save updated reservation state stateReservation.Value.ReservedItems.Add(change); await stateReservation.SaveAsync(); Console.WriteLine($"Reservation in {stateReservation.Value.OrderId} of {SKU} for {quantity}, balance {stateItem.Value.BalanceQuantity}"); } await daprClient.PublishEventAsync <Order>(PubSub, common.Topics.ReservationCompletedTopicName, order); Console.WriteLine($"Reservation in {stateReservation.Value.OrderId} completed"); return(this.Ok()); }
public async Task <ActionResult <Guid> > OnCustomizationFailed(OrderCustomization customization, [FromServices] DaprClient daprClient) { // retrieve state of whole reservation request var stateReservation = await daprClient.GetStateEntryAsync <ReservationState>(StoreName_reservation, customization.OrderId.ToString()); if (stateReservation.Value == null) { return(this.NotFound()); } // ReservedItems: group by SKU & sum by Quantity var groupedReserved = from item in stateReservation.Value.ReservedItems group item by item.SKU into items select new { SKU = items.Key, Quantity = items.Sum(x => x.Quantity) }; // CustomizedItems: group by SKU & sum by Quantity var groupedCustomized = from item in customization.CustomizedItems group item by item.SKU into items select new { SKU = items.Key, Quantity = items.Sum(x => x.Quantity) }; foreach (var itemReserved in groupedReserved) { var customizedItem = groupedCustomized.Where(e => e.SKU == itemReserved.SKU).FirstOrDefault(); var compensateQuantity = (itemReserved.Quantity - (customizedItem == null ? 0 : customizedItem.Quantity)); if (compensateQuantity > 0) { var SKU = itemReserved.SKU; // compensate balance var quantity = -compensateQuantity; // retrieve reservation item from state var stateItem = await daprClient.GetStateEntryAsync <ItemState>(StoreName_item, SKU); // update balance stateItem.Value.BalanceQuantity -= quantity; // record change ItemReservation change = new ItemReservation() { SKU = SKU, Quantity = quantity, ReservedOn = DateTime.UtcNow }; stateItem.Value.Changes.Add(change); if (stateItem.Value.Changes.Count > 10) { stateItem.Value.Changes.RemoveAt(0); } // save item state await stateItem.SaveAsync(); // save updated reservation state stateReservation.Value.ReservedItems.Add(change); await stateReservation.SaveAsync(); Console.WriteLine($"Reservation in {stateReservation.Value.OrderId} of {SKU} for {quantity}, balance {stateItem.Value.BalanceQuantity}"); } } Console.WriteLine($"Acknowledged customization failed for order {customization.OrderId}"); return(this.Ok()); }