Exemplo n.º 1
0
        protected void ReceiveShimpment_Click(object sender, EventArgs e)
        {
            MessageUserControl.TryRun(() => {
                int result              = 0;
                var controller          = new ReceivingController();
                var receivedOrderDetail = new ReceivedOrderDetails();
                var receivedOrderInfo   = new ReceiveOrdersInfo();
                var receivedOrderItems  = new List <ReceiveOrderItemDetails>();
                var productDetails      = new List <ProductDetails>();
                var returnedOrderItems  = new List <ReturnOrderItemDetails>();

                receivedOrderInfo.OrderID     = int.Parse(OrderListDropDown.SelectedValue);
                receivedOrderInfo.ReceiveDate = DateTime.Now;
                receivedOrderInfo.EmployeeID  = EmployeeId.Value;

                foreach (ListViewItem item in UnorderedListView.Items)
                {
                    var unorderedItem = new ReturnOrderItemDetails();

                    var unOrderedItemController = item.FindControl("ItemNameLabel") as Label;
                    var itemQuantityController  = item.FindControl("QuantityLabel") as Label;
                    var vendorController        = item.FindControl("VendorIDLabel") as Label;

                    unorderedItem.Comment         = "Not on original order";
                    unorderedItem.UnOrderedItem   = unOrderedItemController.ToString();
                    unorderedItem.ItemQuantity    = int.Parse(itemQuantityController.Text);
                    unorderedItem.VendorProductID = vendorController.ToString();

                    returnedOrderItems.Add(unorderedItem);
                }

                foreach (GridViewRow item in OrderGridView.Rows)
                {
                    var unorderedItem = new ReturnOrderItemDetails();

                    var itemQuantityController  = item.FindControl("RejectedUnits") as TextBox;
                    var commentController       = item.FindControl("RejectedReason") as TextBox;
                    var orderDetailIDController = item.FindControl("OrderDetailID") as Label;

                    if (int.TryParse(itemQuantityController.Text, out result))
                    {
                        if (int.Parse(itemQuantityController.Text) > 0)
                        {
                            unorderedItem.OrderDetailID = int.Parse(orderDetailIDController.Text);
                            unorderedItem.Comment       = commentController.ToString();
                            unorderedItem.ItemQuantity  = int.Parse(itemQuantityController.Text);

                            returnedOrderItems.Add(unorderedItem);
                        }
                    }
                }

                foreach (GridViewRow item in OrderGridView.Rows)
                {
                    var newreceiveditem = new ReceiveOrderItemDetails();
                    var newProductInfo  = new ProductDetails();

                    var salvagedItems           = item.FindControl("SalvagedItems") as TextBox;
                    var receivedItems           = item.FindControl("ReceivedUnits") as TextBox;
                    var orderDetailIDController = item.FindControl("OrderDetailID") as Label;
                    var unitSize = item.FindControl("UnitSize") as Label;

                    var salvage = 0;
                    var receive = 0;
                    var size    = 0;

                    if (int.TryParse(unitSize.Text, out result) == true)
                    {
                        size = int.Parse(unitSize.Text);
                    }

                    if (int.TryParse(salvagedItems.Text, out result) == true)
                    {
                        salvage = int.Parse(salvagedItems.Text);
                    }

                    if (int.TryParse(receivedItems.Text, out result) == true)
                    {
                        receive = int.Parse(receivedItems.Text);
                    }


                    var totalQuantity = salvage + (receive * size);

                    if (totalQuantity > 0)
                    {
                        newreceiveditem.OrderDetailID = int.Parse(orderDetailIDController.Text);
                        newreceiveditem.ItemQuantity  = totalQuantity;

                        newProductInfo.ProductID        = int.Parse(orderDetailIDController.Text);
                        newProductInfo.QuantityReceived = totalQuantity;

                        receivedOrderItems.Add(newreceiveditem);
                        productDetails.Add(newProductInfo);
                    }
                }
                receivedOrderDetail.OrderID           = int.Parse(OrderListDropDown.SelectedValue);
                receivedOrderDetail.Products          = productDetails;
                receivedOrderDetail.ReceivedOrder     = receivedOrderInfo;
                receivedOrderDetail.ReceiveOrderItems = receivedOrderItems;
                receivedOrderDetail.ReturnOrderItems  = returnedOrderItems;

                controller.ReceiveShimpmentOrder(receivedOrderDetail);
            }, "Receive Shipment", "Shipment Received");

            Response.Redirect("~/Subsystem/Receiving/Receiving");
        }
Exemplo n.º 2
0
        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();
            }
        }