/// <summary> /// Process all orders that are not dispatched and where payment is collected and not refunded. /// </summary> public void BatchProcessOrders() { List <Order> listOfOrders = Orders.GetPendingOrders().ToList(); List <int> listOfProcessed = new List <int>(); var paymentComplete = listOfOrders.Where(o => o.PaymentCompleted).ToList(); var enumerable = paymentComplete.ToList(); foreach (var order in enumerable) { for (int i = 0; i < order.Items.Count; i++) { int tempCode = order.Items[i].Product.Code; Product tempProduct = Products.Find(tempCode); if (!Products.ValidateProduct(tempProduct)) { Orders.RefundOrder(order.Number); return; } } } var ordersNotRefunded = enumerable.Where(o => o.PaymentRefunded == false); bool checkStock = true; bool checkAvailability = true; foreach (var order in ordersNotRefunded) { for (int i = 0; i < order.Items.Count; i++) { int tempCode = order.Items[i].Product.Code; Product tempProduct = Products.Find(tempCode); int stock = Products.GetStock(tempProduct.Code); if (stock < order.Items[i].Count) { checkStock = false; } if (order.Items[i].Product.Firstavailable >= DateTime.Now) { checkAvailability = false; } } if (checkStock && checkAvailability) { for (int i = 0; i < order.Items.Count; i++) { Products.DispatchStock(order.Items[i].Product.Code, order.Items[i].Count); } Orders.DispatchOrder(order.Number); listOfProcessed.Add(order.Number); } checkStock = true; checkAvailability = true; } string processedOrders = string.Join(",", listOfProcessed); MessageBox.Show("Orders: " + processedOrders + " has been processed"); }