Esempio n. 1
0
        public async Task CreateOrder(Order order, bool alreadyCharged, bool useDBKey = false)
        {
            // New orders should only have one product order entry
            ProductOrderEntry entry = order.ProductOrderEntries.FirstOrDefault();

            if (entry.ListingID != 0)
            {
                entry.Listing = listingRepository.GetListingByID(entry.ListingID);
            }

            DateTime date = DateTime.Now;

            order.SaleDate = date;

            String note = "Admin-created order";

            ClaimedProductKey newKey = new ClaimedProductKey();

            int priceToCharge = 0;

            if (order.ProductOrderEntries.Count() > 0)
            {
                priceToCharge = order.ProductOrderEntries.First().SalePrice;
            }

            if (useDBKey)
            {
                ProductKey key = GetProductKey(entry.ListingID);
                newKey = new ClaimedProductKey(key, order.AppUser, date, note);
                listingRepository.DeleteProductKey(key.ProductKeyID);
                unitOfWork.Save();

                priceToCharge = newKey.Listing.SaleOrDefaultPrice();

                entry.AddClaimedProductKey(newKey);//userRepository.InsertClaimedProductKey(newKey);
            }

            if (alreadyCharged == false)
            {
                BalanceEntry balanceEntry = new BalanceEntry();
                balanceEntry.Date           = date;
                balanceEntry.AppUser        = order.AppUser;
                balanceEntry.Notes          = note;
                balanceEntry.PointsAdjusted = priceToCharge;
                order.AppUser.Balance      -= priceToCharge;

                //userRepository.UpdateAppUser(order.AppUser);
                //userRepository.InsertBalanceEntry(balanceEntry);
                order.AppUser.AddBalanceEntry(balanceEntry);
            }

            entry.SalePrice = priceToCharge;
            order.AppUser.AddOrder(order);
            //userRepository.InsertOrder(order);
            await userRepository.UpdateAppUser(order.AppUser);

            unitOfWork.Save();
        }
Esempio n. 2
0
        public void PullNewProductKey(int id)
        {
            ProductOrderEntry entry = userRepository.GetProductOrderEntryByID(id);

            if (entry.Listing.Quantity == 0)
            {
                return;
            }

            ClaimedProductKey[] keys = entry.ClaimedProductKeys.ToArray();

            if (entry.Listing.ProductKeys.Count > 0)
            {
                ProductKey key = entry.Listing.ProductKeys.First();

                if (keys.Count() > 1)
                {
                    for (int i = 0; i < keys.Count(); i++)
                    {
                        userRepository.DeleteClaimedProductKey(keys[i].ClaimedProductKeyID);
                    }

                    entry.AddClaimedProductKey(new ClaimedProductKey(key, entry.Order.AppUser, DateTime.Now, "Admin pulled new key"));
                }
                else
                {
                    keys[0].Key    = key.ItemKey;
                    keys[0].IsGift = key.IsGift;
                }

                Listing listing = key.Listing;
                key.Listing.RemoveProductKeyAndUpdateQuantity(key);
                listingRepository.DeleteProductKey(key.ProductKeyID);
                listingRepository.UpdateListing(listing);
            }
            else if (entry.Listing.ChildListings.All(l => l.ProductKeys.Count > 0))
            {
                for (int i = 0; i < keys.Count(); i++)
                {
                    userRepository.DeleteClaimedProductKey(keys[i].ClaimedProductKeyID);
                }

                foreach (Listing childListing in entry.Listing.ChildListings)
                {
                    ProductKey key = childListing.ProductKeys.First();

                    userRepository.InsertClaimedProductKey(new ClaimedProductKey(key, entry.Order.AppUser, DateTime.Now, "Admin pulled new key"));

                    Listing listing = key.Listing;
                    key.Listing.RemoveProductKeyAndUpdateQuantity(key);
                    listingRepository.DeleteProductKey(key.ProductKeyID);
                    listingRepository.UpdateListing(listing);
                }
            }

            userRepository.UpdateProductOrderEntry(entry);
            unitOfWork.Save();
        }
Esempio n. 3
0
        public Order BuyAndRevealListing(AppUser user, int listingId, int price)
        {
            Order order = null;

            if (user.Balance < price)
            {
                return(order);
            }

            Listing targetListing = listingRepository.GetListingByID(listingId);

            if (targetListing == null)
            {
                return(order);
            }

            DateTime orderDate = new DateTime();

            orderDate = DateTime.Now;

            List <ProductKey> keys;

            ProductKey key = listingRepository.GetProductKeys().Where(k => k.ListingID == listingId).FirstOrDefault();

            if (key == null && targetListing.ChildListings != null)
            {
                keys = new List <ProductKey>();

                foreach (Listing childListing in targetListing.ChildListings)
                {
                    ProductKey temp = listingRepository.GetProductKeys().Where(k => k.ListingID == childListing.ListingID).SingleOrDefault();

                    if (temp != null)
                    {
                        keys.Add(temp);
                    }
                    else
                    {
                        return(order);
                    }
                }
            }
            else
            {
                keys = new List <ProductKey>()
                {
                    key
                };
            }

            order = new Order(user, orderDate);
            userRepository.InsertOrder(order);
            unitOfWork.Save();

            ProductOrderEntry orderEntry = new ProductOrderEntry(order, targetListing);

            order.AddProductOrderEntry(orderEntry);

            //if (targetListing.ChildListings == null || targetListing.ChildListings.Count == 0)
            //{
            foreach (ProductKey productKey in keys)
            {
                listingRepository.DeleteProductKey(productKey.ProductKeyID);

                productKey.Listing.Quantity--;
                //productKey.Listing.UpdateParentQuantities();

                listingRepository.UpdateListing(productKey.Listing);

                ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                user.AddClaimedProductKey(claimedKey);
                claimedKey.IsRevealed = true;
                userRepository.InsertClaimedProductKey(claimedKey);
                unitOfWork.Save();

                orderEntry.AddClaimedProductKey(claimedKey);
            }

            userRepository.InsertProductOrderEntry(orderEntry);

            unitOfWork.Save();

            return(order);
        }
Esempio n. 4
0
        public async Task <Order> CreateOrder()
        {
            AppUser user = await GetCurrentUser();

            ICollection <ShoppingCartEntry> cartEntries = user.ShoppingCartEntries;

            if (user == null || cartEntries == null || cartEntries.Count == 0 || !user.AssertValidOrder())
            {
                return(null);
            }

            DateTime orderDate = DateTime.Now;
            Order    order     = new Order(user, orderDate);

            userRepository.InsertOrder(order);
            unitOfWork.Save();


            foreach (ShoppingCartEntry entry in cartEntries)
            {
                IEnumerable <ProductKey> keys = entry.Listing.ProductKeys.Take(entry.Quantity);
                // part of removed code below
                //int remainingQuantity = entry.Quantity - keys.Count;

                foreach (ProductKey productKey in keys)
                {
                    productKey.Listing.Quantity--;

                    listingRepository.UpdateListingSimple(productKey.Listing);
                    listingRepository.DeleteProductKey(productKey.ProductKeyID);

                    ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                    //user.AddClaimedProductKey(claimedKey);
                    userRepository.InsertClaimedProductKey(claimedKey);

                    ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                    orderEntry.AddClaimedProductKey(claimedKey);

                    order.AddProductOrderEntry(orderEntry);
                }
                #region removed
                //                // the case where there are some product keys, but not enough to fulfill the order (the remainining are keys of child listings bundled together)
                //                // rarely triggered (if ever)
                //                if (entry.Listing.ChildListings != null && entry.Listing.ChildListings.Count > 0 && keys != null && keys.Count > 0 && keys.Count < entry.Quantity)
                //                {
                //                    foreach (ProductKey productKey in keys)
                //                    {
                //                        productKey.Listing.Quantity--;
                //                        productKey.Listing.UpdateParentQuantities();

                //                        listingRepository.UpdateListing(productKey.Listing);
                //                        listingRepository.DeleteProductKey(productKey.ProductKeyID);

                //                        ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                //                        user.AddClaimedProductKey(claimedKey);
                //                        userRepository.InsertClaimedProductKey(claimedKey);
                //                        //unitOfWork.Save();

                //                        ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                //                        order.AddProductOrderEntry(orderEntry);
                //                        userRepository.InsertProductOrderEntry(orderEntry);
                //                        //unitOfWork.Save();
                //                        orderEntry.AddClaimedProductKey(claimedKey);
                //                    }

                //                    keys = new List<ProductKey>();

                //                    foreach (Listing childListing in entry.Listing.ChildListings)
                //                    {
                //                        keys.AddRange(listingRepository.GetProductKeys().Where(k => k.ListingID == childListing.ListingID).Take(remainingQuantity));
                //                    }
                //                }
                //                else if (entry.Listing.ChildListings != null && entry.Listing.ChildListings.Count > 0 && keys.Count < entry.Quantity)
                //                {
                //                    foreach (Listing childListing in entry.Listing.ChildListings)
                //                    {
                //                        keys.AddRange(listingRepository.GetProductKeys().Where(k => k.ListingID == childListing.ListingID).Take(entry.Quantity));
                //                    }
                //                }

                //                if (entry.Listing.ChildListings == null || entry.Listing.ChildListings.Count == 0 || keys.Count == entry.Quantity)
                //                {
                //                    foreach (ProductKey productKey in keys)
                //                    {
                //                        productKey.Listing.Quantity--;
                //                        productKey.Listing.UpdateParentQuantities();

                //                        listingRepository.UpdateListing(productKey.Listing);
                //                        listingRepository.DeleteProductKey(productKey.ProductKeyID);

                //                        ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                //                        user.AddClaimedProductKey(claimedKey);
                //                        userRepository.InsertClaimedProductKey(claimedKey);
                //                        //unitOfWork.Save();

                //                        ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                //                        userRepository.InsertProductOrderEntry(orderEntry);
                //                       // unitOfWork.Save();
                //                        orderEntry.AddClaimedProductKey(claimedKey);

                //                        order.AddProductOrderEntry(orderEntry);
                //                    }
                //                }
                //                else
                //                {
                //                    for (int i = 0; i < remainingQuantity; i++)
                //                    {
                //                        ProductOrderEntry orderEntry = new ProductOrderEntry(order, entry);
                //                        order.AddProductOrderEntry(orderEntry);
                //                        userRepository.InsertProductOrderEntry(orderEntry);
                //                        //unitOfWork.Save();

                //                        foreach (Listing childListing in entry.Listing.ChildListings)
                //                        {
                //                            ProductKey productKey = keys.Where(k => k.Listing.ListingID == childListing.ListingID).First();
                //                            keys.Remove(productKey);

                //                            productKey.Listing.Quantity--;
                //                           // productKey.Listing.UpdateParentQuantities();

                //                            listingRepository.UpdateListing(productKey.Listing);
                //                            listingRepository.DeleteProductKey(productKey.ProductKeyID);

                //                            ClaimedProductKey claimedKey = new ClaimedProductKey(productKey, user, orderDate, "Purchase - Order #" + order.OrderID);
                //                            userRepository.InsertClaimedProductKey(claimedKey);
                //                            //unitOfWork.Save();
                //                            orderEntry.AddClaimedProductKey(claimedKey);
                //                            user.AddClaimedProductKey(claimedKey);
                //                        }

                //                        order.AddProductOrderEntry(orderEntry);
                //                    }
                //                }

                //                //unitOfWork.Save();
                #endregion
            }

            DeleteShoppingCart(user);

            int totalSalePrice = order.TotalSalePrice();

            BalanceEntry balanceEntry = new BalanceEntry(user, "Purchase - Order #" + order.OrderID, 0 - totalSalePrice, orderDate);
            //user.BalanceEntries.Add(balanceEntry);
            userRepository.InsertBalanceEntry(balanceEntry);

            user.Balance -= totalSalePrice;
            //user.AddOrder(order);
            userRepository.UpdateOrder(order);
            await userRepository.UpdateAppUserSimple(user);

            this.unitOfWork.Save();

            return(userRepository.GetOrderByID(order.OrderID));
        }