Example #1
0
        public bool CancelOrder(int OrderId)
        {
            using (FreshChoiceEntities db = new FreshChoiceEntities())
            {
                Order order = db.Orders.Where(w => w.OrderId == OrderId).FirstOrDefault();
                if (order != null)
                {
                    // delete order
                    Cart cart = db.Carts.Where(w => w.CartId == order.CartId).FirstOrDefault();
                    // add amount to user wallet
                    Wallet wallet = db.Wallets.Where(w => w.UserId == currentUserId).FirstOrDefault();
                    if (wallet == null)
                    {
                        // create wallet
                        wallet = new Wallet();
                        wallet.WalletDescription = currentUserId + "_wallet";
                        wallet.WalletTotal       = 0;
                        wallet.UserId            = currentUserId;
                        db.Wallets.Add(wallet);
                        db.SaveChanges();
                    }
                    // create transaction
                    Transaction transaction = new Transaction();
                    transaction.TransactionDate        = DateTime.Now;
                    transaction.TransactionDescription = "cancel_order_" + order.OrderId;
                    transaction.TransactionAmount      = order.OrderAmount;
                    transaction.WalletId = wallet.WalletId;
                    db.Transactions.Add(transaction);
                    wallet.WalletTotal += order.OrderAmount;
                    db.SaveChanges();

                    db.Orders.Remove(order);
                    db.SaveChanges();

                    // add items to list again
                    if (cart != null)
                    {
                        var items = db.CartItems
                                    .Where(w => w.CartId == cart.CartId)
                                    .Join(
                            db.Items,
                            ci => ci.ItemId,
                            i => i.ItemId,
                            (ci, i) => new { Item = i, CartItem = ci })
                                    .ToList();
                        // update available quantity
                        foreach (var item in items)
                        {
                            Item item1 = db.Items.Where(w => w.ItemId == item.Item.ItemId).FirstOrDefault();
                            item1.ItemAvailableQnt += item.CartItem.CartItemQnt;
                            db.SaveChanges();
                        }
                    }
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        public List <OrderCartItem> AddItemToCart(int ItemId, int Quantity = 1)
        {
            using (FreshChoiceEntities db = new FreshChoiceEntities())
            {
                Item item = db.Items.Where(w => w.ItemId == ItemId).FirstOrDefault();
                if (item != null && item.ItemAvailableQnt >= Quantity)
                {
                    bool isNewCart = false;
                    if (currentCart == null)
                    {
                        currentCart = new Cart();
                        currentCart.CartIsActive = true;
                        currentCart.UserId       = currentUserId;
                        db.Carts.Add(currentCart);
                        db.SaveChanges();
                        isNewCart = true;
                    }

                    CartItem cartItem = null;
                    if (!isNewCart)
                    {
                        // check this item is already exist in cart
                        cartItem = db.CartItems
                                   .Where(w => w.CartId == currentCart.CartId && w.ItemId == ItemId)
                                   .FirstOrDefault();
                    }
                    if (cartItem != null)
                    {
                        cartItem.CartItemQnt       += Quantity;
                        cartItem.CartItemTotalPrice = cartItem.CartItemQnt * item.ItemPrice;
                    }
                    else
                    {
                        // New
                        cartItem                    = new CartItem();
                        cartItem.ItemId             = ItemId;
                        cartItem.CartId             = currentCart.CartId;
                        cartItem.CartItemTotalPrice = item.ItemPrice * Quantity;
                        cartItem.CartItemQnt       += Quantity;
                        db.CartItems.Add(cartItem);
                    }
                    db.SaveChanges();
                    item.ItemAvailableQnt -= Quantity;
                    db.SaveChanges();
                }
                else
                {
                    return(null);
                }

                // add it to cart

                // return new cart items
                return(GetOrderCartItems());
            }
        }
Example #3
0
        public ActionResult UpdateOrderStatus(int Id, int StatusOrder, DateTime NextDeadline)
        {
            try
            {
                using (FreshChoiceEntities db = new FreshChoiceEntities())
                {
                    Order order = db.Orders.Where(w => w.OrderId == Id).FirstOrDefault();
                    if (order != null)
                    {
                        if (StatusOrder == OrderProcessingStatus.COMPLETED)
                        {
                            // assign delivery person
                            var user = db.Users
                                       .Join(
                                db.Roles,
                                u => u.RoleId,
                                r => r.RoleId,
                                (u, r) => new { User = u, Role = r })
                                       .Where(w => w.Role.RoleId == RoleTypes.DELIVERY)
                                       .FirstOrDefault();
                            if (user != null)
                            {
                                Delivery delivery = db.Deliveries
                                                    .Where(w => w.DeliveryId == order.DeliveryId)
                                                    .FirstOrDefault();
                                delivery.UserId = user.User.UserId;
                                db.SaveChanges();
                            }
                            else
                            {
                                return(Json(false));
                            }
                        }

                        order.OrderStatusId = db
                                              .OrderStatus.Where(w => w.OrderStatusOrder == StatusOrder)
                                              .Select(s => s.OrderStatusId).FirstOrDefault();
                        order.OrderNextDeadline = NextDeadline;
                        order.OrderUpdatedDate  = DateTime.Now;
                        db.SaveChanges();

                        return(Json(true));
                    }
                }
            }
            catch (Exception)
            {
            }
            return(Json(false));
        }
Example #4
0
        public List <OrderCartItem> RemoveItemFromCart(int ItemId, int Quantity = 1)
        {
            using (FreshChoiceEntities db = new FreshChoiceEntities())
            {
                Item item = db.Items.Where(w => w.ItemId == ItemId).FirstOrDefault();
                if (item != null)
                {
                    if (currentCart == null)
                    {
                        return(null);
                    }

                    CartItem cartItem = db.CartItems
                                        .Where(w => w.CartId == currentCart.CartId && w.ItemId == ItemId)
                                        .FirstOrDefault();
                    if (cartItem != null)
                    {
                        if ((cartItem.CartItemQnt - Quantity) > 0)
                        {
                            // Normal reduce
                            cartItem.CartItemQnt       -= Quantity;
                            cartItem.CartItemTotalPrice = cartItem.CartItemQnt * item.ItemPrice;
                        }
                        else if ((cartItem.CartItemQnt - Quantity) == 0)
                        {
                            // Remove item from cart
                            db.CartItems.Remove(cartItem);
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                    db.SaveChanges();
                    item.ItemAvailableQnt += Quantity;
                    db.SaveChanges();
                }
                else
                {
                    return(null);
                }

                // return new cart items
                return(GetOrderCartItems());
            }
        }
Example #5
0
        public ActionResult SaveBrand(int?CategoryId, string BrandName, int?BrandId)
        {
            bool result = false;

            if ((BrandId == null && CategoryId == null) || BrandName.Length < 1)
            {
                return(Json(result));
            }
            try
            {
                using (FreshChoiceEntities db = new FreshChoiceEntities())
                {
                    Brand brand = new Brand();
                    if (BrandId == null)
                    {
                        brand.CategoryId = (int)CategoryId;
                        brand.BrandName  = BrandName;
                        db.Brands.Add(brand);
                    }
                    else
                    {
                        brand           = db.Brands.Where(w => w.BrandId == BrandId).FirstOrDefault();
                        brand.BrandName = BrandName;
                    }

                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception) { }
            return(Json(result));
        }
Example #6
0
        public ActionResult SaveCategory(int CategoryId, string CategoryName)
        {
            bool result = false;

            try
            {
                using (FreshChoiceEntities db = new FreshChoiceEntities())
                {
                    Category category = new Category();
                    if (CategoryId == 0)
                    {
                        category.CategoryName = CategoryName;
                        db.Categories.Add(category);
                    }
                    else
                    {
                        category = db.Categories.Where(w => w.CategoryId == CategoryId).FirstOrDefault();
                        category.CategoryName = CategoryName;
                    }

                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception) { }
            return(Json(result));
        }
Example #7
0
        public ActionResult Registration([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user)
        {
            bool   Status  = false;
            string message = "";

            //
            // Model Validation
            if (true)
            {
                #region //Email is already Exist
                var isExist = IsEmailExist(user.UserEmail);
                if (isExist)
                {
                    ModelState.AddModelError("EmailExist", "Email already exist");
                    return(View(user));
                }
                #endregion

                #region Generate Activation Code
                user.ActivationCode = Guid.NewGuid();
                #endregion

                #region  Password Hashing
                user.UserName        = user.FirstName + " " + user.LastName;
                user.UserContact     = user.UserContact;
                user.RoleId          = 1;
                user.UserPassword    = Crypto.Hash(user.UserPassword);
                user.ConfirmPassword = Crypto.Hash(user.ConfirmPassword); //
                #endregion
                user.IsEmailVerified = false;

                #region Save to Database
                using (FreshChoiceEntities dc = new FreshChoiceEntities())
                {
                    dc.Users.Add(user);
                    dc.SaveChanges();

                    //Send Email to User
                    SendVerificationLinkEmail(user.UserEmail, user.ActivationCode.ToString());
                    message = "Registration successfully done. Account activation link " +
                              " has been sent to your email id:" + user.UserEmail;
                    Status = true;
                }
                #endregion
            }
            else
            {
                message = "Invalid Request";
            }

            ViewBag.Message = message;
            ViewBag.Status  = Status;
            return(Redirect("/"));
        }
Example #8
0
 public bool DeleteCart()
 {
     if (currentCart == null)
     {
         return(false);
     }
     using (FreshChoiceEntities db = new FreshChoiceEntities()) {
         Cart cart = db.Carts.Where(w => w.CartId == currentCart.CartId).FirstOrDefault();
         cart.CartIsActive = false;
         db.SaveChanges();
         currentCart = null;
         return(true);
     }
 }
Example #9
0
        public ActionResult DeleteItem(int Id)
        {
            bool result = false;

            try
            {
                using (FreshChoiceEntities db = new FreshChoiceEntities())
                {
                    Item item = db.Items.Where(w => w.ItemId == Id).FirstOrDefault();
                    if (item != null)
                    {
                        item.ItemIsDeleted = true;
                        db.SaveChanges();
                        result = true;
                    }
                }
            }
            catch (Exception)
            {
            }
            return(Json(result));
        }
Example #10
0
        public ActionResult VerifyAccount(string id)
        {
            bool Status = false;

            using (FreshChoiceEntities dc = new FreshChoiceEntities())
            {
                dc.Configuration.ValidateOnSaveEnabled = false; // This line I have added here to avoid
                                                                // Confirm password does not match issue on save changes
                var v = dc.Users.Where(a => a.ActivationCode == new Guid(id)).FirstOrDefault();
                if (v != null)
                {
                    v.IsEmailVerified = true;
                    dc.SaveChanges();
                    Status = true;
                }
                else
                {
                    ViewBag.Message = "Invalid Request";
                }
            }
            ViewBag.Status = Status;
            return(View());
        }
Example #11
0
        public ActionResult Address(Address address)
        {
            int userId = int.Parse(Convert.ToString(Session["UserId"]));

            using (FreshChoiceEntities db = new FreshChoiceEntities())
            {
                Address newAddress = new Address();
                if (address.AddressId != 0)
                {
                    newAddress = db.Addresses.Where(w => w.AddressId == address.AddressId).FirstOrDefault();
                }
                newAddress.UserId             = userId;
                newAddress.AddressName        = address.AddressName;
                newAddress.AddressDescription = address.AddressDescription;
                newAddress.AddressLatitude    = address.AddressLatitude;
                newAddress.AddressLongitude   = address.AddressLongitude;
                if (address.AddressId == 0)
                {
                    db.Addresses.Add(newAddress);
                }
                db.SaveChanges();
            }
            return(Redirect("/Cart"));
        }
Example #12
0
        public Order PlaceOrder(string DeliveryType, bool UseWalletCash = false)
        {
            if (currentCart == null)
            {
                return(null);
            }
            List <CartItem> cartItems = GetCartItems();

            if (cartItems == null || cartItems.Count == 0)
            {
                return(null);
            }
            using (FreshChoiceEntities db = new FreshChoiceEntities())
            {
                if (DeliveryType == DeliveryTypes.HOME_DELIVERY)
                {
                    Address address = db.Addresses.Where(w => w.UserId == currentUserId).FirstOrDefault();
                    if (address == null)
                    {
                        return(null);
                    }
                }
                // Set order status and delivery status
                OrderStatu orderStatus = db.OrderStatus.Where(w => w.OrderStatusOrder == OrderProcessingStatus.PLACED).FirstOrDefault();
                Delivery   delivery    = new Delivery();
                delivery.DeliveryType = DeliveryType;
                db.Deliveries.Add(delivery);
                db.SaveChanges();

                // create new order
                Order order = new Order();
                order.OrderDescription    = "New Order";
                order.OrderBillNo         = GenerateBillNo(DeliveryType);
                order.OrderConfirmationNo = GenerateConfirmationNo(DeliveryType);
                order.OrderUpdatedDate    = DateTime.Now;
                order.OrderNextDeadline   = DateTime.Now.AddHours(ORDER_PLACED_DEADLINE);
                order.DeliveryId          = delivery.DeliveryId;
                order.OrderStatusId       = orderStatus.OrderStatusId;
                order.CartId = currentCart.CartId;

                // calculate cart amount
                double total = 0;
                foreach (var cartItem in cartItems)
                {
                    total += cartItem.CartItemTotalPrice;
                }
                // calculate & Add delivery Fees
                double deliveryFee = 0;
                if (DeliveryType == DeliveryTypes.HOME_DELIVERY)
                {
                    Address address = db.Addresses.Where(w => w.UserId == currentUserId).FirstOrDefault();
                    if (address != null)
                    {
                        var userCoordinate = new GeoCoordinate(address.AddressLatitude, address.AddressLongitude);
                        var shopCoordinate = new GeoCoordinate(StoreLocation.STORE_LATITUDE, StoreLocation.STORE_LONGITUDE);

                        var distanceInKM = userCoordinate.GetDistanceTo(shopCoordinate) / 1000;
                        deliveryFee          = distanceInKM * 50;
                        delivery.DeliveryFee = deliveryFee;
                        db.SaveChanges();
                    }
                }

                order.OrderAmount = total + deliveryFee;
                order.UserId      = currentUserId;

                // if using wallet cash
                if (UseWalletCash)
                {
                    // add amount to user wallet
                    Wallet wallet = db.Wallets.Where(w => w.UserId == currentUserId).FirstOrDefault();

                    if (wallet == null || wallet.WalletTotal < order.OrderAmount)
                    {
                        return(null);
                    }

                    // create transaction
                    Transaction transaction = new Transaction();
                    transaction.TransactionDate        = DateTime.Now;
                    transaction.TransactionDescription = "cancel_order_" + order.OrderId;
                    transaction.TransactionAmount      = 0 - order.OrderAmount;
                    transaction.WalletId = wallet.WalletId;
                    db.Transactions.Add(transaction);
                    wallet.WalletTotal -= order.OrderAmount;
                    db.SaveChanges();
                }

                db.Orders.Add(order);
                db.SaveChanges();
                DeleteCart();
                return(order);
            }
        }
Example #13
0
        public ActionResult SaveItem(EditItemViewModel viewModel)
        {
            bool result = false;

            try
            {
                using (FreshChoiceEntities db = new FreshChoiceEntities())
                {
                    //  Get all files from Request object
                    HttpFileCollectionBase files  = Request.Files;
                    List <Image>           images = new List <Image>();

                    // create Item
                    Item item = new Item();
                    if (viewModel.ItemId != 0)
                    {
                        item = db.Items.Where(w => w.ItemId == viewModel.ItemId).FirstOrDefault();
                    }

                    item.ItemName         = viewModel.ItemName;
                    item.ItemDescription  = viewModel.ItemDescription;
                    item.ItemAvailableQnt = viewModel.ItemAvailableQnt;
                    if (viewModel.ItemId == 0)
                    {
                        item.ItemQnt = viewModel.ItemAvailableQnt;
                    }
                    else
                    {
                        item.ItemQnt = (item.ItemQnt + (viewModel.ItemAvailableQnt - item.ItemAvailableQnt));
                    }
                    item.ItemPrice     = viewModel.ItemPrice;
                    item.ItemIsDeleted = false;
                    item.BrandId       = viewModel.ItemBrandId;
                    if (viewModel.ItemId == 0)
                    {
                        db.Items.Add(item);
                    }
                    db.SaveChanges();

                    int imageCount = db.Images.Where(w => w.ItemId == item.ItemId).Count();
                    if (Request.Files.Count > 0)
                    {
                        for (int i = 0; i < files.Count; i++)
                        {
                            HttpPostedFileBase file = files[i];
                            string             fname;

                            // Checking for Internet Explorer
                            if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
                            {
                                string[] testfiles = file.FileName.Split(new char[] { '\\' });
                                fname = testfiles[testfiles.Length - 1];
                            }
                            else
                            {
                                fname = file.FileName;
                            }

                            string[] find      = fname.Split('.');
                            string   extention = find[find.Length - 1];
                            fname = item.ItemName + "_" + (i + imageCount) + "." + extention;

                            // Get the complete folder path and store the file inside it.
                            var finalName = Path.Combine(Server.MapPath("~/Uploads/"), fname);
                            file.SaveAs(finalName);

                            // Create image
                            Image image = new Image();
                            image.ImageDescription = "";
                            image.ImageUrl         = "/Uploads/" + fname;
                            image.ItemId           = item.ItemId;
                            db.Images.Add(image);
                            db.SaveChanges();
                        }
                    }
                    result = true;
                }
            }
            catch (Exception)
            {
            }

            return(Json(result));
        }