Example #1
0
        public ActionResult AddOrderItems(OrderItem item)
        {
            List<Order> myOrderList = new List<Order>();
            //  await order = myHandler.GetOrdersList().Single(ord => ord.DataModified == DateTime.Now);
            TryUpdateModel(item);
            myHandler = new BusinessLogicHandler();
            myHandler.AddOrderItem(item);

            return Json(new { success = true });
        }
Example #2
0
        public ActionResult Checkout(FormCollection collection, ProductViewModel model)
        {
            Delivery shipping = new Delivery();
            IEnumerable<Book> ifBooks = (IEnumerable<Book>)Session["myBooks"];
            IEnumerable<Technology> ifGadget = (IEnumerable<Technology>)Session["myGadget"];
            List<CartItem> myItems = (List<CartItem>)Session["myItems"];
            myHandler = new BusinessLogicHandler();

            #region Get Shipping Data
            try
            {
                shipping = myHandler.GetDeliveryDetails(Convert.ToInt32(collection[1].ToString()));

                if (ModelState.ContainsKey("I_DeliveryList"))
                    ModelState["I_DeliveryList"].Errors.Clear();
            }
            catch
            { ModelState.AddModelError("deliveryHelper.DeliveryServicePrice", "Please select a delivery service from dropdown !"); }
            #endregion

            #region Cathing model errors
            var error = ModelState.Values.SelectMany(e => e.Errors);
            var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();
            #endregion

            int? IID = 0;
            if (ModelState.IsValid)
            {
                #region Get User
                string userName = User.Identity.GetUserName();
                ApplicationDbContext dataSocket = new ApplicationDbContext();
                UserStore<ApplicationUser> myStore = new UserStore<ApplicationUser>(dataSocket);
                userMgr = new ApplicationUserManager(myStore);
                var user = userMgr.FindByEmail(userName);
                #endregion

                try
                {
                    #region Creating the reciept/invoice
                    Invoice reciept = new Invoice { User_Id = user.Id, DateCreated = DateTime.Now, DeliveryAddress = model.deliveryHelper.DeliveryAddress, DeliveryServiceID = Convert.ToInt32(collection[1].ToString()), Status = false };
                    try
                    {
                        InvoiceItem invoiceLine = new InvoiceItem();
                        invoiceLine = myHandler.GetInvoiceLastNumber(reciept);
                        foreach (var item in myItems)
                        {
                            invoiceLine.CartItemID = item.CartItemID;
                            invoiceLine.ProductID = item.ProductID;
                            invoiceLine.Quantity = item.Quantity;

                            #region Get Product Price
                            bool chk = false;
                            chk = myHandler.CheckProductType(item.ProductID);
                            if (chk)
                            {
                                Book book = new Book();
                                book = myHandler.GetBook(item.ProductID);
                                invoiceLine.Price = book.SellingPrice;
                                myHandler.AddinvoiceItem(invoiceLine);
                            }
                            else
                            {
                                Technology device = new Technology();
                                device = myHandler.GetTechnologyDetails(item.ProductID);
                                invoiceLine.Price = device.SellingPrice;
                                myHandler.AddinvoiceItem(invoiceLine);
                            }
                            #endregion


                        }
                        IID = invoiceLine.InvoiceID;

                    }
                    catch { }
                    #endregion


                    #region Placing the order
                    try
                    {

                        #region Prep Utilities

                        Order ord;
                        Book book = new Book();
                        Technology gadget = new Technology();
                        int supplierId = 0;
                        OrderItem orderLine = new OrderItem();
                        myHandler = new BusinessLogicHandler();
                        List<int> orders = new List<int>();
                        List<int> suppliers = new List<int>();

                        #endregion

                        foreach (var item in myItems)
                        {
                            if (myHandler.CheckProductType(item.ProductID))
                            {
                                book = myHandler.GetBook(item.ProductID);
                                supplierId = book.SupplierID;
                                if (suppliers.Contains(book.SupplierID))
                                {
                                    int x = suppliers.IndexOf(supplierId);
                                    orderLine.OrderNo = orders.ElementAt(x);
                                    orderLine.ProductID = item.ProductID;
                                    orderLine.Quantity = item.Quantity;
                                    myHandler.AddOrderItem(orderLine);
                                }
                                else
                                {
                                    suppliers.Add(supplierId);
                                    ord = new Order { DateCreated = DateTime.Now.Date, SupplierID = supplierId, InvoiceID = IID.GetValueOrDefault(), DateLastModified = DateTime.Now.Date, Status = false };
                                    orderLine = myHandler.AddOrder(ord);
                                    orders.Add(orderLine.OrderNo);
                                    orderLine.ProductID = item.ProductID;
                                    orderLine.Quantity = item.Quantity;
                                    myHandler.AddOrderItem(orderLine);
                                }

                            }
                            else
                            {
                                supplierId = ifGadget.SingleOrDefault(m => m.ProductID == item.ProductID).SupplierID;
                                if (suppliers.Contains(supplierId))
                                {
                                    int y = suppliers.IndexOf(supplierId);
                                    orderLine.OrderNo = orders.ElementAt(y);
                                    orderLine.ProductID = item.ProductID;
                                    orderLine.Quantity = item.Quantity;
                                    myHandler.AddOrderItem(orderLine);
                                }
                                else
                                {
                                    suppliers.Add(supplierId);
                                    ord = new Order { DateCreated = DateTime.Now.Date, SupplierID = supplierId, InvoiceID = IID.GetValueOrDefault(), DateLastModified = DateTime.Now.Date, Status = false };
                                    orderLine = myHandler.AddOrder(ord);
                                    orders.Add(orderLine.OrderNo);
                                    orderLine.ProductID = item.ProductID;
                                    orderLine.Quantity = item.Quantity;
                                    myHandler.AddOrderItem(orderLine);
                                }
                            }
                        }
                    }
                    catch { }
                    #endregion
                }
                catch
                {/*Navigate to custom error page*/ }
                Session["deliverData"] = model;
                return RedirectToAction("Receipt", new { IID = IID });
            }
            else
            {
                #region Feed The Model


                CartItem thishereItem = new CartItem();
                try
                {
                    ProductViewModel.CartHelper cartHelp;
                    List<ProductViewModel.CartHelper> itemList = new List<ProductViewModel.CartHelper>();
                    double cartTotal = 0;
                    if (myItems != null)
                    {
                        if (ifBooks != null)
                        {
                            var revised = from rev in ifBooks
                                          join item in myItems on rev.ProductID equals item.ProductID
                                          where rev.ProductID == item.ProductID
                                          select new { rev.ProductID, rev.SellingPrice, item.Quantity };
                            foreach (var ite in revised)
                            {
                                cartHelp = new ProductViewModel.CartHelper();
                                cartHelp.ProductID = ite.ProductID;
                                cartHelp.TotalPerItem = (ite.SellingPrice * ite.Quantity);
                                cartTotal += (ite.SellingPrice * ite.Quantity);
                                itemList.Add(cartHelp);
                            }
                        }
                        if (ifGadget != null)
                        {
                            var revised = from rev in ifGadget
                                          join item in myItems on rev.ProductID equals item.ProductID
                                          where rev.ProductID == item.ProductID
                                          select new { rev.ProductID, rev.SellingPrice, item.Quantity };
                            foreach (var ite in revised)
                            {
                                cartHelp = new ProductViewModel.CartHelper();
                                cartHelp.ProductID = ite.ProductID;
                                cartHelp.TotalPerItem = (ite.SellingPrice * ite.Quantity);
                                cartTotal += (ite.SellingPrice * ite.Quantity);
                                itemList.Add(cartHelp);
                            }
                        }
                    }
                    List<Company> company = new List<Company>(); myHandler = new BusinessLogicHandler();
                    company = myHandler.GetCompanyDetails();
                    double vat = 0;
                    foreach (var item in company)
                    { vat = item.VATPercentage; }
                    //calc
                    double vatAmount = (cartTotal * vat);
                    double subTotal = (cartTotal - vatAmount);
                    ProductViewModel.CartConclude finishing = new ProductViewModel.CartConclude();
                    finishing.CartTotal = cartTotal;
                    finishing.VatAddedTotal = vatAmount;
                    finishing.SubTotal = subTotal;
                    model.ItsA_wrap = new List<ProductViewModel.CartConclude>();
                    model.ItsA_wrap.Add(finishing);

                    model.deliveryHelper.DeliveryServiceName = shipping.ServiceName;
                    model.deliveryHelper.DeliveryServicePrice = shipping.Price;
                    model.deliveryHelper.DeliveryServiceType = shipping.ServiceType;

                    model.secureCart = itemList;
                    model.allBook = new List<Book>();
                    model.allBook = ifBooks.ToList() ;
                    model.allCartItem = new List<CartItem>();
                    model.allCartItem = myItems;
                    model.allTechnology = new List<Technology>();
                    model.allTechnology = ifGadget.ToList();
                }
                catch { }
                #endregion

                #region Drop down data
                DeliveryHandler deliver = new DeliveryHandler();
                IEnumerable<Delivery> delivery = (IEnumerable<Delivery>)deliver.GetDeliveryList();
                var dataStore = from name in delivery
                                select new { Value = name.DeliveryServiceID, Text = name.ServiceName };
                ViewBag.DeliveryList = new SelectList(dataStore.ToList());

                List<SelectListItem> deliveryI = new List<SelectListItem>();
                deliveryI.Add(new SelectListItem { Text = "Delivery Service", Value = "", Selected = true });
                foreach (var item in delivery)
                { deliveryI.Add(new SelectListItem { Text = item.ServiceName, Value = item.DeliveryServiceID.ToString() }); }
                model.I_DeliveryList = new List<SelectListItem>();
                model.I_DeliveryList = deliveryI;
                ViewData["I_Delivery"] = deliveryI;
                #endregion

                return View(model);
            }
        }