private void LoadOrder() { int orderID = Utilities.ParseNullable <int>(Request.QueryString["orderID"]) ?? 0; Facade.IOrder facOrder = new Facade.Order(); var order = facOrder.GetForOrderID(orderID); string msg = null; bool canCancel = false; if (order == null) { msg = "Error: order cannot be found"; } else { eCannotCancelOrderReason?reason; IEnumerable <int> ordersToCancel; canCancel = facOrder.CanRejectOrCancel(order, out ordersToCancel, out reason); if (canCancel) { this.OrdersToCancel = ordersToCancel.ToArray(); var otherOrders = ordersToCancel.Where(oid => oid != orderID); if (otherOrders.Any()) { // Display a warning message... note that canCancel is true so this will be displayed but still allow the user to proceed with cancelling the orders. msg = string.Format( "This order is part of a group which may only be cancelled as a whole. As a result, if you continue, order{0} {1} will also be cancelled.", otherOrders.Count() == 1 ? string.Empty : "s", Entities.Utilities.SentenceMerge(otherOrders.Select(oid => oid.ToString()))); } } else { var messages = new Dictionary <eCannotCancelOrderReason, string> { { eCannotCancelOrderReason.Already_Rejected, "Cannot cancel: the order has already been rejected." }, { eCannotCancelOrderReason.Already_Cancelled, "Cannot cancel: the order has already been cancelled." }, { eCannotCancelOrderReason.Invoiced, "Cannot cancel: this order has been invoiced." }, { eCannotCancelOrderReason.Grouped_With_Delivered, "This order cannot be cancelled because it is part of a group which may only be cancelled as a whole and which contains one or more orders which have already been delivered." }, { eCannotCancelOrderReason.Grouped_With_On_Run, "This order cannot be cancelled because it is part of a group which may only be cancelled as a whole and which contains one or more orders which are on a run. All orders in the group must first be removed from any run they are on." }, }; msg = messages[reason.Value]; } } lblCancellationMessage.Text = msg; pnlCancellationMessage.Visible = !string.IsNullOrEmpty(msg); txtCancellationReason.Text = order.CancellationReason; pnlCancellationReason.Visible = canCancel || txtCancellationReason.Text.Length > 0; btnConfirm.Visible = canCancel; }
public static object CanRejectOrders(string orderIdList) { var orderIDs = orderIdList .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => int.Parse(s)); bool canReject = true; string message = null; var cannotCancelReasons = new Dictionary <int, eCannotCancelOrderReason>(); var ordersToReject = Enumerable.Empty <int>(); Facade.IOrder facOrder = new Facade.Order(); foreach (int orderID in orderIDs) { var order = facOrder.GetForOrderID(orderID); IEnumerable <int> ordersToRejectHere; eCannotCancelOrderReason?reason; canReject &= facOrder.CanRejectOrCancel(order, out ordersToRejectHere, out reason); ordersToReject = ordersToReject.Union(ordersToRejectHere); if (!canReject) { cannotCancelReasons.Add(orderID, reason.Value); } } if (canReject) { var otherOrders = ordersToReject.Where(o => !orderIDs.Contains(o)); if (otherOrders.Any()) { message = string.Format( "One or more of the selected orders are part of a group which may only be rejected as a whole. As a result, if you continue, order{0} {1} will also be rejected or cancelled.", otherOrders.Count() == 1 ? string.Empty : "s", Entities.Utilities.SentenceMerge(otherOrders.Select(oid => oid.ToString()))); } } else { var messages = new Dictionary <eCannotCancelOrderReason, string> { { eCannotCancelOrderReason.Already_Rejected, "Cannot reject order {0}: the order has already been rejected." }, { eCannotCancelOrderReason.Already_Cancelled, "Cannot reject order {0}: the order has already been cancelled." }, { eCannotCancelOrderReason.Invoiced, "Cannot reject order {0}: this order has been invoiced." }, { eCannotCancelOrderReason.Grouped_With_Delivered, "Order {0} cannot be rejected because it is part of a group which may only be rejected as a whole and which contains one or more orders which have already been delivered." }, { eCannotCancelOrderReason.Grouped_With_On_Run, "Order {0} order cannot be rejected because it is part of a group which may only be rejected as a whole and which contains one or more orders which are on a run. All orders in the group must first be removed from any run they are on." }, }; var reasons = cannotCancelReasons.Select(ccr => string.Format(messages[ccr.Value], ccr.Key)); message = string.Concat( "Could not reject order(s):\n\n", string.Join("\n", reasons.ToArray())); } return(new { CanReject = canReject, OrdersToReject = string.Join(",", ordersToReject.Select(o => o.ToString()).ToArray()), Message = message, }); }