コード例 #1
0
        public ActionResult CheckOut(CheckOutVM chkVM)
        {
            //Create Order
            Order order = new Order
            {
                CustomerId = chkVM.Customer.Id,
                OrderDate  = DateTime.Today
            };

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

            foreach (var row in chkVM.ShoppingCart)
            {
                OrderRow orows = new OrderRow
                {
                    Order   = order,
                    MovieId = row.Id,
                    Price   = row.Price
                };
                db.OrderRows.Add(orows);
                db.SaveChanges();
            }

            TempData["Message"] = "Order Placed";

            Session["Cart"] = null;

            return(RedirectToAction("Index", "Home"));
        }
コード例 #2
0
        public ActionResult CheckOut()
        {
            List <Movie> shoppingCart = (List <Movie>)Session["Cart"];

            if (shoppingCart == null)
            {
                TempData["Message"] = "Cart Empty";
                return(RedirectToAction("Browse", "Home"));
            }

            var user = (User)Session["User"];

            if (user != null)
            {
                CheckOutVM chkVM = new CheckOutVM();

                chkVM.Customer     = user.Customer;
                chkVM.ShoppingCart = shoppingCart;
                chkVM.TotalCost    = (int)shoppingCart.Sum(x => x.Price);

                return(View(chkVM));
            }

            TempData["OldAction"] = "CheckOut";

            return(RedirectToAction("Login", "Account"));
        }
コード例 #3
0
            public IActionResult Checkout(CheckOutVM _checkout)
            {
                checkOut = _checkout;
                int idCus, idAdd;

                if (customer == null || customer.id == 0)
                {
                    Customer cus = new Customer()
                    {
                        fullName = _checkout.first_name + "_" + _checkout.last_name,
                        email    = _checkout.email
                    };
                    db.Customer.Add(cus);
                    db.SaveChanges();
                    Address add = new Address()
                    {
                        customer = cus.id,
                        detail   = _checkout.company_name + "_" + _checkout.address_line_1 + "_" + _checkout.address_line_2 + "_" + _checkout.city + "_" + _checkout.country,
                        phone    = _checkout.mobile_phone
                    };
                    db.Address.Add(add);
                    db.SaveChanges();
                    idCus = cus.id;
                    idAdd = add.id;
                }
                else
                {
                    idCus = customer.id;
                    List <Address> c = db.Address.Where(a => a.customer == customer.id).ToList();
                    if (c.Count != 0)
                    {
                        Address defa = c.Where(a => a.isDefaultAddress == true).FirstOrDefault();
                        if (defa == null)
                        {
                            idAdd = c[0].id;
                        }
                        else
                        {
                            idAdd = defa.id;
                        }
                    }
                    else
                    {
                        Address add = new Address()
                        {
                            customer = customer.id,
                            detail   = _checkout.company_name + "_" + _checkout.address_line_1 + "_" + _checkout.address_line_2 + "_" + _checkout.city + "_" + _checkout.country,
                            phone    = _checkout.mobile_phone
                        };
                        db.Address.Add(add);
                        db.SaveChanges();
                        idAdd = add.id;
                    }
                }
                Order ord = new Order()
                {
                    customer = idCus,
                    ordered  = DateTime.Now,
                    address  = idAdd.ToString(),
                    total    = checkOut.total
                };

                db.Order.Add(ord);
                db.SaveChanges();


                if (checkOut.order_items != null)
                {
                    foreach (var p in checkOut.order_items)
                    {
                        Item i = new Item();
                        i.order    = ord.id;
                        i.product  = p.id;
                        i.quantity = checkOut.itemForms[checkOut.order_items.IndexOf(p)].quantity;
                        i.price    = Convert.ToInt32(p.price) * i.quantity;
                        db.Item.Add(i);
                        db.SaveChanges();
                    }
                }

                catalog.cart_product = new List <Product>();
                catalog.cart_images  = new List <Image>();

                checkOut.images      = catalog.cart_images;
                checkOut.order_items = catalog.cart_product;

                return(RedirectToAction("Order", "User"));
            }
コード例 #4
0
        public UserController(ShopDatabaseContext context)
        {
            this.db      = context;
            products     = db.Product.ToList();
            productSizes = db.ProductSize.ToList();
            images       = db.Image.ToList();
            if (category_filters == null || category_filters.Count() == 0)
            {
                category_filters = new List <string>();
            }
            if (size_filters == null || size_filters.Count() == 0)
            {
                size_filters = new List <string>();
            }
            if (price_filters == null || price_filters.Count() == 0)
            {
                price_filters = new List <int>();
            }

            if (cart_products == null || cart_products.Count() == 0)
            {
                cart_products = new List <Product>();
            }
            if (cart_items == null || cart_items.Count == 0)
            {
                cart_items = new List <ItemForm>();
            }
            if (wish_products == null || wish_products.Count() == 0)
            {
                wish_products = new List <Product>();
            }
            if (detail_products == null || detail_products.Count() == 0)
            {
                detail_products = new List <Product>();
            }
            if (cart_images == null || cart_images.Count() == 0)
            {
                cart_images = new List <Image>();
            }
            if (wish_images == null || wish_images.Count() == 0)
            {
                wish_images = new List <Image>();
            }
            if (detail_images == null || detail_images.Count() == 0)
            {
                detail_images = new List <Image>();
            }
            if (customer == null)
            {
                account = new PersonalAccountVM()
                {
                    customer = new Customer()
                    {
                        id       = 0,
                        username = ""
                    },
                    first_name = "",
                    last_name  = "",
                    date       = 1,
                    id         = 0
                };

                address = new PersonalAddressVM()
                {
                    addresses      = new List <Address>(),
                    company_name   = "",
                    country        = "",
                    address_line_1 = "",
                    address_line_2 = "",
                    city           = "",
                    ZIPcode        = "",
                    mobile_phone   = "",
                };
            }
            else
            {
                account = new PersonalAccountVM()
                {
                    customer   = customer,
                    first_name = customer.fullName.Split('_')[0],
                    last_name  = customer.fullName.Split('_')[1],
                    id         = customer.id
                };
                address = new PersonalAddressVM()
                {
                    addresses      = db.Address.Where(a => a.customer == customer.id).ToList(),
                    company_name   = "",
                    country        = "",
                    address_line_1 = "",
                    address_line_2 = "",
                    city           = "",
                    ZIPcode        = "",
                    mobile_phone   = "",
                };
            }
            if (customer != null && customer.id != 0)
            {
                checkOut             = new CheckOutVM();
                checkOut.last_name   = customer.fullName.Split('_')[1];
                checkOut.first_name  = customer.fullName.Split('_')[0];
                checkOut.email       = customer.email;
                checkOut.order_items = new List <Product>();
                checkOut.itemForms   = new List <ItemForm>();

                List <Address> adds = db.Address.Where(a => a.customer == customer.id).ToList();
                if (adds.Count != 0)
                {
                    Address defa = adds.Where(a => a.isDefaultAddress == true).FirstOrDefault();
                    if (defa == null)
                    {
                        defa = adds[0];
                    }
                    checkOut.company_name   = defa.detail.Split('_')[0];
                    checkOut.address_line_1 = defa.detail.Split('_')[1];
                    checkOut.address_line_2 = defa.detail.Split('_')[2];
                    checkOut.city           = defa.detail.Split('_')[3];
                    checkOut.country        = defa.detail.Split('_')[4];
                    checkOut.mobile_phone   = defa.phone;
                }
            }
            else
            {
                checkOut             = new CheckOutVM();
                checkOut.order_items = new List <Product>();
                checkOut.itemForms   = new List <ItemForm>();
            }
            if (customer == null)
            {
                customer = new Customer()
                {
                    fullName = "_",
                    id       = 0,
                };
            }
            if (login == null)
            {
                login = new LoginVM()
                {
                    username   = "",
                    password   = "",
                    isLoggedIn = false,
                };
            }
            if (catalog == null)
            {
                catalog       = new CatalogProductVM();
                catalog.items = new List <ItemForm>();
            }
            initCatalog(products, productSizes, images);
        }
コード例 #5
0
        public async Task <IActionResult> CheckOut(CheckOutVM sale, int?PaymentTypeId)
        {
            if (User.Identity.IsAuthenticated)
            {
                ViewBag.PaymentTypes = _context.PaymentTypes.ToList();
                AppUser appUser = await _userManager.FindByNameAsync(User.Identity.Name);

                List <BasketVM> basketProducts = new List <BasketVM>();
                if (Request.Cookies["basket"] != null)
                {
                    basketProducts = JsonConvert.DeserializeObject <List <BasketVM> >(Request.Cookies["basket"]);
                }
                ViewBag.Basket = basketProducts;
                ViewBag.Total  = 0;
                foreach (BasketVM item in basketProducts)
                {
                    ViewBag.Total += item.Count * item.Price;
                }
                Sale newSale = new Sale();
                foreach (BasketVM item in basketProducts)
                {
                    var dbPro = await _context.Products.FindAsync(item.Id);

                    if (item.Count > dbPro.Count)
                    {
                        TempData["error"] = $"Anbarda {item.Model} məhsulundan seçdiyiniz sayda yoxdur";
                        return(View());
                    }
                }
                List <SaleProduct> saleProducts = new List <SaleProduct>();
                double             total        = 0;
                ViewBag.Total = 0;
                foreach (BasketVM basketProduct in basketProducts)
                {
                    Product dbProduct = await _context.Products.FindAsync(basketProduct.Id);

                    //await DecreaseProductCount(dbProduct, basketProduct);

                    SaleProduct saleProduct = new SaleProduct
                    {
                        Price     = dbProduct.Price,
                        Count     = basketProduct.Count,
                        ProductId = basketProduct.Id,
                        Sale      = newSale,
                        SaleId    = newSale.Id
                    };
                    total        += saleProduct.Price * saleProduct.Count;
                    ViewBag.Total = total;
                    saleProducts.Add(saleProduct);
                    await _context.SaleProducts.AddAsync(saleProduct);
                }

                if (PaymentTypeId == null)
                {
                    ModelState.AddModelError("PaymentTypeId", "Xahis edirik brandi secin!");
                    ViewBag.SelectError = "Please select PaymentType";
                    return(View());
                }
                foreach (BasketVM item in basketProducts)
                {
                    var dbPro = await _context.Products.FindAsync(item.Id);

                    dbPro.SaleCount += item.Count;
                    dbPro.Count     -= item.Count;
                }
                newSale.Total              = total;
                newSale.CustomerName       = sale.Name;
                newSale.CustomerSurname    = sale.Surname;
                newSale.CustomerPhone      = sale.PhoneNumber;
                newSale.CustomerFatherName = sale.FatherName;
                newSale.AppUserId          = appUser.Id;
                newSale.PaymentTypeId      = (int)PaymentTypeId;
                newSale.Date            = DateTime.Now;
                newSale.ShippingAddress = sale.City + ' ' + sale.District + ' ' + sale.DetalizedAddress;
                newSale.SaleStatus      = SaleStatus.Waiting;
                newSale.Information     = sale.Information;


                await _context.Sales.AddAsync(newSale);

                await _context.SaveChangesAsync();

                Response.Cookies.Delete("basket");
                return(RedirectToAction("OrderCompleted", "Basket"));
            }
            else
            {
                return(RedirectToAction("Login", "Account"));
            }
        }