Exemple #1
0
        // GET: Order
        public ActionResult Index()
        {
            var model = new PizzaPizzaAttributesViewModel
            {
                PizzaSizes = _context.PizzaAttributes.Where(x => x.Name == "Size").Select(p => new SelectListItem {
                    Text = p.Value + @"(" + p.Size + @" inch - $" + p.Amount + @")", Value = p.Id.ToString()
                }),
                PizzaCrusts = _context.PizzaAttributes.Where(x => x.Name == "Crust").Select(p => new SelectListItem {
                    Text = p.Value + (Math.Abs(p.Amount) == 0 ? "" : @"(+ $" + p.Amount + @")"), Value = p.Id.ToString()
                }),
                PizzaToppings = _context.PizzaAttributes.Where(x => x.Name == "Toppings").Select(p => new SelectListItem {
                    Text = p.Value + @"(+ $" + p.Amount + @")", Value = p.Id.ToString()
                }),
                PizzaTopping   = _context.PizzaAttributes.Where(x => x.Name == "Toppings" && x.IsSelected).Select(p => p.Id),
                PaymentMethods = _context.PaymentMethods.Select(pm => new SelectListItem {
                    Text = pm.Name, Value = pm.Id.ToString()
                })
            };

            return(View(model));
        }
Exemple #2
0
        public ActionResult Save(PizzaPizzaAttributesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var customer = new Customer();
                if (!_context.Customers.Any(c => c.Phone == model.Customer.Phone))
                {
                    customer = new Customer
                    {
                        Name    = model.Customer.Name,
                        Address = model.Customer.Address,
                        Zip     = model.Customer.Zip,
                        Phone   = model.Customer.Phone
                    };
                    _context.Customers.Add(customer);
                }
                var pizza = new Pizza
                {
                    Size    = model.PizzaSize,
                    Crust   = model.PizzaCrust,
                    Topping = model.PizzaTopping != null?string.Join(",", model.PizzaTopping) : null
                };
                _context.Pizzas.Add(pizza);
                _context.SaveChanges();

                model.PizzaTopping.ForEach(pt =>
                {
                    var pizzaTopping1 = new PizzaTopping {
                        Pizza = _context.Pizzas.OrderByDescending(p => p.Id).First().Id, Topping = pt
                    };
                    _context.PizzaToppings.Add(pizzaTopping1);
                });
                _context.SaveChanges();

                TempData["customer"] = _context.Customers.First(c => c.Phone == (customer.Phone ?? model.Customer.Phone));
                //pizza = _context.Pizzas.OrderByDescending(p => p.Id).First();

                var amount = _context.PizzaAttributes.FirstOrDefault(pa => pa.Id == model.PizzaSize).Amount + _context.PizzaAttributes.FirstOrDefault(pa => pa.Id == model.PizzaCrust).Amount + (model.PizzaTopping != null ? model.PizzaTopping.Sum(topping => _context.PizzaAttributes.FirstOrDefault(pa => pa.Id == topping).Amount) : 0);
                TempData["amount"] = amount;
                //var order = new Order
                //{
                //    CustomerId = customer.Id,
                //    PizzaId = pizza.Id,
                //    Amount = amount
                //};
                //_context.Orders.Add(order);
                //_context.SaveChanges();
                //return View();

                if (model.PaymentMethod == 1)
                {
                    var redirecturl = "";

                    //Mention URL to redirect content to paypal site
                    redirecturl += "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" +
                                   ConfigurationManager.AppSettings["paypalemail"];

                    //First name i assign static based on login details assign this value
                    redirecturl += "&first_name=" + model.Customer.Name;

                    //Product Name
                    redirecturl += "&item_name=Pizza";

                    //Amount
                    redirecturl += "&amount=1";// + amount;

                    //Phone No
                    redirecturl += "&night_phone_a=" + model.Customer.Phone;

                    //Address
                    redirecturl += "&address1=" + model.Customer.Address + ", " + model.Customer.Zip;

                    //Shipping charges if any
                    //redirecturl += "&shipping=0";

                    //Handling charges if any
                    //redirecturl += "&handling=0";

                    //Tax amount if any
                    //redirecturl += "&tax=0";

                    //Add quatity i added one only statically
                    //redirecturl += "&quantity=1";

                    //Currency code
                    redirecturl += "&currency=$" + "currency";

                    //Success return page url
                    redirecturl += "&return=" +
                                   ConfigurationManager.AppSettings["SuccessURL"];

                    //Failed return page url
                    redirecturl += "&cancel_return=" +
                                   ConfigurationManager.AppSettings["FailedURL"];

                    Response.Redirect(redirecturl);
                }
                else
                {
                    return(RedirectToAction("Save"));
                }
            }
            model.PizzaSizes = _context.PizzaAttributes.Where(x => x.Name == "Size").Select(p => new SelectListItem
            {
                Text  = p.Value + @"(" + p.Size + @" inch - $" + p.Amount + @")",
                Value = p.Id.ToString()
            });
            model.PizzaCrusts = _context.PizzaAttributes.Where(x => x.Name == "Crust").Select(p => new SelectListItem
            {
                Text  = p.Value + (Math.Abs(p.Amount) == 0 ? "" : @"(+ $" + p.Amount + @")"),
                Value = p.Id.ToString()
            });
            model.PizzaToppings = _context.PizzaAttributes.Where(x => x.Name == "Toppings")
                                  .Select(p => new SelectListItem {
                Text = p.Value + @"(+ $" + p.Amount + @")", Value = p.Id.ToString()
            });
            model.PaymentMethods =
                _context.PaymentMethods.Select(pm => new SelectListItem {
                Text = pm.Name, Value = pm.Id.ToString()
            });

            return(View("Index", model));
        }