// GET: /Allocation/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            AllocationLot allocationLot = db.AllocationLots.Find(id);

            if (allocationLot == null)
            {
                return(HttpNotFound());
            }
            return(View(allocationLot));
        }
        public ActionResult AllocateProducts(Allocate allocatedStuff, [Bind(Include = "Id,OfficeId,ProductId,ProductCount,ManufacturerId,VendorId,IsSplit,LastEditDateTime,LastEditUser")] Allocation allocation, AllocationLot allocationLot, Lot lots, AllocationOffice allocationOffices, OrderProduct orderProducts)
        {
            string user           = HDUtilities.UserInformation.GetCurrentUserName();
            var    ordProdsArray  = db.OrderProducts.Where(r => r.LastEditUser == user && r.Order.Submitted != true).ToArray();
            var    ordProdIdsList = db.OrderProducts.Where(r => r.LastEditUser == user && r.Order.Submitted != true).Select(r => r.ProductId).ToList();
            var    productlist    = db.Products.Where(x => ordProdIdsList.Any(d => d == x.Id)).ToList();
            bool   successful     = false;
            var    offices        = dbOffice.DDB_HDC_OFFICEINFO.ToList();


            Allocation allocationList = new Allocation();

            for (int y = 0; y < allocatedStuff.RSCDB.Length; y++)
            {
                var office = allocatedStuff.RSCDB[y];


                allocationList.AllocationOffices.Add(new AllocationOffice()
                {
                    AllocationId     = allocation.Id,
                    OfficeId         = office,
                    LastEditDateTime = DateTime.Now,
                    LastEditUser     = user
                });
            }
            for (int x = 0; x < ordProdsArray.Length; x++)
            {
                int     ProductCount = ordProdsArray[x].Quantity;
                decimal totalOrdered = ProductCount;

                int splitCount = 1;
                if (allocatedStuff.IsSplit == true)
                {
                    splitCount = allocatedStuff.RSCDB.Count();
                }

                allocationList.LastEditUser     = user;
                allocationList.LastEditDateTime = DateTime.Now;


                while (ProductCount > 0)
                {
                    var product = productlist.Where(r => r.Id == ordProdsArray[x].ProductId && r.Lots.Any(d => d.Quantity != 0)).FirstOrDefault();

                    Lot lot = product.Lots.Where(r => r.Quantity != 0).OrderBy(d => d.Price).FirstOrDefault();

                    allocationList.AllocationLots.Add(new AllocationLot()
                    {
                        AllocationId     = allocation.Id,
                        Quantity         = (ProductCount > lot.Quantity ? lot.Quantity : ProductCount),
                        LotId            = lot.Id,
                        LastEditDateTime = DateTime.Now,
                        LastEditUser     = user
                    });

                    var price = lot.Price;
                    if (ProductCount < lot.Quantity)
                    {
                        lot.Quantity -= ProductCount;
                        ProductCount  = 0;
                    }
                    else
                    {
                        ProductCount -= lot.Quantity;
                        lot.Quantity  = 0;
                    }
                }
                var order = db.OrderProducts.Find(ordProdsArray[x].Id);
                db.OrderProducts.Remove(order);
            }

            db.Allocations.Add(allocationList);
            db.SaveChanges();
            successful = true;

            return(Json(new { Result = successful }, JsonRequestBehavior.AllowGet));
        }