示例#1
0
        public bool Insert(int orderId, int subProductId)
        {
            OrderSubProduct item = new OrderSubProduct();

            item.OrderId = orderId;
            try
            {
                this.UnitOfWork.OrderSubProductRepository.Add(item);
                this.UnitOfWork.Save();
                return(true);
            } catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }
        }
示例#2
0
        public IActionResult Payment(OrderModel orderModel)
        {
            if (ModelState.IsValid)
            {
                SessionModel session = HttpContext.Session.GetSession("session");

                if (session != null && session.Products.Count() != 0 && session.CustomerId != 0)
                {
                    Order order = new Order();
                    List <OrderSubProduct> listOrderSubProduct = new List <OrderSubProduct>();
                    List <OrderProduct>    listOrderProduct    = new List <OrderProduct>();
                    OrderSubProduct        temp  = new OrderSubProduct();
                    OrderProduct           temp1 = new OrderProduct();

                    int a = this.SubProductService
                            .CheckBeforeCheckout(session.Products.Select(x => x.SubProductId).ToArray());

                    if (a == -1)
                    {
                        var listSubProducts = this.SubProductService
                                              .GetProductByListSubProductId(session.Products.Select(x => x.SubProductId).ToArray());

                        var productIds   = listSubProducts.Select(x => x.ProductId).Distinct();
                        var promotionNow = this.PromotionService.GetNowPromotion();

                        foreach (int id in productIds)
                        {
                            var price = listSubProducts.Where(x => x.Product.Id == id)
                                        .Select(x => x.Product.PriceDetails.OrderByDescending(y => y.CreatedDate)
                                                .Select(y => y.Price).FirstOrDefault()).FirstOrDefault();
                            foreach (var promotion in promotionNow)
                            {
                                if (promotion.PromotionProducts.Select(x => x.ProductId).Contains(id))
                                {
                                    price = price - price * promotion.PromotionPercent / 100;
                                }
                            }

                            temp1 = new OrderProduct(id, price);
                            listOrderProduct.Add(temp1);
                        }


                        foreach (var item in session.Products)
                        {
                            temp = new OrderSubProduct();
                            temp.SubProductId = item.SubProductId;
                            temp.Quantity     = item.NumberProduct;
                            listOrderSubProduct.Add(temp);
                        }
                        order.PhoneNumber      = orderModel.PhoneNumber;
                        order.Address          = orderModel.Address;
                        order.OrderSubProducts = listOrderSubProduct;
                        order.OrderProducts    = listOrderProduct;
                        //order.CustomerId = session.CustomerId;
                        order.IsDeleted   = false;
                        order.CreatedDate = DateTime.Now;
                        order.PaymentType = orderModel.PaymentType.ToString();

                        ////////////////////////
                        List <ShopCartViewModel> shopCart = new List <ShopCartViewModel>();
                        int   total         = 0;
                        int[] subProductIds = session.Products.Select(x => x.SubProductId).ToArray();
                        var   subProducts   = this.SubProductService.GetForShopCart(subProductIds);
                        shopCart = Mapper.Map <IEnumerable <SubProduct>, IEnumerable <ShopCartViewModel> >(subProducts).ToList();
                        foreach (var item in shopCart)
                        {
                            foreach (var item1 in session.Products)
                            {
                                if (item.SubProductId == item1.SubProductId)
                                {
                                    item.ProductOrder = item1.NumberProduct;
                                    if (item.PromotionPercent != 0)
                                    {
                                        total += (item.Price * item.ProductOrder) - (item.Price * item.ProductOrder * item.PromotionPercent / 100);
                                    }
                                    else
                                    {
                                        total += item.Price * item.ProductOrder;
                                    }
                                }
                            }
                        }
                        order.Total = total;
                        PayViewModel payViewModel = new PayViewModel(shopCart, total);
                        if (orderModel.PaymentType.ToString() == "PayPal")
                        {
                            TempData["PaymentType"] = "PayPal";
                        }
                        else
                        {
                            TempData["PaymentType"] = "None";
                        }
                        ViewBag.total = total;

                        //if (order.PaymentType == "PayPal")
                        //{
                        //    HttpContext.Session.SetSession("order", order);
                        //    return View("Pay", payViewModel);
                        //}
                        //else
                        //{
                        int orderId = this.OrderService.InsertOrder(order, GetCustomerId());
                        if (orderId != 0)
                        {
                            var customer = this.CustomerService.GetById(order.CustomerId);
                            SendMail(customer.Name, customer.Mail, orderId);
                            session.Products = new List <ProductInSessionModel>();
                            HttpContext.Session.SetSession("session", session);
                            return(View("Pay", payViewModel));
                        }
                        else
                        {
                            ModelState.AddModelError("PaymentType", "Error");
                            return(View(orderModel));
                        }
                        //}
                    }
                    else if (a == 0)
                    {
                        ModelState.AddModelError("Address", "Error");
                        return(View(orderModel));
                    }
                    else
                    {
                        var subProduct = this.SubProductService.GetById(a);
                        ModelState.AddModelError("Address", "Sản phẩm "
                                                 + subProduct.Product.ProductName + " đã hết hàng. Chúng tôi rất xin lỗi về vấn đề này!");
                        return(View(orderModel));
                    }
                }
                else
                {
                    ModelState.AddModelError("PaymentType", "Error");
                    return(View(orderModel));
                }
            }
            else
            {
                return(View(orderModel));
            }
        }