Ejemplo n.º 1
0
        public ActionResult GuestOrder([Bind(Include = "FirstName,Email,Phone,City,Adress,ConfirmPrivace,Delivery,PayMethod,BonusPoints,UseBonusPoints,DeliveryCost", Exclude = "LastName")] OrderModel model)
        {
            if (!model.ConfirmPrivace)
            {
                ModelState.AddModelError("ConfirmPrivace", "Для регистрации необходимо принять соглашение");
            }
            else
            {
                if (model.Delivery == 0)
                {
                    ModelState.AddModelError("Delivery", "Необходимо выбрать один из перечисленных вариантов");
                }
                else
                {
                    if (model.PayMethod == 0) {
                        ModelState.AddModelError("PayMethod", "Необходимо выбрать один из перечисленных вариантов");
                    }   
                else
                {
                    if (ModelState.IsValid)
                    {
                        var cart = db.CartRepository.GetById(int.Parse(UserContext.Current.CartID));

                        if (cart != null)
                        {
                            try
                            {
                                var scgStatus = db.ShareCodesGroupRepository.GetAll().FirstOrDefault(i => i.Code.Equals("ORDER_STATUS"));

                                var newStatus = scgStatus != null ? scgStatus.t_Share_Code.FirstOrDefault(f => f.Code == "ORDER_NEW").ID: 0;

                                var sumPrice = cart.t_Ref_Cart_Product.Sum(i => i.t_Product.Price);
                                var insModel = new t_Order()
                                {
                                    Name = model.FirstName,
                                    Email = model.Email,
                                    Phone = model.Phone,
                                    DeliveryMethodID = model.Delivery,
                                    City = model.City,
                                    Adress = model.Adress,
                                    Date = DateTime.Now,
                                    PayMethodID = model.PayMethod,
                                    SumPrice = sumPrice ,
                                    UserID = UserContext.Current.ID,
                                    BonusPoints = 0,
                                    WholePrice = sumPrice,
                                    StatusID = newStatus,
                                    DeliveryCost = model.DeliveryCost
                                };

                                if (model.UseBonusPoints && UserContext.Current.IsAuthenticated)
                                {
                                    var user = db.UserRepository.GetById(UserContext.Current.ID);
                                    var usersBonus = user.BonusPoints;
                                    var bonus = model.BonusPoints < 0 ? 0 : model.BonusPoints;
                                    insModel.BonusPoints = bonus > usersBonus ? usersBonus : bonus;
                                    insModel.WholePrice = sumPrice - insModel.BonusPoints;
                                    user.BonusPoints = usersBonus - insModel.BonusPoints;
                                    db.UserRepository.Update(user);
                                }

                                var insVal = db.OrderRepository.InsertReturn(insModel);
                                
                                db.Save();

                                ICollection<t_Ref_Order_Product> collProd = new List<t_Ref_Order_Product>();

                                foreach (var p in cart.t_Ref_Cart_Product)
                                {
                                    collProd.Add(new t_Ref_Order_Product()
                                    {
                                        OrderID = insVal.ID,
                                        ProductID = p.ProductID
                                    });
                                   
                                }



                                insVal.t_Ref_Order_Product = collProd;

                                db.OrderRepository.Update(insVal);
                                db.RefCartProductRepository.DeleteRange(cart.t_Ref_Cart_Product);
                                foreach (var p in insVal.t_Ref_Order_Product)
                                {
                                    if (p.t_Product.Count > 0) p.t_Product.Count--;
                                }
                                db.Save();

                                //Send message for Managers about new order
                                var managerEmails = getManagerEmailsAsList();
                                if (managerEmails.Any())
                                {
                                    var messageText = string.Format("<div>Вам поступил новый заказ #{0}</div>",insVal.ID);
                                    var email = new EmailController().ClientDelivery(managerEmails.ToList(), messageText);
                                    Task.Factory.StartNew(() => email.Deliver());
                                }
                                

                                MainInfoUser.Instance.Dispose();

                                return RedirectToAction("OrderDone", new {id = insVal.ID, clientName = insVal.Name});
                            }
                            catch (Exception ex)
                            {

                                Logger.Instance.Error(
                                    "Произошла ошибка при формировании заказа, корзины {0} ошибка:{1}",
                                    UserContext.Current.CartID, ex);
                            }

                        }

                    }
                }
            }

        }
            
            IEnumerable<t_Share_Code> scg = null;
            t_Share_Codes_Group companyInfo = null;
            t_Share_Codes_Group payInfo = null;
            ViewBag.EmailDisabled = false;

            scg = db.ShareCodesGroupRepository.GetAll().FirstOrDefault(i => i.Code.Equals("DELIVERY_TYPE")).t_Share_Code;

            companyInfo = db.ShareCodesGroupRepository.GetAll().FirstOrDefault(i => i.Code.Equals("COMPANY_INFO"));
            payInfo = db.ShareCodesGroupRepository.GetAll().FirstOrDefault(i => i.Code.Equals("PAY_METHOD"));

            model.DeliveryPickUp = scg.FirstOrDefault(i => i.Code.Equals("DELIVERY_PICKUP"));
            model.DeliveryCompany = scg.FirstOrDefault(i => i.Code.Equals("DELIVERY_COMPANY"));

            //show address form
            ViewBag.VisibleDeliveryAdress = false;
            if (model.Delivery == model.DeliveryCompany.ID && !string.IsNullOrWhiteSpace(model.Adress) && model.Adress != "-")
            {
                ViewBag.VisibleDeliveryAdress = true;
            }
 
            if (companyInfo != null)
            {
                model.CompanyAdress = companyInfo.t_Share_Code.FirstOrDefault(i => i.Code.Equals("ADRESS")).Value;
                model.CompanyMode = companyInfo.t_Share_Code.FirstOrDefault(i => i.Code.Equals("WORK_MODE")).Value;
            }
            model.PayCash = payInfo.t_Share_Code.FirstOrDefault(i => i.Code.Equals("PAY_CASH"));
            model.PayCard = payInfo.t_Share_Code.FirstOrDefault(i => i.Code.Equals("PAY_CARD"));
            model.PayOnline = payInfo.t_Share_Code.FirstOrDefault(i => i.Code.Equals("PAY_ONLINE"));

            var uc = UserContext.Current;

            if (uc.IsAuthenticated) {
                model.FirstName = uc.FirstName;
                model.Email = uc.Email;
                model.Phone = uc.Phone;
                ViewBag.EmailDisabled = true;
                ViewBag.BonusPoints = db.UserRepository.GetById(uc.ID).BonusPoints;
            }
            ViewBag.SumItem = MainInfoUser.Instance.CartItemsSum;
            
            ViewBag.IsAuth = uc.IsAuthenticated;
            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult OrderEditProductsPartial(t_Order model)
        {

            return PartialView(model);
        }