//function ssave order when order success!
        public void saveOrder(Morder order, string paymentMethod, int StatusPayment, string ordercode)
        {
            var cart = Session[SessionCart];
            var list = new List <Cart_item>();

            if (cart != null)
            {
                list = (List <Cart_item>)cart;
            }

            if (ModelState.IsValid)
            {
                order.code   = ordercode;
                order.userid = 1;
                order.deliveryPaymentMethod = paymentMethod;
                order.StatusPayment         = StatusPayment;
                order.created_ate           = DateTime.Now;
                order.updated_by            = 1;
                order.updated_at            = DateTime.Now;
                order.updated_by            = 1;
                order.status     = 2;
                order.exportdate = DateTime.Now;
                db.Orders.Add(order);
                db.SaveChanges();
                Session["OrderId"] = ordercode;
                ViewBag.name       = order.deliveryname;
                ViewBag.email      = order.deliveryemail;
                ViewBag.address    = order.deliveryaddress;
                ViewBag.code       = order.code;
                ViewBag.phone      = order.deliveryphone;
                Mordersdetail orderdetail = new Mordersdetail();

                foreach (var item in list)
                {
                    float price = 0;
                    int   sale  = (int)item.product.pricesale;
                    if (sale > 0)
                    {
                        price = (float)item.product.price - (int)item.product.price / 100 * (int)sale * item.quantity;
                    }
                    else
                    {
                        price = (float)item.product.price * (int)item.quantity;
                    }
                    orderdetail.orderid   = order.ID;
                    orderdetail.productid = item.product.ID;
                    orderdetail.priceSale = (int)item.product.pricesale;
                    orderdetail.price     = item.product.price;
                    orderdetail.quantity  = item.quantity;
                    orderdetail.amount    = price;

                    db.Ordersdetails.Add(orderdetail);
                    db.SaveChanges();
                    var updatedProduct = db.Products.Find(item.product.ID);
                    db.Products.Attach(updatedProduct);
                    db.Entry(updatedProduct).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
        }
        public ActionResult formCheckOut(FormCollection f)
        {
            //Muser user = (Muser)Session[Common.CommonConstants.CUSTOMER_SESSION];
            var    tenKH    = f["deliveryname"];
            var    SDT      = f["deliveryphone"];
            var    email    = f["deliveryemail"];
            var    diaChi   = f["deliveryaddress"];
            var    TongTien = f["sumOrder"];
            Morder order    = new Morder();

            order.deliveryname    = tenKH;
            order.deliveryphone   = SDT;
            order.deliveryemail   = email;
            order.deliveryaddress = diaChi;
            order.updated_at      = DateTime.Today;
            db.Orders.Add(order);
            db.SaveChanges();
            var cart  = Session[SessionCart];
            var list1 = new List <Cart_item>();

            if (cart != null)
            {
                list1 = (List <Cart_item>)cart;
            }
            var orderid = db.Orders.OrderByDescending(m => m.ID).ToList()[0].ID;

            foreach (var item in list1)
            {
                Mordersdetail detail = new Mordersdetail();
                detail.orderid   = orderid;
                detail.productid = item.product.ID;
                detail.price     = item.product.price;
                detail.quantity  = item.quantity;
                detail.priceSale = item.product.pricesale;
                detail.amount    = (detail.price * (100 - detail.priceSale) / 100) * detail.quantity;
                db.Ordersdetails.Add(detail);
                db.SaveChanges();
            }
            return(View("OrderComplete"));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Index(Morder order)
        {
            var cart = Session[SessionCart];
            var list = new List <Cart_item>();

            if (cart != null)
            {
                list = (List <Cart_item>)cart;
            }
            Random rand = new Random((int)DateTime.Now.Ticks);
            int    RandomNumber;

            RandomNumber = rand.Next(100, 100000);
            if (ModelState.IsValid)
            {
                order.code        = RandomNumber;
                order.userid      = 1;
                order.created_ate = DateTime.Now;
                order.updated_by  = 1;
                order.updated_at  = DateTime.Now;
                order.status      = 2;
                order.exportdate  = DateTime.Now;
                db.Orders.Add(order);
                db.SaveChanges();

                ViewBag.name    = order.deliveryname;
                ViewBag.email   = order.deliveryemail;
                ViewBag.address = order.deliveryaddress;
                ViewBag.code    = order.code;
                ViewBag.phone   = order.deliveryphone;
                Mordersdetail orderdetail = new Mordersdetail();

                foreach (var item in list)
                {
                    float price = 0;
                    int   sale  = (int)item.product.pricesale;
                    if (sale > 0)
                    {
                        price = (float)item.product.price - (int)item.product.price / 100 * (int)sale * item.quantity;
                    }
                    else
                    {
                        price = (float)item.product.price * (int)item.quantity;
                    }
                    orderdetail.orderid   = order.ID;
                    orderdetail.productid = item.product.ID;
                    orderdetail.priceSale = (int)item.product.pricesale;
                    orderdetail.price     = item.product.price;
                    orderdetail.quantity  = item.quantity;
                    orderdetail.amount    = price;

                    db.Orderdetails.Add(orderdetail);
                    await db.SaveChangesAsync();

                    //ViewBag.sump = list.Sum((Func<Cart_item, int>)(m => (int)m.product.price * (int) m.quantity));
                    // change number product
                    var updatedProduct = db.Products.Find(item.product.ID);
                    updatedProduct.catid      = item.product.catid;
                    updatedProduct.Submenu    = item.product.Submenu;
                    updatedProduct.name       = item.product.name;
                    updatedProduct.slug       = item.product.slug;
                    updatedProduct.img        = item.product.img;
                    updatedProduct.detail     = item.product.detail;
                    updatedProduct.number     = (int)updatedProduct.number - (int)item.quantity;
                    updatedProduct.price      = item.product.price;
                    updatedProduct.pricesale  = item.product.pricesale;
                    updatedProduct.metakey    = item.product.metakey;
                    updatedProduct.metadesc   = item.product.metadesc;
                    updatedProduct.created_by = item.product.created_by;
                    updatedProduct.created_at = item.product.created_at;
                    updatedProduct.updated_by = item.product.updated_by;
                    updatedProduct.updated_at = item.product.updated_at;
                    updatedProduct.status     = item.product.status;
                    db.Products.Attach(updatedProduct);
                    db.Entry(updatedProduct).State = EntityState.Modified;
                    await db.SaveChangesAsync();
                }
                ViewBag.cart           = (List <Cart_item>)cart;
                Session["SessionCart"] = null;
                var listProductOrder = db.Orderdetails.Where(m => m.orderid == order.ID);
                return(View("payment", listProductOrder.ToList()));
            }
            ViewBag.error = "Đặt hàng thất bại";
            return(View("index"));
        }