public void ReceiveShimpmentOrder(ReceivedOrderDetails order) { /* command modify Products, ReceiveOrders, ReceiveOrderItems and ReturnOrderItems */ List <Exception> errors = new List <Exception>(); using (var context = new eRaceContext()) { var existing = context.Orders.Find(order.OrderID); if (order == null) { errors.Add(new ArgumentNullException(nameof(order), $"No {nameof(order)} was supplied for ")); } else { if (existing.Closed == true) { errors.Add(new BusinessRuleException <bool>("The order you are accessing is already closed.", nameof(existing.Closed), existing.Closed)); } if (order.OrderID == 0) { errors.Add(new BusinessRuleException <int>("The OrderID is required.", nameof(order.OrderID), order.OrderID)); } if (order.ReceivedOrder.OrderID == 0) { errors.Add(new BusinessRuleException <int>("The OrderID is required.", nameof(order.ReceivedOrder.OrderID), order.ReceivedOrder.OrderID)); } if (order.ReceivedOrder.EmployeeID == 0) { errors.Add(new BusinessRuleException <int>("The Employee ID is required.", nameof(order.ReceivedOrder.EmployeeID), order.ReceivedOrder.EmployeeID)); } foreach (var item in order.ReceiveOrderItems) { var existingOrderDetail = context.OrderDetails.Find(item.OrderDetailID); var itemsize = context.OrderDetails.Find(item.OrderDetailID).OrderUnitSize; if (existingOrderDetail == null) { errors.Add(new ArgumentNullException(nameof(item.OrderDetailID), $"There is no Order Detail with the ID given.")); } if (item.ItemQuantity < 1) { errors.Add(new BusinessRuleException <int>("The item quantity must be greater than 0", nameof(item.ItemQuantity), item.ItemQuantity)); } if (item.ItemQuantity / itemsize >= context.OrderDetails.Find(item.OrderDetailID).Quantity + 1) { errors.Add(new BusinessRuleException <int>("The total amount received cannot exceed the amount ordered by 1 package.", nameof(item.ItemQuantity), item.ItemQuantity)); } } foreach (var item in order.Products) { if (item.QuantityReceived < 1) { errors.Add(new BusinessRuleException <int>("The number of received items must be postive. ", nameof(item.QuantityReceived), item.QuantityReceived)); } } foreach (var item in order.ReturnOrderItems) { if (item.ItemQuantity < 1) { errors.Add(new BusinessRuleException <int>("The number of returned items is required.", nameof(item.ItemQuantity), item.ItemQuantity)); } } } if (errors.Any()) { throw new BusinessRuleCollectionException("Unable to receive shipment.", errors); } // ReceiveOrder var newReceiveOrder = new ReceiveOrder { OrderID = order.ReceivedOrder.OrderID, ReceiveDate = order.ReceivedOrder.ReceiveDate, EmployeeID = order.ReceivedOrder.EmployeeID }; context.ReceiveOrders.Add(newReceiveOrder); int id = newReceiveOrder.ReceiveOrderID; // ReceiveOrderItems foreach (var item in order.ReceiveOrderItems) { var newReceiveOrderItem = new ReceiveOrderItem { ReceiveOrderID = id, OrderDetailID = item.OrderDetailID, ItemQuantity = item.ItemQuantity }; context.ReceiveOrderItems.Add(newReceiveOrderItem); } // ReturnOrderItems foreach (var item in order.ReturnOrderItems) { var newReturnOrderItem = new ReturnOrderItem { ReceiveOrderID = id, OrderDetailID = item.OrderDetailID, ItemQuantity = item.ItemQuantity, Comment = item.Comment, UnOrderedItem = item.UnOrderedItem, VendorProductID = item.VendorProductID }; context.ReturnOrderItems.Add(newReturnOrderItem); } // Products foreach (var item in order.Products) { var given = context.Products.Find(context.OrderDetails.Find(item.ProductID).ProductID); if (given == null) { throw new ArgumentException($"The given product id of {item.ProductID} does not exist in the database.", nameof(item.ProductID)); } given.QuantityOnHand = given.QuantityOnHand + item.QuantityReceived; given.QuantityOnOrder = given.QuantityOnOrder - item.QuantityReceived; if (given.QuantityOnOrder < 0) { given.QuantityOnOrder = 0; } var existingProduct = context.Entry(given); existingProduct.Property(nameof(given.QuantityOnHand)).IsModified = true; existingProduct.Property(nameof(given.QuantityOnOrder)).IsModified = true; } // Check if the order has been filled var outstanding = context.Orders.Find(order.OrderID).OrderDetails; var num = 0; foreach (var item in outstanding) { if (context.OrderDetails.Find(item.OrderDetailID).Quantity *context.OrderDetails.Find(item.OrderDetailID).OrderUnitSize - (context.OrderDetails.Find(item.OrderDetailID).ReceiveOrderItems.Select(x => x.ItemQuantity)).FirstOrDefault() == 0) { num++; } } if (num == outstanding.Count) { var givenOrder = context.Orders.Find(order.OrderID); givenOrder.Closed = true; givenOrder.Comment = "Order Complete"; var existingOrder = context.Entry(givenOrder); existingOrder.Property(nameof(givenOrder.Comment)).IsModified = true; existingOrder.Property(nameof(givenOrder.Closed)).IsModified = true; } context.SaveChanges(); } }
public void ReceiveShipment(int orderid, List <TransactionItemPOCO> received, List <RejectedItemPOCO> rejected) { List <string> errors = new List <string>(); using (var context = new RaceContext()) { var closed = (from x in context.Orders where x.OrderID == orderid select x.Closed).FirstOrDefault(); if (closed) { errors.Add("You cannot receive a closed order"); } else if (received.Any(x => x.ItemQuantity < 0)) { errors.Add("You cannot have a negative received amount"); } else { int newrecieveorderid = context.ReceiveOrders.Select(x => x.ReceiveOrderID).Max(); newrecieveorderid++; var recieveorderlist = new List <ReceiveOrderItem>(); var returnorderlist = new List <ReturnOrderItem>(); foreach (var item in received) { int newrecieveorderitemid = context.ReceiveOrderItems.Select(x => x.ReceiveOrderItemID).Max(); newrecieveorderid++; int orderDetailID = (from x in context.Products from y in x.OrderDetails where x.ItemName == item.ItemName && y.OrderID == orderid select y.OrderDetailID).FirstOrDefault(); recieveorderlist.Add(new ReceiveOrderItem() { ReceiveOrderItemID = newrecieveorderitemid, ReceiveOrderID = newrecieveorderid, OrderDetailID = orderDetailID, ItemQuantity = item.ItemQuantity, OrderDetail = (from x in context.OrderDetails where x.OrderDetailID == orderDetailID select x).FirstOrDefault(), ReceiveOrder = (from x in context.ReceiveOrders where x.ReceiveOrderID == newrecieveorderid select x).FirstOrDefault() }); } if (rejected.Count() > 0) { foreach (var item in rejected) { int newreturnorderitemid = context.ReturnOrderItems.Select(x => x.ReturnOrderItemID).Max(); newreturnorderitemid++; int orderDetailID = (from x in context.Products from y in x.OrderDetails where x.ItemName == item.ItemName && y.OrderID == orderid select y.OrderDetailID).FirstOrDefault(); returnorderlist.Add(new ReturnOrderItem() { ReturnOrderItemID = newreturnorderitemid, ReceiveOrderID = newrecieveorderid, OrderDetailID = orderDetailID, UnOrderedItem = null, ItemQuantity = item.ItemQuantity, Comment = item.Reason, VendorProductID = null, OrderDetail = (from x in context.OrderDetails where x.OrderDetailID == orderDetailID select x).FirstOrDefault(), ReceiveOrder = (from x in context.ReceiveOrders where x.ReceiveOrderID == newrecieveorderid select x).FirstOrDefault() }); } } ReceiveOrder receiveOrder = new ReceiveOrder { ReceiveOrderID = newrecieveorderid, OrderID = orderid, ReceiveDate = DateTime.Now, EmployeeID = 56, Employee = (from x in context.Employees where x.EmployeeID == 56 select x).FirstOrDefault(), Order = (from x in context.Orders where x.OrderID == orderid select x).FirstOrDefault(), ReceiveOrderItems = recieveorderlist, ReturnOrderItems = returnorderlist }; context.ReceiveOrders.Add(receiveOrder); foreach (var item in recieveorderlist.ToList()) { ReceiveOrderItem receiveOrderItem = new ReceiveOrderItem { ReceiveOrderItemID = item.ReceiveOrderItemID, ReceiveOrderID = item.ReceiveOrderID, OrderDetailID = item.OrderDetailID, ItemQuantity = item.ItemQuantity, OrderDetail = item.OrderDetail, ReceiveOrder = item.ReceiveOrder }; var query = (from x in context.OrderDetails join prod in context.Products on x.ProductID equals prod.ProductID where x.OrderDetailID == receiveOrderItem.OrderDetailID select prod).FirstOrDefault(); query.QuantityOnHand = query.QuantityOnHand + receiveOrderItem.ItemQuantity; query.QuantityOnOrder = query.QuantityOnOrder - receiveOrderItem.ItemQuantity; } if (returnorderlist.Count > 0) { foreach (var item in returnorderlist.ToList()) { ReturnOrderItem returnOrderItem = new ReturnOrderItem() { ReturnOrderItemID = item.ReturnOrderItemID, ReceiveOrderID = item.ReceiveOrderID, OrderDetailID = item.OrderDetailID, UnOrderedItem = item.UnOrderedItem, ItemQuantity = item.ItemQuantity, Comment = item.Comment, VendorProductID = item.VendorProductID, OrderDetail = item.OrderDetail, ReceiveOrder = item.ReceiveOrder }; var query = (from x in context.OrderDetails join prod in context.Products on x.ProductID equals prod.ProductID where x.OrderDetailID == returnOrderItem.OrderDetailID select prod).FirstOrDefault(); } } context.SaveChanges(); } if (errors.Count > 0) { throw new BusinessRuleException("Receive Shipment", errors); } } }