public int Update(OrderDetailModel entity)
        {
            TblOrderDetail obj = _mapper.Map <OrderDetailModel, TblOrderDetail>(entity);

            _repository.Update(obj);
            return(obj.OrderDetailId);
        }
        private void LoadGridOrderDetail(IList <OrderDetail> details)
        {
            TblOrderDetail tbl = new TblOrderDetail(_unitOfWork, details);

            this.gridOrderDetail.DataSource = tbl;
            this.gridOrderDetail.ClearSelection();
            this.SetUpGridOrderDetail();
        }
        public IActionResult Checkout()
        {
            if (!ModelState.IsValid)
            {
                return(View("Cart"));
            }
            var rawUser  = HttpContext.Session.GetString("USER");
            var user     = JsonConvert.DeserializeObject <TblUser>(rawUser);
            var products = GetCartItems(); //get products in cart session


            var newOrder = new TblOrder();

            newOrder.CreatedDate = DateTime.Now;
            newOrder.IdUser      = user.Id;
            newOrder.Payment     = "COD";
            newOrder.Status      = "Pending";

            _context.TblOrder.Add(newOrder);
            _context.SaveChanges();

            foreach (var product in products)
            {
                var orderDetail = new TblOrderDetail();

                orderDetail.IdOrder        = newOrder.Id;
                orderDetail.IdProduct      = product.product.Id;
                orderDetail.BoughtQuantity = product.quantity;

                //Update quantity
                var boughtItem = _context.TblProduct.Where(item => item.Id == product.product.Id).FirstOrDefault();
                if (boughtItem.Quantity < product.quantity || product.quantity < 0)
                {
                    if (product.quantity < 0)
                    {
                        TempData["ErrorQtt"] = "Quantity > 0 ";
                    }
                    else
                    {
                        TempData["ErrorQtt"] = "Not enough quantity for " + boughtItem.Name;
                    }
                    return(RedirectToAction("Cart", "Products"));
                }
                boughtItem.Quantity = Convert.ToInt32(boughtItem.Quantity - product.quantity);
                _context.TblProduct.Update(boughtItem);

                orderDetail.Tax = 10; //default
                _context.TblOrderDetail.Add(orderDetail);
            }

            _context.SaveChanges();

            SaveCartSession(new List <CartItem>());

            TempData["success"] = "Order successfully";

            return(RedirectToAction("Cart"));
        }
Beispiel #4
0
        public async Task AddOrderDetails(string orderID, Cart cart)
        {
            foreach (var item in cart.Items)
            {
                TblOrderDetail detail = new TblOrderDetail();
                detail.OrderId   = orderID;
                detail.ProductId = item._shopping_product.ProductId;
                detail.Quantity  = item._shopping_quantity;
                detail.Price     = item._shopping_product.Price;
                _context.TblOrderDetails.Add(detail);

                await _context.SaveChangesAsync();

                TblProduct product = _context.TblProducts.SingleOrDefault(s => s.ProductId == item._shopping_product.ProductId);
                if (product != null)
                {
                    product.Quantity = product.Quantity - item._shopping_quantity;
                    _context.TblProducts.Update(product);
                    await _context.SaveChangesAsync();
                }
            }
        }
        public OrderDetailModel GetById(int id)
        {
            TblOrderDetail obj = _repository.GetById(id);

            return(obj == null ? new OrderDetailModel() : Map(obj));
        }
 private OrderDetailModel Map(TblOrderDetail unit)
 {
     return(_mapper.Map <OrderDetailModel>(unit));
 }
Beispiel #7
0
        public ActionResult Command(FormCollection collection, string tag)
        {
            if (collection["btnorder"] != null)
            {
                try
                {
                    string   Name        = collection["Name"];
                    string   Address     = collection["Address"];
                    string   DateByy     = collection["DateByy"];
                    string   Mobile      = collection["Mobile"];
                    string   Mobiles     = collection["Mobiles"];
                    string   Email       = collection["Email"];
                    string   Name1       = collection["Name1"];
                    string   Address1    = collection["Address"];
                    string   Mobile1     = collection["Mobile1"];
                    string   Mobile1s    = collection["Mobile1s"];
                    string   Email1      = collection["Email1"];
                    string   rdtt        = collection["rdtt"];
                    string   rdvc        = collection["rdvc"];
                    string   NameCP      = collection["NameCP"];
                    string   AddressCP   = collection["AddressCP"];
                    string   MST         = collection["MST"];
                    string   Description = collection["Description"];
                    var      Sopping     = (clsGiohang)Session["giohang"];
                    TblOrder order       = new TblOrder();
                    order.Name          = Name;
                    order.Name1         = Name1;
                    order.Address       = Address;
                    order.Address1      = Address1;
                    order.DateByy       = DateTime.Now;
                    order.Mobile        = Mobile;
                    order.Mobile1       = Mobile1;
                    order.Mobiles       = Mobiles;
                    order.Mobile1s      = Mobile1s;
                    order.TypePay       = int.Parse(rdtt);
                    order.Email1        = Email1;
                    order.Email         = Email;
                    order.NameCp        = NameCP;
                    order.AddressCp     = AddressCP;
                    order.Mst           = MST;
                    order.TypePay       = int.Parse(rdtt);
                    order.TypeTransport = int.Parse(rdvc);
                    order.Status        = false;
                    order.Description   = Description;
                    order.Active        = true;

                    TblOrderDetail orderdetail = new TblOrderDetail();
                    var            MaxOrd      = db.TblOrder.OrderByDescending(p => p.Id).Take(1).ToList();
                    int            idOrder     = 1;

                    if (MaxOrd.Count > 0)
                    {
                        idOrder = MaxOrd[0].Id;
                    }
                    for (int i = 0; i < Sopping.CartItem.Count; i++)
                    {
                        orderdetail.IdProduct = Sopping.CartItem[i].Id;
                        orderdetail.Name      = Sopping.CartItem[i].Name;
                        orderdetail.Price     = Sopping.CartItem[i].Price;
                        orderdetail.Quantily  = Sopping.CartItem[i].Ord;
                        orderdetail.SumPrice  = Sopping.CartItem[i].SumPrice;
                        orderdetail.IdOrder   = idOrder;
                        db.TblOrderDetail.Add(orderdetail);
                        db.SaveChanges();
                    }
                    TblConfig config       = db.TblConfig.First();
                    var       fromAddress  = config.UserEmail;
                    var       toAddress    = config.Email;
                    var       orders       = db.TblOrder.OrderByDescending(p => p.Id).Take(1).ToList();
                    string    fromPassword = config.PassEmail;
                    string    ararurl      = Request.Url.ToString();
                    var       listurl      = ararurl.Split('/');
                    string    urlhomes     = "";
                    for (int i = 0; i < listurl.Length - 2; i++)
                    {
                        if (i > 0)
                        {
                            urlhomes += "/" + listurl[i];
                        }
                        else
                        {
                            urlhomes += listurl[i];
                        }
                    }
                    order.Tolar = Sopping.CartTotal;
                    db.TblOrder.Add(order);
                    db.SaveChanges();
                    string subject   = "Đơn hàng mới từ " + urlhomes + "";
                    string chuoihtml = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>Thông tin đơn hàng</title></head><body style=\"background:#f2f2f2; font-family:Arial, Helvetica, sans-serif\"><div style=\"width:750px; height:auto; margin:5px auto; background:#FFF; border-radius:5px; overflow:hidden\">";
                    chuoihtml += "<div style=\"width:100%; height:40px; float:left; margin:0px; background:#1c7fc4\"><span style=\"font-size:14px; line-height:40px; color:#FFF; margin:auto 20px; float:left\">" + DateTime.Now.Date + "</span><span style=\"font-size:14px; line-height:40px; float:right; margin:auto 20px; color:#FFF; text-transform:uppercase\">Hotline : " + config.HotlineIn + "</span></div>";
                    chuoihtml += "<div style=\"width:100%; height:auto; float:left; margin:0px\"><div style=\"width:35%; height:100%; margin:0px; float:left\"><a href=\"/\" title=\"\"><img src=\"" + urlhomes + "" + config.Logo + "\" alt=\"Logo\" style=\"margin:8px; display:block; max-height:95% \" /></a></div><div style=\"width:60%; height:100%; float:right; margin:0px; text-align:right\"><span style=\"font-size:18px; margin:20px 5px 5px 5px; display:block; color:#ff5a00; text-transform:uppercase\">" + config.Name + "</span><span style=\"display:block; margin:5px; color:#515151; font-size:13px; text-transform:uppercase\">Lớn nhất - Chính hãng - Giá rẻ nhất việt nam</span> </div>  </div>";
                    chuoihtml += "<span style=\"text-align:center; margin:10px auto; font-size:20px; color:#000; font-weight:bold; text-transform:uppercase; display:block\">Thông tin đơn hàng</span>";
                    chuoihtml += " <div style=\"width:90%; height:auto; margin:10px auto; background:#f2f2f2; padding:15px\">";
                    chuoihtml += "<p style=\"font-size:14px; color:#464646; margin:5px 20px\">Đơn hàng từ website : <span style=\"color:#1c7fc4\">" + urlhomes + "</span></p>";
                    chuoihtml += "<p style=\"font-size:14px; color:#464646; margin:5px 20px\">Ngày gửi đơn hàng : <span style=\"color:#1c7fc4\">Vào lúc " + DateTime.Now.Hour + " giờ " + DateTime.Now.Minute + " phút ( ngày " + DateTime.Now.Day + " tháng " + DateTime.Now.Month + " năm " + DateTime.Now.Year + ") </span></p>";
                    chuoihtml += "<p style=\"font-size:14px; color:#464646; margin:5px 20px\">Mã đơn hàng : <span style=\"color:#1c7fc4\">" + idOrder + " </span></p>";
                    chuoihtml += "<p style=\"font-size:14px; color:#1b1b1b; margin:5px 20px; text-decoration:underline; text-transform:uppercase\">Danh sách sản phẩm : </p>";
                    chuoihtml += "<table style=\"width:100%; height:auto; margin:10px auto; background:#FFF; border:1px\" border=\"0\">";
                    chuoihtml += " <tr style=\" background:#1c7fc4; color:#FFF; text-align:center; line-height:25px; font-size:12px\">";

                    chuoihtml += "<td>STT</td>";
                    chuoihtml += "<td>Tên sản phẩm</td>";
                    chuoihtml += "<td>Đơn giá (vnđ)</td>";
                    chuoihtml += "<td>Số lượng</td>";
                    chuoihtml += "<td>Thành tiền (vnđ)</td>";
                    chuoihtml += "</tr>";

                    for (int i = 0; i < Sopping.CartItem.Count; i++)
                    {
                        chuoihtml += "<tr style=\"line-height:20px; font-size:13px; color:#000; text-indent:5px; border-bottom:1px dashed #cecece; margin:1px 0px;\">";
                        chuoihtml += "<td style=\"text-align:center; width:7%\">" + i + "</td>";
                        chuoihtml += "<td style=\"width:45%\">" + Sopping.CartItem[i].Name + "</td>";
                        int gia = Convert.ToInt32(Sopping.CartItem[i].Price.ToString());
                        chuoihtml += "<td style=\"text-align:center; width:15%\">" + gia.ToString().Replace(",", "") + "</td>";
                        chuoihtml += "<td style=\"text-align:center; width:10%\">" + Sopping.CartItem[i].Ord + "</td>";
                        float thanhtien = Sopping.CartItem[i].Price * Sopping.CartItem[i].Ord;
                        chuoihtml += "<td style=\"text-align:center; font-weight:bold\">" + thanhtien.ToString().Replace(",", "") + "</td>";
                        chuoihtml += " </tr>";
                    }
                    chuoihtml += "<tr style=\"font-size:12px; font-weight:bold\">";
                    chuoihtml += "<td colspan=\"4\" style=\"text-align:right\">Tổng giá trị đơn hàng : </td>";
                    chuoihtml += "<td style=\"font-size:14px; color:#F00\">" + Sopping.CartTotal + " vnđ</td>";
                    chuoihtml += " </tr>";
                    chuoihtml += "</table>";
                    chuoihtml += "<div style=\" width:100%; margin:15px 0px\">";
                    chuoihtml += "<div style=\"width:100%; height:auto; float:left; margin:0px; border:1px solid #d5d5d5\">";
                    chuoihtml += "<div style=\" width:100%; height:30px; float:left; background:#1c7fc4; font-size:12px; color:#FFF; text-indent:15px; line-height:30px\">    	Thông tin người gửi     </div>";

                    chuoihtml += "<div style=\"width:100%; height:auto; float:left\">";
                    chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Họ và tên :<span style=\"font-weight:bold\"> " + Name + "</span></p>";
                    chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Địa chỉ :<span style=\"font-weight:bold\"> " + Address + "</span></p>";
                    chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Điện thoại :<span style=\"font-weight:bold\"> " + Mobile + " - " + Mobiles + "</span></p>";
                    chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Email :<span style=\"font-weight:bold\">" + Email + "</span></p>";
                    if (rdtt == "1")
                    {
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Hình thức thanh toán :<span style=\"font-weight:bold\">Chuyển hàng thu tiền tại nhà</span></p>";
                    }
                    if (rdtt == "2")
                    {
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Hình thức thanh toán :<span style=\"font-weight:bold\">Nhận hàng và thanh toán tại Loiloc</span></p>";
                    }
                    if (rdtt == "3")
                    {
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Hình thức thanh toán :<span style=\"font-weight:bold\">Chuyển khoản ATM & Ngân hàng</span></p>";
                    }
                    if (rdtt == "4")
                    {
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Hình thức thanh toán :<span style=\"font-weight:bold\">Thanh toán visa, mastercard</span></p>";
                    }
                    if (rdvc == "1")
                    {
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Thời gian giao hàng :<span style=\"font-weight:bold\">Bất kể giờ nào</span></p>";
                    }
                    if (rdvc == "2")
                    {
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Thời gian giao hàng :<span style=\"font-weight:bold\">Trong giờ hàng chính</span></p>";
                    }
                    if (rdvc == "3")
                    {
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Thời gian giao hàng :<span style=\"font-weight:bold\">Ngoài giờ hành chính</span></p>";
                    }

                    chuoihtml += "</div>";
                    chuoihtml += "</div>";
                    if (Name1 != null)
                    {
                        chuoihtml += "<div style=\" width:100%; height:30px; float:left; background:#1c7fc4; font-size:12px; color:#FFF; text-indent:15px; line-height:30px\">    	Người nhận hàng   </div>";

                        chuoihtml += "<div style=\"width:100%; height:auto; float:left\">";
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Họ và tên :<span style=\"font-weight:bold\"> " + Name1 + "</span></p>";
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Địa chỉ :<span style=\"font-weight:bold\"> " + Address1 + "</span></p>";
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Điện thoại :<span style=\"font-weight:bold\"> " + Mobile1 + " - " + Mobile1s + "</span></p>";
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Email :<span style=\"font-weight:bold\">" + Email1 + "</span></p>";
                        chuoihtml += "</div>";
                        chuoihtml += "</div>";
                    }
                    if (NameCP != null)
                    {
                        chuoihtml += "<div style=\" width:100%; height:30px; float:left; background:#1c7fc4; font-size:12px; color:#FFF; text-indent:15px; line-height:30px\">    	Thông tin yêu cầu hóa đơn  </div>";

                        chuoihtml += "<div style=\"width:100%; height:auto; float:left\">";
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Tên công ty :<span style=\"font-weight:bold\"> " + NameCP + "</span></p>";
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">Địa chỉ :<span style=\"font-weight:bold\"> " + AddressCP + "</span></p>";
                        chuoihtml += "<p style=\"font-size:12px; margin:5px 10px\">MST:<span style=\"font-weight:bold\"> " + MST + "</span></p>";
                        chuoihtml += "</div>";
                        chuoihtml += "</div>";
                    }
                    chuoihtml += "<div style=\"width:90%; height:auto; margin:10px auto; border:1px solid #d5d5d5\">";
                    chuoihtml += "<div style=\" width:100%; height:30px; float:left; background:#1c7fc4; font-size:12px; color:#FFF; text-indent:15px; line-height:30px\">   	Yêu cầu của người gửi       </div>";
                    chuoihtml += " <div style=\"width:100%; height:auto; float:left\">";
                    chuoihtml += "<p style=\"font-size:12px; margin:5px 10px; font-weight:bold; color:#F00\"> - " + Description + "</p>";
                    chuoihtml += "</div>";
                    chuoihtml += "</div>";
                    var listo = db.TblOrder.OrderByDescending(p => p.Id).Take(1).ToList();
                    chuoihtml += " <a href=\"" + urlhomes + "/Orderad/ActiveOrder?id=" + listo[0].Id + "\" title=\"\" style=\"padding:5px; color:#FFF; background:#F00; display:inline-block; text-align:center; margin:10px auto\">Đã check thông tin >></a>";
                    chuoihtml += "</div>";


                    chuoihtml += "<div style=\"width:100%; height:auto; float:left; margin:0px\">";
                    chuoihtml += "<hr style=\"width:80%; height:1px; background:#d8d8d8; margin:20px auto 10px auto\" />";
                    chuoihtml += "<p style=\"font-size:12px; text-align:center; margin:5px 5px\">" + config.Address + "</p>";
                    chuoihtml += "<p style=\"font-size:12px; text-align:center; margin:5px 5px\">Điện thoại : " + config.MobileIn + " - " + config.HotlineIn + "</p>";
                    chuoihtml += " <p style=\"font-size:12px; text-align:center; margin:5px 5px; color:#ff7800\">Thời gian mở cửa : Từ 7h30 đến 18h30 hàng ngày (làm cả thứ 7, chủ nhật). Khách hàng đến trực tiếp xem hàng giảm thêm giá.</p>";
                    chuoihtml += "</div>";
                    chuoihtml += "<div style=\"clear:both\"></div>";
                    chuoihtml += " </div>";
                    chuoihtml += " <div style=\"width:100%; height:40px; float:left; margin:0px; background:#1c7fc4\">";
                    chuoihtml += "<span style=\"font-size:12px; text-align:center; color:#FFF; line-height:40px; display:block\">Copyright (c) 2002 – 2015 Loiloc VIET NAM. All Rights Reserved</span>";
                    chuoihtml += " </div>";
                    chuoihtml += "</div>";
                    chuoihtml += "</body>";
                    chuoihtml += "</html>";
                    string body = chuoihtml;

                    var smtp = new System.Net.Mail.SmtpClient();
                    {
                        smtp.Host           = config.Host;
                        smtp.Port           = int.Parse(config.Port.ToString());
                        smtp.EnableSsl      = true;
                        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                        smtp.Credentials    = new NetworkCredential(fromAddress, fromPassword);
                        smtp.Timeout        = int.Parse(config.Timeout.ToString());
                    }
                    using (var message = new MailMessage(fromAddress, toAddress)
                    {
                        Subject = subject,
                        Body = body,
                        IsBodyHtml = true,
                    })
                    {
                        smtp.Send(message);
                    }


                    Session["Status"] = "<script>$(document).ready(function(){ alert('Bạn đã đặt hàng thành công !') });</script>";
                    return(RedirectToAction("Success"));
                }
                catch (Exception ex)
                {
                    Session["Status"] = "<script>$(document).ready(function(){ alert('Bạn đã đặt hàng thành công " + ex.Message + "!') });</script>";
                    return(RedirectToAction("OrderIndex"));
                }
            }
            return(RedirectToAction("OrderIndex"));
        }