public IActionResult Checkout(cartVM CartVM)
        {
            if (ModelState.IsValid)
            {
                Product     product = new Product();
                List <Item> cart    = new List <Item>();
                cart = SessionHelper.GetObjectFromJson <List <Item> >(HttpContext.Session, "cart");

                CartVM.OrderHeader.OrderDate = DateTime.Now;
                CartVM.OrderHeader.Status    = "Submitted";

                foreach (var item in cart)
                {
                    CartVM.OrderHeader.OrderTotal += (item.Product.Price * item.Quantity);
                }
                var userId = _userManager.GetUserId(this.User);

                CartVM.OrderHeader.UserId = userId;
                _iOrderH.Add(CartVM.OrderHeader);

                foreach (var item in cart)
                {
                    product = _iProd.GetFirstOrDefault(item.Product.Id);
                    OrderDetails orderDetails = new OrderDetails
                    {
                        OrderHeaderId   = CartVM.OrderHeader.Id,
                        ProductId       = item.Product.Id,
                        ProductQuantity = item.Quantity,
                        ProductName     = item.Product.Name,
                        ProductPrice    = (item.Product.Price * item.Quantity)
                    };
                    product.Quantity = product.Quantity - item.Quantity;

                    _iProd.Update(product);
                    _iOrderD.Add(orderDetails);
                }
                cart.Clear();
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);

                return(RedirectToAction("OrderConfirmation", "Cart", new { id = CartVM.OrderHeader.Id }));
            }
            return(View(nameof(Index)));
        }
示例#2
0
        public async Task <IActionResult> SummaryPost(ProductUserVM ProductUserVM)
        {
            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            //we need to create an order

            OrderHeader orderHeader = new OrderHeader()
            {
                ApplicationUserId = claim.Value,
                FinalOrderTotal   = ProductUserVM.ProductList.Sum(x => x.TempQty * x.Price),
                City          = ProductUserVM.ApplicationUser.City,
                StreetAddress = ProductUserVM.ApplicationUser.StreetAddress,
                PostalCode    = ProductUserVM.ApplicationUser.PostalCode,
                FullName      = ProductUserVM.ApplicationUser.FullName,
                Email         = ProductUserVM.ApplicationUser.Email,
                PhoneNumber   = ProductUserVM.ApplicationUser.PhoneNumber,
                OrderDate     = DateTime.Now,
                OrderStatus   = WC.StatusPending
            };

            _orderHRepo.Add(orderHeader);
            _orderHRepo.Save();

            foreach (var prod in ProductUserVM.ProductList)
            {
                OrderDetail orderDetail = new OrderDetail()
                {
                    OrderHeaderId = orderHeader.Id,
                    PricePerUnit  = prod.Price,
                    Qty           = prod.TempQty,
                    ProductId     = prod.Id
                };
                var prodPopularity = _prodRepo.FirstOrDefault(u => u.Id == prod.Id);
                prodPopularity.ProductPopularity++;
                _prodRepo.Update(prodPopularity);
                _orderDRepo.Add(orderDetail);
            }
            _orderDRepo.Save();
            TempData[WC.Success] = "Order is placed successfully";
            return(RedirectToAction(nameof(OrderConfirmation), new { id = orderHeader.Id }));
        }
示例#3
0
        public IActionResult SummaryPost(IFormCollection collection, ProductUserVM ProductUserVM)
        {
            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            OrderHeader orderHeader = new OrderHeader()
            {
                CreatedByUserId = claim.Value,
                FinalOrderTotal = ProductUserVM.ProductList.Sum(x => x.Unit * x.Price),
                City            = ProductUserVM.ApplicationUser.City,
                StreetAddress   = ProductUserVM.ApplicationUser.StreetAddress,
                State           = ProductUserVM.ApplicationUser.State,
                PostalCode      = ProductUserVM.ApplicationUser.PostalCode,
                FullName        = ProductUserVM.ApplicationUser.FullName,
                Email           = ProductUserVM.ApplicationUser.Email,
                PhoneNumber     = ProductUserVM.ApplicationUser.PhoneNumber,
                OrderDate       = DateTime.Now,
                OrderStatus     = WebConstant.StatusPending
            };

            _orderHRepo.Add(orderHeader);
            _orderHRepo.Save();
            return(RedirectToAction(nameof(Index)));
        }
示例#4
0
        public async Task <IActionResult> SummaryPost(IFormCollection collection, ProductUserVM ProductUserVM)
        {
            var claimsIdentity = (ClaimsIdentity)User.Identity;
            var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            if (User.IsInRole(WC.AdminRole))
            {
                //we need to create an order
                //var orderTotal = 0.0;
                //foreach(Product prod in ProductUserVM.ProductList)
                //{
                //    orderTotal += prod.Price * prod.TempSqFt;
                //}
                OrderHeader orderHeader = new OrderHeader()
                {
                    CreatedByUserId = claim.Value,
                    FinalOrderTotal = ProductUserVM.ProductList.Sum(x => x.TempSqFt * x.Price),
                    City            = ProductUserVM.ApplicationUser.City,
                    StreetAddress   = ProductUserVM.ApplicationUser.StreetAddress,
                    State           = ProductUserVM.ApplicationUser.State,
                    PostalCode      = ProductUserVM.ApplicationUser.PostalCode,
                    FullName        = ProductUserVM.ApplicationUser.FullName,
                    Email           = ProductUserVM.ApplicationUser.Email,
                    PhoneNumber     = ProductUserVM.ApplicationUser.PhoneNumber,
                    OrderDate       = DateTime.Now,
                    OrderStatus     = WC.StatusPending
                };
                _orderHRepo.Add(orderHeader);
                _orderHRepo.Save();

                foreach (var prod in ProductUserVM.ProductList)
                {
                    OrderDetail orderDetail = new OrderDetail()
                    {
                        OrderHeaderId = orderHeader.Id,
                        PricePerSqFt  = prod.Price,
                        Sqft          = prod.TempSqFt,
                        ProductId     = prod.Id
                    };
                    _orderDRepo.Add(orderDetail);
                }
                _orderDRepo.Save();

                string nonceFromTheClient = collection["payment_method_nonce"];

                var request = new TransactionRequest
                {
                    Amount             = Convert.ToDecimal(orderHeader.FinalOrderTotal),
                    PaymentMethodNonce = nonceFromTheClient,
                    OrderId            = orderHeader.Id.ToString(),
                    Options            = new TransactionOptionsRequest
                    {
                        SubmitForSettlement = true
                    }
                };

                var gateway = _brain.GetGateway();
                Result <Transaction> result = gateway.Transaction.Sale(request);

                if (result.Target.ProcessorResponseText == "Approved")
                {
                    orderHeader.TransactionId = result.Target.Id;
                    orderHeader.OrderStatus   = WC.StatusApproved;
                }
                else
                {
                    orderHeader.OrderStatus = WC.StatusCancelled;
                }
                _orderHRepo.Save();
                return(RedirectToAction(nameof(InquiryConfirmation), new { id = orderHeader.Id }));
            }
            else
            {
                //we need to create an inquiry
                var PathToTemplate = _webHostEnvironment.WebRootPath + Path.DirectorySeparatorChar.ToString()
                                     + "templates" + Path.DirectorySeparatorChar.ToString() +
                                     "Inquiry.html";

                var    subject  = "New Inquiry";
                string HtmlBody = "";
                using (StreamReader sr = System.IO.File.OpenText(PathToTemplate))
                {
                    HtmlBody = sr.ReadToEnd();
                }
                //Name: { 0}
                //Email: { 1}
                //Phone: { 2}
                //Products: {3}

                StringBuilder productListSB = new StringBuilder();
                foreach (var prod in ProductUserVM.ProductList)
                {
                    productListSB.Append($" - Name: { prod.Name} <span style='font-size:14px;'> (ID: {prod.Id})</span><br />");
                }

                string messageBody = string.Format(HtmlBody,
                                                   ProductUserVM.ApplicationUser.FullName,
                                                   ProductUserVM.ApplicationUser.Email,
                                                   ProductUserVM.ApplicationUser.PhoneNumber,
                                                   productListSB.ToString());


                await _emailSender.SendEmailAsync(WC.EmailAdmin, subject, messageBody);

                InquiryHeader inquiryHeader = new InquiryHeader()
                {
                    ApplicationUserId = claim.Value,
                    FullName          = ProductUserVM.ApplicationUser.FullName,
                    Email             = ProductUserVM.ApplicationUser.Email,
                    PhoneNumber       = ProductUserVM.ApplicationUser.PhoneNumber,
                    InquiryDate       = DateTime.Now
                };

                _inqHRepo.Add(inquiryHeader);
                _inqHRepo.Save();

                foreach (var prod in ProductUserVM.ProductList)
                {
                    InquiryDetail inquiryDetail = new InquiryDetail()
                    {
                        InquiryHeaderId = inquiryHeader.Id,
                        ProductId       = prod.Id,
                    };
                    _inqDRepo.Add(inquiryDetail);
                }
                _inqDRepo.Save();
                TempData[WC.Success] = "Inquiry submitted successfully";
            }



            return(RedirectToAction(nameof(InquiryConfirmation)));
        }